﻿function UserLogin(userid){
        if(userid>0)
        {
            window.location = RootPath + 'User/UserInfo.aspx';
        }
        else
        {
            var d = new Date().getTime();
            loginwin = GB_showCenter('用户登录','',200,400);
            var h = [
                '<div id="sendWappushBox" style="width:396px;height:206px;font-size:16px; background-color:#fff;border:2px solid #000;">',
                '<table style="width:380px;margin-left:10px;">',
                '<tr>',
                '<td height="36px" width="60px">用户名:</td>',
                String.Format('<td><input type="text" size="20" id="username_txt" /></td>'),
                '</tr>',
                '<tr>',
                '<td height="36px" width="60px">密　码:</td>',
                String.Format('<td><input type="password" size="20" id="password_txt" /></td>'),
                '</tr>',
                 '<tr>',
                '<tr>',
                '<td height="36px" width="60px">验证码:</td>',
                String.Format('<td><input type="text" size="10" id="checkcode_txt" /><img id="checkcode_img" src="{1}inc/ajax/checkImg.ashx?d={0}" alt="单击改变图片" onclick="changeCheckNum(this)" /><span style="color:red;font-size:12">请输入左边的字符<span></td>',d,RootPath),
                '</tr>',
                '<td colspan=2 align="center" height="50px" >',
                '<input type="button" class="userinfo_btn" onclick="ZZYLogin()" value="登　录" />&nbsp;&nbsp;',
                '<input type="button" class="userinfo_btn" onclick="UserExit(0)" value="注  册" />&nbsp;&nbsp;',
                '<a href="javascript:window.location=RootPath+\'user/ForgetPassWord.aspx\'">忘记密码？</a>',
                '</td>',
                '</tr>',
                '<tr>',
                '<td colspan=2 align="center"><div id="usermsg_box" style="color:red;font-size:18px;"></div></td>',
                '</tr>',
                '</table>',
                '</div>'
            ].join('');
            
            loginwin.setHTML(h);	
            xAddEventListener(document, 'keydown',capLoginKey, false);
        }
    }
    
    function ZZYLogin(){
        var userName = $('username_txt').value;
        var password = $('password_txt').value;
        var checkCode = $('checkcode_txt').value;
        
        var errMsg = "";
        if(userName=="")
        {
            errMsg = "用户名不能为空";
        }
        else if(userName.len()<6 || userName.len()>20)
        {
            errMsg = "用户名错误";
        }
        else if(password=="")
        {
            errMsg = "密码不能为空";
        }
        else if(password.len()<6)
        {
            errMsg = "密码错误";
        }
        else if(checkCode=="")
        {
            errMsg = "请输入验证码";
        }
        
        if(errMsg != "")
        {
            $("usermsg_box").innerHTML = errMsg;  
            return false;
        }
        else
        {
            myAjax({
                url:AjaxFuncUrl,
                ajaxProMethod : 'UserLogin',
                params:{
                    userName:userName,
                    password:password,
                    checkCode:checkCode
                },
                success : function(backinfo , params){
                    if(backinfo)
                    {
                        if(backinfo.ErrNo==0)
                        {
                            //$("menu_userinfo_box").innerHTML = String.Format('<a href="javascript:UserInfo({0});">{1}</a> | <a href="javascript:UserExit()">退出</a>',backinfo.Data.UserId,backinfo.Data.UserName);
                            window.location = window.location;
                            //loginwin.close(); 
                            xRemoveEventListener(document, 'keydown',capLoginKey, false);
                        }
                        else
                        {
                            $("usermsg_box").innerHTML = backinfo.ErrMsg;  
                            $('password_txt').value = '';
                            $('checkcode_txt').value = '';
                            $('checkcode_img').src = RootPath + 'inc/ajax/checkImg.ashx?d='+new Date().getTime();
                        }
                    } 
                }
            }); 
        }
    }
    
    function UserExit(userid){
        userid = 0 || userid;
        if(userid==0)
        {
            window.location = RootPath + 'user/Register.aspx?purl='+encodeURIComponent(window.location);
        }
        else
        {
            myAjax({
                url:AjaxFuncUrl,
                ajaxProMethod : 'ExitLogin',
                params:{},
                success : function(backinfo , params){
                    if(backinfo)
                    {
                        if(backinfo.ErrNo==0)
                        {
                            $("menu_userinfo_box").innerHTML = '<span class="text_04">您还没有登录!</span>　　<a href="javascript:UserLogin();">登录</a> | <a href="'+RootPath+'user/Register.aspx">注册</a>';    
                        }
                        else
                        {
                            alert(backinfo.ErrMsg);  
                        }
                    } 
                }
            }); 
        }
        
    }
    
    function  UserInfo(userid)
    {
        userid = userid || 0;
        window.location = RootPath + 'user/userinfo.aspx?uid='+userid;
    }
    
    function  capLoginKey(oEvent)
    {
        var e = new xEvent(oEvent);
        if(e.keyCode==13)
        {
            ZZYLogin();
            return false;
        }   
    } 