var loginreturn="0";
var loginUserName="";
var Cookies={
   Decode:function(str) 
   { 
　     var strArr="ddd"; 
　     var strRtn=""; 
       if( str != null && str.indexOf("a")>1)
       {
　           strArr=str.split("a"); 

　           for (var i=strArr.length-1;i>=0;i--) 
             {
　               strRtn+=String.fromCharCode(eval(strArr[i])); 
             }
　            return strRtn;
        }
        return null;
    } ,
	Code:function(str) 
    { 
　      var strRtn=""; 
　      for (var i=str.length-1;i>=0;i--) 
　      { 
　　         strRtn+=str.charCodeAt(i); 
　　         if (i) strRtn+="a"; 
　      } 
　      return strRtn; 
     } , 
	Get:function(name)   
    {   
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
        {
            return unescape(arr[2]);
        }
        else 
        {
	        return null;
	    }
     },
    Set:function(name, value, expires, path, domain)
	{
        var   exp     =   new   Date();
        exp.setTime(exp.getTime() + expires*24*60*60*1000); 
		document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + exp.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "");
    },
    Del:function(cookiesname,domain)   
    {   
		 var exp = new Date();
         exp.setTime(exp.getTime() - 1);
		 var cval=this.Get(cookiesname);
         if(cval!=null) document.cookie=cookiesname +"="+cval+";expires="+exp.toGMTString() +
        ((domain) ? "; domain=" + domain : "");
    }  
}


function G(id){
    return document.getElementById(id);
}
function GC(t){
   return document.createElement(t);
}
String.prototype.trim = function(){
          return this.replace(/(^\s*)|(\s*$)/g, '');
}
function isIE(){
      return (document.all && window.ActiveXObject && !window.opera) ? true : false;
} 

loginUserName=Cookies.Decode(Cookies.Get("iResearchCNusername"));

/*检测是否已经登录*/
function isLogined()
{
    loginUserName=Cookies.Decode(Cookies.Get("iResearchCNusername"));
	if (loginUserName=="" || loginUserName=="undefined" ||loginUserName==null)
	{
	     G("loginState").innerHTML ="|　<input type=\"button\" class=\"login\" value=\"登 录\" onclick=\"popLoginFlow(1);\" /><input type=\"button\" class=\"reg\" value=\"注 册\" onclick=\"location.href='/register/'\"/>　<a href=\"http://reg.iresearch.cn/ForgetPassword.aspx\">忘记密码?</a>";
	}
	else
	{
	    G("loginState").innerHTML ="|　欢迎："+loginUserName+"　<a onclick=\"window.location='/register/ModReg.aspx'\" style='cursor:pointer;'>修改资料</a>　<a onclick='loginOut();' style='cursor:pointer;'>退出</a>";
	}    
}

/*检测是否已经登录结束*/


/*login 登录*/

var loginDivWidth = 350
var Login_in_flow = ' <div class="login_new">'
+ '<ul class="ul_line b_caption">'
+ '<li class="name" id="caption_32">'
+ '<h3><a href="#">登录</a></h3>'
+ ' </li>'
+ ' <li class="fr" style="line-height:25px;padding-right:10px;"><a href="javascript:cancelLogin()">关闭</a></li>'
+ '</ul>'
+ '<div class="login_bs"><br>'
+ '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" >'
+ '<tr>'
+ '<td align="right" valign="middle" width=80>用户名：</td>'
+ '<td align="left" valign="middle"><input name="username" type="text" id="username" size="30" class="register_bo"> <span id="username_text"></span></td>'
+ '</tr>'
+ '<tr>'
+ '<td align="right" valign="middle">密　码：</td>'
+ '<td align="left" valign="middle"><input name="username" type="password" id="password" size="30" class="register_bo"> <span id="password_text"></span></td>'
+ '</tr>'
+ '<tr>'
+ '<td colspan="2" align="center" valign="middle" height=50><span id="result"></span><input type="submit" value="登 录" name="reg" class="register_button" onclick="loginCheck(1);" id="Login_button">&nbsp'
+ '<input type="reset" value="取 消" name="reset" class="register_button"  onclick="cancelLogin()">&nbsp<input type="button" class="register_button" value="注 册" onclick="location.href=/register/" /> <a href=\"http://reg.iresearch.cn/ForgetPassword.aspx\">忘记密码?</a></td>'
+ ' </tr>'
+ '</table>'
+ '</div>'
+ '</div>';
function loadLoginInFlow(){
   G("Login_div").innerHTML = Login_in_flow;
}

function cancelLogin(){
    G("Login_div").style.display = 'none';
    G("cover_div").style.display = 'none';
    document.body.style.overflow = 'hidden';
}

