$(document).ready(function () {
var baseURL = "http://pay.wanlou.com/";
    var teststart = getParam("test");
    $("#wl_divProduct").html("<form class='wl-product-form' id='form_order' target='_blank' name='form_order'  method='post'  action='" + baseURL + "OrderVip.aspx?test=" + teststart + "'> <div class='wl-field radio-wrap'> <label  class='label'>名称：</label> <div class='inputbox' id='divProductGroup'>  &nbsp;</div> </div><div class='wl-field'> <label class='label'>您的姓名：</label> <div  class='inputbox'> <input class='text' type='text' name='txtBuyerName'  id='txtBuyerName' maxlength='30' /></div> </div> <div class='wl-field'> <label  class='label'>手机号码：</label> <div class='inputbox'> <input class='text'  type='text' name='txtBuyerPhone' id='txtBuyerPhone' maxlength='11' /></div>  </div> <div class='wl-field'> <label class='label'>QQ邮箱：</label> <div  class='inputbox'> <input class='text' type='text' name='txtBuyerEmail'  id='txtBuyerEmail' maxlength='50' /></div> </div><div class='wl-field'>  <label class='label'>支付方式：</label> <input id='rblPayMethod' type='radio' value='10' name='rblPayMethod' checked='checked'><label for='rblPayMethod'>支付宝</label> </div><div class='wl-field'> <label  class='label'>您的留言：</label> <div class='inputbox'> <textarea  class='messagebox' name='txtRemark' id='txtRemark'></textarea></div> </div> <div  class='wl-field'> <div class='inputbox'> <input type='hidden' id='hdUcode'  name='hdUcode' value='' /><input name='txtOrderNum'  type='hidden' class='text' id='txtOrderNum' value='1' maxlength='3' /><input type='hidden' id='hdDcode'  name='hdDcode' value='' /><input class='wl-btn-order' type='submit'  value='提交' /></div> </div> </form>");
    //取ucode
    var ucode = getParam("ucode");
    var ducode = form_product_param.ucode;
    $("#hdDcode").val(ducode);
    if (ucode != null && ucode != "null" && ucode != "") {
        $("#hdUcode").val(ucode);
        $("#ShowuCode").text(ucode);
        SetCookies("ucode", ucode, 15);
    }
    else if (GetCookies("ucode") != null) {
        $("#hdUcode").val(GetCookies("ucode"));
        $("#ShowuCode").text(GetCookies("ucode"));
    }
    else {
        $("#hdUcode").val(ducode);
        $("#ShowuCode").text(ducode);
    }
    //表单数据
    $.getJSON(baseURL + "ajax/ProductFormHandler.ashx?callback=?", { pid: form_product_param.pid }, function (data) {
        if (data.Status.Success) {
            BindProductGroup(data.ProductGroup);
        }
        else {
            alert(data.Status.Message);
        }
    });


    //表单验证
    $("#form_order").validate({
        rules: {
           txtBuyerName: {
                required: true
            },
            txtBuyerPhone: {
                required: true,
                checkmobile: true
            },
            txtBuyerEmail: {
                required: true,
                email: true
            }
        },
        messages: {
            txtBuyerName: {
                required: "请输入你的姓名！"
            },
            txtBuyerPhone: {
                required: "请输入手机号码！"
            },
            txtBuyerEmail: {
                required: "请输入你的QQ邮箱！",
                email: "邮箱格式不正确，请重新输入！"
            }
        },
        errorElement: "em",
        success: function (label) {
            label.text("OK！").addClass("success");
        }
    });

});

//检查手机格式是否正确
$.validator.addMethod(
                "checkmobile",
                function (value, element) {
                    var reg = /^(\+86)?(1[0-9]{10})$/;
                    return reg.test(value);
                },
                '手机号码格式不正确，请重新输入！'
            );


//绑定产品
function BindProductGroup(data) {
    var str = "";
    $.each(data, function (idx, item) {
        str += "<p><input name='rblProductGroup' id='rblProductGroup_" + idx + "' type='radio' value='" + item.GroupID + "'  /><label for='rblProductGroup_" + idx + "'>" + item.GroupName + "</label></p>";
    })
    $("#divProductGroup").html(str);
    $("input[name=rblProductGroup]:eq(0)").attr("checked", 'checked');
}

//取参数值
function getParam(a) {
    var c = location.href;
    c = c.substring(c.indexOf("?") + 1, c.length).split("&");
    b = {};
    for (i = 0; j = c[i]; i++) b[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    a = b[a.toLowerCase()];
    return typeof a == "undefined" ? "null" : a.split("#")[0]
}

function GetCookies(name) {
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            return document.cookie.substring(offset, end);
        }
        else
            return null;
    }
}
function SetCookies(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expires + "; path=/" + ";domain=" + window.location.host;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}