//共用，主物件
function FormObject(_form){
    this.form=_form;
        
    this.item=new Array();
    this.add=function(_item){
    	this.item[this.item.length]=_item;
    }		 
    this.checkData=function(){
    	try{ 
    		for(var i=0;i<this.item.length;i++){
    		   this.item[i].checkData(this.form);
    		}
    		eval(this.form+".submit();");
    	}catch(e){
    		//this.executeObject.focus();
    		throw new Error(e.message);
    		 
    	}	 
    }		 
}	

//共用，判斷是不是空值		 
function isEmpty(_arg){
    for (var i = 0; i < _arg.length; i++){
    	if (_arg.charAt(i)!=" "){
    		return false;
    	}	 
    }		 
    return true;
}
//下拉選單
function SelectField(_fieldName, _errorMessage) {
	return new SelectObject(_fieldName, _errorMessage);
}
function SelectObject(_fieldName, _errorMessage) { 
	this.fieldName = _fieldName;
	this.errorMessage= _errorMessage;
	this.checkData = function(_form) {
		var form=eval(_form+"."+this.fieldName);
		
		//空值
		if (form.options.length==0) {
			this.errorMessage += "欄位未選";
			throw new Error(this.errorMessage);
		}
		//if (form.selectedIndex==-1 || isEmpty( form.options[ form.selectedIndex ].value ) ) {
		if (form.selectedIndex==-1 || isEmpty( form.options[ form.selectedIndex ].value ) ||  form.options[ form.selectedIndex ].value==-1) {
			this.errorMessage += "欄位未選";
			throw new Error(this.errorMessage);
		}
		return true;
	}
}
	
