var display_HTBOption_1 = "";
var display_HTBOption_2 = "";
var display_ShowListBtn = "";

//intializations
$(document).ready(function(){ 

    //reset Criteria on page load
    $("#startForm select, #startForm input").val("");
    $("#coSize").val(oElqUserInfo.companySize);    //elq auto populate
    $("#zip").val(oElqUserInfo.zipPostal);        //elq auto populate

    //click on Continue button 
    $("#starthere #btn_continue a").click(function(){
         clickContinue();
    });
    
    //hit Enter on Start Form 
    $("#startForm").submit(function(){
         clickContinue();
         return false;
    });
  
    //default click on Show List button    
    $("#btn_showlist a").click();
    
    //click on Revise Criteria button
    $("#btn_revise a").click(function(){
        closeRecommendation();
        return false;
    });
    
    //restore styling on Form Validation Error items
    $(".reqd input,.reqd select").change(function(){
        $(this).parent().removeClass("formerror");
    });
    
    //zipcode validation while typing
    $(".zipcode input").each(function(i){
        $(this).keyup(function(){
            var PostalCodeVal = this.value;
            var testPostalCodeVal = /^(\d{5}|\d{9}|\d{5}-\d{4}|[A-Z]\d[A-Z]\s*\d[A-Z]\d)$/.test(PostalCodeVal);
            if((this.value.length != 5 && this.value.length != 10 && this.value.length != 7) || testPostalCodeVal == false) {
                $(this).parent().addClass("formerror");
            }
            else {
                $(this).parent().removeClass("formerror");
            }
        });
    });
    
    //preselect the Product Line select menu
    $("#prodLine").focus();
    
    //display Contact Preferences based on Company Size selection
    $("#coSize").change(displayContactPref);
    $("#prodLine").change(displayContactPref);
               
}); //initializations


function parseResellersResults(data) {
    startPoint = data.indexOf("<form");
    endPoint = data.indexOf("</form>",6);
    newData = data.substring(startPoint, endPoint);
    return newData;
} //parseResellersResults


function displayContactPref() {
    coSizeValue = $("#coSize").val();
    prodLineValue = $("#prodLine").val();
    if((coSizeValue == '1-24' || coSizeValue == '25-49' || coSizeValue == '50-99' || coSizeValue == '100-499') && (prodLineValue != '3' && prodLineValue != '5' && prodLineValue != '7' && prodLineValue != '')) {
        if(coSizeValue == '1-24') {
            $("#fs_contactpref_1 label span").fadeOut("fast").text("Contact me immediately.").fadeIn("medium");
            $("#fs_contactpref_2 label span").fadeOut("fast").text("Locate online and nearby resellers.").fadeIn("medium");
        }
        else {
            $("#fs_contactpref_1 label span").fadeOut("fast").text("Contact me today.").fadeIn("medium");
            $("#fs_contactpref_2 label span").fadeOut("fast").text("Locate nearby resellers.").fadeIn("medium");
        }
        $("#fs_contactpref").show("medium");
    }
    else {
        if(coSizeValue == '500-999' || coSizeValue == '1000-4999' || coSizeValue == '5000-9999' || coSizeValue == '10000+' || coSizeValue == '') {
            $("#fs_contactpref").hide("medium");
        }
        if(prodLineValue == '3' || prodLineValue == '5' || prodLineValue == '7') {
            $("#fs_contactpref").hide("medium");
        }
    }
} //displayContactPref


function clickContinue() {
    if(validateStartForm() != false) {
         postSelectedCriteria();
         createRecommendation();
         $("#starthere").fadeOut("slow", function(){
             $("#recommendation").fadeIn("slow", function(){
                 $("#btn_revise").fadeIn("slow");
             });
         });
     }          
  return false;
} //clickContinue


function validateStartForm() {
    var formError = 0;
    $(".reqd input,.reqd select").each(function(i){
        if(this.value == "") {
            formError = 1;
            $(this).parent().addClass("formerror");
        }
    });
    $(".zipcode input").each(function(i){
        var PostalCodeVal = this.value;
        if(PostalCodeVal.length != 5 && PostalCodeVal.length != 10 && PostalCodeVal.length != 7) {
            $(this).parent().addClass("formerror");
            formError = 1;
        }
        var testPostalCodeVal = /^(\d{5}|\d{9}|\d{5}-\d{4}|[A-Z]\d[A-Z]\s*\d[A-Z]\d)$/.test(PostalCodeVal);
        if(testPostalCodeVal == false) {
            formError = 1;
            $(this).parent().addClass("formerror");
        }
    });
    if(formError == 1) {
        alert("Please complete or correct the field(s) highlighted in red.");
        formError = 0;
        return false;
    }
} //validateStartForm


