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

var marginTop_htbOption = "0";
var marginBtm_htbOption_1 = "0";
var marginTop_htbOption_2 = "0";

var htboptionFrameHeight_1 = 0; 
var htboptionFrameHeight_2 = 0;

var frameCounter = 0;

// 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 valiation 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");
            }
        });
    });
    
    //onload in iFrame #1
    $("#htboptionFrame_1").load(loadedFrame1);
    
    //onload in iFrame #2
    $("#htboptionFrame_2").load(loadedFrame2);
    
    //preselect the Product Line select menu
    $("#prodLine").focus();
    
    //display Contact Preferences based on Company Size selection
    $("#coSize").change(displayContactPref);
    $("#prodLine").change(displayContactPref);
               
}); //end initializations


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");
        }
    }
} //end displayContactPref


function loadedFrame1() {
    htboptionFrameHeight_1 = $("#htboptionFrame_1").attr("offsetHeight");
    if(htboptionFrameHeight_1 != 0) {
        if(prodLineValue == "3" || prodLineValue == "5" || prodLineValue == "7" || coSizeValue == '500-999' || coSizeValue == '1000-4999' || coSizeValue == '5000-9999' || coSizeValue == '10000+') {
            $("#htboptionFrame_1").css({height: htboptionFrameHeight_1 + 15});
            htboptionFrameHeight_1 = htboptionFrameHeight_1 + 15;
        }
        //alert(htboptionFrameHeight_1);
        return true;
    }
    else return false;
}

function loadedFrame2() {
    htboptionFrameHeight_2 = $("#htboptionFrame_2").attr("offsetHeight");
    if(htboptionFrameHeight_2 != 0) {
        return true;
    }
    else {
        if(display_ShowListBtn == "no") {
            return true;
        }
        else {
            return false;
        }
    }
} //end loadedFrame2

