﻿var regExpPassLength = /^[a-zA-Z0-9]+$/;
function load181(msg)
{   	
  	if(msg.indexOf("帳號") > -1 ){
  		alert(msg); 		
  		document.forms[2].residentNo.focus();		
 	}else
	if(msg.indexOf("密碼") > -1 ){
  		alert(msg); 		
  		document.forms[2].password.focus();		
 	}else
 	if(msg.indexOf("驗證碼") > -1 ){
  		alert(msg); 
  		document.forms[2].validateCode.value = "";		
  		document.forms[2].validateCode.focus();		
 	}else{
 		alertMessage(msg);
 	}

    var loginButton = document.getElementById("loginButton");   
    loginButton.onclick = function(event){
    	
    	document.forms[2].elements["buttonName"].value = "login";        	
    	document.forms[2].elements["method"].value = "login";    		  	
    	if(checkLoginField()){  
    		document.forms[2].submit();
    	} 
    }; 
    /*
    var updateButton = document.getElementById("updateButton");     
    updateButton.onclick = function(event){     
    	document.forms[2].elements["buttonName"].value = "update";       	
    	document.forms[2].elements["method"].value = "login";    		  	
    	if(checkLoginField()){    	
    		document.forms[2].submit();
    	} 
    }; 
    */
    
} 

function passwordPage() {
	document.forms[2].elements["method"].value = "preparePassword";	
	if(checkIdField()){			
		document.forms[2].submit();    	
    } 
}

function sendCertPage() {
	document.forms[2].elements["method"].value = "prepareCert";	
	if(checkIdField()){			
		document.forms[2].submit();    	
    } 
}

function checkLoginField() {
	var errMsg = "";
	var emptyMsg = "";
	var count = 1;	
	
	var form = document.forms[2];
	var residentNo = form.elements["residentNo"].value;
	var memberType = form.elements["memberType"].value;
	var password = form.elements["password"].value;
	var validateCode = form.elements["validateCode"].value;	
	
	//檢查欄位是否為空
	if(residentNo == ""){
		if(emptyMsg == ""){form.elements["residentNo"].focus();}
		if(memberType=='1'){
			emptyMsg = (count++) + ". 請輸入居留證號碼!\n";
		}
		else if(memberType=='2'){
			emptyMsg = (count++) + ". 請輸入身分證字號!\n";
		}
		
	}
	if(password == ""){
		if(emptyMsg == ""){form.elements["password"].focus();}
		emptyMsg = emptyMsg + (count++) + ". 請輸入密碼!\n";
	}
	if(validateCode == ""){
		if(emptyMsg == ""){form.elements["validateCode"].focus();}
		emptyMsg = emptyMsg + (count++) + ". 請輸入驗證碼!\n";
	}
	
	//檢查欄位合理性
	if(emptyMsg == ""){ //必填欄位已填寫	
		
		if(!checkID(residentNo) && memberType=='2') {
			form.elements["residentNo"].focus();
			errMsg = (count++) + ". 身分證字號輸入錯誤!\n";					
		}
		else if(memberType=='2' && residentNo==''){
			form.elements["residentNo"].focus();
			errMsg = (count++) + ". 居留證號碼輸入錯誤!\n";
		}
		
		//密碼
		//if(password.length < 6){ //畫面限制最多8碼
			//if(errMsg == ""){form.elements["password"].focus();}
			//errMsg = errMsg + (count++) + ". 請輸入6~8碼的密碼!\n";
		//} else {
			if(password.search(regExpPassLength)==-1) {
				if(errMsg == ""){form.elements["password"].focus();}
				errMsg = errMsg + (count++) + ". 密碼輸入錯誤，請重新輸入!\n";
			}
		//}
		//驗證碼		
		if(validateCode.search(regExpValidateCode)==-1) {
			if(errMsg == ""){form.elements["validateCode"].focus();}
			errMsg = errMsg + (count++) + ". 驗證碼輸入錯誤!\n";					
		}	
		if(errMsg == ""){
			return true;
		}else {
			if(errMsg.indexOf("2.") < 0 ){
				errMsg = errMsg.substring(3, errMsg.length);
			}
			alert(errMsg);
			return false;
		}				
	}else { //尚有必填欄位未填寫
		if(emptyMsg.indexOf("2.") < 0 ){
			emptyMsg = emptyMsg.substring(3, emptyMsg.length);
		}
		alert(emptyMsg);
		return false;
  	}	
}	
	
