$(document).ready(function() {

    var pnlAutomotiveServiceCenter = $("div[id$='pnlAutomotiveServiceCenter']");
    var pnlNonAutomotiveServiceCenter = $("div[id$='pnlNonAutomotiveServiceCenter']");


    if ($("input[id$='rbAutomotiveServiceCenter']:checked").val()) {
        pnlAutomotiveServiceCenter.removeClass("Hidden");
    }
    else {
        pnlNonAutomotiveServiceCenter.removeClass("Hidden");
    }

    $("select[id$='States']").change(function(event) {
        SetCityMode();
    });

    $("input[id$='ZipCode']").keydown(function(event) {
        SetZipMode();
    });

    $("input[id$='City']").keydown(function(event) {
        SetCityMode();
    });

    $("#SearchChoices input[type='radio']").click(function(event) {

        SetSerachCriteriaPanelVisibility();
    });

    $("#automotiveServiceCenterHeader").append($("#sourceAutomotiveServiceCenterHeader"));
    $("#nonAutomotiveServiceCenterHeader").append($("#sourceNonAutomotiveServiceCenterHeader"));

    $("input[type='radio']").click(function() {
        $("input[type='text']").val("");
        $("select[id$='States']").val("Select");
        $("select[id$='ddlVehicleMake']").val('Select a Vehicle Make');
        $("input[type='checkbox']").attr('checked',false);
    });

});

function LoadAutoServiceCenters() {
    
        var strVehicleMake = '';
        var searchResultUrl = $('input[id$=hdnSearchResultUrl]').val();
        var locatorId = $("span:has(input[id$='rbAutomotiveServiceCenter'])").attr('LocatorId');
        var zipCode = $("input[id$='AutoSCZipCode']").val();
        var city = $("input[id$='AutoSCCity']").val();
        var distance = $("select[id$='AutoSCDistance']").val();
        var state = $("select[id$='AutoSCStates']").val();
        if ($("select[id$='ddlVehicleMake']").val() != 'Select a Vehicle Make' &&
            $("select[id$='ddlVehicleMake']").val() != '' && $("select[id$='ddlVehicleMake']").val() != null) 
        {
                strVehicleMake = $("select[id$='ddlVehicleMake']").val();
        }
        var SpecialOffers = '';
        if ($("input[id$='chkAutoSCSpecialOffers']").attr('checked')) {
            SpecialOffers = 'true';
        }
        else
            SpecialOffers = 'false';    
        
        var searchWindow = window.open('', '_blank', 'height=600,width=685,scrollbars=yes,resizable=no,toolbar=no');
        searchWindow.location = searchResultUrl
            + "?Zip=" + zipCode
           + "&Distance=" + distance
            + "&State=" + state
            + "&City=" + city + "&VehicleMake=" + strVehicleMake
            + "&SpecialOffers=" + SpecialOffers
            + "&LocatorId=" + locatorId;
           
           
    }

    function LoadNonAutoServiceCenters() {
        var searchResultUrl = $('input[id$=hdnSearchResultUrl]').val();     
        var zipCode = $("input[id$='NonAutoSCZipCode']").val();
        var city = $("input[id$='NonAutoSCCity']").val();
        var distance = $("select[id$='NonAutoSCDistance']").val();
        var state = $("select[id$='NonAutoSCStates']").val();
        var locatorId = '';
        if ($("input[id$='rbDieselServiceCenter']").attr('checked')) {
            locatorId = $("span:has(input[id$='rbDieselServiceCenter'])").attr('LocatorId');
        }
        if ($("input[id$='rbDieselServiceDealer']").attr('checked')) {
            locatorId = $("span:has(input[id$='rbDieselServiceDealer'])").attr('LocatorId');
        }
        if ($("input[id$='rbHeavyDutyElectricCenter']").attr('checked')) {
            locatorId = $("span:has(input[id$='rbHeavyDutyElectricCenter'])").attr('LocatorId');
        }
        var SpecialOffers = '';
        if ($("input[id$='chkNonAutoSCSpecialOffers']").attr('checked')) {
            SpecialOffers = 'true';
        }
        else
            SpecialOffers = 'false';    
           
        var searchWindow = window.open('', '_blank', 'height=600,width=685,scrollbars=yes,resizable=no,toolbar=no');
        searchWindow.location = searchResultUrl
            + "?Zip=" + zipCode
            + "&Distance=" + distance
            + "&State=" + state
            + "&City=" + city
            + "&LocatorId=" + locatorId
            + "&SpecialOffers=" + SpecialOffers;
    }

function SetCityMode() {
    $("input[id$='ZipCode']").val("");
}

function SetZipMode() {
    $("input[id$='City']").val("");
    $("select[id$='States']").val("Select");
}


function SetSerachCriteriaPanelVisibility() {
    var pnlAutomotiveServiceCenter = $("div[id$='pnlAutomotiveServiceCenter']");
    var pnlNonAutomotiveServiceCenter = $("div[id$='pnlNonAutomotiveServiceCenter']");
    if ($("input[id$='rbAutomotiveServiceCenter']:checked").val()) {

        pnlAutomotiveServiceCenter.removeClass("Hidden");
        pnlAutomotiveServiceCenter.slideDown('slow');
        pnlNonAutomotiveServiceCenter.slideUp('slow');
    }
    else {
        pnlNonAutomotiveServiceCenter.removeClass("Hidden");
        pnlAutomotiveServiceCenter.slideUp('slow');
        pnlNonAutomotiveServiceCenter.slideDown('slow');
    }

}

function ValidateAndLoadAutoServiceCenterResults(sender, args) 
{
    var ctlAutoSCZipcode = $("input[id$='txtAutoSCZipCode']");
    var ctlAutoSCState = $("select[id$='ddlAutoSCStates']");

    if ((ctlAutoSCZipcode.val() == "" ||
        ctlAutoSCZipcode.val() == undefined) &&
        (ctlAutoSCState.val() == "Select" ||
        ctlAutoSCState.val() == undefined)) {
       args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }

    if (args.IsValid) {
        LoadAutoServiceCenters();
    }
}

function ValidateAndLoadNonAutoServiceCenterResults(sender, args) {

    var ctlNonAutoSCZipcode = $("input[id$='txtNonAutoSCZipCode']");
    var ctlNonAutoSCState = $("select[id$='ddlNonAutoSCStates']");

    if ((ctlNonAutoSCZipcode.val() == "" ||
        ctlNonAutoSCZipcode.val() == undefined) &&
        (ctlNonAutoSCState.val() == "Select" ||
        ctlNonAutoSCState.val() == undefined)) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }

    if (args.IsValid)
    {
        LoadNonAutoServiceCenters();
    }
}

function LoadAutoServiceCenterResults() {

    var validatorControl = $("span[id$='cvAutoSCSearchCriteria']");
    ValidatorValidate(validatorControl[0], "automotiveServiceCenterGroup", null);
}

function LoadNonAutoServiceCenterResults() {

    var validatorControl = $("span[id$='cvNonAutoSCSearchCriteria']");
    ValidatorValidate(validatorControl[0], "NonAutomotiveServiceCenterGroup", null);
}