function loginOut()
{
	//var url = '/Logout.aspx'; //查询URL 
	//var pars = 't='+new Date().getMilliseconds();
	//var myAjax = new Ajax.Request( 
	//url,
	//{method: 'get', parameters: pars, onComplete: showResponse}
	//);
	//Cookies.Del("iResearchCNusername");
    //isLogined();
    window.location="/logout.aspx";
}

function loginCheck(isLoginIn){
    if (isLoginIn == 1) 
    {
       G("username_text").innerHTML = "";
       G("password_text").innerHTML = ""; 
       G("result").innerHTML="";
       if(G("username").value=='')
       {
           G("username_text").innerHTML = "<font color=red>*</font>";
           G("username").focus();
       }

       else if(G("password").value=='')
       {
           G("password_text").innerHTML = "<font color=red>*</font>";
           G("password").focus();
       }
       else
       {
	       //登录处理，我关闭这个窗口
	        var password = G('password').value;
			var username = G('username').value;
			var url = '/Login.aspx'; //查询URL 
			var pars = 'userName=' + username + '&passWord=' + password+"&t="+(new Date).getTime();
			var myAjax = new Ajax.Request( 
			url, 
			{method: 'get', parameters: pars, onComplete: showResponse}
			); 

       }
     }
};

function showResponse(originalRequest) 
{ 
	if(originalRequest.responseText=="0")
	{
		G("result").innerHTML = "<font color=red>登录失败，错误的帐号或密码！</font><BR>";
	}
	else
	{
	    //Cookies.Set("iResearchCNusername", Cookies.Code(originalRequest.responseText),"","/",".iresearch.com.cn");
	    isLogined();
	    //G("result").innerHTML = "<font color=red>登录成功！</font><BR>";
	    //setTimeout(cancelLogin,1000);
	    //cancelLogin();
	    window.location.reload();
	}
}
			
//使窗口变灰
function popCoverDiv(){
   if (G("cover_div")) {
    G("cover_div").style.display = '';
   } else {
    var coverDiv = GC('div');
    document.body.appendChild(coverDiv);
    coverDiv.id = 'cover_div';
    with(coverDiv.style) {
     position = 'absolute';
     background = '#000000';
     left = '0px';
     top = '0px';
     var bodySize = getBodySize();
     width = bodySize[0] + 'px'
     height = bodySize[1] + 'px';
     zIndex = 99;
     if (isIE()) {
      filter = "Alpha(Opacity=40)";
     } else {
      opacity = 0.6;
     }
    }
   }
}
//获取窗口大小
function getBodySize(){
   var bodySize = [];
   with(document.documentElement) {
    bodySize[0] = (scrollWidth>clientWidth)?scrollWidth:clientWidth;
    bodySize[1] = (scrollHeight>clientHeight)?scrollHeight:clientHeight;
   }
   return bodySize;
}

//登录窗口显示
function popLogin(isLogin){
   if (G("Login_div")) {
    G("Login_div").style.display = '';
   } else {
    var LoginDiv = GC('div');
    document.body.appendChild(LoginDiv);
    LoginDiv.id = 'Login_div';
    LoginDiv.align = "center";
    LoginDiv.onkeypress = function(evt){
          var e = window.event?window.event:evt;
          if (e.keyCode==13 || e.which==13) {
           if (G("Login_button")) {
            G("Login_div").focus();
            G("Login_button").click();
           }
          }
         };
     with (LoginDiv.style) {
     position = 'absolute';
     //left = (document.documentElement.clientWidth - loginDivWidth)/2 + 'px';
     //top = (document.documentElement.clientHeight -170)/2 + 'px';
     



     width = loginDivWidth + 'px';
     height='170px';
     
     top=(document.documentElement.scrollTop+(document.documentElement.clientHeight-LoginDiv.offsetHeight)/2)+"px";
    left=(document.documentElement.scrollLeft+(document.documentElement.clientWidth-LoginDiv.offsetWidth)/2)+"px";
     

     
     zIndex = 2000;
     background = '#FFFFFF';
     padding='0px';
     //border = '#66CCFF solid 1px';
    }
   }
   if(isLogin) {
    G("Login_div").innerHTML = Login_in_flow;
   } else {
    G("Login_div").innerHTML = str;//换成其它的
   }
  
}

function popLoginFlow(isLogin) {
   popCoverDiv();  
   popLogin(isLogin);
   document.body.style.overflow = "hidden";
   if(isLogin) {
     G("username").focus();
   }
}


function checkOldPwd(){
   if(G("old_pwd").value.trim() == ''){
    return '<div style="color:#FF0000";">Old Password field is required.</div>';
   }
   return '';
}

function copyToClipBoard(title)
{
	var clipBoardContent = ''; 
	clipBoardContent += title;
	clipBoardContent += "\r\n" + location.href;
	window.clipboardData.setData("Text",clipBoardContent);
    alert('复制成功，您可以推荐给您的好友！');
}
