﻿function BindModelDropDown(makeControlId, modelControlId) {
    var ret = true;
    var context = modelControlId;

    if ($j("#" + makeControlId).val() == "0") {
        $j("#" + modelControlId).find("option").remove();
        InitializeDropDowns({ "0": modelControlId });
    }

    $j.ajax({
        async: true,
        type: "POST",
        url: "/data/AdvancedSearchLookup.aspx/GetModels",
        data: "{'makeId': '" + $j("#" + makeControlId).val() + "'}",
        context: context,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 3000,
        success: function (response) { ret = GetModelsSuccess(response, this.context); },
        error: function (response) { ret = GetModelsFail(response, this.context); }
    });
    return ret;
}

function GetModelsSuccess(response, context) {
    $j("#" + context).find("option").remove();

    for (i = 0; i < response.d.length; i++) {
        $j("#" + context).append($j("<option></option>").val(response.d[i].Id).html(response.d[i].Name));
    }

    $j("#" + context).removeAttr("disabled");
}

function GetModelsFail(response, context) { /* no action required */ }
    
function BindMakeDropDowns(controlList) {
    $j.ajax({
        async: true,
        type: "POST",
        url: "/data/AdvancedSearchLookup.aspx/GetMakes",
        data: "{}",
        context: controlList,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 3000,
        success: function (response) { ret = GetMakesSuccess(response,this.context); },
        error: function (response) { ret = GetMakesFail(response, this.context); }
    });
    return true;
}

function GetMakesSuccess(response, context) {
    if (context != null && response.d) {
        $j.each(context, function (key, value) {
            for (i = 0; i < response.d.length; i++) {
                $j("#" + value).append($j("<option></option>").val(response.d[i].Id).html(response.d[i].Name));
            }
        });
    }
}

function GetMakesFail(response, context) {
    InitializeDropDowns({ "0": "ddMake1", "1": "ddMake2", "2": "ddMake3" });
}

function InitializeMakeModel(zipId) {
    BindMakeDropDowns({ "0": "ddMake1", "1": "ddMake2", "2": "ddMake3" });
    InitializeDropDowns({ "0": "ddModel1", "1": "ddModel2", "2": "ddModel3" });
    SetZip(zipId);
    ClearValues();
}

function InitializeDropDowns(controlList) {
    $j.each(controlList, function (key, value) {
        $j("#" + value).append($j("<option></option>").val("0").html("Show me all"));
    });
}

function SetEnabled(targetId) {
    $j("#" + targetId).removeAttr("disabled");
}

function CopyValue(clientId, sourceId) {
    var o = $j("input[clientid=" + clientId + "]");
    var i = $j("#" + sourceId);

    o.val(i.val());
}

function ClearValues() {
    $j("input[clientid]").each(function (index) {
        $j(this).val("0");
    });
}

function SetErrMsg(zip, errId) {
    var err = $j("#" + errId);
    err.text("\"" + zip + "\"" + " is not a valid ZIP code.");
    setTimeout("RemoveErrMsg(\"" + errId + "\")", 2500);
}

function RemoveErrMsg(errId) {
    var err = $j("#" + errId);
    err.text("");
}

function SetZip(clientId) {
    var i = document.cookie.indexOf('Zip=');
    if (i > -1) {
        var z = document.cookie.substr(i + 4, 5);
        if (document.getElementById && !isNaN(z)) {
            var t = document.getElementById(clientId);
            t.value = z;
        }
    }
}

function CheckZip(zipId, errId) {
    var context = zipId + "|" + errId;

    $j.ajax({
        async: true,
        type: "POST",
        url: "/data/AdvancedSearchLookup.aspx/IsvalidZip",
        data: "{'zip': '" + $j("#" + zipId).val() + "'}",
        context: context,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 3000,
        success: function (response) { ret = CheckZipSuccess(response, this.context); },
        error: function (response) { ret = CheckZipFail(response, this.context); }
    });
    return true;
}

function CheckZipSuccess(response, context) {
    if (response.d == false) {

        var zipControlId;
        var errControlId;

        if (context != null && context != '') {

            var qs = context.split('|')
            zipControlId = qs[0];
            errControlId = qs[1];
            var box = $j("#" + zipControlId);
            SetErrMsg(box.val(), errControlId);
            box.removeAttr("onfocus");
            SetZip(zipControlId);
            box.focus();
            box.select();
        }
    }
}

function CheckZipFail(response, context) { /*no action required*/ }
