var groupDP = $('#sgroup');
var sectionsDP = $('#ssection');
var sideDP = $('#sson');
var amountTable = $("#PriceList");
var amountDP = $("#amount");
var priceDiv = $("#price");
var totalDiv = $("#total");
var discountDiv = $("#discount");

$(document).ready(function(){
    groupDP.change(function(){
        fAmount(r[groupDP.val()]);
        BindSection(s[groupDP.val()]);        
    })
    
    sectionsDP.change(function(){
        BindSectionSon(s[groupDP.val()][sectionsDP[0].selectedIndex][2]);
    })
    sideDP.change(function(){
        fList();
    })
    amountDP.change(function(){
        fList();
    })
	$("#test").click(function(){
		sectionsDP.focus();
	})	
})


function BindSection(result){
    disabled();
    
    if (result == '' || result == null) {
        return;
    }
    sectionsDP[0].options.length = 0;
    
    for (i = 0; i < result.length; i++) {
    
        sectionsDP[0].options[i] = new Option(result[i][1], result[i][0]);
    }    
    BindSectionSon(result[sectionsDP[0].selectedIndex][2]);
}

function BindSectionSon(result){

    if (result == '' || typeof result != "object") {
        sideDP[0].options.length = 0;
        sideDP[0].style.display = 'none';
        enabled();
        fList();
        return;
    }
    else {
        sideDP[0].style.display = '';
    }
    
    //读取上次选中的二级分类的数据
    var tempLength = sideDP[0].options.length;
    var selectedValue = sideDP[0].selectedIndex;
    
    sideDP[0].options.length = 0;
    
    for (i = 0; i < result.length; i++) {
        sideDP[0].options[i] = new Option(result[i][1], result[i][0]);
    }
    
    //设定上次选中的二级分类
    if (tempLength == result.length && selectedValue > 0) {
        sideDP[0].selectedIndex = selectedValue;
    }
    fList();
    enabled();
}

function fList(){
    
    if (sideDP[0].style.display == "none") {
        var s1 = s[groupDP.val()][sectionsDP[0].selectedIndex][2];
    }
    else {
        var s1 = s[groupDP.val()][sectionsDP[0].selectedIndex][2][sideDP[0].selectedIndex][2];
    }
    
    var r2 = r[groupDP.val()];
    var tHead = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"PriceTable\"><tr><td colspan=\"2\" class=\"head\">" + vdname + "</td></tr><tr><td width=\"50%\" nowrap=\"nowrap\" class=\"head\">" + aname + "</td><td bgcolor=\"#FFFFFF\" class=\"head\">" + tname + "</td></tr>";
    var tFoot = "</table>";
    var tBody = "";
    
    for (i = 0; i < r2.length; i++) {
        tBody += "<tr><td class=\"amount\">" + r2[i][0] + " " + r2[i][1] + "</td><td bgcolor=\"#FFFFFF\" class=\"price\">" + cs + " " + (r2[i][0] * s1 * r2[i][2]).toFixed(2) + "</td></tr>";
    }
    amountTable.html(tHead + tBody + tFoot);
    
    priceDiv.html(cs + " " + s1);
    totalDiv.html(cs + " " + (amountDP.val() * s1 * r2[amountDP[0].selectedIndex][2]).toFixed(2));
    discountDiv.html(cs + " " + ((amountDP.val() * s1 * r2[amountDP[0].selectedIndex][2]) - amountDP.val() * s1).toFixed(2))
}

function fAmount(a){
    if (a == "" || a == undefined) {
        amountDP[0].options.length = 0;
        disabled();
        return;
    }
    
    var n = amountDP[0].options.length;
    var s = amountDP[0].selectedIndex;
    
    amountDP[0].options.length = 0;
    
    for (i = 0; i < a.length; i++) {
        amountDP[0].options[i] = new Option(a[i][0] + " " + a[i][1], a[i][0]);
    }
    
    if (n == a.length && s > 0) 
        amountDP[0].selectedIndex = s;
    else 
        amountDP[0].options[Math.ceil(amountDP[0].options.length / 2)].selected = true;
    //amountChange();			
}

function disabled(){
    sectionsDP[0].disabled = true;
    sideDP[0].disabled = true;
    amountDP[0].disabled = true;
}

function enabled(){
    sectionsDP[0].disabled = false;
    sideDP[0].disabled = false;
    amountDP[0].disabled = false;
}

