﻿var iptemail = $('#email'), emailMsg = $('#emailMsg'), iptPassword = $('#password'), passwordMsg = $('#passwordMsg'), iptConfirmPassword = $('#confirmPassword'), confirmPasswordMsg = $('#confirmPasswordMsg');
var okhtml = '<img src="' + Incool.IMG + 'right.gif" width="13" height="13" />';
var _Passport = {
    SendEmailForReg: function(email, username) {
        $.ajax({
            dataType: 'json',
            type: 'post',
            url: Incool.ROOT + "Proxy/passport.aspx",
            data: { act: 'reg.sendemail', email: escape(email), username: escape(username) },
            success: function(r) {
                if (r.s >= 0) {
                    if (r.result == 1) {
                        Incool.Alert('确认信已经重发到你的邮箱' + email + '，请查收');
                    }
                    else {
                        Incool.Alert(r.msg);
                    }
                }
                else
                    Incool.Error(r.msg);
            }
        });
    },
    checkEmail: function() {
        if ($.trim(iptemail.val()) == '') {
            emailMsg.text('请输入Email地址');
            iptemail.attr('class', 'input1_err');
            return false;
        }
        else if (!Incool.Reg['email'].test(iptemail.val())) {
            emailMsg.text('您输入的Email格式不正确');
            iptemail.attr('class', 'input1_err');
            return false;
        }
        return true;
    },
    checkPassword: function() {
        var len = 0;
        for (var i = 0; i < iptPassword.val().length; i++) { if (iptPassword.val().charCodeAt(i) > 127 || iptPassword.val().charCodeAt(i) == 94) { len = len + 2; } else { len = len + 1; } }
        if (len == 0) {
            passwordMsg.text("请输入密码");
            iptPassword.attr('class', 'input1_err');
        }
        else if (len < 6 || len > 16) {
            passwordMsg.text("密码输入有误");
            iptPassword.attr('class', 'input1_err');
        }
        else {
            passwordMsg.html(okhtml);
            return true;
        }
        return false;
    },
    checkConfirmPassword: function() {
        if ($.trim(iptConfirmPassword.val()).length == 0) {
            confirmPasswordMsg.text("请再次输入密码");
        }
        else if (iptPassword.val() != iptConfirmPassword.val()) {
            confirmPasswordMsg.text("两次输入的密码不一致");
        }
        else {
            confirmPasswordMsg.html(okhtml);
            return true;
        }
        iptConfirmPassword.attr('class', 'input1_err');
        return false;
    }
};

//登录
_Passport.LoginInit = function() {
    okhtml = '';

    $('input:text').hover(function() {
        $(this).attr('class', 'input1_on');
    }, function() { $(this).attr('class', 'input1'); });
    $('input:password').hover(function() {
        $(this).attr('class', 'input1_on');
    }, function() { $(this).attr('class', 'input1'); });

    //email    
    iptemail.focus(function() {
        $(this).attr('class', 'input1_on');
        emailMsg.text('');
    });
    iptemail.blur(function() {
        $(this).attr('class', 'input1');
        _Passport.checkEmail();
    });

    //密码    
    iptPassword.focus(function() {
        $(this).attr('class', 'input1_on');
        passwordMsg.text('');
    });
    iptPassword.blur(function() {
        $(this).attr('class', 'input1');
        _Passport.checkPassword();
    });

    //记住登陆        
    $('#save').click(function() {
        if ($('#save').attr("checked") == true) {
            $('#save').val('1');
        }
        else {
            $('#save').val('0');
        }
    });

    $('input').focus(function() { }).keydown(function(e) { if (13 == e.keyCode) { $('#btnLogin').click(); } });

    //登录事件
    $('#btnLogin').click(function() {
        if (_Passport.checkEmail() && _Passport.checkPassword()) {
            $.ajax({
                dataType: 'json',
                type: 'post',
                url: Incool.ROOT + 'Proxy/passport.aspx',
                data: {
                    act: 'login',
                    email: escape(iptemail.val()),
                    password: escape(iptPassword.val()),
                    save: $('#save').val()
                },
                success: function(r) {
                    if (r.s >= 0) {
                        if (r.result == 0) {
                            //location.href = $('#returnurl').val();
                            location.href = 'http://www.incool.com/aspx1.asp?userid=' + r.userid + '&code=' + r.code + '&returnurl=' + escape($('#returnurl').val());
                        }
                        else if (r.result == -13) {
                            location.href = Incool.ROOT + 'passport/regconfirmmail.aspx?email=' + escape(iptemail.val()) + '&username=' + escape(r.msg);
                        }
                        else {
                            $('#trError').find('div').text(r.msg);
                            $('#trError').show();
                        }
                    }
                    else
                        Incool.Error(r.msg);
                }
            });
        };
    });
};

