/*
=======================================================================================================
File Name     : form.js
Language     :JavaScript
Last Modify    :20000/06/22
CopyingRight:Ho Yi-Lin
Descrition		: Form Object Addon
Functional: 
Platform: Internet Explorer,Netscape Navigator
=======================================================================================================
*/
// must include string.js
// must include prototype.js

// 強制註冊執行 init
document.forms[0].onload = formObjectAddon();

//initialisize
function formObjectAddon(){
	
	for(i=0;i<document.forms.length;i++){
		//document.forms[i].submitCheck=submitCheck;
		//document.forms[i].onsubmit=submitCheck;		
		document.forms[i].lock=lock;
		document.forms[i].unlock=unlock;
		document.forms[i].getTextCol=getTextCol;
		document.forms[i].anyhow=anyhow;
		var textCol=document.forms[i].getTextCol();
		for(j=0;j<textCol.length;j++){
			//check 繼承
			if(j==0&&(!textCol[j].check)) textCol[j].check="U10";
			//if(!textCol[j].check) textCol[j].check=textCol[j-1].check;
			if(textCol[j].check == null) textCol[j].check="U10";
			//if(!textCol[j].check) textCol[j].check="U10";
			textCol[j].checkValue=checkValue;
			if(navigator.appName=="Microsoft Internet Explorer") textCol[j].onblur=checkValue;
			else textCol[j].onchange=checkValue;			
			textCol[j].checkType=textCol[j].check.charAt(0);
			textCol[j].allowNull=textCol[j].check.charAt(1);
			textCol[j].minLength=textCol[j].check.substring(2);
			// Reset BackColor
			textCol[j].onkeydown= resetBackColor;
		}
	}
	
}

// get TextBox Collection
function getTextCol(){
	var textCol=[];var j=0;
	for(i=0;i<this.elements.length;i++)
	{
	  if (this.elements[i].type=="text"||this.elements[i].tagName=="TEXTAREA")
	  {
		textCol[j] = this.elements[i];
		j++;
		
	  }
	}
	return textCol;
}

function error(obj,number){
	this.number=number;
	this.msg=errorHashTable(number,arguments[2]);
	if(obj.alias) this.msg=obj.alias+this.msg;
	obj.value="";
	obj.focus();
	alert(this.msg);
	// Clor 反白	
	var errBackcolor = {'background-color':'#FFFFC0'};
	Element.setStyle(obj.id,errBackcolor);
	if(obj.check){obj.value="";obj.focus();}
	
}

function resetBackColor()
{
	try
	{
		var Backcolor = {'background-color':''};
		Element.setStyle(this.id,Backcolor);
	}
	catch(ex)
	{
		alert(ex.message);
	}
}

//Error Message
function errorHashTable(key){
	
	var error=new Array();
	error[0]="不允許空白！";
    error[1]="需為十位數！";
	error[2]="第一個字需為英母！";
	error[3]="後九位需為數字！";
	error[4]="號碼驗證錯誤！";
	error[5]="請輸入數字！";
	error[6]="請輸入英文字母！";
	error[7]="為無效的Email格式！\n\n例：xxx@xxx.com.tw";
	error[8]="為無效的郵遞區號！";
	error[9]="請輸入整數！";
	error[10]="為無效的日期格式！\n\n例：0891231"
	error[11]="請輸入最少"+arguments[1]+"位數！";
	error[12]="為錯誤的電話號碼格式!\n\n例:02-27377000";
	error[13]="為必要填寫之欄位！";
	error[14]="請填寫英文字母或整數！";
	error[15]="為無效的日期! 例:2007/03/01";
	error[16]="為無效的小時格式!";
	error[17]="為無效的分鐘格式!";
	error[18]="輸入日期大於今天日期!";
	error[19]="請最少輸入"+arguments[1]+"個欄位";

	return error[key];
}

//form送回server 前的Check
function submitCheck(){

	var textCol=this.getTextCol();
		for(i=0;i<textCol.length;i++){
			if(textCol[i].allowNull==0&&textCol[i].value.isEmpty()&&!textCol[i].disabled){
				new error(textCol[i],13);
				return false;
			}
			
		}
	
	return true;
	
}

//textbox的檢查
function checkValue(){

		
	//檢查是否為空值
	if(this.value.isEmpty()) return false;
	//檢查位數
	if(this.value.length < this.minLength){
		new error(this,11,this.minLength);
		return false;
	}

    //各種格式檢查
	switch(this.checkType){
			case "I":return idnoCheck(this);	//身份證字號 format
     		case "F":return numCheck(this);		//Number format
			case "A":return alphaCheck(this);	//英文字 format
			case "E":return emailCheck(this);	//Email format
			case "Z":return zipCheck(this);		//Zip Code format
    		case "W":return intCheck(this);		//Integer format
			case "D":return chDate(this);		//Date format(2000/12/31)
			case "T":return telCheck(this);		//Phone number format
			case "B":return iaCheck(this);		//英數字format
			case "C":return cdateCheck(this);	//民國年 format(89/12/31)
			case "H":return hourCheck(this);	//時 format
			case "M":return minuteCheck(this);	//分 format
			case "d":return dateCheck(this);	//Date format(2000/12/31)
			case "U":return true;				//free format
	}
}

 //數字鎖定(使用KeyPress觸發)
 function lockNum(){

		if ((event.keyCode < 47) || (event.keyCode > 58)) 
		     event.keyCode = 0;
	
 }

