var winAlert = function(content, head, isModel){
    head = head ? head : "提示";
    content = "<div style='text-align:center;'><div>" + content + "</div></div>"
    Cuvsd.ui.window.AicuWindow.alert(head, content, isModel, 320, 240);
}
// 弹出window函数
function popAlert(title, content, model, fn){
    Cuvsd.ui.Alert(title, content, model, fn);
}

//写cookies函数
function setCookie(name, value){ //两个参数，一个是cookie的名子，一个是值
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date(); //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name){ //读取cookies函数
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) 
        return unescape(arr[2]);
    return null;
}

function delCookie(name){ //删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) 
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

Login = {
    form: {
        formName: 'loginForm',
        'textField': ['j_username', 'j_password', 'valCode'],
        'checkField': ['_spring_security_remember_me'],
        submit: function(){
            setTimeout(function(){
                $('#loginForm')[0].submit()
            }, 0);
        },
        checkField: function(){
            var tag = true;
            $.each(Login.form.textField, function(i, n){
                var val = $("#" + n).val();
                if (n == "valCode" && val == "") {
                    popAlert('提示', '请输入验证码！', false);
                    tag = false;
                }
                if (n != "valCode" && val == "") {
                    popAlert('提示', '请正确输入登录信息！', false, function(){
                        $('#' + n)[0].focus();
                    });
                    //                    alert("请正确输入登录信息");
                    // $('#' + n)[0].focus();	
                    
                    //                    winAlert("请正确输入登录信息！");
                    tag = false;
                    return tag;
                }
            })
            return tag;
        }
    },
    eventInit: function(){
        $("#login_btn").click(function(){
            var tag = Login.form.checkField();
            if (tag) {
                var username = $("#j_username").val();
                username = $.trim(username);
                $("#j_username").val(username);
                setTimeout(function(){
                    $('#loginForm')[0].submit()
                }, 0);
            }
        })
        //        $("#login_btn").hover(function(){
        //            $(this).css('cursor', 'pointer');
        //            
        //        }, function(){
        //            $(this).css('cursor', 'auto');
        //        })
        
        $("#getPwdBack_btn").click(function(){
        
        })
        $("#linkChangeCode").click(function(){
            var src = $("#codeImg")[0].src;
            var pos = src.indexOf("?");
            if (pos > -1) {
                src = src.substring(0, pos);
            }
            $("#codeImg")[0].src = src + "?time=" + (new Date()).getTime();
        });
        $("input[@type=checkbox][@name=rememverPwdInput]").click(function(){
            if ($(this).attr('checked') == true) {
                $("#rememverPwdInput").val('true');
            }
            else {
                $("#rememverPwdInput").val('false');
            }
        })
        
    },
    pageInit: function(){
        //        $("#login_btn").hover(function(){
        //            $(this).css('cursor', 'hand');
        //            
        //        }, function(){
        //            $(this).css('cursor', 'default');
        //        })
        //在cookie中找用户，添加都输入框 
        var username = getCookie("username");
        if (username != undefined && username != null && username != "undefined") {
            username = username.replace(/\"/g, "");
            $("#j_username").val(username);
        }
        delCookie("username");
        $("#getPwdBack_btn").click(function(){
            Login.getPassword();
        });
        $("#linkChangeCode").click(function(){
            var src = $("#codeImg")[0].src;
            var pos = src.indexOf("?");
            if (pos > -1) {
                src = src.substring(0, pos);
            }
            setCookie("username", $("#j_username").val());
            //            $("#codeImg")[0].src = src + "?time=" + (new Date()).getTime();
            window.location.reload();
        });
    },
    checkError: function(){
        var errorc = getCookie("error");
        var typec = getCookie("type");
        var params = {};
        params.error = errorc;
        params.type = typec;
        if (params.error != undefined && params.error != null && params.error != "undefined") {
            //说明有错
            if (params.type != null && params.type != undefined && params.type != "undefined") {
                //说明有错误类型
                if (params.type == 'code') {
                    //注册码错误
                    //                    popAlert('错误','验证码有误，请重新输入');
                    popAlert('提示', '验证码有误，请重新输入！', false, function(){
                        $("#valCode")[0].focus();
                    });
                    //                    alert("验证码有误，请重新输入");
                    //                    winAlert('验证码有误，请重新输入');
                }
                else 
                    if (params.type == "nouser") {
                        //                        popAlert('错误', '无此用户');
                        popAlert('提示', '无此用户！', false, function(){
                            $("#j_username")[0].focus();
                        });
                    //                        alert("无此用户");
                    
                    //                        winAlert("无此用户");
                    }
                    else 
                        if (params.type == "pwderror") {
                            popAlert('提示', '用户名和密码不匹配请重新输入！', false, function(){
                                $("#j_password")[0].focus();
                            });
                        //                            alert("用户名和密码不匹配请重新输入！");
                        //							$("#j_password")[0].focus();
                        //                            winAlert('用户名和密码不匹配请重新输入！');
                        }
                        else 
                            if (params.type == "tuiding") {
                                popAlert('提示', '您已退订，请重新开通邮箱！', false, function(){
                                    $("#j_username")[0].focus();
                                });
                            }
            }
            else {
                //                popAlert('错误', '用户名和密码不匹配请重新输入！');
                popAlert('提示', '用户名和密码不匹配请重新输入！', false, function(){
                    $("#j_password")[0].focus();
                });
                //                alert("用户名和密码不匹配请重新输入！");
                //				$("#j_password")[0].focus();
                //                winAlert('用户名和密码不匹配请重新输入！');
            }
        }
        delCookie("error");
        delCookie("type");
        var pa=window.location.search;
        if(pa.indexOf("error=true")!=-1){
        	                popAlert('提示', '用户名或密码错误，请重新输入！', false, function(){
                    $("#j_password")[0].focus();
                });
        }
    },
    getPassword: function(){
        var mdn = $("#j_username").val();
        var tag = true;
        var patternMobile = new RegExp(/^((\(\d{3}\))|(\d{3}\-))?13\d{9}|15\d{9}$/);
        if (!patternMobile.test(mdn)) {
            //		alert('手机号码输入不正确');
            tag = false;
        }
        if (tag) {
            //发出找密码请求
            $.ajax({
                url: 'getPassword.action',
                type: 'get',
                data: 'mdn=' + mdn,
                error: function(){
                    popAlert('提示', '系统错误！', false);
                    //                    alert("系统错误！");
                    //                    winAlert('系统错误！');
                },
                success: function(result){
                    //					result =result.
                    //                    alert(result);
                    if (result == "success\r\n") {
                    
                        popAlert('提示', '密码已下发到手机上！', false);
                        //                        alert("密码已下发到手机上！");
                        //                        winAlert("密码已下发到手机上")
                    }
                    else {
                    
                        popAlert('提示', '系统错误！', false);
                        //                        alert("系统错误！");
                        //                        winAlert('系统错误！');
                    }
                }
            });
            
        }
        else {
            //            alert("请确认输入正确手机号码！");
            popAlert('提示', '请确认输入正确手机号码！', false);
            //            winAlert('请确认输入正确手机号码！');
        }
    }
}
Login.eventInit();
Login.pageInit();
Login.checkError();