function postSelectedCriteria() {
    var text_Criteria = $("#prodLine option:selected").text();
    $("#prodLineSelected").val(text_Criteria);
    
    text_Criteria = $("#coSize option:selected").text();
    $("#coSizeSelected").val(text_Criteria);
    
    text_Criteria = $("#zip").val();
    $("#zipSelected").val(text_Criteria);
} //postSelectedCriteria


function createRecommendation() {
    zipValue = $("#zip").val();
    prodLineValue = $("#prodLine").val();
    coSizeValue = $("#coSize").val();
    contactprefValue = document.startForm.contactpref[0].checked;
    
    //recommendation settings for "1-24 Employees" AND "Locate resellers." selections
    if((prodLineValue != "3" || prodLineValue != "5" || prodLineValue != "7") && coSizeValue == '1-24' && contactprefValue != true) {
        $("#htboption_1_content").removeClass("resellers nearby");
        $("#htboption_1_content").addClass("resellers online");
        $("#htboption_1_content").load("/us/11166.html #resellerslist",function(){         
            showRecommendation();
            loadPageTracking("/us/11166.html");
        });
        
        $("#htboption_2_content").removeClass("resellers online");
        $("#htboption_2_content").addClass("resellers nearby");
        $.post("/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=1",function(data){
            $("#htboption_2_content").html(parseResellersResults(data));
            $(".resellers.nearby .viewinfo").live("click",function(){
                $(this).css("display","none");
                $(this).parent().children(".adr,.tel").css("display","block");
                return false;
            });
            showRecommendation();
            loadPageTracking("/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=1");
        });
        $("#recommendation h4").text("We recommend the following SonicWALL resellers listed below:");
        $("#htboption_1 h4").text("Online Resellers");
        $("#htboption_2 h4").text("Nearby Resellers");
        display_HTBOption_1 = "yes";
        display_HTBOption_2 = "yes";
        display_ShowListBtn = "no";
    }
    
    //recommendation settings for "25-49, 50-99 or 100-499 Employees" AND "Locate resellers." selections
    if((prodLineValue != "3" || prodLineValue != "5" || prodLineValue != "7") && (coSizeValue == '25-49' || coSizeValue == '50-99' || coSizeValue == '100-499') && contactprefValue != true) {
        
        $("#htboption_1_content").removeClass("resellers online");
        $("#htboption_1_content").addClass("resellers nearby");
        $.post("/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=1",function(data){
            $("#htboption_1_content").html(parseResellersResults(data));
            showRecommendation();
            loadPageTracking("/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=1");
            $(".resellers.nearby .viewinfo").live("click",function(){
                $(this).css("display","none");
                $(this).parent().children(".adr,.tel").css("display","block");
                return false;
            });
        });
                
        $("#htboption_2_content").removeClass("resellers nearby");
        $("#htboption_2_content").addClass("resellers online");
        $("#htboption_2_content").load("/us/11166.html #resellerslist",function(){
            showRecommendation();   
        });

        $("#recommendation h4").text("We recommend the following SonicWALL resellers listed below:");
        $("#htboption_1 h4").text("Nearby Resellers");
        $("#htboption_2 h4").text("Online Resellers");
        $("#btn_showlist .btn_label").text("view Online Resellers");
        display_HTBOption_1 = "yes";
        display_HTBOption_2 = "no";
        display_ShowListBtn = "yes";    
        $("#btn_showlist a").click(function(){
            loadPageTracking("/us/11166.html");
            $("#btn_showlist").fadeOut("fast", function(){
                $("#htboption_2 h4").fadeIn("slow");
                $("#htboption_2").slideDown(1000,function(){
                    var offsetTarget = $("#htboption_2").offset().top;
                    $("html, body").animate({scrollTop: offsetTarget}, "slow");
                });
            });
        });
    }

    //recommendation settings for "Contact me immediately." selection
    if(contactprefValue == true && (prodLineValue != "3" && prodLineValue != "5" && prodLineValue != "7" && coSizeValue != '500-999' && coSizeValue != '1000-4999' && coSizeValue!= '5000-9999' && coSizeValue != '10000+')) {
        if(coSizeValue == "1-24") {
            var frameURL = "/us/11435.html";
        }
        else {
            var frameURL = "/us/11170.html";
        }            

        $("#htboption_1_content").removeClass("resellers online");
        $("#htboption_1_content").removeClass("resellers nearby");
        $("#htboption_1_content").load(frameURL + " #content",function(){
            showRecommendation();
            loadPageTracking("frameURL");
        });

        $("#recommendation h4").text("We recommend you contact SonicWALL Sales directly via the form below:");
        $("#htboption_1 h4").text("Contact SonicWALL Sales");
        display_HTBOption_1 = "yes";
        display_HTBOption_2 = "no";
        display_ShowListBtn = "no";    
    }
    
    //recommendation settings for "500-999, 1000-4999, 5000-9999 and 10000+ Employees" selection
    if(prodLineValue == "3" || prodLineValue == "5" || prodLineValue == "7" || coSizeValue == '500-999' || coSizeValue == '1000-4999' || coSizeValue == '5000-9999' || coSizeValue == '10000+') {
        $("#htboption_1_content").removeClass("resellers online");
        $("#htboption_1_content").removeClass("resellers nearby");
        $("#htboption_1_content").load("/us/11170.html #content", function(){
            showRecommendation();
            loadPageTracking("/us/11170.html");
        });

        $("#htboption_2_content").removeClass("resellers online");
        $("#htboption_2_content").addClass("resellers nearby");
        $.post("/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=1",function(data){
            $("#htboption_2_content").html(parseResellersResults(data));
            $(".resellers.nearby .viewinfo").live("click",function(){
                $(this).css("display","none");
                $(this).parent().children(".adr,.tel").css("display","block");
                return false;
            });
            showRecommendation();
        });

        $("#recommendation h4").text("We recommend you contact SonicWALL Sales directly via the form below:");
        $("#htboption_1 h4").text("Contact SonicWALL Sales");
        $("#htboption_2 h4").text("Nearby Resellers");
        display_HTBOption_1 = "yes";
        display_HTBOption_2 = "no";
        display_ShowListBtn = "yes";
        $("#btn_showlist .btn_label").text("view Nearby Resellers");
        $("#btn_showlist a").click(function(){
            loadPageTracking("/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=1");
            $("#btn_showlist").fadeOut("fast", function(){
                $("#htboption_2 h4").fadeIn("slow");                
                $("#htboption_2").slideDown(1000, function(){
                    var offsetTarget = $("#htboption_2").offset().top - 20;
                    $("html, body").animate({scrollTop: offsetTarget}, "slow");
                });
            });
            return false;
        });
    }
} //createRecommendation


