﻿// JScript File
var isUSA = false;

function validateURL() {
    var isValid = true;
    var company = document.getElementById(companyID);
    var webAddress = document.getElementById(webAddressID);
    var formValid = document.getElementById(cFormValid);
    formValid.value = "1";
    if (company.value == "") {
        isValid = false;
        alert("Please complete the following items:\n\nCompany Name\nWeb Address");
        company.focus();
    }
    else {
        if (webAddress.value == "") {
            isValid = false;
            alert("Please complete the following items:" + "\n" + "Web Address");
            webAddress.focus();
        }
    }
    if ((company.value != "") && (webAddress.value != "")) {
        isValid = webAddressValidate(webAddress.value);
        if (!isValid) {
            alert("Web Address is NOT Valid");
            webAddress.focus();
        }
    }
    if (!isValid) {
        formValid.value = "0";
    }
    return isValid;
}

function webAddressValidate(sURL) {
   var urlRegxp = /(http:\/\/[a-zA-Z][\.]|https:\/\/[a-zA-Z][\.]|[a-zA-Z])([\w]+)([\w]+){1,}[\.]([\w]+){2,3}/;
   return urlRegxp.test(sURL);
}

function validateForm() {
    var isValid = true;
    var formValid = document.getElementById(cFormValid);
    formValid.value = "1";
    var signUPFormV = document.getElementById(signUPForm);
    var tbls = signUPFormV.getElementsByTagName("table");
    var tblCount = tbls.length;
    var rows;
    var columns;
    var contr;
    var impText;
    var contrName = "";
    var rowsCount;
    var iID;
    var iIDEnd;
    var errMsg = "";
    var firstContr;
    var blnState;
    var blnDDL;
    for (var t = 1; t < tblCount; t++) 
    {   
        rows = tbls[t].getElementsByTagName("tr");
        rowsCount = rows.length;
        for (var r = 0; r < rowsCount; r++) 
        {
            blnState = false;
            blnDDL = false;
            columns = rows[r].getElementsByTagName("td"); 
            if (columns[2].innerHTML == "*") {
                contrName = columns[0].innerHTML;
                contrName = contrName.replace(":&nbsp;", "\n");
                contr = columns[1].innerHTML;
                if (contr.indexOf("SELECT") >= 0){
                    blnDDL = true;
                    if (contr.indexOf("ddlCountry") <0) {
                        blnState = true;
                        contrName="State\n";
                    }
                }
                if ((contr.indexOf("INPUT") > 0) || (blnState) || (blnDDL)) {
                    impText = contr.split(" ");
                    for (var i = 0; i < impText.length; i++) {
                        iID = impText[i].indexOf("id");
                        if (iID >= 0) { 
                           contr = document.getElementById(impText[i].substring(3, impText[i].length));
                           if (blnDDL) {
                                if (contr.options[contr.selectedIndex].innerText == "United States")
                                    isUSA = true;  
                           }
                           if (contr.value == "") {
                                if (errMsg == "") {
                                    firstContr = contr;
                                } 
                                errMsg += contrName;
                                isValid = false;
                            } 
                            break; 
                        }
                    }
                }
            } 
        }
    }
    if (!isValid) { 
        alert("Please complete the following items:\n\n" + errMsg);
        firstContr.focus();
    }
    else {
        //validate phone, email, zip
        isValid = isEmailValid();
        if (isValid && isUSA) {
            //validate phone, zip only for USA
            var zip = document.getElementById(ZipContr); 
            if (!validZip(zip.value))  {
                alert("Postal code is Not Valid");
                zip.focus();
                isValid = false;
            } 
            else {
                var phone = document.getElementById(phoneContr);
                var phoneT = document.getElementById(phoneTContr);
                if (!validPhone(phone.value)) {
                    alert("Phone Number is Not Valid");
                    phone.focus();
                    isValid = false;
                }
                else {
                    if (!validPhone(phoneT.value)) {
                        alert("Phone Number for Technical Contact is Not Valid");
                        phoneT.focus();
                        isValid = false;
                    }
                }
            }     
        }
    }
    return isValid;
}

