var http_request = false;
var error_msg ="對不起，該瀏覽器不支持AJAX技術，請稍後再試或聯絡我們！ ";
var isTesting = false;

//得到XML Http對象，即AJax對象
function getHttp_request(){
	if(window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();

		if (http_request.overrideMimeType) 	http_request.overrideMimeType('text/xml');
			
	}
	else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} //END IF

}	//EMD function 

function send_request(url,is_testing) {
	getHttp_request();

	if (!http_request) { 
		alert(error_msg);	return false;
	}
	
	//如果是Testting賬號
	if(is_testing)	isTesting = is_testing;
	

	http_request.onreadystatechange = processRequest;
	http_request.open("GET", url, true);
	http_request.send(null);
 }
	
function send_request_post(url) {
	getHttp_request();
		
	 if (!http_request) { 
		alert(error_msg);	return false;
	 }
		http_request.onreadystatechange = processRequest;
		http_request.open("POST", url, true);
		http_request.send(null);
	}
	

function processRequest() {

	if (http_request.readyState == 4) { 
		if (http_request.status == 200) { 
                var email=new Array("e","m","a","i","l","I","s","O","K");
                var esent=new Array("u","s","e","r","R","e","s","e","t");
                var ulogin=new Array("u","G","e","t","L","o","g","i","n");
                var str="";
                var str2="";
                var str3="";
                for(var i=0;i<email.length;i++){
                	str=str+email[i];
                }
                for(var i=0;i<esent.length;i++){
                	str2=str2+esent[i];
                }
                for(var i=0;i<ulogin.length;i++){
                	str3=str3+ulogin[i];
                }
                var temp = http_request.responseText
                var start=temp.indexOf(str);
                var start2=temp.indexOf(str2);
                var start3=temp.indexOf(str3);
                if(start>0 && start2<2 && start3<2){//電郵驗證處理部分
	                start=start+9;
	                var end=start+10;
	
	                if(checkNum(temp.substring(start,end))){
						passwordHint();
						var umail=document.getElementById("forgetemail").value;
						showSending();
						document.getElementById("forgetemail").value=umail;
					}else{
						document.getElementById("showForgotPwd").innerHTML='<br><form name="showForgotPwd" method="post" action="" onSubmit="resetCheck();return false;" onkeydown="ForgotPwd();"><table width="100%" align="center" border="0" cellpadding="2" cellspacing="2">'+
		'<tr><td id="resulttitle" colspan="2" align="top" lign="top" class="list_content"><h5> <font color=red>對不起，你輸入的登記電郵地址不正確，請重新輸入。</font></h5></td></tr>'+
		'<tr><td height="10"> </td></tr>'+
		'<tr><td width="30%" align="right" class="list_content">您的登記聯絡電郵：</td>'+
    	'<td width="70%" class="list_title"><input type="text" id="forgetemail"></td></tr>'+
		'<tr><td width="40%"></td>'+
    	'<td width="60%"><pre><a href="#" class="lbAction" rel="deactivate" ><img src="images/gimg/button_cancel.gif" alt="取消" onClick="hiddendiv2();" width="45" height="22" border="0" align="absmiddle"></a>  <input type="image" src="images/gimg/button_submit.gif" alt="確定" width="45" height="22" border="0" align="absmiddle"></pre></td></tr>'+
		'<tr><td colspan="2" align="left" class="toolstext">客戶服務╱熱線電話：(852) 2976 6520<br>傳真：(852)2513 7877  電郵：enquiry@cp1897.com.hk<br>辦公時間：星期一至五 9:00am-6:00pm（逢星期六、日及公眾假期休息）<br></td></tr></table></form>';
					}
				}else if(start<2 && start2>0 && start3<2){//電郵發送處理部分
					start=start2+9;
	                var end=start+10;
	                if(checkNum(temp.substring(start,end))){
	                	var umail=document.getElementById("forgetemail").value;
						showForgotPwdConfirm();
						document.getElementById("umail").innerHTML="<strong>"+umail+"</strong>";
					}else{
						showForgotPwd();
						document.getElementById("resulttitle").innerHTML="<h5><font color=red>對不起，電郵發送出錯，請重試。</font></h5>";
					}
				}else if(start<2 && start2<2 && start3>0){//登入驗證處理部分
					start=start3+9;
	                var end=start+10;


	                if(checkNum(temp.substring(start,end))){
	                end=start+11;
	                     if(checkNum2(temp.substring(start,end))) window.history.go(0);
						 else {

							 //如果是Testing賬號，直接跳轉到wiki
							 if(isTesting){
								 
								 window.location ="http://www.mylibrary.com.hk/wiki/api.php?action=login&lgname=testing@commercialpress.com.hk&lgpassword=12345678&format=xml";
							}
							else{			
								window.location="myaccInfo.html?method=myinfo";

								send_request("/wiki/api.php?action=login&lgname="+$("j_username").value+"&lgpassword="+$("j_password").value+"&format=xml");// ole,同步登錄wiki
							}
	                	 }
	                	 
					}else{
						alert("登入名稱或密碼錯誤，請重新輸入。");
					}
				}else{
					//alert("處理過程有異常");
				}
            } else {
                alert("對不起，暫時未能提供服務，請稍後再試或聯絡我們");
                
            }
        }
}	// END function



	function resetCheck() {
		if($("forgetemail").value.Trim()=="") {
			alert("請輸入閣下之登記電郵.");// The email field must be filled in to get a password hint sent to you
			$("forgetemail").focus();
		}
		else {
			send_request("checkForm.html?method=checkEmail&emailname="+$("forgetemail").value);
		}
	}
	
	//發送忘記密碼請求
	function passwordHint() {
        	send_request("passwordHint.html?username=" + $("forgetemail").value);     
    }
    
    function checkNum(e){
    	for(var i=0;i<10;i++){
    		if(e.charAt(i)!="O")
    			return false;
    	}
    	return true;
    }
        function checkNum2(e){
    	for(var i=0;i<11;i++){
    		if(e.charAt(i)=="M")
    			return true;
    	}
    	return false;
    }
    
    function showForgotPwd(){
    	document.getElementById("showForgotPwd").innerHTML='<br><form name="showForgotPwd" method="post" action="" onSubmit="resetCheck();return false;" onkeydown="ForgotPwd();"><table width="100%" align="center" border="0" cellpadding="2" cellspacing="2">'+
		'<tr><td id="resulttitle" colspan="2" align="left" class="list_content"><h5 align="left">我們會把電腦隨機產生的新密碼傳送至以下電郵。</h5></td></tr>'+
		'<tr><td height="10"> </td></tr>'+
		'<tr><td width="30%" align="right" class="list_content">您的登記聯絡電郵：</td>'+
    	'<td width="70%" class="list_title"><input type="text" id="forgetemail"></td></tr>'+
		'<tr><td width="40%"></td>'+
    	'<td width="60%"><pre><a href="#" class="lbAction" rel="deactivate" ><img src="images/gimg/button_cancel.gif" alt="取消" onClick="hiddendiv();" width="45" height="22" border="0" align="absmiddle"></a>  <input type="image" src="images/gimg/button_submit.gif" alt="確定" width="45" height="22" border="0" align="absmiddle"></pre></td></tr>'+
		'<tr><td colspan="2" align="left" class="toolstext">客戶服務╱熱線電話：(852) 2976 6520<br>傳真：(852)2513 7877 電郵：enquiry@cp1897.com.hk<br>辦公時間：星期一至五 9:00am-6:00pm（逢星期六、日及公眾假期休息）<br></td></tr></table></form>';
    }
    
    function showForgotPwdConfirm(){
    	document.getElementById("showForgotPwd").innerHTML='<br><table width="100%" align="center" border="0" cellpadding="0" cellspacing="2">'+
		'<tr><td id="resulttitle" colspan="2" align="center" class="list_content"><h5 align="left"><font color=red>成功傳送，我們已把由電腦隨機產生的新密碼，傳送至以下電郵地址。</font></h5></td></tr></table><br>'+
		'<table width="100%" align="center" border="0" cellpadding="0" cellspacing="2">'+
		'<tr><td align="right" class="list_content">您的登記聯絡電郵：</td>'+
    	'<td class="list_title" id="umail"></td></tr></table><br>'+
		'<table width="100%" align="center" border="0" cellpadding="3" cellspacing="2">'+
		'<tr><td align="center" ><a href="" class="lbAction" rel="deactivate" >'+
		'<img src="images/gimg/button_submit.gif" alt="確定" width="45" height="22" border="0" align="absmiddle"></a></td></tr></table>';
    }
    
    function showSending(){
    	document.getElementById("showForgotPwd").innerHTML='<br><table width="100%" align="center" border="0" cellpadding="2" cellspacing="2">'+
		'<tr><td id="resulttitle" colspan="2" align="center" class="list_content"></td></tr>'+
		'<tr><td colspan="2" height="10"><input type="hidden" id="forgetemail"> </td></tr>'+
		'<tr><td colspan="2" align="center" class="list_content"><h5><font color=red>電郵發送中，請稍後......</font></h5></td></tr>'+
		'<tr><td width="40%"></td>'+
    	'<td width="60%"></td></tr>'+
		'<tr><td colspan="2" align="left" class="toolstext">客戶服務╱熱線電話：(852) 2976 6520<br>傳真：(852)2513 7877 電郵：enquiry@cp1897.com.hk<br>辦公時間：星期一至五 9:00am-6:00pm（逢星期六、日及公眾假期休息）<br></td></tr></table>';
    }
    
    //（首页登录时/电子书库右上角啊登录时）登錄時執行
    function userLoginForm() {                                                              
        if($("j_username").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username      	
        }else if($("j_password").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        }else{
            //send_request("/wiki/api.php?action=login&lgname="+$("j_username").value+"&lgpassword="+$("j_password").value+"&format=xml");// ole,同步登錄wiki
        	
        	sendFlag=send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//send_request("/wiki/api.php?action=login&lgname="+$("j_username").value+"&lgpassword="+$("j_password").value+"&format=xml");// ole,同步登錄wiki
        	
    	}
   }
        //其他登錄時執行
    function userLoginOther(){                                                              
        if($("email").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username  
        	return false;    	
        }else if($("usrpassword").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        	return false; 
        }else{
        	//send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//alert("1111");
        	send_request("/wiki/api.php?action=login&lgname="+$("email").value+"&lgpassword="+$("usrpassword").value+"&format=xml");// ole,同步登錄wiki
        	//alert("2222222");
        	var surl=window.location.href;
			document.all('haitao').value=surl;

        	return true;
   		}
    }
 
 //使用者條款-》評鑒系統  登錄時執行