function showRecommendation(){
    if(display_HTBOption_1 == 'yes') {
        $("#htboption_1 h4").fadeIn(500, function(){
            $("#htboption_1").slideDown(1000, function(){
                var offsetTarget = $("#pgdialog").offset().top - 10;
                $("html, body").animate({scrollTop: offsetTarget}, "slow");    
                if(display_ShowListBtn == 'yes') {
                    $("#btn_showlist").fadeIn(500);
                }
            });
            $("#htboption_1_content").fadeIn(500);
        });
    }
                
    if(display_HTBOption_2 == 'yes') {
        $("#htboption_2 h4").fadeIn(500, function(){
            $("#htboption_2").slideDown(1000);
            $("#htboption_2_content").fadeIn(500);
        });     
    }
} //showRecommendation
 

function closeRecommendation() {    
    $("html, body").animate({scrollTop: 0}, "slow");
    $("#btn_showlist").fadeOut("fast");
    $("#btn_showlist a").unbind("click");
    $(".htboption").slideUp(500,function(){
        $(".htboption_content").empty();
        removePageTracking();
    });
    $("#btn_revise").fadeOut("fast");
    $(".htboption h4").fadeOut("slow");
    $("#recommendation").fadeOut("slow", function(){    
        $("#starthere").fadeIn("slow");
    });
} //closeRecommendation


function loadPageTracking(pageURL) {
    $("body").append("<iframe class='pagetracking' style='width:0; height:0' src='" + pageURL + "'></iframe>");
} //loadPageTracking


function removePageTracking() {
    $(".pagetracking").remove();
} //removePageTracking