function sameContact(theChkBox) {
    var signUPFormV = document.getElementById(signUPForm);
    var elmID = signUPFormV.id.substring(0, signUPFormV.id.lastIndexOf("_") + 1);
    if (theChkBox.checked) {
        document.getElementById(elmID + "txtFirstNameT").value = document.getElementById(elmID + "txtFirstName").value;
        document.getElementById(elmID + "txtLastNameT").value = document.getElementById(elmID + "txtLastName").value;
        document.getElementById(elmID + "txtPositionT").value = document.getElementById(elmID + "txtPosition").value;
        document.getElementById(elmID + "txtPhoneT").value = document.getElementById(elmID + "txtPhone").value;
        document.getElementById(elmID + "txtPhoneExtT").value = document.getElementById(elmID + "txtPhoneExt").value;
        document.getElementById(elmID + "txtEmailT").value = document.getElementById(elmID + "txtEmail").value;
    }
    else {
        document.getElementById(elmID + "txtFirstNameT").value = "";
        document.getElementById(elmID + "txtLastNameT").value = "";
        document.getElementById(elmID + "txtPositionT").value = "";
        document.getElementById(elmID + "txtPhoneT").value = "";
        document.getElementById(elmID + "txtPhoneExtT").value = "";
        document.getElementById(elmID + "txtEmailT").value = "";
    }
}

function hideShowStateProvince(theCountryDDL) {
    var country = theCountryDDL.options[theCountryDDL.selectedIndex].innerHTML;
    var lblStateContr = document.getElementById(lblStatePr);
    var ddlStateContr = document.getElementById(StateDDL);
    var txtProvinceContr = document.getElementById(ProvinceTBox);
    
    if (country != "United States") {
        lblStateContr.innerHTML = "Province/Territory";
        ddlStateContr.className = "hideStateProvince";
        txtProvinceContr.className = "";
        document.getElementById("requiredState").innerHTML="&nbsp;";
    }
    else {
        lblStateContr.innerHTML = "State";
        ddlStateContr.className = "";
        txtProvinceContr.className = "hideStateProvince";
        document.getElementById("requiredState").innerHTML="*";
    }
     
}

function isEmailValid() {
    var rExp = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    var signUPFormV = document.getElementById(signUPForm);
    var elmID = signUPFormV.id.substring(0, signUPFormV.id.lastIndexOf("_") + 1);
    var email = document.getElementById(elmID + "txtEmail");
    var emailT = document.getElementById(elmID + "txtEmailT");
    var emailconf = document.getElementById(elmID + "txtEmailConfirm");
    if (rExp.test(email.value)) 
    {
        if (email.value != emailconf.value) {
            alert("Email Addresses do not match.");
            emailconf.focus();
            return false;
        }
        else {
            if ((email.value != emailT.value) && (!rExp.test(emailT.value))){
                alert("Email Addresses for Technical contact is Not Valid.");
                emailT.focus();
             return false;
			}
			else {    
            return true;
			}
		}
	}
    else {
        alert("Email Address is Not Valid");
        email.focus();
        return false;
    }
}
function validPhone(phoneValue) {
    var rExp = new RegExp("((\\(\\d{3}\\) ?)|(\\d{3}\-?|d{3}\s))?\\d{3}\-?\|d{3}\s\\d{4}");
    return(rExp.test(phoneValue));
}