//日期欄位
function DateField(_fieldName,_formate,_errorMessage){
	return new DateObject(_fieldName,_formate,_errorMessage);
}
function DateObject(_fieldName,_format,_errorMessage){
	this.fieldName=_fieldName;
	this.format=_format;
    this.errorMessage;
    this.checkData=function(_form){
    	 var form=eval(_form+"."+this.fieldName);
    	 if(isEmpty(form.value)){
    	 	   form.focus();
    	 	   this.errorMessage=_errorMessage+"欄位沒有填";
    	 	   throw new Error(this.errorMessage); 
    	 }else{
			 if(this.format=="YYYYMMDD") {
				var pattern = new RegExp("^[0-9]{8,8}$", "g");
				if( !pattern.test(form.value) ) { 
				  form.focus();
					this.errorMessage  =_errorMessage+ "格式不正確(YYYYMMDD)"; 
					throw new Error(this.errorMessage);
				}
				var yearFormat =  form.value.substring(0,4);
				var monthFormat =  form.value.substring(4,6);
				var dayFormat =  form.value.substring(6,8);
			}
			if(this.format == "YYYY/MM/DD") {
				var pattern = new RegExp("^[0-9]{4,4}/[0-9]{1,2}/[0-9]{1,2}$", "g");
				if( !pattern.test(form.value) ) { 
				  form.focus();
					this.errorMessage = _errorMessage+"格式不正確(YYYY/MM/DD)"; 
					throw new Error(this.errorMessage);
				}
				formatArray = form.value.split("/");
				var yearFormat =  formatArray[0];
				var monthFormat =  formatArray[1];
				var dayFormat =  formatArray[2];
			}
			var dayArray = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
			yearFormat = parseInt(yearFormat,10);
			monthFormat = parseInt(monthFormat,10);
			dayFormat = parseInt(dayFormat,10);
			
			if(monthFormat==2 && (yearFormat % 4)==0 ) {
				dayArray[2] = 29;
			}
			if( dayFormat > dayArray[monthFormat]) {
			  form.focus();
				this.errorMessage = _errorMessage+ "格式不正確"; 
				throw new Error(this.errorMessage);
			}

		  if(dayArray[monthFormat]==undefined){
          form.focus();
				  this.errorMessage = _errorMessage+ "格式不正確"; 
				  throw new Error(this.errorMessage);
      }else{
          if(dayFormat>dayArray[monthFormat] || dayFormat==0){
              //alert(dayFormat+"/"+dayArray[monthFormat]);
              form.focus();
				      this.errorMessage = _errorMessage+ "格式不正確"; 
				      throw new Error(this.errorMessage);
          }
      }
    	}
    	return true;
    };		 

}
//Email格式確認
function emailField (_fieldName,_errorMessage) {
    return new emailObject(_fieldName,_errorMessage);
}
function emailObject (_fieldName,_errorMessage) {
    this.fieldName=_fieldName;
    this.errorMessage=_errorMessage;
    this.checkData=function(_form){
       var form=eval(_form+"."+this.fieldName);
    	 if(isEmpty(form.value)){
    	 	   form.focus();
    	 	   this.errorMessage=_errorMessage+"欄位沒有填";
    	 	   throw new Error(this.errorMessage); 
    	 }else{
				   var pattern = new RegExp("^[^.@][A-Za-z0-9_\.]+@[A-Za-z0-9_\.]+[^.@]$", "g");
				   if( !pattern.test(form.value) ) { 
				      form.focus();
					    this.errorMessage = _errorMessage+"格式不正確!"; 
					    throw new Error(this.errorMessage);
				   }
       }
    return true;
    }
}
//mobile格式確認
function MobileField(_fieldName,_errorMessage) {
    return new MobileObject(_fieldName,_errorMessage);
}
function MobileObject(_fieldName,_errorMessage) {
    this.fieldName=_fieldName;
    this.errorMessage=_errorMessage;
    this.checkData=function(_form){
       var form=eval(_form+"."+this.fieldName);
    	 if(isEmpty(form.value)){
    	 	   form.focus();
    	 	   this.errorMessage=_errorMessage+"欄位沒有填";
    	 	   throw new Error(this.errorMessage); 
    	 }else{
				   var pattern = new RegExp("^09[0-9]{2}-[0-9]{6}$", "g");
				   if( !pattern.test(form.value) ) { 
					    this.errorMessage = _errorMessage+"格式不正確!"; 
					    throw new Error(this.errorMessage);
				   }
       }
    return true;
    }
}
//mobile格式確認
function PhoneField(_fieldName,_errorMessage,_pattern) {
    return new PhoneObject(_fieldName,_errorMessage,_pattern);
}
function PhoneObject(_fieldName,_errorMessage,_pattern) {
    this.fieldName=_fieldName;
    this.errorMessage=_errorMessage;
    this.pattern=_pattern;
    this.checkData=function(_form){
       var form=eval(_form+"."+this.fieldName);
    	 if(isEmpty(form.value)){
    	 	   form.focus();
    	 	   this.errorMessage=_errorMessage+"欄位沒有填";
    	 	   throw new Error(this.errorMessage); 
    	 }else{
				   var pattern = new RegExp(this.pattern, "g");
				   if( !pattern.test(form.value) ) { 
					    this.errorMessage = _errorMessage+"格式不正確!"; 
					    throw new Error(this.errorMessage);
				   }
       }
    return true;
    }
}
//比較日期大小
function CompareDateField(_sfieldName,_efieldName){
	return new CompareDateObject(_sfieldName,_efieldName);
}
function CompareDateObject(_sfieldName,_efieldName){
	this.sfieldName=_sfieldName;
	this.efieldName=_efieldName;
	this.errorMessage="開始日期不可大於結束日期";
	this.checkData=function(_form){
		 var sfield=eval(_form+"."+this.sfieldName);
		 var efield=eval(_form+"."+this.efieldName);
		 if(new Date(sfield.value)>new Date(efield.value)){
		 	throw new Error(this.errorMessage); 
		 }
		 return true;
	}
}
function OptionOne_TextField(_field1,_name1,_field2,_name2){
    this.field1=_field1;
    this.field2=_field2;
    this.errorMessage=_name1+"或"+_name2+"欄位至少要填一個";

    this.checkData=function(_form){
      
    	 var form1=eval(_form+"."+this.field1);
    	 var form2=eval(_form+"."+this.field2);
    	 if(isEmpty(form1.value)&& isEmpty(form2.value)){
    	 	   form1.focus();
    	 	   throw new Error(this.errorMessage); 
    	 }	 
    	 return true;
    }; 
}
//文字欄位			 
function TextField(_fieldName,_errorMessage,_length){
    return new TextObject(_fieldName,_errorMessage,_length);	
}			 
			 
