/*
 +------------------------------------------------------------------------------+
 Project: Straightsell Standard Templates
 +------------------------------------------------------------------------------+
 COPYRIGHT 2007 - STRATEGIC ECOMMERCE
 +------------------------------------------------------------------------------+
 Description: documents/site.js
 Primary Javascript file.
 Functions are namespaced using "SEL" and functions are called using
 the syntax: SEL.functionName(params);
 Ensure that any new functions are placed within the outer "return" block,
 and that each function is terminated with a "," (except for the last
 function), ie:
 functionName: function(a,b,c) {
 var x =  a + b + c;
 ..
 },
 +------------------------------------------------------------------------------+
 CSS Label: NA
 +------------------------------------------------------------------------------+
 Created: December 13 2007
 +------------------------------------------------------------------------------+
 Current Version: 1.0
 +------------------------------------------------------------------------------+
 Change History:
 December 13 2007 - Created
 +------------------------------------------------------------------------------+
 Author(s): Paul Johnson
 +------------------------------------------------------------------------------+
 */
var popUpWin=0; // Initialise variable - used by the popUpWindow() function

var SEL = function(){
    return {
    
        // ===== General functions =====
        
        trim: function(s){
            return s.replace(/^\s&#42;(\S&#42;(\s+\S+)&#42;)\s&#42;&#36;/, "&#36;1");
        },
        
        onlyNumbers: function(e){ // Allow only numbers to be entered into text fields. Checks input as it is being typed.
            var keynum;
            var keychar;
            var numcheck;
            
            if (window.event) // IE
            {
                keynum = e.keyCode;
            }
            else 
                if (e.which) // Netscape/Firefox/Opera
                {
                    keynum = e.which;
                }
            if (!keynum) {
                return true;
            }
            //alert(keynum);
            //if backspace - for firefox
            if (keynum == 8) {
                return true;
            }
            
            keychar = String.fromCharCode(keynum);
            
            if ((keychar >= "0") && (keychar <= "9")) {
                return true;
            }
            else {
                return false;
            }
        },
		
		
		popUpWindow: function(URLStr, left, top, width, height) {
			if(popUpWin) {
				if(!popUpWin.closed) popUpWin.close();
			}
			popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
		},
        
        // ===== Contact, Registration, Request Info/Quote and Login forms. =====
        
        changeStates: function(form){ // Dynamically update the States entry field based on the Country selected.
            var countryList = form.Country;
            var selectedCountry = countryList.options[countryList.selectedIndex].value;
            
            if (selectedCountry == "AU") {
                document.getElementById("StatesAU").style.display = "block";
                document.getElementById("StatesUS").style.display = "none";
                document.getElementById("StatesOther").style.display = "none";
            }
            else 
                if (selectedCountry == "US") {
                    document.getElementById("StatesAU").style.display = "none";
                    document.getElementById("StatesUS").style.display = "block";
                    document.getElementById("StatesOther").style.display = "none";
                }
                else {
                    document.getElementById("StatesAU").style.display = "none";
                    document.getElementById("StatesUS").style.display = "none";
                    document.getElementById("StatesOther").style.display = "block";
                }
        },
        
        /* Contact Form validation. Also handles the Request Information and Request for Quote forms.
         Checks for a hidden field named "FormType" and sets the form introduction text and field data accordingly. */
        checkContactForm: function(){
            var frm = document.getElementById("ContactForm");
            var vEmail = frm.ContactEmail.value;
            
            if (document.getElementById("StatesAU").style.display == "block") {
                var stateList = frm.StateAU;
                var selectedState = stateList.options[stateList.selectedIndex].value;
            }
            else 
                if (document.getElementById("StatesUS").style.display == "block") {
                    var stateList = frm.StateUS;
                    var selectedState = stateList.options[stateList.selectedIndex].value;
                }
                else 
                    if (document.getElementById("StatesOther").style.display == "block") {
                        var stateList = frm.StateOther;
                        var selectedState = stateList.value;
                    }
            
            if (SEL.trim(frm.ContactName.value) == "") {
                alert("Please enter a Contact Name.");
                frm.ContactName.focus();
                return false;
            }
            else 
                if (SEL.trim(frm.ContactEmail.value) == "") {
                    alert("Please enter an Email Address.");
                    frm.ContactEmail.focus();
                    return false;
                }
                else 
                    if ((SEL.trim(frm.ContactEmail.value) != "") && (vEmail.indexOf("@") == -1 || vEmail.indexOf(".") == -1)) {
                        alert("Please enter a valid Email Address (ie yourname@yourdomain.com)");
                        frm.ContactEmail.focus();
                        return false;
                    }
                    else {
                    
                        frm.State.value = selectedState;
                        newline = "\n";
                        space = " ";
                        
                        if (frm.FormType.value == "prodInfo") { // Request for Information
                            var eContent = "A Request for Information Form has been submitted through your StraightSell website. Here are the details:" + newline + newline;
                            eContent += "Requested Product:" + space + frm.TheProduct.value + newline + newline;
                        }
                        else 
                            if (frm.FormType.value == "prodQuote") { // Request for Quote
                                var eContent = "A Request Quote has been submitted through your StraightSell website. Here are the details:" + newline + newline;
                                eContent += Products + newline;
                                eContent += "==========================================================\n";
                            }
                            else { // Standard Contact
                                var eContent = "A Contact form has been submitted through your StraightSell website. Here are the details:" + newline + newline;
                            }
                        
                        eContent += "Contact Name:" + space + frm.ContactName.value + newline;
                        eContent += "Email Address:" + space + frm.ContactEmail.value + newline + newline;
                        eContent += "Phone Number:" + space + frm.ContactPhone.value + newline;
                        eContent += "Address:" + space + frm.Address1.value + newline;
                        eContent += "Address2:" + space + frm.Address2.value + newline;
                        eContent += "City:" + space + frm.City.value + newline;
                        eContent += "State:" + space + frm.State.value + newline;
                        eContent += "Country:" + space + frm.Country.options[frm.Country.selectedIndex].text + newline;
                        eContent += "Postcode:" + space + frm.Postcode.value + newline;
                        if (frm.Comments.value == "Comments/Feedback") {
                            frm.Comments.value = "";
                        }
                        eContent += "Comments:" + space + frm.Comments.value + newline;
                        
                        frm.EmailFrom.value = frm.ContactEmail.value;
                        frm.EmailContent.value = eContent;
                        return true;
                    }
        },
        
        checkRegForm: function(){ // Registration Form validation.
			var frm = document.getElementById("AddUserForm");
            var vEmail = frm.AdminEmail.value;
            // var vContactSource = frm.contactsource;
            // var selectedContactSource = vContactSource.options[vContactSource.selectedIndex].value;
            
            if (document.getElementById("StatesAU").style.display == "block") {
                var stateList = frm.StateAU;
                var selectedState = stateList.options[stateList.selectedIndex].value;
            }
            else 
                if (document.getElementById("StatesUS").style.display == "block") {
                    var stateList = frm.StateUS;
                    var selectedState = stateList.options[stateList.selectedIndex].value;
                }
                else 
                    if (document.getElementById("StatesOther").style.display == "block") {
                        var stateList = frm.StateOther;
                        var selectedState = stateList.value;
                    }
            
            if (SEL.trim(frm.AdminUserName.value) == "") {
                alert("Please enter a Username.");
                frm.AdminUserName.focus();
                return false;
            }
            else 
                if (frm.AdminPassword.value == "" || frm.RetypeAdminPassword.value == "" || frm.AdminPassword.value != frm.RetypeAdminPassword.value) {
                    alert("Please enter a Password - twice to confirm.");
                    frm.AdminPassword.focus();
                    return false;
                }
                else 
                    if (SEL.trim(frm.AdminName.value) == "") {
                        alert("Please enter your full name.");
                        frm.AdminName.focus();
                        return false;
                    }
                    else 
                        if (SEL.trim(frm.AdminEmail.value) == "") {
                            alert("Please enter an Email Address.");
                            frm.AdminEmail.focus();
                            return false;
                        }
                        else 
                            if ((SEL.trim(frm.AdminEmail.value) != "") && (vEmail.indexOf("@") == -1 || vEmail.indexOf(".") == -1)) {
                                alert("Please enter a valid Email Address (ie yourname@yourdomain.com)");
                                frm.AdminEmail.focus();
                                return false;
                            }
                            else 
                                if (SEL.trim(frm.PostCode.value) == "") {
                                    alert("Please enter your postcode.");
                                    frm.PostCode.focus();
                                    return false;
                                }
                                else 
                                    if (SEL.trim(selectedState) == "") {
                                        alert("Please enter your State.");
                                        stateList.focus();
                                        return false;
                                    }
                                    else 
                                        if (SEL.trim(frm.AdminPhone.value) == "") {
                                            alert("Please enter your phone number.");
                                            frm.AdminPhone.focus();
                                            return false;
                                        }
                                        else {
                                            frm.State.value = selectedState;
                                            // frm.ContactSourceSelected.value = selectedContactSource;
                                            return true;
                                        }
        }
        
    };
}
();
