﻿$(document).ready(function () {
    if ($.cookie("cf_hideIntroWidow") == "true") {
        $("[id$='searchIntroWrapper']").hide();
    } else {
        $("[id$='searchIntroWrapper']").css("display", "block");
        $("[id$='searchIntroWrapper']").animate({ opacity: 1 }, 250);
    }
});

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45)
        return false;

    return true;
}

function searchHandler() {
    var vars = "?t=0";

    if ($('[id$="nameSearch"]').val()) {
        vars += "&n=" + $('[id$="nameSearch"]').val();
    }

    if ($('[id$="zipCode"]').val()) {
        vars += "&z=" + $('[id$="zipCode"]').val();
    } else {
        if ($('[id$="city"]').val()) {
            vars += "&c" + $('[id$="city"]').val();
        }
    
    }

    window.location = "/search.aspx"+vars;
}

function updateSearchTxt() {
    var searchString = "Search for ";

    $('[id$="cityWrapper"]').css("color", "#FFFFFF");
    $('[id$="stateWrapper"]').css("color", "#FFFFFF");
    $('[id$="city"]').attr("disabled", "");
    $('[id$="state"]').attr("disabled", "");

    if ($('[id$="dealership"]').is(':checked')) {
        searchString += "dealership(s) ";

        if ($('[id$="makes"]').val()) {
            searchString += "of make " + $('[id$="makes"] option:selected').text()  + " "
        }

    } else {
        searchString += "an employee(s) ";
    }

    if ($('[id$="nameSearch"]').val()) {
        searchString += "with name \"" + $('[id$="nameSearch"]').val() + "\" ";
    }

    if (!$('[id$="zipCode"]').val() && !$('[id$="state"]').val() && !$('[id$="city"]').val()) {
        searchString += "anywhere";
    } else {
        searchString += "within " + $('[id$="range"]').val() + " miles of ";

        if ($('[id$="zipCode"]').val()) {
            $('[id$="cityWrapper"]').css("color", "#333333");
            $('[id$="stateWrapper"]').css("color", "#333333");
            $('[id$="city"]').attr("disabled", "disabled");
            $('[id$="state"]').attr("disabled", "disabled");
            searchString += "zipcode " + $('[id$="zipCode"]').val();
        } else {
            if ($('[id$="city"]').val()) {
            searchString += $('[id$="city"]').val() + "";
            }

            if ($('[id$="state"]').val()) {
                if($('[id$="city"]').val()) {
                    searchString += ", ";
                }

                searchString += $('[id$="state"]').val()
            }
        
        }
    }

    searchString += ".";
    $("#searchString").html(searchString);
}

function hideSearchIntro() {
    $("[id$='searchIntroWrapper']").fadeOut("normal");

    if ($("#ds").is(":checked") || $("#ds1").is(":checked")) {
        $.cookie("cf_hideIntroWidow", "true");
    }
}
