﻿function GetGrearsPriceInfo() {
    var grearsIDs = GetGrearsIDs();
    if ("" == grearsIDs) {
        $("input[id$='tt_price']").val(0);
        $("input[id$='tt_time']").val(0);
        return;
    }
    AjaxMethodPL.GetGearsPriceInfo
    (
        parseInt($("input[id$='hdServiceConfigID']").val()),
        grearsIDs,
        function (result) {
            if (null == result.error) {
                if (result.value != null) {
                    var princeInfo = eval(result.value);
                    $("span[mark='currencyMark']").text(princeInfo.CurrencyMark);
                    $("input[id$='tt_price']").val(princeInfo.Amount);
                    $("input[id$='tt_time']").val(princeInfo.Days);
                }
            }
        }
    )
}

function GetGrearsIDs() {
    var grearsIDs = "";
    $("input[type='checkbox'][id$='_Gears']").each(function () {
        if ($(this).attr("checked")) {
            grearsIDs += ",";
            grearsIDs += $(this).attr("gearsID");
        }
    })
    if (grearsIDs.length > 0) {
        grearsIDs = grearsIDs.substring(1, grearsIDs.length); //删除第一个","
    }
    return grearsIDs;
}

function GetName() {
    AjaxMethodPL.GetGearsName
    (
        parseInt($("input[id$='hdServiceConfigID']").val()),
        parseInt($("select[id$='DDL_TypeOfSets'] option:selected").val()),
        function (result) {
            if (null == result.error) {
                if (result.value != null) {
                    var dt = result.value;
                    var sel_Name = $("select[id$='DDL_Name']")[0];
                    sel_Name.options.length = 0;
                    for (i = 0; i < dt.Rows.length; ++i) {
                        sel_Name.options.add(new Option(dt.Rows[i].GearsName, dt.Rows[i].GearsID));
                    }
                }
            }
            setTimeout("GearsNameChange()", 10);
        }
    )
}

function GearsNameChange() {
    AjaxMethodPL.GetGearsDetail
    (
        parseInt($("input[id$='hdServiceConfigID']").val()),
        parseInt($("select[id$='DDL_Name'] option:selected").val()),
        function (result) {
            if (null == result.error) {
                if (result.value != null) {
                    var dt = result.value;
                    if (0 == dt.Rows.length) {
                        ResetInfo();
                        return;
                    }
                    for (i = 0; i < dt.Rows.length; ++i) {
                        if (1 == dt.Rows[i].TypeID) {
                            $("input[type='checkbox'][id$='CheckHead_Gears']").attr("gearsID", dt.Rows[i].GearsID);
                        }
                        else if (2 == dt.Rows[i].TypeID) {
                            $("input[type='checkbox'][id$='CheckChest_Gears']").attr("gearsID", dt.Rows[i].GearsID);
                        }
                        else if (3 == dt.Rows[i].TypeID) {
                            $("input[type='checkbox'][id$='CheckShoulder_Gears']").attr("gearsID", dt.Rows[i].GearsID);
                        }
                        else if (4 == dt.Rows[i].TypeID) {
                            $("input[type='checkbox'][id$='CheckHand_Gears']").attr("gearsID", dt.Rows[i].GearsID);
                        }
                        else if (5 == dt.Rows[i].TypeID) {
                            $("input[type='checkbox'][id$='CheckLegs_Gears']").attr("gearsID", dt.Rows[i].GearsID);
                        }
                    }
                }
            }
            GetGrearsPriceInfo();
        }
    )
}

function ResetInfo() {
    $("input[type='checkbox'][id$='CheckHead_Gears']").attr("gearsID", 0);
    $("input[type='checkbox'][id$='CheckChest_Gears']").attr("gearsID", 0);
    $("input[type='checkbox'][id$='CheckShoulder_Gears']").attr("gearsID", 0);
    $("input[type='checkbox'][id$='CheckHand_Gears']").attr("gearsID", 0);
    $("input[type='checkbox'][id$='CheckLegs_Gears']").attr("gearsID", 0);
    $("#Amount").text(0);
    $("#Times").text(0);
    GetGrearsPriceInfo();
}

function Add() {
    var grearsIDs = GetGrearsIDs();
    if ("" == grearsIDs || "0" == $("#Amount").text()) {
        alert("Please select the service");
        return;
    }
    AjaxMethodPL.AddGrearsOrder
    (
        parseInt($("input[id$='hdServiceConfigID']").val()),
        parseInt($("select[id$='DDL_TypeOfSets'] option:selected").val()),
        parseInt($("select[id$='DDL_Name'] option:selected").val()),
        grearsIDs,
        function (result) {
            //函数在common文件里
            AddTemp_CallBack(result);
        }
    )
}
