﻿var ShouldGetStoreList = false;

function GetStoreList() {
    var zip = $j("#txtHeaderZip");
    var states = $j("select[simpleId = ddStates]");
    var selectedIndex = states.attr("selectedIndex")
    var type;

    $j("#divInvalidZipMessage").hide();

    if (selectedIndex == 0 || (zip.val().length != 0 && selectedIndex != 0)) {
        ValidateZip();
        type = "zip";
        value = zip.val();
    }
    else {
        type = "state";
        value = states.val();
        ShouldGetStoreList = true;
    }

    if (ShouldGetStoreList) {

        $j.ajax({
            async: true,
            type: "POST",
            url: "/data/AdvancedSearchLookup.aspx/GetStoreList",
            data: "{'type' : '" + type + "','value' : '" + value + "','path' : '" + window.location.href + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            timeout: 3000,
            success: function (response) { ret = GetStoreListSuccess(response); },
            error: function (response) { ret = GetStoreListFail(response); }
        });
    }

    return true;
}

function GetStoreListSuccess(response) {

    $j("#divStoreList").html("");

    //forces scroll bar reset
    setTimeout(function () {

       $j("#divStoreList").html(response.d);
        $j("#third_promo").show();
    
    }, 1);
}

function GetStoreListFail(response) {
    /* swallow error, no client side action */
}


function ValidateZip() {
    $j.ajax({
        async: false,
        type: "POST",
        url: "/data/AdvancedSearchLookup.aspx/IsValidZip",
        data: "{'zip': '" + $j("#txtHeaderZip").val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 3000,
        success: function (response) { ret = ValidateZipSuccess(response); },
        error: function () { ret = ValidateZipFail(); }
    });

    return true;
}

function ValidateZipSuccess(response) {
    if (response.d == false) {
        $j("#divInvalidZipMessage").show();
        $j("#third_promo").hide();
        ShouldGetStoreList = false;
    }
    else {
        $j("#divInvalidZipMessage").hide();
        ShouldGetStoreList = true;
    }
}

function ValidateZipFail() {
    /* swallow error, no client side action */
}

var ChangeClick = function (event) {
    event.preventDefault();
    event.stopPropagation();
    ContactClose();
    $j("#top_promo_img").show();
    $j("#second_promo").show();
    $j("#txtHeaderZip").focus();
}

var ContactClick = function () {
    /* event.preventDefault(); */
    /* event.stopPropagation(); */
    ChangeClose();
    $j("#top_promo_img_contact").show();
    $j("#top_promo_contact").show();
}

var ChangePressEnter = function (event) {
    var code = (event.keyCode ? event.keyCode : event.which);
    if (code == 13) {
        $j("#aGetStoreList").click();
        return false;
    }
}

function ChangeClose() {
    $j("#top_promo_img").hide();
    $j("#second_promo").hide();
    $j("#third_promo").hide();
    $j("#nearestStoreInvalidZip").hide();
    $j("#txtHeaderZip").val("");
    $j("select[simpleId = ddStates]").get(0).selectedIndex = 0;
    $j("#divStoreList").html("");
}

function ContactClose() {
    $j("#top_promo_img_contact").hide();
    $j("#top_promo_contact").hide();
}