function validateSurveyForm() {
    var isValid = true;
    var contr;
    var inputs;
    var qNumber = 1;
    var arrayQust = new Array(13);
    var sWarning = "";
    var sError = "";
    var columnsQ;
    var inputsCount;
    var firstMsg = false;
    var otherText = false;
    
    arrayQust[0] = "";
    arrayQust[1] = "Please select whether this is your first visit.";
	arrayQust[2] = "Please select an age range.";
	arrayQust[3] = "Please select gender.";
	arrayQust[4] = "Please select a household income.";
	arrayQust[5] = "Please check the purpose of your visit.";
	arrayQust[6] = "Please check which vehicle(s) you own.";
	arrayQust[7] = "";
	arrayQust[8] = "Enter how did you hear about NADAguides.com."; 
	arrayQust[9] = "Please check whether you are a member of a club or organization.";
    arrayQust[10] = "";
    arrayQust[11] = "";
    arrayQust[12] = "Please check if we can contact you for future campaigns.";
    
    var questionID = "";
    for (intI = 0; intI < arrayQust.length - 1; intI++)
    {
        questionID = "question" + qNumber ;
        sWarning = arrayQust[qNumber];
            
        columnsQ = document.getElementById(questionID);
        if (columnsQ != null)
        {
            // 7, 10, 11 are not required
            if (sWarning == "" && qNumber != 7 && qNumber != 10 && qNumber != 11)
                sWarning = questionID;

            inputs = columnsQ.getElementsByTagName("INPUT");
            inputsCount = inputs.length;
            // check if collection have text box and other check box checked
            otherText = findTextBox(inputs, inputsCount);
            for (var j = 0; j < inputsCount; j++) 
            {
                if (((inputs[j].type == "radio") || (inputs[j].type == "checkbox"))
                    && inputs[j].checked) 
                {
                    // if check box other selected
                    if (!otherText)
                        sWarning = "";
                    break;
                }    
            }
            if (sWarning != "") 
            {
                if (otherText != null)
                {
                    switch (qNumber) 
                    {
                        case 6:
                            sWarning = "What other vehicle you currently own?";
                            break;
                        case 8:
                            sWarning = "Enter how did you hear about NADAguides.com.";
                            break;
                        case 9:
                            sWarning = "Which other club or organization you are member of.";
                            break;
                    }  
                }
                sError += sWarning + "\n";
                sWarning = "";
                if (!firstMsg)  {
                        contr = inputs[j - 1];
                    firstMsg = true;
                }
            }
        }
		qNumber++;

    }
    if (qNumber == 13)
        qNumber = 12;

	if ((document.getElementById(ContactYN + "_0").checked) 
	    && (document.getElementById(EmailTB).value == "")) {
	    sError +="Please enter your email address we could contact you regarding future campaigns.";
	    if (!firstMsg)
	        contr = document.getElementById(EmailTB);
	}
	if (sError != "") 
	{
		alert(sError);
		contr.focus(); 
		return false;
	}
	if ((sError == "") && (document.getElementById(EmailTB).value != "")) {
	    var email = document.getElementById(EmailTB).value;
	    var rExp = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	    if (!rExp.test(email)) {
	        alert("Email address is not valid");
	        document.getElementById(EmailTB).focus();
	        return false;
	    }
	    return true;
	}    

 }

function findTextBox(textBoxes, itemsCount)
{
    var isTextOther = false;
    var isCheckdOther = false;
    var otherTextID = "";

    for (var j = 0; j < itemsCount; j++) 
    {
        otherTextID = textBoxes[j].id;
        if (((textBoxes[j].type == "radio") || (textBoxes[j].type == "checkbox"))  
                    && textBoxes[j].checked)
        {
            if (otherTextID.toLowerCase().indexOf("other") > 0) 
                isCheckdOther = true;    
        } 
        if (textBoxes[j].type == "text" && isCheckdOther) 
        {
            if (otherTextID.toLowerCase().indexOf("other") > 0 && textBoxes[j].value == "") 
                isTextOther = true;
            break;                
        }
    }
    return isTextOther;

}
 
function unclickAll(optBtn) {
    var referral = document.getElementById(ReferralsList);
    var optBtnID = optBtn.id;
//    var elemPref = optBtnID.substring(0, optBtnID.lastIndexOf("_"));
    var elemID = "";
    if (optBtnID.indexOf("Other") < 0) { 
       elemID = optBtnID + "Other";
       document.getElementById(elemID).checked = false;
    }
    if (optBtnID.indexOf("Other") >= 0) {
        for (i = 0; i < 7; i++) {
            elemID = optBtnID.replace("Other", "_") + i;
            document.getElementById(elemID).checked = false;       
        }
    }
        
 }
 
function noneClubs(checkBox) {
    var checkBoxID = checkBox.id;
    var isNone = false;
    var checkBoxCur;
    if (checkBoxID.indexOf("None") >= 0)
        isNone = true;        
    // none
    var clubs = document.getElementById(ClubsList);
    var lenClubs = clubs.getElementsByTagName("td").length;
    var elemID;
    
    if (isNone) {
    
        for (i = 0; i < lenClubs; i++) {
            elemID = ClubsList + "_" + i;
            checkBoxCur = document.getElementById(elemID);
            if ((elemID != checkBoxID))
                checkBoxCur.checked = false;
        }
        elemID = ClubsList + "Other";
        document.getElementById(elemID).checked = false;
        elemID = ClubsList + "OtherTB";
        document.getElementById(elemID).value = "";
    } 
    if (!isNone) {
        document.getElementById(ClubsList + "None").checked = false; 
    }      
 }
