﻿// Function to Update the Shipping info to match the Billing info
function CopyCustomerSignupDetails() {
    if (document.getElementById("shipsameasbill").checked) {
        // Insert the billing details into the shipping fields
        document.getElementById("shippingtitle").value = document.getElementById("billingtitle").value;
        document.getElementById("shippingfirstname").value = document.getElementById("billingfirstname").value;
        document.getElementById("shippinglastname").value = document.getElementById("billinglastname").value;
        document.getElementById("shippingaddress1").value = document.getElementById("billingaddress1").value;
        document.getElementById("shippingaddress2").value = document.getElementById("billingaddress2").value;
        document.getElementById("shippingtown").value = document.getElementById("billingtown").value;
        document.getElementById("shippingcounty").value = document.getElementById("billingcounty").value;
        document.getElementById("shippingpostcode").value = document.getElementById("billingpostcode").value;
        document.getElementById("shippingcountry").value = document.getElementById("billingcountry").value;
        document.getElementById("shippingdaytimeareacode").value = document.getElementById("billingdaytimeareacode").value;
        document.getElementById("shippingdaytimephone").value = document.getElementById("billingdaytimephone").value;
        document.getElementById("shippingdaytimeext").value = document.getElementById("billingdaytimeext").value;
        document.getElementById("shippingeveningareacode").value = document.getElementById("billingeveningareacode").value;
        document.getElementById("shippingeveningphone").value = document.getElementById("billingeveningphone").value;
    }
    else
    {
        document.getElementById("shippingtitle").value = "";
        document.getElementById("shippingfirstname").value = "";
        document.getElementById("shippinglastname").value = "";
        document.getElementById("shippingaddress1").value = "";
        document.getElementById("shippingaddress2").value = "";
        document.getElementById("shippingtown").value = "";
        document.getElementById("shippingcounty").value = "";
        document.getElementById("shippingpostcode").value = "";
        document.getElementById("shippingcountry").value = "";
        document.getElementById("shippingdaytimeareacode").value = "";
        document.getElementById("shippingdaytimephone").value = "";
        document.getElementById("shippingdaytimeext").value = "";
        document.getElementById("shippingeveningareacode").value = "";
        document.getElementById("shippingeveningphone").value = "";
    }
}


//Function to Validate Email
function Validate_Email(field) {
    var str = document.getElementById(field).value;
    var filter = /^.+@.+\..{2,3}$/

    if (str == "") {
        alert("Please input a valid email address!\n"); return false;
    }
    else {
        if (filter.test(str)) {
            return true
        }
        else {
            alert("Please input a valid email address!\n"); return false;
        }
    }
}