function checkIdField(){	
	var residentNo = document.forms[2].elements["residentNo"].value;
	if(residentNo != "" && !checkID(residentNo)){
		document.forms[2].elements["residentNo"].focus();
		alert("身分證字號輸入錯誤!\n");
		return false;	
	}else{
		return true;
	}
}

//檢查身分識別號    ----> 包括身分證,居留證
//用於驗證控制項
function check_id(){
	var form = document.forms[2];
	var id = form.residentNo.value.toUpperCase();
	var memberType = form.memberType.value; //國籍:1.非本國,2.本國
	if(id==''){
		return false;
	}
	//檢查身分證字號
	if (memberType=="2"){
		if (id.length != 10){
			alert('身分證字號格式不正確');
			form.residentNo.value='';
			return false;
		}

		if (isNaN(id.substr(1,9)) || (id.substr(0,1)<"A" || id.substr(0,1)>"Z")){
			alert('身分證字號格式不正確');
			form.residentNo.value='';
			return false;				
		}
		
		var head="ABCDEFGHJKLMNPQRSTUVXYWZIO";
		id = (head.indexOf(id.substring(0,1))+10) +''+ id.substr(1,9)
		s =parseInt(id.substr(0,1)) + 
		parseInt(id.substr(1,1)) * 9 + 
		parseInt(id.substr(2,1)) * 8 + 
		parseInt(id.substr(3,1)) * 7 + 			
		parseInt(id.substr(4,1)) * 6 + 
		parseInt(id.substr(5,1)) * 5 + 
		parseInt(id.substr(6,1)) * 4 + 
		parseInt(id.substr(7,1)) * 3 + 
		parseInt(id.substr(8,1)) * 2 + 
		parseInt(id.substr(9,1)) + 
		parseInt(id.substr(10,1));

		//判斷是否可整除
		if ((s % 10) != 0){
			alert('身分證字號格式不正確');
			form.residentNo.value='';
			return false;
		}
			   
	//檢查居留證號碼
	}else if  (memberType=="1"){
		if (id.length != 10){
			alert('居留證號碼格式不正確');
			form.residentNo.value='';
			return false;
		}

		if (isNaN(id.substr(2,8)) || (id.substr(0,1)<"A" ||id.substr(0,1)>"Z") || (id.substr(1,1)<"A" ||id.substr(1,1)>"Z")){
			alert('居留證號碼格式不正確');
			form.residentNo.value='';
			return false;					
		}
		
		var head="ABCDEFGHJKLMNPQRSTUVXYWZIO";
		id = (head.indexOf(id.substr(0,1))+10) +''+ ((head.indexOf(id.substr(1,1))+10)%10) +''+ id.substr(2,8)
		s =parseInt(id.substr(0,1)) + 
		parseInt(id.substr(1,1)) * 9 + 
		parseInt(id.substr(2,1)) * 8 + 
		parseInt(id.substr(3,1)) * 7 + 			
		parseInt(id.substr(4,1)) * 6 + 
		parseInt(id.substr(5,1)) * 5 + 
		parseInt(id.substr(6,1)) * 4 + 
		parseInt(id.substr(7,1)) * 3 + 
		parseInt(id.substr(8,1)) * 2 + 
		parseInt(id.substr(9,1)) + 
		parseInt(id.substr(10,1));

		//判斷是否可整除
		if ((s % 10) != 0){
			alert('居留證號碼格式不正確');
			form.residentNo.value='';
			return false;
		}
	}
}

