$(function () {
    var BuyNum = $("#BuyNum"),
    db_User = $("#db_User"),
    db_Name = $("#db_Name"),
    PassWord = $("#PassWord"),
    RePassWord = $("#RePassWord"),
    agreement = $("#agreement"),
    allFields = $([])
    .add(BuyNum)
    .add(db_User)
    .add(db_Name)
    .add(PassWord)
    .add(RePassWord)
    .add(agreement)
    ;
    function ValidTemp() {
        var bValid = true;
        allFields.removeClass("ui-state-error");
        bValid = bValid && checkNumlength(BuyNum, "购买数量", 1, 2147483647);
        bValid = bValid && checkRegexp(db_User, Regex_EnUserName, "登录名格式错误");
        bValid = bValid && checkRegexp(db_Name, Regex_EnUserName, "数据库名格式错误");
        bValid = bValid && checkLength(PassWord, "登录密码", 6, 15);
        bValid = bValid && checkEquals("登录密码", PassWord, RePassWord);
        bValid = bValid && checkCheckBoxIsCheck(agreement, "");
        return bValid;
    }
    db_User.keyup(function () {
        db_Name.val(db_User.val());
    });
    $("#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);
            //得到服务器赠送域名字符串并填充
            var ServerSelect = "";
            var FreeDomains = DataArr[1].split(',');
            if (FreeDomains[0] == "") {
                alert("系统错误，该服务器缺少赠送域名设置，请联系管理员解决");
                window.close();
            }
            for (i = 0; i < FreeDomains.length; i++) {
                ServerSelect += "<option value=\"" + FreeDomains[0] + "\">" + FreeDomains[1] + "</option>";
            }
            $("[name=FreeDomain]").html(ServerSelect);
            //重新填充服务器DIY价格配置数据包
            //alert(DataArr[2]);
            $(".DiyPriceData").html(DataArr[2]);
            CheckBuyMoney();//重新计算价格
        });
    });
    function CheckBuyMoney() {//价格查询事件
        //自定义选项值
        var Diy_dbSpace = 0;
        var holder_dbSpace = $(".slidervalue[name=db_MaxSize]");
        if (holder_dbSpace.length > 0) {
            Diy_dbSpace = holder_dbSpace.val();
        }
        //计算各DIY价格
        var PriceData = $(".DiyPriceData");

        if ($(".RelMon").length > 0) {
            var MonPrice = DefaultMonPrice
                + PriceData.find(".Space .MonPrice").html() * Diy_dbSpace;
            MonPrice = Math.round(MonPrice * 10000) / 10000;
            $(".RelMon").html(MonPrice);
        }
        if ($(".RelTh").length > 0) {
            var ThPrice = DefaultThPrice
                + PriceData.find(".Space .ThPrice").html() * Diy_dbSpace;
            ThPrice = Math.round(ThPrice * 10000) / 10000;
            $(".RelTh").html(ThPrice);
        }
        if ($(".RelHf").length > 0) {
            var HfPrice = DefaultHfPrice
                + PriceData.find(".Space .HfPrice").html() * Diy_dbSpace;
            HfPrice = Math.round(HfPrice * 10000) / 10000;
            $(".RelHf").html(HfPrice);
        }
        if ($(".RelYear").length > 0) {
            var YearPrice = DefaultYearPrice
                + PriceData.find(".Space .YearPrice").html() * Diy_dbSpace;
            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());//填充订单价格
        }
    }
});