function TextObject(_fieldName,_errorMessage,_length){
    
    this.fieldName=_fieldName;
    this.errorMessage=_errorMessage+"欄位沒有填";
    this.checkLength=_length;//確認長度
 
    this.checkData=function(_form){
    	 var form=eval(_form+"."+this.fieldName);
    	 if(isEmpty(form.value)){
    	 	   form.focus();
    	 	   throw new Error(this.errorMessage); 
    	 }
       if(this.checkLength>0 && form.value.length<this.checkLength){
          form.focus();
          throw new Error(_errorMessage+"長度至少要"+this.checkLength+"碼!"); 
       }
    	 return true;
    }; 
    		 
}		 
//CheckBox欄位
function CheckBoxField(_fieldName,_errorMessage){
  return new CheckBoxObject(_fieldName,_errorMessage);
}			 
function CheckBoxObject(_fieldName,_errorMessage){
    this.fieldName=_fieldName;
    this.errorMessage=_errorMessage+"欄位沒有勾";
    this.checkData=function(_form){
    	 var form=eval(_form+"."+this.fieldName);
    	 //var form=eval("document.getElementsByName('"+this.fieldName+"')");
       //alert(document.getElementsByName("tid").length);
       for(i=0;i<form.length;i++){
    	 	if(form[i].checked){
            return true;
    	 	}
    	 }
    	 throw new Error(this.errorMessage); 
    };
}		 
//Radio欄位			 
function RadioField(_fieldName,_errorMessage){
    return new RadioObject(_fieldName,_errorMessage);
}			 
			 
function RadioObject(_fieldName,_errorMessage){
    this.fieldName=_fieldName;
    this.errorMessage=_errorMessage+"欄位沒有填";
    this.checkData=function(_form){
    	 var form=eval(_form+"."+this.fieldName);
    	 for(i=0;i<form.length;i++){
    	 	if(form[i].checked){
    	 	   return true;
    	 	}
    	 }
    	 throw new Error(this.errorMessage); 
    };		 
}

//Password再次確認欄位
function PassWordCheckField (_fieldName1,_fieldName2,_errorMessage) {
    return new PassWordCheckObject(_fieldName1,_fieldName2,_errorMessage);
}
function PassWordCheckObject (_fieldName1,_fieldName2,_errorMessage) {
    this.fieldName1=_fieldName1;
    this.fieldName2=_fieldName2;
    this.errorMessage=_errorMessage;
    this.checkData=function(_form){
       var pw1=eval(_form+"."+this.fieldName1);
       var pw2=eval(_form+"."+this.fieldName2);
       if(pw1.value!=pw2.value){
            pw1.value="";pw2.value="";
            pw1.focus();
		 	      throw new Error(this.errorMessage); 
		   }
		   return true;
    }
}
//身份證字號
function PersonIDField(_fieldName,_errorMessage){
    return new PersonIDObject(_fieldName,_errorMessage);
}
function PersonIDObject(_fieldName,_errorMessage){
  this.fieldName=_fieldName;
  this.errorMessage=_errorMessage;
  
  this.checkData=function(_form){
      var form=eval(_form+"."+this.fieldName);
      if(isEmpty(form.value)){
    	 	   form.focus();
    	 	   throw new Error(this.errorMessage+"欄位沒有填"); 
    	}
      personID=form.value;
  		var UserID = personID.toUpperCase();
  		var AreaCode = UserID.charAt(0);
  		// 取得首碼對應的區域碼，A ->10, B->11, ..H->17,I->34, J->18...
  		var AreaNo = ("ABCDEFGHJKLMNPQRSTUVXYWZIO".indexOf(AreaCode)) + 10;
  		// 確定身分證有10碼
  		if ((UserID.length) != 10) {
  			throw new Error(this.errorMessage + "格式不正確！");
  		}
  		// 確定首碼在A-Z之間
  		if ((AreaCode < "A") || (AreaCode > "Z")) {
  			throw new Error(this.errorMessage + "格式不正確！");
  		}
  		// 確定2-10碼是數字
  		if (isNaN(parseInt(UserID.substring(1,10)))){
  			throw new Error(this.errorMessage + "格式不正確！");
  		}
  		UserID = AreaNo.toString() + UserID.substring(1,10);
  		// 取得CheckSum的值
  		CheckSum = parseInt(UserID.charAt(0)) + parseInt(UserID.substring(10,11));
  		for(var i=2;i<=10;i++){
  			CheckSum = CheckSum + parseInt(UserID.substring(i-1,i)) * (11 - i);
  		}
  		if ((CheckSum % 10) != 0){
  			throw new Error(this.errorMessage + "格式不正確！");
  		}
  		return true;
  }
}