//用户在线留言
function SubmitOnlineMsg(formname) {
    var result = Validate(formname, true);
    if (result) {
        $.ajax({
            url: $("#" + formname).attr("action"),
            type: "post",
            data: $("#" + formname).serialize(),
            beforeSend: function (XMLHttpRequest) {
                //DisabledAdd("CmsSubmit");
            },
            success: function (data) {
                if (data == "" || data == "true") {
                    alert('在线留言成功,请等待管理员回复!');
                    document.getElementById(formname).reset();
                } else { alert(data); }
                //DisabledRemove("CmsSubmit");
            },
            error: function () {
                alert("数据提交页面错误");
                //DisabledRemove("CmsSubmit");
            },
        });
    }
    return false;
}

//个人简历投递
function SubmitHrBio() {
    var result = Validate("formhrbio", true);
    if (result) {
        $.ajax({
            url: $("#formhrbio").attr("action"),
            type: "post",
            data: $("#formhrbio").serialize(),
            beforeSend: function (XMLHttpRequest) {
                //DisabledAdd("CmsSubmit");
            },
            success: function (data) {
                if (data == "" || data == "true") {
                    alert('简历投递成功,请等待管理员联系!');
                    document.getElementById("formhrbio").reset();
                } else { alert(data); }
                //DisabledRemove("CmsSubmit");
            },
            error: function () {
                alert("数据提交页面错误");
                //DisabledRemove("CmsSubmit");
            },
        });
    }
    return false;
}

//在线投票
function SubmitVote() {
    $.ajax({
        url: $("#formvote").attr("action"),
        type: "post",
        data: $("#formvote").serialize(),
        beforeSend: function (XMLHttpRequest) { },
        success: function (data) {
            if (data == "" || data == "true") {
                alert('在线投票成功,感谢您的参与!');
                document.getElementById("formvote").reset();
            } else { alert(data); }
        },
        error: function () {
            alert("数据提交页面错误");
        },
    });
}

//会员登录
function SubmitLogin() {
    var result = Validate("formlogin", true);
    if (result) {
        $.ajax({
            url: $("#formlogin").attr("action"),
            type: "post",
            data: $("#formlogin").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }

    return false;
}

//注册会员
function SubmitRegister() {
    var result = Validate("formregister", false);
    if ($("#RegUserPwd").val() != $("#RegUserPwdSure").val()) {
        ValidateCustom("RegUserPwdSure", false);
        result = false;
    }
    
    if (result) {
        $.ajax({
            url: $("#formregister").attr("action"),
            type: "post",
            data: $("#formregister").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }
    
    return false;
}

//找回密码
function SubmitBackPwd() {
    var result = Validate("formbackpwd", false);
    if ($("#UserPwd").val() != $("#UserPwdSure").val()) {
        ValidateCustom("UserPwdSure", false);
        result = false;
    }

    if (result) {
        $.ajax({
            url: $("#formbackpwd").attr("action"),
            type: "post",
            data: $("#formbackpwd").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }

    return false;
}

//编辑会员信息
function SubmitMemberEdit() {
    var result = Validate("formmemberupdateedit", true);
    if (result) {
        $.ajax({
            url: $("#formmemberupdateedit").attr("action"),
            type: "post",
            data: $("#formmemberupdateedit").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }
    return false;
}

//修改会员密码
function SubmitMemberPwd() {
    var result = Validate("formmemberupdatepwd", true);
    if ($("#NewPwd").val() != $("#SurePwd").val()) {
        ValidateCustom("SurePwd", false);
        result = false;
    }
    if (result) {
        $.ajax({
            url: $("#formmemberupdatepwd").attr("action"),
            type: "post",
            data: $("#formmemberupdatepwd").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
                document.getElementById("formmemberupdatepwd").reset();
            },
            error: function () { },
        });
    }
    return false;
}

//修改收货地址
function SubmitMemberAddress() {
    var result = Validate("formmemberupdateaddress", true);
    if (result) {
        $.ajax({
            url: $("#formmemberupdateaddress").attr("action"),
            type: "post",
            data: $("#formmemberupdateaddress").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }
    return false;
}

//加入购物车
function SubmitAddCart() {
    var result = Validate("formshopcart", true);
    if (result) {
        $.ajax({
            url: $("#formshopcart").attr("action"),
            type: "post",
            data: $("#formshopcart").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }
    return false;
}

//加入购物车 ajax更新
function SubmitAddCartAjax() {
    var result = Validate("formshopcart", true);
    if (result) {
        $.ajax({
            url: $("#formshopcart").attr("action"),
            type: "post",
            data: $("#formshopcart").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResultAjax(data);
                ShopCartLoad();
            },
            error: function () { },
        });
    }
    return false;
}


//下单
function SubmitShopIndent() {
    var result = Validate("formshopindent", true);
    if (result) {
        $.ajax({
            url: $("#formshopindent").attr("action"),
            type: "post",
            data: $("#formshopindent").serialize(),
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                SubmitResult(data);
            },
            error: function () { },
        });
    }
    return false;
}

//添加产品收藏
function CollectionAdd(url, id) {
    $.get(url, { Action: "get", type: "membercollectionadd", id: id, time: Math.random() }, function (data, textStatus) { SubmitResult(data); });
}
//删除产品收藏
function CollectionDelete(url, id) {
    $.get(url, { Action: "get", type: "membercollectiondelete", id: id, time: Math.random() }, function (data, textStatus) {
        DynamicHtml($("#MemberCollection .DynamicHtml"));
    });
}


/**
以下是公用方法
**/

//自定义验证
function ValidateCustom(id, vt) {    
    if (vt) {
        var attr = eval("(" + $("#" + id).attr("attr") + ")");
        alert(attr.msg);
    }
    else {
        $("#" + id).parent().find("label").attr("class", "error");
    }
}

//验证控件
function Validate(id, vt) {
    if (document.getElementById(id)) {
        var result = true;
        var objs = "#" + id + " input[type=text],#" + id + " input[type=password],#" + id + " input[type=hidden],textarea";
        if (vt == true) {
            $(objs).each(function () {
                var att = $(this).attr("attr");                
                if (att != "" && att != "{}") {
                    var attr = eval("(" + $(this).attr("attr") + ")");
                    if (attr != null) {
                        if (attr.regex != undefined && attr.regex != "") {
                            if (ValidateRegex($(this).val(), attr.regex)) {
                                alert(attr.msg);
                                $(this).focus();
                                result = false;
                                return false;
                            }
                        }
                    }
                }
            });
            return result;
        } else {
            $(objs).each(function () {
                var att = $(this).attr("attr");                
                if (att != "" && att != "{}") {
                    var attr = eval("(" + $(this).attr("attr") + ")");
                    if (attr != null) {
                        if (attr.regex != undefined && attr.regex != "") {
                            var obj = $(this).parent().find("label");
                            if (ValidateRegex($(this).val(), attr.regex)) {
                                $(this).parent().find("label").attr("class", "error");
                                result = false;
                            } else {
                                $(this).parent().find("label").attr("class", "success");
                            }
                        }
                    }
                }
            });
            return result;
        }
    }
    return false;
}

//正则匹配验证
function ValidateRegex(val, f) {
    var r;
    if (f.charAt(0) == "/")
        r = eval(f);
    else
        r = new RegExp(f);
    return !r.test($.trim(val));
}