﻿$(function () {
    initializeBsm(true);
    brandJq.change(function () {
        initializeBsm(false);
        if ($(this).val()) {
            if (typeof (seriesVal) != 'undefined') seriesVal = '';
            if (typeof (modelVal) != 'undefined') modelVal = '';
            brandChange($(this).val());
        }
    });
    if (typeof (modelJq) != 'undefined') {
        seriesJq.change(function () {
            if (typeof (modelJq) != 'undefined') {
                modelJq.html("<option value=''>选择型号</option>");
                modelJq[0].disabled = "disabled";
            }
            if ($(this).val()) {
                if (typeof (modelJq) != 'undefined') {
                    modelVal = '';
                    seriesChange($(this).val());
                }
            }
        })
    }
    if (typeof (brandVal) != 'undefined') {
        if (brandVal) {
            brandJq.val(brandVal);
            brandChange(brandVal);
            if (typeof (modelVal) != 'undefined') seriesChange(seriesVal);
        }
    }
})

function brandChange(bval) {
    if (!bval) return;
    backSeries();
    
    var params = { 'madeid': bval }
    if (typeof (hasCount) != 'undefined' && hasCount) params['hasCount'] = hasCount; //是否包含数字, 默认false
    if (typeof (countType) != 'undefined' && countType) params['countType'] = countType; //0:新旧、1:新、2:旧, 默认1新
    if (typeof (isDealer) != 'undefined' && isDealer) params['isDealer'] = isDealer; //true:经销商、false:车, 默认false车
    $.ajax({
        type: "post",
        url: "/content/js/ajax/GetSeries.ashx",
        data: params,
        success: function(data) {
            if (data.substring(0, 4) == "error") {
                data = '<option value="">加载失败!</option>';
            }
            setTimeout('setSeriesDisable("' + data + '")', 300);
        },
        error: function(data) {

        }
    })
}

function seriesChange(sval) {
    if (!sval) return;
    backModel();

    var params = { 'seriesId': sval }
    if (typeof (hasCount) != 'undefined' && hasCount) params['hasCount'] = hasCount; //是否包含数字, 默认false
    if (typeof (countType) != 'undefined' && countType) params['countType'] = countType; //0:新旧、1:新、2:旧, 默认1新
    if (typeof (isDealer) != 'undefined' && isDealer) params['isDealer'] = isDealer; //true:经销商、false:车, 默认false车
    $.ajax({
        type: "post",
        url: "/content/js/ajax/GetModel.ashx",
        data: params,
        success: function(data) {
            if (data.substring(0, 4) == "error") {
                data = '<option value="">加载失败!</option>';
            }
            setTimeout('setModelDisable("' + data + '")', 300);
        },
        error: function(data) {

        }
    })
}
function setSeriesDisable(data) {
    seriesJq[0].disabled = "";
    seriesJq.html(data);
    if (typeof (seriesVal) != 'undefined') {
        if (seriesVal) {
            try{
            seriesJq.val(seriesVal);
            }catch(ex){}
        }
    }
}
function setModelDisable(data) {
    modelJq[0].disabled = "";
    modelJq.html(data);
    if (typeof (modelVal) != 'undefined') {
        if (modelVal) {
            try {
            modelJq.val(modelVal);
            }catch(ex){}
        }
    }
}
function backSeries() {
    seriesJq.html("<option value=''>正在读取中...</option>");
    seriesJq[0].disabled = "disabled";
}
function backModel() {
    modelJq.html("<option value=''>正在读取中...</option>");
    modelJq[0].disabled = "disabled";
}
function initializeBsm(isRequestLoadBrand) {
    if (isRequestLoadBrand) brandJq.val("");
    if (typeof (seriesJq) != 'undefined') {
        seriesJq.html("<option value=''>选择系列</option>");
        seriesJq[0].disabled = "disabled";
    }
    if (typeof (modelJq) != 'undefined') {
        modelJq.html("<option value=''>选择型号</option>");
        modelJq[0].disabled = "disabled";
    }
}