//注册
_Passport.RegInit = function() {
    $('input:text').hover(function() {
        $(this).attr('class', 'input1_on');
    }, function() { $(this).attr('class', 'input1'); });
    $('input:password').hover(function() {
        $(this).attr('class', 'input1_on');
    }, function() { $(this).attr('class', 'input1'); });

    //email
    var checkEmailRptResult = false;
    iptemail.focus(function() {
        $(this).attr('class', 'input1_on');
        emailMsg.text('');
    });
    iptemail.blur(function() {
        $(this).attr('class', 'input1');
        if (_Passport.checkEmail()) {
            checkEmailRpt();
        }
    });
    function checkEmailRpt() {
        emailMsg.text('检测中...');
        $.ajax({
            cache: true,
            dataType: 'json',
            type: 'post',
            url: Incool.ROOT + "Proxy/passport.aspx",
            data: { act: 'validemail', email: escape(iptemail.val()) },
            success: function(r) {
                if (r.s >= 0) {
                    if (r.result == 1) {
                        emailMsg.html(okhtml);
                        checkEmailRptResult = true;
                    }
                    else {
                        emailMsg.text('该Email地址已被使用，请更换');
                        iptemail.attr('class', 'input1_err');
                    }
                }
                else {
                    Incool.Error(r.msg);
                }
            }
        });
    };

    //密码
    iptPassword.focus(function() {
        $(this).attr('class', 'input1_on');
        passwordMsg.text('');
    });
    iptPassword.blur(function() {
        $(this).attr('class', 'input1');
        _Passport.checkPassword();
    });

    //确认密码    
    iptConfirmPassword.focus(function() {
        $(this).attr('class', 'input1_on');
        confirmPasswordMsg.text('');
    });
    iptConfirmPassword.blur(function() {
        $(this).attr('class', 'input1');
        _Passport.checkConfirmPassword();
    });

    //姓名
    var iptUserName = $('#username'), usernameMsg = $('#usernameMsg');
    iptUserName.focus(function() {
        $(this).attr('class', 'input1_on');
        usernameMsg.text('');
    });
    iptUserName.blur(function() {
        $(this).attr('class', 'input1');
        checkUserName();
    });
    function checkUserName() {
        if ($.trim(iptUserName.val()).length == 0) {
            usernameMsg.text('请输入您的真实姓名');
            iptUserName.attr('class', 'input1_err');
            return false;
        }
        else {
            var _pattern = /[.=%*\\\'\r\n\"]|系统|係統|系統|客服|官方|工作人员|工作人員|管理|ADMIN|低一你挨得斯此我无土阜|高校罢餐|罢餐|罢吃|罢食|《激流中国》|《伪水》|激流中国|伪水|法轮功|惊悚奇情 神洲电影|刊文回谢|禁网禁片|中华养生益智功|张宏堡|菩提功|狄玉明|《天音》|观音法门|六四|89事件|89学潮|黄海暗杀|领导班子名单外泄|第五代红人|总理接班人|下届总理人选|太子党|六代接班人|中国海外腐败兵团|中共第六代|自由门|无界8.6|动态网|破网软件|姜凤阁|新学期的大学食堂|五个部长一个省长/gi;
            var len = 0;
            for (var i = 0; i < iptUserName.val().length; i++) { if (iptUserName.val().charCodeAt(i) > 127 || iptUserName.val().charCodeAt(i) == 94) { len = len + 2; } else { len = len + 1; } }
            if (len < 2 || len > 16) {
                usernameMsg.text("您输入姓名格式不正确");
            }
            else if (_pattern.test(iptUserName.val()) || /^GM/.test(iptUserName.val())) {
                usernameMsg.text('姓名中含有非法字符');
            } else if (/系|係/gi.test(iptUserName.val()) && /统|統/gi.test(iptUserName.val())) {
                usernameMsg.text('姓名输入非法');
            } else {
                return true;
            }
            iptUserName.attr('class', 'input1_err');
            return false;
        }
    };

    //省份城市
    function checkState() {
        if ($('#state').val() == "") {
            $('#cityMsg').text('请选择省份');
            return false
        }
        return true;
    };
    function checkCity() {
        if ($('#city').val() == "") {
            if ($('#state').val() == "35") {
                $('#cityMsg').text('请选择海外国家');
            }
            else {
                $('#cityMsg').text('请选择城市');
            }
            return false
        }
        return true;
    };

    //头像上传
    function checkUserface() {
        if ($('#user_face').val() == "") {
            $('#userfaceMsg').text('请上传头像');
            return false;
        };
        return true;
    };

    //验证码
    var vt = $('#CheckCode'), vi = $('#imgCheckCode');
    //vt.focus(function() { vi.click(); });
    vi.click(function() {
        this.src = Incool.ROOT + 'proxy/verifycode.aspx?act=get&ts=' + new Date().valueOf();
    });
    vt.focus(function() { }).keydown(function(e) { if (13 == e.keyCode) { $('#btnReg').click(); } });

    //注册协议
    $('span.pink12').click(function() {
        $('#regpact').show();
    });
    $('#regli2').click(function() {
        $('#regpact').hide();
    });
    $('#button').click(function() {
        $('#regpact').hide();
        $('#agree').attr("checked", "checked");
        $('#btnReg').removeAttr('disabled');
    });
    $('#agree').click(function() {
        if ($('#agree').attr("checked") == true) {
            $('#btnReg').removeAttr('disabled');
        }
        else {
            $('#btnReg').attr('disabled', 'disabled');
        }
    });

    //注册事件
    $('#btnReg').click(function() {
        if (_Passport.checkEmail() && checkEmailRptResult && _Passport.checkPassword() && _Passport.checkConfirmPassword() && checkState() && checkCity() && checkUserface()) {
            $.ajax({
                dataType: 'json',
                type: 'post',
                url: Incool.ROOT + 'Proxy/passport.aspx',
                data: {
                    act: 'reg',
                    email: escape(iptemail.val()),
                    password: escape(iptPassword.val()),
                    username: escape(iptUserName.val()),
                    sex: $('input:radio[name=usersex]:checked').val(),
                    state: $('#state').val(),
                    city: $('#city').val(),
                    user_face: $('#user_face').val(),
                    f:escape($('#f').val()),
                    vcode: vt.val()
                },
                success: function(r) {
                    if (r.s >= 0) {
                        if (r.result == 0) {
                            location.href = Incool.ROOT + 'passport/regconfirmmail.aspx?email=' + escape(iptemail.val()) + '&username=' + escape(iptUserName.val());
                            //location.href = "http://i.incool.com/myzone/";
                        }
                        else {
                            Incool.Alert(r.msg);
                        }
                    }
                    else
                        Incool.Error(r.msg);
                }
            });
        };
    });
};

//忘记密码
_Passport.ForgetPwdInit = function() {
    //email
    emailMsg = $('#error');
    iptemail.focus();
    iptemail.focus(function() {
        $(this).attr('class', 'input1_on');
        emailMsg.text('');
        emailMsg.hide();
    });
    iptemail.blur(function() {
        $(this).attr('class', 'input1');
        checkEmail();
    });
    function checkEmail() {
        if ($.trim(iptemail.val()) == '') {
            emailMsg.text('请输入Email地址');
            emailMsg.show();
            iptemail.attr('class', 'input1_err');
            return false;
        }
        else if (($.trim(iptemail.val()) != '' && !Incool.Reg['email'].test(iptemail.val()))) {
            emailMsg.text('您输入的Email地址不正确');
            emailMsg.show();
            iptemail.attr('class', 'input1_err');
            return false;
        }
        return true;
    };

    //重设密码事件
    $('#btnReset').click(function() {
        if (checkEmail()) {
            $.ajax({
                dataType: 'json',
                type: 'post',
                url: Incool.ROOT + 'Proxy/passport.aspx',
                data: {
                    act: 'pwd.sendmail',
                    email: escape(iptemail.val())
                },
                success: function(r) {
                    if (r.s >= 0) {
                        if (r.result == 1) {
                            location.href = Incool.ROOT + 'passport/sendpwdemailok.aspx?email=' + escape(iptemail.val());
                        }
                        else {
                            emailMsg.text(r.msg);
                            emailMsg.show();
                        }
                    }
                    else
                        Incool.Error(r.msg);
                }
            });
        };
    });
};

//重设密码
_Passport.ResetPwdInit = function() {
    $('input:password').hover(function() {
        $(this).attr('class', 'input1_on');
    }, function() { $(this).attr('class', 'input1'); });

    //密码
    iptPassword.focus(function() {
        $(this).attr('class', 'input1_on');
        passwordMsg.text('');
    });
    iptPassword.blur(function() {
        $(this).attr('class', 'input1');
        _Passport.checkPassword();
    });

    //确认密码
    iptConfirmPassword.focus(function() {
        $(this).attr('class', 'input1_on');
        confirmPasswordMsg.text('');
    });
    iptConfirmPassword.blur(function() {
        $(this).attr('class', 'input1');
        _Passport.checkConfirmPassword();
    });

    //重设密码事件
    SaveResetPwd = function(email, t, s) {
        if (_Passport.checkPassword() && _Passport.checkConfirmPassword()) {
            $.ajax({
                dataType: 'json',
                type: 'post',
                url: Incool.ROOT + 'Proxy/passport.aspx',
                data: {
                    act: 'pwd.reset',
                    email: escape(email),
                    password: escape(iptPassword.val()),
                    t: t,
                    s: s
                },
                success: function(r) {
                    if (r.s >= 0) {
                        if (r.result == 1) {
                            location.href = 'http://i.incool.com/myzone/';
                        }
                        else {
                            Incool.Alert(r.msg);
                        }
                    }
                    else
                        Incool.Error(r.msg);
                }
            });
        };
    };
};