function clickContinue() {
    if(validateStartForm() != false) {
         postSelectedCriteria();
         createRecommendation();
         $("#starthere").fadeOut("slow", function(){
             $("#recommendation").fadeIn("slow", function(){
                 showRecommendation();
                 $("#btn_revise").fadeIn("slow");
             });
         });
     }          
  return false;
} //end 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;
    }
} //end 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);
}

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) {
        $("#htboptionFrame_1").attr("src", function(){
            return "/us/11166.html";
        });
        $("#htboptionFrame_2").attr("src", function(){
            return "/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=" + prodLineValue;
        });
        $("#recommendation h4").text("We recommend the following SonicWALL Resellers listed below:");
        $("#htboption_1 h4").text("Online Resellers");
        $("#htboption_2 h4").text("Nearby Resellers");
        marginTop_htbOption = "-1em";
        /*@cc_on
        marginBtm_htbOption_1 = "-3em";
        @*/
        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) {
        $("#htboptionFrame_1").attr("src", function(){
            return "/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=" + prodLineValue;
        });
        $("#htboptionFrame_2").attr("src", function(){
            return "/us/11166.html";
        });
        $("#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");
        marginTop_htbOption = "-1em";
        /*@cc_on
        marginBtm_htbOption_1 = "-2em";
      @*/
        display_HTBOption_1 = "yes";
        display_HTBOption_2 = "no";
        display_ShowListBtn = "yes";    
        $("#btn_showlist a").click(function(){
            $("#btn_showlist").fadeOut("fast", function(){
                $("#htboption_2 h4").fadeIn("slow");
                $("#htboption_2").animate({marginTop:"0"},1000);
                $("#htboption_2 div").animate({height: htboptionFrameHeight_2}, 1000, function(){
                    var offsetTarget = $("#htboption_2").offset().top - 20;
                    $("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+')) {
        $("#htboptionFrame_1").attr("src", function(){
            if(coSizeValue == "1-24") {
                return "/us/11435.html";
            }
            else {
                return "/us/11170.html";
            }            
        });
        $("#recommendation h4").text("We recommend you contact SonicWALL Sales directly via the form below:");
        $("#htboption_1 h4").text("Contact SonicWALL Sales");
        marginTop_htbOption = "-1em";
        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+') {
        $("#htboptionFrame_1").attr("src", function(){
            return "/us/11170.html";
        });
        $("#htboptionFrame_2").attr("src", function(){
            return "/partnerLocator/ResellerListing.aspx?zip=" + zipValue + "&rmile=500" + "&p=" + prodLineValue;
        });
        $("#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");
        marginTop_htbOption = "-1em";
        /*@cc_on
        marginBtm_htbOption_1 = "0";
        @*/
        marginTop_htbOption_2 = "-3em";
        display_HTBOption_1 = "yes";
        display_HTBOption_2 = "no";
        display_ShowListBtn = "yes";
        $("#btn_showlist .btn_label").text("view Nearby Resellers");
        $("#btn_showlist a").click(function(){
            $("#btn_showlist").fadeOut("fast", function(){
                $("#htboption_2 h4").fadeIn("slow");
                $("#htboption_2").animate({marginTop: marginTop_htbOption_2}, 1000);                
                $("#htboption_2 div").animate({height: htboptionFrameHeight_2}, 1000, function(){
                    var offsetTarget = $("#htboption_2").offset().top - 20;
                    $("html, body").animate({scrollTop: offsetTarget}, "slow");
                });
            });
            return false;
        });
    }
}

function showRecommendation(){
    if(loadedFrame1() == true && loadedFrame2() == true) {
    
    if(display_HTBOption_1 == 'yes') {
        $("#htboption_1 h4").fadeIn("medium", function(){
            /*@cc_on
        $("#htboption_1").css({marginBottom: marginBtm_htbOption_1});
        @*/
            $("#htboption_1 div").animate({height: htboptionFrameHeight_1}, 1000, function(){
                if(marginTop_htbOption != "") {
                    $(".htboption div").animate({marginTop: marginTop_htbOption}, "medium");
                }
                var offsetTarget = $("#pgdialog").offset().top - 10;
                $("html, body").animate({scrollTop: offsetTarget}, "slow");    
            });
            if(display_ShowListBtn == 'yes') {
                $("#btn_showlist").fadeIn("slow");
            }
        });
    }
                
    if(display_HTBOption_2 == 'yes') {
        $("#htboption_2 h4").fadeIn("medium", function(){
            $("#htboption_2 div").animate({height: htboptionFrameHeight_2}, 1000);
            $("#htboption_2").animate({marginTop:"-1em"}, 500);
        });     
    }
    
    }
    else {
        if(frameCounter <= 30) {
            frameCounter = frameCounter + 1;
            loadedFrame1();
            loadedFrame2();
            setTimeout("showRecommendation()", 300);
        }
        else {
            return false;
        }
    }
}
 
function closeRecommendation() {    
    $("html, body").animate({scrollTop: 0}, "slow");
    $("#btn_showlist").fadeOut("fast");
    $("#btn_showlist a").unbind("click");
    $("#htboption_2").animate({marginTop:0},500);
    $(".htboption div").animate({marginTop: "0"}, "medium");
    /*@cc_on
        $("#htboption_1").animate({marginBottom: "0"}, "slow");
    @*/
    $(".htboption div").animate({height: 0},1000, function(){
        $("#btn_revise").fadeOut("fast");
        $(".htboption h4").fadeOut("slow");
        $("#recommendation").fadeOut("slow", function(){    
           $("#starthere").fadeIn("slow");
            $(".htboption iframe").css({height: 0});
        });
        marginTop_htbOption = "0";
        marginBtm_htbOption_1 = "0";
        marginTop_htbOption_2 = "0";
        htboptionFrameHeight_1 = "0";
        htboptionFrameHeight_2 = "0";
     });
}//end closeRecommendation