﻿//定义全局变量
if(window.common==undefined)
{
    window.common={isuser:0}
}
//获取登录的用户名
function getcookie() {
    var $status = $("#userinfo");
    $.ajax({
        url: "/ajax.aspx?q=getname&time=" + new Date().toString(),
        type: "GET",
        timeout: 50000,
        beforeSend: function() {
            $status.html("<li style=\"border:0;\">加载中</li>");
        },
        error: function() {
            $status.html("<li style=\"border:0;\"><a href=\"/login.html\">会员登录</a></li><li><a href=\"/reg.html\">新用户注册</a></li>");
        },
        success: function() {
            $status.html(arguments[0]);
        }
    });
}

function logout() {
    var $status = $("#userinfo");
    $.ajax({
        url: "/ajax.aspx?q=logout&time=" + new Date().toString(),
        type: "GET",
        timeout: 50000,
        beforeSend: function() {
            $status.html("<li style=\"border:0;\">注销中</li>");
        },
        error: function() {
            $status.html("<font color=red>注销失败</font>");
            setTimeout("getcookie()", 2000);
        },
        success: function() {
            window.location = ("/");
        }
    });
}
function getpcount() {
    var $status = $("#topcart");
    $.ajax({
        url: "/ajax.aspx?q=getpcount&time=" + new Date().toString(),
        type: "GET",
        timeout: 50000,
        beforeSend: function() {
            $status.html("<a href=\"/products/shopcart.html\" class=\"white\">您的 购物车 有 <strong>0</strong> 件商品<img src=\"/images/cart_button.gif\" align=\"absmiddle\" alt=\"进入购物车结算\" /></a>");
        },
        error: function() {
        },
        success: function() {
            $status.html(arguments[0]);
        }
    });
}
function getpcount2() {
    var $status = $("#topcart2");
    if ($status.length > 0) {
        $.ajax({
            url: "/ajax.aspx?q=getpcount2&time=" + new Date().toString(),
            type: "GET",
            timeout: 50000,
            beforeSend: function() {
                //$status.html("<a href=\"/products/shopcart.html\" class=\"white\">您的 购物车 有 <strong>0</strong> 件商品<img src=\"/images/cart_button.gif\" align=\"absmiddle\" alt=\"进入购物车结算\" /></a>");
                $status.text(0);
            },
            error: function() {
            },
            success: function() {
                //alert(arguments[0]);
                $status.text(arguments[0]);
            }
        });
    } else {
        //alert("label pcount");
    }
}
function setbg() {
    var $value = $("#txtemail").val();
    if ($value == "请填写您的E-mail地址")
        $("#txtemail").val("");
    if ($value == "")
        $("#txtemail").val("请填写您的E-mail地址");
}
function createsubscribe() {
    var $email = $("#txtemail").val();
    if ($email.length < 1) {
        alert("Email地址不能为空！");
        $("#txtemail").focus();
        return;
    }
    else if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.(\w)+)*(\.(\w){2,3})$/.test($email)) {
        alert("请输入有效的Email地址！");
        $("#txtemail").focus();
        return;
    }

    $.ajax({
        url: "/ajax.aspx?q=createsubscribe&email=" + escape($email) + "&time=" + new Date().toString(),
        type: "GET",
        timeout: 50000,
        beforeSend: function() {

        },
        error: function() {
            alert("订阅失败，请重试！");
            $("#txtemail").focus();
        },
        success: function() {
            if (arguments[0] == "-1")
                alert("您已经订阅成功，请勿重复订阅！");
            else if (arguments[0] == "-2") {
                alert("订阅失败，请重试！");
                $("#txtemail").focus();
            }
            else
                alert("订阅成功，非常感谢您订阅美易商城电子期刊！");
        }
    });
}

$(document).ready(function() {
    //回车自动提交内容
    $("#txtemail").keypress(function(e) {
        if (e.keyCode == 13) {
            $("#subscribe").trigger("click");
        }
    })
});

