//all items from /headers/Header.js
//subValLogin modified for this customer    
    
    function openHelpWindow(xURL, winName, features) {
        features = features + ",left=50,top=50"
        window.open(xURL, winName, features);
        window.top.name = 'mainWindow';
    }    
    
    function funcDrawValMsg(yName) {
        xMsg = xMsg + " - Enter a " + yName + "<br>"
        xCont = "1"
        //alert (yName)
    }

    var xEmailMsg = ""
    function funcValidateEMail(xMailStr) {
        xEmailMsg = ""
        //then val Email addresses for validity
        var aryEmail = xMailStr.split("@")
        //alert('aryEmail.length='+aryEmail.length)

        if (aryEmail.length != 2) {
            xEmailMsg = xEmailMsg + "<br>a - Enter a Valid Email address"
        } else {
            //alert('aryEmail[0]='+aryEmail[0]+'/'+aryEmail[0].length)
            //alert('aryEmail[1]='+aryEmail[1]+'/'+aryEmail[1].length)
            //either side is 0 length
            if (aryEmail[0].length == 0 || aryEmail[1].length == 0) {
                xEmailMsg = xEmailMsg + "<br>b - Enter a Valid Email address"
            } else {
                var aryEmail2 = aryEmail[1].split(".")
                var aryL = aryEmail2.length
                //alert('aryEmail2.length='+aryL)
                if (aryL < 2) {
                    xEmailMsg = xEmailMsg + "<br>c - Enter a Valid Email address"
                } else {
                    //alert('aryEmail2[aryL-1]='+aryEmail2[aryL-2])
                    //alert('aryEmail2[aryL]='+aryEmail2[aryL-1])
                    for (i = 0; i < aryL; i++) {
                        if (aryEmail2[i].length == 0) {
                            xEmailMsg = xEmailMsg + "<br>d - Enter a Valid Email address"
                        }
                    }
                    if (xEmailMsg == "") {
                        if (aryEmail2[aryL - 1].length < 2 || aryEmail2[aryL - 1].length > 3) {
                            xEmailMsg = xEmailMsg + "<br>e - .xxx is <> 2 or 3 chars - Enter a Valid Email address"
                        }
                    }
                }
            }
        }
        if (xEmailMsg != "") {
            //alert(xEmailMsg)
        }
        xEmailMsg = xEmailMsg + xEmailMsg
    }


    function subValLogin(xForm, yForm, zForm) {
        //alert(xForm + '\n' + yForm + '\n' + zForm)
    //  return;
        var iHeight = 250
        var today = new Date();
        xCont = "0"
        xMsg = ""
        switch (yForm) {
            case 'Login':
                //alert("Looooogin");
                if (window.document.forms[xForm].Email2.value.length == 0) {
                    xCont = "1"
                    xMsg = xMsg + "<BR> - Enter an Email"
                } else {
                    xEmailMsg = ""
                    funcValidateEMail(window.document.forms[xForm].Email2.value)
                    if (xEmailMsg.length > 0) {
                        xCont = "1"
                        xMsg = xMsg + "<BR> - Enter a Valid Email address"
                    }
                }
                if (window.document.forms[xForm].Password2.value.length == 0) {
                    xCont = "1"
                    xMsg = xMsg + "<BR> - Enter a Password"
                }
                xMsg = "To Log in to Your Account<BR>Please correct the following fields<BR>" + xMsg
                window.document.forms[xForm].xProfile.value = "Login"
                window.document.forms[xForm].AcctType.value = "Existing"
                iHeight = 250
                break;

            case 'ForgotPassword':
                //alert('ForgotPassword Here')
                if (window.document.forms[xForm].Email2.value.length == 0) {
                    xCont = "1"
                    xMsg = xMsg + "<BR> - Enter an Email"
                } else {
                    xEmailMsg = ""
                    funcValidateEMail(window.document.forms[xForm].Email2.value)
                    if (xEmailMsg.length > 0) {
                        xCont = "1"
                        xMsg = xMsg + "<BR> - Enter a Valid Email address"
                    } else {
                        xCont = "2"
                        openHelpWindow('/MessageWindow.asp?MID=Forgot Password&H=' + jsH + '&EmailLogin=Email&Email=' + window.document.forms[xForm].Email2.value + '', 'openHelpWindow', 'width=250,height=250,resizable=0,scrollbars=yes');
                        window.document.forms[xForm].xProfile.value = "ForgotPassword"
                    }
                }
                iHeight = 250
                break;

            case 'Edit':
                //alert("editedit");
                //alert("zForm=" + zForm);
                xMsg = "To Save Your Account<BR>Please correct the following fields<BR>"

                if (zForm == 'New') {//only validate for new, existing is hedden
                    if (window.document.forms[xForm].Email.value.length == 0) {
                        funcDrawValMsg('Email')
                    } else {
                        xEmailMsg = ""
                        funcValidateEMail(window.document.forms[xForm].Email.value)
                        if (xEmailMsg.length > 0) {
                            xCont = "1"
                            xMsg = xMsg + " - Enter a Valid Email address<BR>"
                        } else {
                            if (window.document.forms[xForm].Email.value != window.document.forms[xForm].EmailEA.value) {
                                xMsg = xMsg + " - Same Email address Must be entered twice<BR>"
                                xCont = "1"
                            }
                        }
                    }

                    if (window.document.forms[xForm].Password.value.length == 0) {
                        funcDrawValMsg('Password')
                    } else {
                        if (window.document.forms[xForm].Password.value.length < 5) { funcDrawValMsg('Password with at least 5 characters') }
                        if (window.document.forms[xForm].Password.value.length > 15) { funcDrawValMsg('Password with less than 15 characters') }
                        if (window.document.forms[xForm].Password.value.indexOf("&") > -1) { funcDrawValMsg('Password without the ampersand symbol') }
                        if (window.document.forms[xForm].Password.value != window.document.forms[xForm].PasswordEA.value) {
                            xMsg = xMsg + " - Same Password Must be entered twice<BR>"
                            xCont = "1"
                        }
                    }


                } //zForm == 'New


                if (window.document.forms[xForm].FirstName.value.length == 0) { funcDrawValMsg('First Name') }
                if (window.document.forms[xForm].FirstName.value.length > 50) { funcDrawValMsg('First Name with less than 50 characters') }
                if (window.document.forms[xForm].FirstName.value.indexOf("&") > -1) { funcDrawValMsg('First Name without the ampersand symbol') }
                if (window.document.forms[xForm].LastName.value.length == 0) { funcDrawValMsg('Last Name') }
                if (window.document.forms[xForm].LastName.value.length > 50) { funcDrawValMsg('LastName with less than 50 characters') }
                if (window.document.forms[xForm].LastName.value.indexOf("&") > -1) { funcDrawValMsg('Last Name without the ampersand symbol') }
                if (window.document.forms[xForm].Address1.value.length == 0) { funcDrawValMsg('Address') }
                if (window.document.forms[xForm].Address1.value.length > 50) { funcDrawValMsg('Address with less than 50 characters') }
                if (window.document.forms[xForm].Address1.value.indexOf("&") > -1) { funcDrawValMsg('Address without the ampersand symbol') }
                //Address2 not required
                if (window.document.forms[xForm].Address2.value.length > 50) { funcDrawValMsg('Suite No. with less than 50 characters') }
                if (window.document.forms[xForm].Address2.value.indexOf("&") > -1) { funcDrawValMsg('Suite No. without the ampersand symbol') }
                if (window.document.forms[xForm].City.value.length == 0) { funcDrawValMsg('City') }
                if (window.document.forms[xForm].City.value.length > 50) { funcDrawValMsg('City with less than 50 characters') }
                if (window.document.forms[xForm].City.value.indexOf("&") > -1) { funcDrawValMsg('City without the ampersand symbol') }
                if (window.document.forms[xForm].State.value.length == 0) { funcDrawValMsg('State') }

                switch (window.document.forms[xForm].State.value) {
                    case 'AB':
                    case 'BC':
                    case 'MB':
                    case 'NB':
                    case 'NL':
                    case 'NS':
                    case 'NT':
                    case 'NU':
                    case 'ON':
                    case 'PE':
                    case 'QC':
                    case 'SK':
                    case 'YT':
                    case 'MEXICO':
                    case 'OTHER':
                        if (window.document.forms[xForm].Zip.value.length == 0) { funcDrawValMsg('Postal Code') }
                        break;
                    default:
                        if (window.document.forms[xForm].Zip.value.length != 5) {
                            funcDrawValMsg('5 Digit Zip Code')
                        } else {
                            if (isNaN(window.document.forms[xForm].Zip.value.length) == true) {
                                funcDrawValMsg('5 Digit Zip Code')
                            }
                        }
                        break;
                }

                if (window.document.forms[xForm].Country.value.length == 0 && window.document.forms[xForm].State.value == 'OTHER') { funcDrawValMsg('Country') }

                document.forms[xForm].xProfile.value = "Continue"
                iHeight = 350
                break;

        }

        switch (xCont) {
            case '0':
                //alert('ok')
                window.document.forms[xForm].submit()
                break;
            case '1':
                //alert('xMsg='+xMsg)
                openHelpWindow('/MessageWindow.asp?MID=Please Complete All Fields&H=' + jsH + '&Msg=' + xMsg + '', 'openHelpWindow', 'width=250,height=' + iHeight + ',resizable=0,scrollbars=yes');
                break;
            case '2':
                //opened above to email password
                break;   
        }
    }	
