$(function () {
    var BuyNum = $("#BuyNum"), agreement = $("#agreement"),
    allFields = $([])
    .add(agreement).add(BuyNum)
    ;
    function ValidTemp() {
        var bValid = true;
        allFields.removeClass("ui-state-error"); bValid = bValid && checkNumlength(BuyNum, "购买数量", 1, 2147483647);
        bValid = bValid && checkCheckBoxIsCheck(agreement, "");
        return bValid;
    }
    $("#CreateForm").submit(function () {
        showDiv("<img src=\"/common/images/loading.gif\">订单处理中，请稍候...", false);
        //return false;
    });
    $("a[name=CreateSubmit]").click(function () {
        if (ValidTemp()) {
            $("#CreateForm").submit();
        }
    });
    /*数据初始化开始*/
    var DefaultMonPrice = 0;
    var DefaultThPrice = 0;
    var DefaultHfPrice = 0;
    var DefaultYearPrice = 0;
    if ($(".RelMon").length > 0) {
        DefaultMonPrice = $(".RelMon").html() - 0;
    }
    if ($(".RelTh").length > 0) {
        DefaultThPrice = $(".RelTh").html() - 0;
    }
    if ($(".RelHf").length > 0) {
        DefaultHfPrice = $(".RelHf").html() - 0;
    }
    if ($(".RelYear").length > 0) {
        DefaultYearPrice = $(".RelYear").html() - 0;
    }
    //初始化订单价格
    var BuyType = $("[name=BuyType]").val();

    var BuyTypeHolderName = "";//购买方式容器名称
    if (BuyType == "1")//月付
    {
        BuyTypeHolderName = "Mon";
    }
    else if (BuyType == "2")//季付
    {
        BuyTypeHolderName = "Th";
    }
    else if (BuyType == "3")//半年付
    {
        BuyTypeHolderName = "Hf";
    }
    else if (BuyType == "4")//年付
    {
        BuyTypeHolderName = "Year";
    }
    $(".PayPrice").html($(".Rel" + BuyTypeHolderName).html());//填充订单价格
    /*初始化结束*/
    $(".ProAttr li").mouseover(function () {
        $(this).css("background-color", "#eee");
    });
    $(".ProAttr li").mouseout(function () {
        $(this).css("background-color", "");
    });
    $(".slider").slider({
        range: "min",
        step: 1,
        min: 0,
        max: 100,
        slide: function (event, ui) {
            var DefaultValue = $(this).parent().find(".defaultvalue").html() - 0;
            var Setp = $(this).parent().find(".step").html() - 0;
            $(this).parent().find(".slidervalue").val(ui.value * Setp);
            $(this).parent().find(".showvalue").html(DefaultValue + ui.value * Setp);
            CheckBuyMoney();//调用价格查询事件
        }
    });
    $("[name=UseFree],[name=BuyType]").change(function () {//试用按钮、付款方式
        CheckBuyMoney();
    });
    $("[name=BuyNum]").keyup(function () {//购买期限
        CheckBuyMoney();
    });
    $("[name=PlaceId]").change(function () {//服务器选择事件
        //获取默认价格
        CommonEventAjax(5000, "/common/ashx/product.ashx", { "Action": "GetPriceValues", "PlaceId": $("[name=PlaceId]").val(), "Id": $(".DiyPriceData").attr("ProId") }, function (bdata) {
            //alert(bdata);
            var DataArr = bdata.split("|||");
            var Prices = DataArr[0].split(',');
            var ReplaceString = "<span class=\"price [PriceType]\">[Price]</span>";//要替换处理的字符串
            var PriceString = "";
            //得到价格字符串
            if (Prices[0] - 0 > 0) {
                PriceString += ReplaceString.replace("[PriceType]", "RelMon").replace("[Price]", Prices[0]) + "元/月 ";
            }
            if (Prices[1] - 0 > 0) {
                PriceString += ReplaceString.replace("[PriceType]", "RelTh").replace("[Price]", Prices[1]) + "元/季 ";
            }
            if (Prices[2] - 0 > 0) {
                PriceString += ReplaceString.replace("[PriceType]", "RelHf").replace("[Price]", Prices[2]) + "元/半年 ";
            }
            if (Prices[3] - 0 > 0) {
                PriceString += ReplaceString.replace("[PriceType]", "RelYear").replace("[Price]", Prices[3]) + "元/年 ";
            }
            $("#DefaultProuctValue").html(PriceString);//填充价格字符串
            //默认值填充以及得到购买方式下拉框
            var BuyTypeSelectString = "";
            if ($(".RelMon").length > 0) {
                DefaultMonPrice = $(".RelMon").html() - 0;
                BuyTypeSelectString += "<option value=\"1\">月付</option>";
            }
            if ($(".RelTh").length > 0) {
                DefaultThPrice = $(".RelTh").html() - 0;
                BuyTypeSelectString += "<option value=\"2\">季付</option>";
            }
            if ($(".RelHf").length > 0) {
                DefaultHfPrice = $(".RelHf").html() - 0;
                BuyTypeSelectString += "<option value=\"3\">半年付</option>";
            }
            if ($(".RelYear").length > 0) {
                DefaultYearPrice = $(".RelYear").html() - 0;
                BuyTypeSelectString += "<option value=\"4\">年付</option>";
            }
            $("[name=BuyType]").html(BuyTypeSelectString);
            //重新填充服务器DIY价格配置数据包
            $(".DiyPriceData").html(DataArr[1]);
            CheckBuyMoney();//重新计算价格


        });
    });
    function CheckBuyMoney() {//价格查询事件

        //自定义选项值
        var Diy_IpNum = 0;
        var Diy_BandWidth = 0;
        var Diy_Flow = 0;
        var Diy_CPUNum = 0;
        var Diy_SATA = 0;
        var Diy_DDR = 0;
        //可选对象容器
        var holder_IpNum = $(".slidervalue[name=IpNum]");

        var holder_BandWidth = $(".slidervalue[name=BandWidth]");
        var holder_Flow = $(".slidervalue[name=Flow]");
        var holder_CPUNum = $(".slidervalue[name=CPUNum]");
        var holder_SATA = $(".slidervalue[name=SATA]");
        var holder_DDR = $(".slidervalue[name=DDR]");
        //取得DIY值
        if (holder_IpNum.length > 0) {
            Diy_IpNum = holder_IpNum.val();
        }
        if (holder_BandWidth.length > 0) {
            Diy_BandWidth = holder_BandWidth.val();
        }
        if (holder_Flow.length > 0) {
            Diy_Flow = holder_Flow.val();
        }
        if (holder_CPUNum.length > 0) {
            Diy_CPUNum = holder_CPUNum.val();
        }
        if (holder_SATA.length > 0) {
            Diy_SATA = holder_SATA.val();
        }
        if (holder_DDR.length > 0) {
            Diy_DDR = holder_DDR.val();
        }
        //计算各DIY价格
        var PriceData = $(".DiyPriceData");

        if ($(".RelMon").length > 0) {

            var MonPrice = DefaultMonPrice
                + PriceData.find(".IPs .MonPrice").html() * Diy_IpNum
                + PriceData.find(".BandWidth .MonPrice").html() * Diy_BandWidth
                + PriceData.find(".Flow .MonPrice").html() * Diy_Flow
                + PriceData.find(".VPSCPUPrice .MonPrice").html() * Diy_CPUNum
                + PriceData.find(".VPSSATAPrice .MonPrice").html() * Diy_SATA
                + PriceData.find(".VPSDDRPrice .MonPrice").html() * Diy_DDR;
            MonPrice = Math.round(MonPrice * 10000) / 10000;
            $(".RelMon").html(MonPrice);

        }
        if ($(".RelTh").length > 0) {
            var ThPrice = DefaultThPrice
                + PriceData.find(".IPs .ThPrice").html() * Diy_IpNum
                + PriceData.find(".BandWidth .ThPrice").html() * Diy_BandWidth
                + PriceData.find(".Flow .ThPrice").html() * Diy_Flow
                + PriceData.find(".VPSCPUPrice .ThPrice").html() * Diy_CPUNum
                + PriceData.find(".VPSSATAPrice .ThPrice").html() * Diy_SATA
                + PriceData.find(".VPSDDRPrice .ThPrice").html() * Diy_DDR;
            ThPrice = Math.round(ThPrice * 10000) / 10000;
            $(".RelTh").html(ThPrice);
        }
        if ($(".RelHf").length > 0) {
            var HfPrice = DefaultHfPrice
                + PriceData.find(".IPs .HfPrice").html() * Diy_IpNum
                + PriceData.find(".BandWidth .HfPrice").html() * Diy_BandWidth
                + PriceData.find(".Flow .HfPrice").html() * Diy_Flow
                + PriceData.find(".VPSCPUPrice .HfPrice").html() * Diy_CPUNum
                + PriceData.find(".VPSSATAPrice .HfPrice").html() * Diy_SATA
                + PriceData.find(".VPSDDRPrice .HfPrice").html() * Diy_DDR;
            HfPrice = Math.round(HfPrice * 10000) / 10000;
            $(".RelHf").html(HfPrice);
        }
        if ($(".RelYear").length > 0) {
            var YearPrice = DefaultYearPrice
                + PriceData.find(".IPs .YearPrice").html() * Diy_IpNum
                + PriceData.find(".BandWidth .YearPrice").html() * Diy_BandWidth
                + PriceData.find(".Flow .YearPrice").html() * Diy_Flow
                + PriceData.find(".VPSCPUPrice .YearPrice").html() * Diy_CPUNum
                + PriceData.find(".VPSSATAPrice .YearPrice").html() * Diy_SATA
                + PriceData.find(".VPSDDRPrice .YearPrice").html() * Diy_DDR;
            YearPrice = Math.round(YearPrice * 10000) / 10000;
            $(".RelYear").html(YearPrice);
        }

        //填充本次订单价格
        if ($("[name=UseFree]").attr("checked")) {//如果是试用，则填充本次价格为0元
            $(".PayPrice").html("0");//填充订单价格
        } else {
            BuyType = $("[name=BuyType]").val();
            if (BuyType == "1")//月付
            {
                BuyTypeHolderName = "Mon";
            }
            else if (BuyType == "2")//季付
            {
                BuyTypeHolderName = "Th";
            }
            else if (BuyType == "3")//半年付
            {
                BuyTypeHolderName = "Hf";
            }
            else if (BuyType == "4")//年付
            {
                BuyTypeHolderName = "Year";
            }
            $(".PayPrice").html($(".Rel" + BuyTypeHolderName).html() * $("[name=BuyNum]").val());//填充订单价格
        }
    }
});