//验证email
function checkemail() {
    var $email = $("#txtemail").val();
    var $error = $("#usernameerror");
    var $info = $("#usernameinfo");
    if ($email.length < 1) {
        $error.html("Email不能为空");
        $error.show();
        window.common.isuser=0;
        return;
    }
    else if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.(\w)+)*(\.(\w){2,3})$/.test($email)) {
        $error.html("请请填写正确的Email");
        $error.show();
        window.common.isuser=0;
        return;
    }
    else {
        $.ajax({
            url: "/ajax.aspx?q=checkemail&email=" + escape($email) + "&time=" + new Date().toString(),
            type: "GET",
            timeout: 50000,
            beforeSend: function() {
                window.common.isuser=0;
            },
            error: function() {
            },
            success: function() {
                //alert(arguments[0]);
                if (arguments[0] == "1") {//当前邮箱可用
                    $info.html("&nbsp;&nbsp;<img src=\"/images/img_01.gif\" alt=\"E-Mail有效\">&nbsp;&nbsp;该E-Mail将自动成为您的账号");
                    $error.html("默认密码为：" + $email + "");
                    
                    window.common.isuser=1;
                    $error.show();
                }
                else if (arguments[0] == "-1") {//当前邮箱已经存在，提示登录
                    $info.html("");
                    $error.html("您的E-Mail之前已经注册美易商城会员，请<a style=\"cursor:pointer;\" onclick=\"Showlogin()\">登录账号</a>如果您不想登录，也可以直接填写下面的收货人信息购买");
                    $error.show();
                    window.common.isuser=2;
                }
            }
        });
    }
}
//验证收货人
function checklinkman() {
    var $linkman = $("#txtName").val();
    var $error = $("#linkmanerror");
    var $linkinfo = $("#linkinfo");
    if ($linkman.length < 2) {
        $linkinfo.html("");
        $error.html("请输入正确的收货人姓名");
        $error.show();
        return;
    }
    else {
        $error.html("");
        $error.hide();
        $linkinfo.html("&nbsp;&nbsp;<img src=\"/images/img_01.gif\" alt=\"收货人姓名有效\">")
    }
}

//验证收货地址
function checkaddress() {
    var $address = $("#txtAddress").val();
    var $addinfo = $("#addinfo");

    if ($address.length < 6) {
        $addinfo.html("<br /><div class=\"yz\" id=\"adderror\">请填写详细地址,最少6个字</div>");
        $("#adderror").show();
        return;
    }
    else {
        $addinfo.html("&nbsp;&nbsp;<img src=\"/images/img_01.gif\" alt=\"收货地址有效\">");
    }
}

//验证手机
function checkphone() {
    var $phone = $("#txtPhone").val();
    var $phoneinfo = $("#phoneinfo");

    var $mo = /^13\d{9}$|^15\d{9}$|^0\d{11}$|^0\d{10}$|^02\d{9}$|^02\d{8}$|^010\d{8}$|^18\d{9}$/gi;


    if (!$mo.test($phone)) {
        $phoneinfo.html("<br /><div class=\"yz\" id=\"phoneerror\">请输入正确的手机号码</div>");
        $("#phoneerror").show();
        return;
    }
    else {
        $phoneinfo.html("&nbsp;&nbsp;<img src=\"/images/img_01.gif\" alt=\"手机号码有效\">");
    }
}


function Showlogin() {
    var pop = new Popup({ contentType: 1, scrollType: 'no', isReloadOnClose: false, width: 430, height: 200 });
    pop.setContent("contentUrl", "http://www.meiyi.cn/login2.html?" + escape($("#txtemail").val()));
    pop.setContent("title", "登录");
    pop.build();
    pop.show();
}

function payonline($number) {
    var $payment = "";
    var vnums = $("#paypt").find("input[type='radio'][checked]");
    var checkitem = vnums.length;
    if (checkitem == 0) {
        alert("请选择付款方式");
        return;
    }
    else {
        for (var i = 0; i < checkitem; i++) {
            var $id = $("#" + vnums[i].id);
            if ($id.attr("checked") == true) {
                $payment = $id.val();
                break;
            }
        }
        //更新支付方式，跳转到支付页面
        $.ajax({
            url: "/ajax.aspx?q=updatepayment&payment=" + escape($payment) + "&number=" + $number + "&time=" + new Date().toString(),
            type: "GET",
            timeout: 50000,
            beforeSend: function() {

            },
            error: function() {
            },
            success: function() {
                if (arguments[0] == "1") {//更新成功，转到支付页面
                    window.location = ("http://www.meiyi.cn/pay/payorder.html?" + $number);
                }
                else {
                    alert("更新支付方式失败");
                }
            }
        });
    }
}
function strLength(str) {
    return str.replace(/[^\x00-\xFF]/g, '**').length;
}