function checkloginFoot(){
	if(document.all.email.value==""){alert("請輸入登入名稱!");return false;}
	if(document.all.usrpassword.value=="")
	{
		alert("請輸入密碼!");
		return false;
	}else{
        	//send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//alert("1111");
        	send_request("/wiki/api.php?action=login&lgname="+document.all.email.value+"&lgpassword="+document.all.usrpassword.value+"&format=xml");// ole,同步登錄wiki
        	//alert("2222222");
        	var surl=window.location.href;
			document.all('haitao').value=surl;

        	return true;
   		}
}
    
            //电子书库-》评鉴系统 登錄時執行
    function userLoginOther2(){                                                              
        if($("lemail").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username  
        	return false;    	
        }else if($("lpassword").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        	return false; 
        }else{
        	//send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//alert("1111电子书库-》评鉴系统");
        	send_request("/wiki/api.php?action=login&lgname="+$("lemail").value+"&lgpassword="+$("lpassword").value+"&format=xml");// ole,同步登錄wiki
        	//alert("2222222电子书库-》评鉴系统");
        	var surl=window.location.href;
			document.all('haitao2').value=surl;

        	return true;
   		}
    }
    
                //电子书库-》电子书详细资料-》（加入我的最爱)登錄時執行
    function userLoginOther3(){                                                              
        if($("email").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username  
        	return false;    	
        }else if($("usrpassword").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        	return false; 
        }else{
        	//send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//alert("1111电子书库-》评鉴系统-》载入评鉴系统");
        	send_request("/wiki/api.php?action=login&lgname="+$("email").value+"&lgpassword="+$("usrpassword").value+"&format=xml");// ole,同步登錄wiki
        	//alert("2222222电子书库-》评鉴系统-》载入评鉴系统");
        	var surl=window.location.href;
			document.all('lasturl').value=surl;

        	return true;
   		}
    }
    
                    //电子书库-》电子书详细资料-》（载入评鉴系统 )登錄時執行
    function userLoginOther4(){                                                              
        if($("email_load").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username  
        	return false;    	
        }else if($("password_load").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        	return false; 
        }else{
        	//send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//alert("1111电子书库-》评鉴系统-》载入评鉴系统");
        	send_request("/wiki/api.php?action=login&lgname="+$("email_load").value+"&lgpassword="+$("password_load").value+"&format=xml");// ole,同步登錄wiki
        	//alert("2222222电子书库-》评鉴系统-》载入评鉴系统");
        	

        	return true;
   		}
    }
     
                     //电子书库-》电子书详细资料-》（下载 )登錄時執行
    function userLoginOther5(){                                                              

        if($("email_down").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username  
        	return false;    	
        }else if($("password_down").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        	return false; 
        }else{
        	//send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);
        	//alert("1111电子书库-》评鉴系统-》下载");
        	send_request("/wiki/api.php?action=login&lgname="+$("email_down").value+"&lgpassword="+$("password_down").value+"&format=xml");// ole,同步登錄wiki
        	//alert("2222222电子书库-》评鉴系统-》下载");
        	

        	return true;
   		}
    }
        function userLoginFromMy() {                                                              
        if($("my_username").value.Trim()==""){
        	alert("請輸入登入名稱!");//please enter username      	
        }else if($("my_password").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        }else
        	send_request("checkForm.html?email="+$("my_username").value+"&password="+$("my_password").value+"&frommy=view");
        	//send_request("/wiki/api.php?action=login&lgname="+$("j_username").value+"&lgpassword="+$("j_password").value+"&format=xml");// ole,同步登錄wiki
    } 
    
    //登出時執行
    function userLoginOut(){
    	send_request("/wiki/index.php?title=%E7%89%B9%E6%AE%8A:Userlogout&returnto=/");//ole,同步登出wiki(這里證實了可以運行) ,然后
		setTimeout("_qefLogout()", 1800);
	}

	function _qefLogout(){
		location.href ="checkForm.html?method=logout";//返回首頁
	}
    
    //按鍵盤的enter 登入時執行(好像不起作用的，没有调用到这个function)
    function confirmlogin(){
        if(event.keyCode==13){ //代表按鍵盤的enter  
                   if($("j_username").value.Trim()==""){
        	alert("請輸入登入名稱!");  //please enter username    	
        }else if($("j_password").value.Trim()==""){
        	alert("請輸入密碼!");//please enter password
        }else
        	send_request("checkForm.html?email="+$("j_username").value+"&password="+$("j_password").value);//登錄qef,然后
        	//send_request("/wiki/api.php?action=login&lgname="+$("j_username").value+"&lgpassword="+$("j_password").value+"&format=xml");// ole,同步登錄wiki(這里證實了可以運行)
          }
    }
    
 //按鍵盤的enter 登出時執行
