﻿function dialoglogin()
{
    var content="<div>";
    content += "<a id=\"email\" hrf=\"#\" onclick=\"ChangeLogin(0)\" style=\"cursor:pointer;color:#333;\">E-Mail登录</a>&nbsp;&nbsp;&nbsp;";
    content += "<a id=\"uname\" hrf=\"#\" onclick=\"ChangeLogin(1)\" style=\"cursor:pointer;color:0068c4;\">用户名登录</a>";
    content += "</div><div>&nbsp;</div>";
    content += "<div style='width:100%;float:left'>";
    content += "<span id=\"loginemail\" style=\"display:block;\">E-Mail：<input type='text' id='userMail' name='userMail' class='input_blue'></span>";
    content += "<span id=\"loginuname\" style=\"display:none;\">用户名：<input type='text' id='userName' name='userName' class='input_blue'></span>";
    content += "<br/><br/>";
    content += "密　码：<input type='password' id='password' name='password' class='input_blue' onkeydown=\"enterlogin('1','000','event.keyCode')\">";
    content += "<span id='pwdinfo'></span></div>";
    content += "<div style='height:5px; clear:both;'>&nbsp;</div><br/>";
    content += "<div><input type='button' id='loginbutton' value='登录' style='border:1px solid #7f9db9; background-color: #FFFFFF;' onclick=\"login('1','000')\">";
    content += "&nbsp;&nbsp;<input type='button' value='返回' style='border:1px solid #7f9db9; background-color: #FFFFFF;' onclick='Dialog.okCallback()'>";
    content += "</div>";
    content += "<div><span id='errinfo'></span><br/>";
    content += "<a href='reg.asp?url="+window.location+"'>还不是会员,10秒钟快速注册</a><br/>";
    content += "</div>";
    content += "<input id=\"hiddentype\" type=\"hidden\" value=\"0\">";
    Dialog.info(content, 
               {windowParameters: {className: "alphacube", width:320, height:220},  
                ok:function(win) { return true;}, cancel:function(win) {return true;}});
}
function ChangeLogin(type)
{
    document.getElementById("hiddentype").value = type;
    if(type == "0")
    {
        document.getElementById("loginemail").style.display = "block";
        document.getElementById("loginuname").style.display = "none";
        document.getElementById("email").style.color = "#333";
        document.getElementById("uname").style.color = "#0068c4";
        document.getElementById("email").style.fontWeight = "bold";
        document.getElementById("uname").style.fontWeight = "normal";
    }
    else
    {
        document.getElementById("loginemail").style.display = "none";
        document.getElementById("loginuname").style.display = "block";
        document.getElementById("email").style.color = "#0068c4";
        document.getElementById("uname").style.color = "#333";
        document.getElementById("email").style.fontWeight = "normal";
        document.getElementById("uname").style.fontWeight = "bold";
    }
}
function login(type,page)
{
    var username = "";
    var usermail = "";
    if(document.getElementById("hiddentype") != null)
    {
        type=document.getElementById("hiddentype").value;
    }
    var password=document.getElementById("password").value;
    if(type == 0)
    {
        usermail=document.getElementById("userMail").value;
        if(usermail=="")
        {
            document.getElementById("errinfo").innerHTML="<font color='red'>请填写邮箱</font>";
            document.getElementById("userMail").focus();
            return false;
        }
    }
    else
    {
        username=document.getElementById("userName").value;
        if(username=="")
        {
            document.getElementById("errinfo").innerHTML="<font color='red'>请填写用户名</font>";
            document.getElementById("userName").focus();
            return false;
        }
    }
    if(password=="")
    {
        document.getElementById("errinfo").innerHTML="<font color='red'>请填写密码</font>";
        document.getElementById("password").focus();
        return false;
    }
    document.getElementById("loginbutton").disabled=true;
        var data='username='+encodeURIComponent(username)+'&userpwd='+password+'&usermail='+usermail+'&type='+type;
         new Ajax.Request('chklogin.ashx?'+data,
         {method:'post',
         parameters:data,
         onSuccess:function(transport){
         var result = transport.responseText;
		 if(result !=0 && result != null)
		 {
		    if(page != null && page != "" && page != "000")
		    {
		        window.location = page+".aspx";
		    }
		    else
		    {
		        location.reload();
		        Dialog.okCallback();
		    }
		 }
		 else
		 {
		    var errorinfo = "";
		    if(type == "0")
		    {
		        errorinfo = "<font color='red'>Email或密码错误</font>";
		    }
		    else
		    {
		        errorinfo = "<font color='red'>用户名或密码错误</font>";
		    }
		     document.getElementById("errinfo").innerHTML = errorinfo;
		     document.getElementById("loginbutton").disabled=false;return false;
		 }
         },
         onFailure:function(){dialog("登录失败。");}
         }
         );
}

function dialogin(username)
{
    var content="<div>注册成功,请登录</div><div>&nbsp;</div>";
    
    content +="<div style='width:100%;float:left'>E_mail：<input type='text' id='userName' name='userName' value='"+username+"' class='input_blue'><br/><br/>密　码：<input type='password' id='password' name='password' class='input_blue' style='margin-top:5px;'><span id='pwdinfo'></span></div><div style='height:15px'>&nbsp;</div><br/><div><input type='button' id='loginbutton' value='登录' style='border:1px solid #7f9db9; background-color: #FFFFFF;' onclick='urllogin()'>&nbsp;&nbsp;<input type='button' value='返回' style='border:1px solid #7f9db9; background-color: #FFFFFF;' onclick='Dialog.okCallback()'></div><div><span id='errinfo'></span><br/><a href='reg.asp'>还不是会员,10秒钟快速注册</a></div>";
    Dialog.info(content, 
               {windowParameters: {className: "alphacube", width:320, height:200},  
                ok:function(win) { return true;}, cancel:function(win) {return true;}});
}

function urllogin()
{
    var username=document.getElementById("userName").value;
    var password=document.getElementById("password").value;
    if(username=="")
    {
        document.getElementById("errinfo").innerHTML="<font color='red'>请填写用户名</font>";
        document.getElementById("userName").focus();
        return false;
    }
    if(password=="")
    {
        document.getElementById("errinfo").innerHTML="<font color='red'>请填写密码</font>";
        document.getElementById("password").focus();
        return false;
    }
    document.getElementById("loginbutton").disabled=true;
        var data='usermail='+encodeURIComponent(username)+'&userpwd='+password+'&username='+'&type=0';
         new Ajax.Request('chklogin.ashx?'+data,
         {method:'post',
         parameters:data,
         onSuccess:function(transport){
         var result = transport.responseText;
		 if(result !=0&&result!=null){Dialog.okCallback();window.location='UserBaseInfoUpdate.aspx';}
		 else{document.getElementById("errinfo").innerHTML="<font color='red'>Email或密码错误</font>";document.getElementById("loginbutton").disabled=false;return false;}
         },
         onFailure:function(){dialog("登录失败。");}
         }
         );
}

//首页登录块
function IndexChangeLogin(command)
{
    Updater("Control/Login.ascx","LoginDIV","command="+command);
}

function enterlogin(type,page,e)
{
    var isie = (document.all) ? true:false; 
    var key; 
    var ev; 
    if(isie)
    { 
        key = window.event.keyCode; 
        ev = window.event; 
        if(key == 13)
        {
            login(type,page);
        }
    }
    else
    { 
        key = e.which; 
        ev = e;
        if(ev == 13)
        {
            login(type,page);
        } 
    } 
}