//英文字母鎖定(使用KeyPress觸發)
 function lockAlpha(){
 
	if ((event.keyCode < 65) || (event.keyCode > 90&&event.keyCode <97 ||event.keyCode >122 ))
        event.keyCode = 0;
 }

//鎖定
function lock(){

	for (i=0;i<this.elements.length;i++){
		if (this.elements[i].name=="edit") continue;
		this.elements[i].style.color="red";
		this.elements[i].disabled=true;
		for(j=0;j<arguments.length;j++){
			if(this.elements[i].name==arguments[i]){this.elements[i].disabled=false;this.elements[i].color="black";}
		}
	}
}

//解除
function unlock(){
	
	for (i=0;i<this.elements.length;i++){
		if (this.elements[i].name=="edit"){this.elements[i].disabled=true;continue;}
		this.elements[i].disabled=false;
		this.elements[i].style.color="black";
		for(j=0;j<arguments.length;j++){
			if(this.elements[i].name==arguments[i]){this.elements[i].readOnly=true;this.elements[i].color="gray"}
		}
	}

	for (i=0;i<this.elements.length;i++ ){
		if (this.elements[i].type=="text"&&!this.elements[i].readOnly){
			this.elements[i].focus();
			break;
		}
	}

}

//檢查身份證號碼
function idnoCheck(obj){

	with(obj){
		if(!value.charAt(0).isAlpha()){new error(obj,2);return false;}
		if(!value.allowLength(10)){new error(obj,1);return false;}
		if(!value.substring(1).isNum()){new error(obj,3);return false;}
		if(!value.isIDNo()){new error(obj,4);return false;}
	}
	return true;
}

//檢查數字和英文字
function iaCheck(obj){

	with(obj){
		if(!value.isIA()){new error(obj,14);return false;}
	}
	return true;
}

//檢查數字
function numCheck(obj){

	with(obj){
		if(!value.isNum()){new error(obj,5);return false;}
	}
	return false;
}

//檢查英文字母
function alphaCheck(obj){

	with(obj){
		if(!value.isAlpha()){new error(obj,6);return false;}
	}
	return true;
}

//檢查E-mail
function emailCheck(obj){

	if (obj.value.trimSpace() == '') { return true; } 
	with(obj){
		if((value.indexOf("@")==-1|| value.indexOf(".")==-1)){new error(obj,7);return false;}
	}
	return true;
 }

//檢查郵遞區號
 function zipCheck(obj){

 	with(obj){
		if(!(value.allowLength(3)&&value.isNum())){new error(obj,8);return false;}
	}
	return true;
 }

//檢查整數
function intCheck(obj){

	with(obj){
		if(!value.isInt()){new error(obj,9);return false;}
	}
	return true;
}

//檢查日期
function dateCheck(obj){

	with(obj){
		if(!value.isDate()){new error(obj,15);return false;}
	}
	return true;
}

//檢查電話號碼
function telCheck(obj){

  if (obj.value.trimSpace() == '') { return true; } 
  with(obj){
	if(value.length < 6) {new error(obj,12);return false;}
	else{
		for(i=0;i<value.length;i++){
				if(value.charAt(i)=="-" || value.charAt(i)=="(" || value.charAt(i)==")") continue;
				if(value.charAt(i) < "0" || value.charAt(i) > "9"){new error(obj,12); obj.focus();return false;} 
		}
	} 
  }
  return true;
}

//檢查民國日期(89/12/31)
function cdateCheck(obj){

	var tempArray=obj.value.split("/")
	if(tempArray.length==3){
		if(tempArray[0].isInt()&&tempArray[0].isInt()&&tempArray[0].isInt()){
			if(parseInt(tempArray[0]) <150){
				tempArray[0]=parseInt(tempArray[i])+1911;
				var temp=tempArray.join("/")
				if(temp.isDate()) return true;
			}
		}
	}
	new error(obj,15);
	return false;
}

//檢查小時
function hourCheck(obj){

  if(obj.value.isInt()){
	if(parseInt(obj.value) <= 23 && parseInt(obj.value) >= 0) return true;
  }
  new error(obj,16);
  return false;
}

//檢查分鐘
function minuteCheck(obj){

  if(obj.value.isInt()){
	if(parseInt(obj.value) <=  59 && parseInt(obj.value) >= 0) return true;
  }
  new error(obj,17);
  return false;
}

//檢查民國日期(0891231)
function chDate(obj){

	with(obj){
		value=(value.length==7)?value:"0"+value;
		var year=new Number(value.substring(0,3));
		var month=new Number(value.substring(3,5));
		var day=new Number(value.substring(5,7));
		var temp=new Date(year+1911,month-1,day);
		if(temp.getMonth()!=month-1){new error(obj,10);return false};
		var today=new Date();
		if((today-temp)<0){new error(obj,18);return false};
	}
	return true;
}

//檢查至少n個欄位有填
function anyhow(n){
	var j=0;
	var textCol=this.getTextCol();
	for(i=0;i<textCol.length;i++){if(textCol[i].value!="") j++;}
	if(j>=n) return true;
	else{new error(this,19,n);return false;}
}