﻿

ieNavHover = function() {
    var oListItems = document.getElementById("navMenu").getElementsByTagName("li");
    for (var n = 0; n < oListItems.length; n++) {
            oListItems[n].onmouseover = function() {
            this.className = "navHover";
        }
        oListItems[n].onmouseout = function() {
            //this.className = this.className.replace(new RegExp("navHover\\b"), "");
            this.className = this.className.replace("navHover", "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", ieNavHover);


function CheckRequestBrochure() {
    
    var aBrochureFields = new Array("FirstNameBroc", "LastNameBroc", "Address", "City", "State", "ZipCode");
    var sFirstNameBroc = $("#fldFirstNameBroc").val();
    var sLastNameBroc = $("#fldLastNameBroc").val();
    var sAddress = $("#fldAddress").val();
    var sCity = $("#fldCity").val();
    var sState = $("#fldState").val();
    var sZipCode = $("#fldZipCode").val();
    var bCheckBrochure = true;
    var sSkippedBrochureFields = "";
    var sBrochureValues = "";
    var sBrochureField = "";    
    
    for(var b=0; b<aBrochureFields.length; b++) {

        sBrochureField = "fld" + aBrochureFields[b];
        ChangeInputStyle(sBrochureField, "#fff8d5", "#724522");
    }

    //Check for first name
    if (sFirstNameBroc == "") {
        bCheckBrochure = false;
        ChangeInputStyle("fldFirstNameBroc", "#D94040", "#000000");
        sSkippedBrochureFields = "First Name\n";
    }

    //Check for last name
    if (sLastNameBroc == "") {
        bCheckBrochure = false;        
        ChangeInputStyle("fldLastNameBroc", "#D94040", "#000000");        
        sSkippedBrochureFields += "Last Name\n";        
    }

    //Check for address
    if (sAddress == "") {
        bCheckBrochure = false;
        ChangeInputStyle("fldAddress", "#D94040", "#000000");
        sSkippedBrochureFields += "Address\n";
    }

    //Check for city
    if (sCity == "") {
        bCheckBrochure = false;
        ChangeInputStyle("fldCity", "#D94040", "#000000");
        sSkippedBrochureFields += "City\n";
    }

    //Check for state
    if (sState == "0") {
        bCheckBrochure = false;
        ChangeInputStyle("fldState", "#D94040", "#000000");
        sSkippedBrochureFields += "State\n";
    }

    //Check for zip code
    if (sZipCode == "") {
        bCheckBrochure = false;
        ChangeInputStyle("fldZipCode", "#D94040", "#000000");
        sSkippedBrochureFields += "Zip Code\n";
    }
    
    //If any fields are blank, trigger alert window & message
    if (bCheckBrochure == false) {
        window.alert("The following required fields have not been completed:\n\n" + sSkippedBrochureFields + "\nPlease complete these fields and resubmit.");
    }
    else {
        //If all fields completed, concatenate all values into semi-colon delimited string
        sBrochureValues = sFirstNameBroc + ";" + sLastNameBroc + ";" + sAddress + ";" + sCity + ";" + sState + ";" + sZipCode;
        
        //Call function to log user brochure request
        //GetHomePageForm(sBrochureValues, 'Brochure');
        GetHomePageForm(sBrochureValues, 'brochure', '');
    }
}

function ChangeInputStyle(FormFieldId, BgColor, FontColor) {
    document.getElementById(FormFieldId).style.backgroundColor = BgColor;
    document.getElementById(FormFieldId).style.color = FontColor;
}

function CheckEmailAddress() {

    var aNewsletterFields = new Array("FirstNameNews", "LastNameNews", "EmailAddress");
    var sFirstNameNews = $("#fldFirstNameNews").val();
    var sLastNameNews = $("#fldLastNameNews").val();
    var sEmailAddress = $("#fldEmailAddress").val();
    var bCheckNewsletter = true;
    var bCheckEmail = true;
    var sSkippedNewsletterFields = "";
    var sNewsletterField = "";
    
    for(var n=0; n<aNewsletterFields.length; n++) {
        sNewsletterField = "fld" + aNewsletterFields[n];
        ChangeInputStyle(sNewsletterField, "#FFF8D5", "#724522");
        //document.getElementById(sNewsletterField).style.backgroundColor = "#fff8d5";    
    }   

    //Check for first name
    if (sFirstNameNews == "") {
        bCheckNewsletter = false;
        ChangeInputStyle("fldFirstNameNews", "#D94040", "#000000");
        sSkippedNewsletterFields = "First Name\n";
    }

    //Check for last name
    if (sLastNameNews == "") {
        bCheckNewsletter = false;
        ChangeInputStyle("fldLastNameNews", "#D94040", "#000000");
        sSkippedNewsletterFields += "Last Name\n";

    }

    //Check for email address
    if (sEmailAddress == "") {
        bCheckNewsletter = false;
        ChangeInputStyle("fldEmailAddress", "#D94040", "#000000");
        sSkippedNewsletterFields += "Email Address\n";
    }
    else {
        //if (sEmailAddress.indexOf("@") == -1) {
        if (ValidateEmailAddress('EmailAddress') == false) {
            bCheckEmail = false;            
        }
    }

    if (bCheckNewsletter == false) {
        window.alert("The following required fields have not been completed:\n\n" + sSkippedNewsletterFields + "\nPlease complete these fields and resubmit.");
    }
    else if (bCheckEmail == false) {
        window.alert("A valid email address must be entered to successfully sign up for our email newsletter.");
    }
    else {
        sNewsletterValues = sFirstNameNews + ";" + sLastNameNews + ";" + sEmailAddress;

        //If value found, call function to log user email address
        //GetHomePageForm(sNewsletterValues, 'Newsletter');
        GetHomePageForm(sNewsletterValues, 'email', sEmailAddress);
    }
}

function GetHomePageForm(GetHomePageFormValues, GetFormId, EmailAddress) {

    //var sHomePageLoader = "#load" + GetFormId;
    //var sHomePageFieldset = "#fs" + GetFormId;
    //var sHomePageForm = "form" + GetFormId;

    var sHomePageLoader = "#" + GetFormId + "Loader";
    var sHomePageFieldset = "#" + GetFormId + "Fs";
    var sHomePageForm = GetFormId + "Form";
    var sUrl = "";
    var sFormPadding;
    
    //Set padding to display confirmation message correctly
    //document.getElementById(sHomePageForm).style.padding = "23px 7px 5px 7px";
    //document.getElementById(sHomePageForm).style.padding = "21px 0px 0px 0px";
    document.getElementById(sHomePageForm).style.padding = "0px 0px 0px 0px";
    //document.getElementById(sHomePageForm).style.height = "54px";
    if (GetFormId == "brochure") {
        document.getElementById(sHomePageForm).style.width = "491px";
        sFormPadding = "15px 7px 0px 7px";
        //sUrl = "Forms.asmx/LogHomePageForm";
        //sUrl = "Forms.asmx/GetBrochureRequest";
    }
    else if (GetFormId == "email") {
        $(".signUp").hide();
        document.getElementById(sHomePageForm).style.width = "444px";
        sFormPadding = "3px 7px 0px 7px";
        //sUrl = "Forms.asmx/LogHomePageForm";
        //sUrl = "Forms.asmx/GetEmailList";
    }
    //url: "Default.aspx/LogHomePageForm",
    $(sHomePageFieldset).hide();
    $(sHomePageLoader).show();

    $.ajax({
        type: "POST",
        url: "Forms.asmx/GetForm",
        data: "{'FormFieldValues':'" + GetHomePageFormValues + "','FormId':'" + GetFormId + "','EmailAddress':'" + EmailAddress + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Replace the div's content with the page method's return.
            $(sHomePageLoader).hide();
            document.getElementById(sHomePageForm).style.padding = sFormPadding;
            $("#" + sHomePageForm).html(msg.d);
        }
    });
}

function CheckEmailUs() {
    var aAllFields = new Array("FirstName", "LastName", "Address", "City", "State", "ZipCode", "EmailAddress", "Phone", "Topic", "ResponseType", "Comments");
    var aEmailFields = new Array("FirstName", "LastName", "EmailAddress", "Comments");
    var aMailFields = new Array("FirstName", "LastName", "Address", "City", "State", "ZipCode", "Comments");
    var aPhoneFields = new Array("FirstName", "LastName", "Phone", "Comments");
    var oResponseType = document.getElementsByName("fldResponseType");
    var iResponseType = oResponseType.length;
    var t;
    var sResponseType = "";
    var sTopic = "";
    var sFieldNames = "";
    var sInputValue = "";
    var sFieldId;
    var sLabelId;
    var bCheckEmailUs = true;
    var sAllLabelId;
    var sPhoneNumber;
    var bCheckPhone = true;

    //Loop all required fields to reset label text formatting
    for (var f = 0; f < aAllFields.length; f++) {
        sAllLabelId = "lbl" + aAllFields[f];
        document.getElementById(sAllLabelId).style.font = "normal 11px trebuchet ms";
        document.getElementById(sAllLabelId).style.color = "#724522";
    }
    
    //Check response type
    for (t = 0; t < iResponseType; t++) {
        
        //Check if radio button is selected
        if (oResponseType.item(t).checked == true) {
            
            //Get selected value
            sResponseType = oResponseType.item(t).value;

            //bResponseType = true;  //Set radio button checked status to true
        }
    }

    //Handle empty response type value
    if (sResponseType == "") {
        //If no response type select, validation fails
        bCheckEmailUs = false;
        sFieldNames = "Respond By\n";
        //Set response type text to red
        document.getElementById("lblResponseType").style.font = "bold 12px trebuchet ms";
        document.getElementById("lblResponseType").style.color = "#990000";
    }
    
    //Handle selected response type value
    if (sResponseType != "") {
    
        //Reset response type field label
        document.getElementById("lblResponseType").style.font = "normal 11px trebuchet ms";
        document.getElementById("lblResponseType").style.color = "#724522";
        //Get selected message topic
        sInputValue = document.getElementById("fldTopic").options[document.getElementById("fldTopic").selectedIndex].value;
        
        //Handle empty message topic value
        if (sInputValue == "0") {
            bCheckEmailUs = false;
            sFieldNames += "Message Topic\n";
            document.getElementById("lblTopic").style.font = "bold 12px trebuchet ms";
            document.getElementById("lblTopic").style.color = "#990000";
        }
        else {
            //Reset topic field label
            document.getElementById("lblTopic").style.font = "normal 11px trebuchet ms";
            document.getElementById("lblTopic").style.color = "#724522";
        }
        
        //Handle response type of email
        if (sResponseType == "Email") {
            
            //Loop email fields array
            for (var e = 0; e < aEmailFields.length; e++) {

                sFieldId = "fld" + aEmailFields[e];  //Set field id
                sLabelId = "lbl" + aEmailFields[e];  //Set field label id

                //Reset field label font weight/size/color
                document.getElementById(sLabelId).style.font = "normal 11px trebuchet ms";
                document.getElementById(sLabelId).style.color = "#724522";
                
                //Get input value
                sInputValue = document.getElementById(sFieldId).value;
                
                //Handle empty input value
                if (sInputValue == "") {
                    bCheckEmailUs = false;
                    document.getElementById(sLabelId).style.font = "bold 12px trebuchet ms";
                    document.getElementById(sLabelId).style.color = "#990000";
                }
            }
        }

        //Handle response type of mail
        if (sResponseType == "Mail") {

            //Loop mail fields array
            for (var m = 0; m < aMailFields.length; m++) {
            
                sFieldId = "fld" + aMailFields[m];  //Set field id
                sLabelId = "lbl" + aMailFields[m];  //Set field label id
                
                //Reset field label font weight/size/color
                document.getElementById(sLabelId).style.font = "normal 11px trebuchet ms";
                document.getElementById(sLabelId).style.color = "#724522";
                
                //Handle state drop down menu
                if (aMailFields[m] == "State") {
                    //Get selected state
                    sInputValue = document.getElementById(sFieldId).options[document.getElementById(sFieldId).selectedIndex].value;

                    //Handle empty state value
                    if (sInputValue == "0") {
                        bCheckEmailUs = false;
                        sFieldNames += "Message Topic\n";
                        document.getElementById(sLabelId).style.font = "bold 12px trebuchet ms";
                        document.getElementById(sLabelId).style.color = "#990000";
                    }
                }
                else {                    
                    //Get text input value
                    sInputValue = document.getElementById(sFieldId).value;

                    //Handle empty input value
                    if (sInputValue == "") {
                        bCheckEmailUs = false;
                        document.getElementById(sLabelId).style.font = "bold 12px trebuchet ms";
                        document.getElementById(sLabelId).style.color = "#990000";
                    }
                }
            }
        }

        //Handle response type of phone
        if (sResponseType == "Phone") {
            
            //Loop phone fields array
            for (var p = 0; p < aPhoneFields.length; p++) {
            
                sFieldId = "fld" + aPhoneFields[p];  //Set field id
                sLabelId = "lbl" + aPhoneFields[p];  //Set field label id

                //Reset field label font weight/size/color
                document.getElementById(sLabelId).style.font = "normal 11px trebuchet ms";
                document.getElementById(sLabelId).style.color = "#724522";

                //Handle phone number
                if (aPhoneFields[p] == "Phone") {
                    //Get three phone number field values
                    var sPhoneValue_1 = document.getElementById("fldPhone_1").value;
                    var sPhoneValue_2 = document.getElementById("fldPhone_2").value;
                    var sPhoneValue_3 = document.getElementById("fldPhone_3").value;

                    //Handle empty values
                    if (sPhoneValue_1 == "" || sPhoneValue_2 == "" || sPhoneValue_3 == "") {
                        bCheckEmailUs = false;
                        document.getElementById(sLabelId).style.font = "bold 12px trebuchet ms";
                        document.getElementById(sLabelId).style.color = "#990000";                        
                    }
                    else {
                        //Phone number validation code goes here
                        var oRegex = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
                        sPhoneNumber = "(" + sPhoneValue_1 + ") " + sPhoneValue_2 + "-" + sPhoneValue_3;

                        if (!oRegex.test(sPhoneNumber)) {
                            bCheckPhone = false;
                            document.getElementById(sLabelId).style.font = "bold 12px trebuchet ms";
                            document.getElementById(sLabelId).style.color = "#990000";  
                            window.alert("A valid phone number using the following format must be entered:\nxxx xxx-xxxx");
                        }
                    }
                    
                }
                else {                
                    //Get text input value
                    sInputValue = document.getElementById(sFieldId).value;

                    //Handle empty input value
                    if (sInputValue == "") {
                        bCheckEmailUs = false;
                        document.getElementById(sLabelId).style.font = "bold 12px trebuchet ms";
                        document.getElementById(sLabelId).style.color = "#990000";
                    }
                }
            } 
        }
        
    }

    if (bCheckEmailUs == false) {
        if (bCheckPhone == true) {
            window.alert("Required fields in this form have been skipped.  Please complete the fields marked with bold, red text.");
        }
    }
    else {

        //Make sure email address is valid
        if (document.getElementById("fldEmailAddress").value != "") {
            if (ValidateEmailAddress('EmailAddress') == false) {
                document.getElementById("lblEmailAddress").style.font = "bold 12px trebuchet ms";
                document.getElementById("lblEmailAddress").style.color = "#990000";

                window.alert("The email address you entered is not valid");

            }
            else {
                document.forms["aspnetForm"].submit();
            }   
        }
        else {
            document.forms["aspnetForm"].submit();
        }
    }
}

function ValidateEmailAddress(FieldId) {
    var sValidateFieldId = "fld" + FieldId;
    var sValidateEmailAddress = document.getElementById(sValidateFieldId).value;

    if (sValidateEmailAddress != "") {
        var iIndex = sValidateEmailAddress.indexOf("@");
        if (iIndex == -1) {
            return false;
        }
        else if (sValidateEmailAddress.indexOf(".", iIndex) == -1) {
            return false;
        }
    }
    return true;
}

function JumpToField(FieldId, JumpTo, MaxLength) {
    var sJumpToField = document.getElementById(FieldId).value;
    var sValueLength = sJumpToField.length;
    //window.alert(document.getElementById(FieldId).value);
    //document.write(sJumpToField);
    if (sValueLength == MaxLength) {
        document.getElementById(JumpTo).focus();
        //document.write("text");
    }    
}