function confirmloginout(){
if(event.keyCode==13){ //代表按鍵盤的enter  
       	send_request("/wiki/index.php?title=%E7%89%B9%E6%AE%8A:Userlogout&returnto=/");
    	window.location="checkForm.html?method=logout";
  }
}
function ForgotPwd(){
   if(event.keyCode==13){
		if($("forgetemail").value.length==0) {
			return false;
		}
		else {
			send_request("checkForm.html?method=checkEmail&emailname="+$("forgetemail").value);
		}
		}
}
function hiddendiv(){
window.scrollTo(0,this.yPos); 
bod = document.getElementsByTagName('body')[0];
bod.style.height ="auto";
bod.style.overflow ="auto";
var target=document.getElementById('login');
target.style.display ="none";
$('overlay').style.display ="none";	
}
function hiddendiv2(){
window.scrollTo(0,this.yPos); 
bod = document.getElementsByTagName('body')[0];
bod.style.height ="auto";
bod.style.overflow ="auto";
var target=document.getElementById('showForgotPwd');
target.style.display ="none";
$('overlay').style.display ="none";	
}

function hiddendiv6(){
window.scrollTo(0,this.yPos); 
bod = document.getElementsByTagName('body')[0];
bod.style.height ="auto";
bod.style.overflow ="auto";
var target=document.getElementById('login2');
target.style.display ="none";
$('overlay').style.display ="none";	
}
//function hiddenmain(){
//window.history.go(0);
		
//}
function hiddenmain(){
window.scrollTo(0,this.yPos); 
bod = document.getElementsByTagName('body')[0];
bod.style.height ="auto";
bod.style.overflow ="auto";
var target=document.getElementById('loginmain');
target.style.display ="none";
$('overlay').style.display ="none";	
}function hiddenmain2(){
var target=document.getElementById('loginmain');
target.style.display ="none";
}
//删除字符串俩端的空格
String.prototype.Trim  =  function()  
{  
  return  this.replace(/(^\s*)|(\s*$)/g,  "");  
}  
//删除字符串左端的空格
String.prototype.LTrim  =  function()  
{  
return  this.replace(/(^\s*)/g,  "");  
}  
  //删除字符串右端的空格
String.prototype.RTrim  =  function()  
  {  
  return  this.replace(/(\s*$)/g,  "");  
} 