//James form validation
   function countInstances(string, word) {
        var substrings = string.split(word);
        return substrings.length - 1;
   }

   function CheckEmail(field,lable) {
        
        AtPos = field.value.indexOf("@")
        StopPos = field.value.lastIndexOf(".")
        message = ""

        if (field.value.length == 0) {
            message = "*" //No Input
            document.getElementById(lable).style.color = 'red';
            document.getElementById(lable).innerHTML= message;
        }
        else
        if (field.value.length < 6) {
            message = "* Invalid" //Email to small
            document.getElementById(lable).style.color = 'red';
            document.getElementById(lable).innerHTML= message;
        }
        else
        if (countInstances(field.value,'@') > 1) {
            message = "* Invalid" //More than 1 @
            document.getElementById(lable).style.color = 'red';
            document.getElementById(lable).innerHTML= message;
        }
        else
        if (AtPos == -1 || StopPos == -1) {
            message = "* Invalid" //No . or @
            document.getElementById(lable).style.color = 'red';
            document.getElementById(lable).innerHTML= message;
        }
        else
        if (StopPos < AtPos) {
            message = "* Invalid" //Full Stop wrong place
            document.getElementById(lable).style.color = 'red';
            document.getElementById(lable).innerHTML= message;
        }
        else
        if (StopPos - AtPos == 1) {
            message = "* Invalid" //@ and . next to each other
            document.getElementById(lable).style.color = 'red';
            document.getElementById(lable).innerHTML= message;
        } 
        else{
            message = "*" //Valid
            document.getElementById(lable).style.color = 'green';
            document.getElementById(lable).innerHTML= message;
        }
        
        //### Need to check for no value after the . and value before @
        //### Can have more than one . but need to check not next to each other
    }

    function CheckValue(field,lable){
        //var message="";
      
        if((field.value == "")){
            //message="*"
            document.getElementById(lable).style.color = 'red';
            //document.getElementById(field).style.border = '1px solid #7F9DB9';
            //document.getElementById(lable).innerHTML= message;
        }else{
            document.getElementById(lable).style.color = 'green';
            //document.getElementById(lable).innerHTML= message;
        }
    }
    
    function SubmitCheckEmail(field,lable) {
        
            var message = "";
            
            AtPos = field.value.indexOf("@")
            StopPos = field.value.lastIndexOf(".")

            if (field.value.length == 0) {
                message = "*" //No Input
                field.style.border = '1px solid red'; 
            }
            else
            if (field.value.length < 6) {
                message = "* Invalid" //Email to small
                field.style.border = '1px solid red'; 
            }
            else
            if (countInstances(field.value,'@') > 1) {
                message = "* Invalid" //More than 1 @ 
                field.style.border = '1px solid red'; 
            }
            else
            if (AtPos == -1 || StopPos == -1) {
                message = "* Invalid" //No . or @
                field.style.border = '1px solid red'; 
            }
            else
            if (StopPos < AtPos) {
                message = "* Invalid" //Full Stop wrong place
                field.style.border = '1px solid red'; 
            }
            else
            if (StopPos - AtPos == 1) {
                message = "* Invalid" //@ and . next to each other
                field.style.border = '1px solid red'; 
            } 
            else{
                //Valid Don't set message 
                field.style.border = '1px solid #7F9DB9'; 
            }
             return message;  
             
             //### Need to check for no value after the . and value before @
             //### Can have more than one . but need to check not next to each other
        }
               
        function SubmitValidateEmpty(field,label) {
            var error = "";
         
            if (field.value.length == 0) {
                field.style.border = '1px solid red'; 
                error = "Required"
            } else {
                field.style.border = '1px solid #7F9DB9'; 
            }
            return error;
        }


        function SubmitValidatePasswordMatch(field1,field2,label) {
            var error = "";
            
            if ((field1.value) == (field2.value)) {
                field1.style.border = '1px solid #7F9DB9';
                field2.style.border = '1px solid #7F9DB9';
            }
            else {
                field1.style.border = '1px solid red';
                field2.style.border = '1px solid red';
                //label.innerHTML = 'NO MATCH';
                error = "Required"
            }
            
            return error;
        }
        //end James form validation



//Popup WIndow

        function FP_openNewWindow2(w, h, nav, loc, sts, menu, scroll, resize, name, url) {//v1.0

            var windowProperties = '';
            if (nav == false) windowProperties += 'toolbar=no,';
            else windowProperties += 'toolbar=yes,';

            if (loc == false) windowProperties += 'location=no,';
            else windowProperties += 'location=yes,';

            if (sts == false) windowProperties += 'status=no,';
            else windowProperties += 'status=yes,';

            if (menu == false) windowProperties += 'menubar=no,';
            else windowProperties += 'menubar=yes,';

            if (scroll == false) windowProperties += 'scrollbars=no,';
            else windowProperties += 'scrollbars=yes,';

            if (resize == false) windowProperties += 'resizable=no,';
            else windowProperties += 'resizable=yes,';

            if (w != "") windowProperties += 'width=' + w + ',';
            if (h != "") windowProperties += 'height=' + h;

            if (windowProperties != "") {
                if (windowProperties.charAt(windowProperties.length - 1) == ',')
                    windowProperties = windowProperties.substring(0, windowProperties.length - 1);
            }
            window.open(url, name, windowProperties);
        }



    function IsNumeric(strString) {
        var strValidChars = "123456789";
        var strChar;
        var blnResult = true;

        if (strString.length == 0) return false;

        //  test strString consists of valid characters listed above
        for (i = 0; i < strString.length && blnResult == true; i++) {
            strChar = strString.charAt(i);
            if (strValidChars.indexOf(strChar) == -1) {
                blnResult = false;
            }
        }
        return blnResult;
    }
    
        function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    }