
function trim(arg) {
	if(arg==null) {
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	return new String(arg).replace(/(^\s*)|(\s*$)/g, "");
	
}
String.prototype.trim = trim;

//判斷特定欄位值是否有填
function isEmpty(str) {
	for (var i = 0; i < str.length; i++)
		if (" " != str.charAt(i))return false;
	return true;
}
//信用卡
function CreditCardConstruct(cardNo , cardType, errorMessage, isNecessary) {
	this.cardNo = cardNo;
	this.cardType = cardType;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.VISA = "VISA";
	this.MASTER = "MASTER";
	this.JCB = "JCB";
	this.U_CARD = "U_CARD";
	
	this.creditCardCheck = function(cardNoData, cardTypeData) {
		//空值
		if (isEmpty(cardNoData)) {
			//表示必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			chv = parseInt(cardNoData.substring(0,2));
			if(cardTypeData==this.MASTER){
				chv  -= 50;
				if (chv<0 || chv >10 || cardNoData.length != 16) {
					throw new Error(-1, "您的卡號有問題，請重新輸入 !\n");
      				}
      				return true;
			}
			if(cardTypeData==this.VISA){
				chv  -= 40;
				if (chv<0 || chv >10 || cardNoData.length != 16) {
					throw new Error(-1, "您的卡號有問題，請重新輸入 !\n");
      				}
      				return true;
			}
			if(cardTypeData==this.JCB){
				if ((chv != "34" && chv != "37") ||  cardNoData.length != 15) {
					throw new Error(-1, "您的卡號有問題，請重新輸入 !\n");
      				}
      				return true;
			}
		}
		return true;
	}
	
	this.isValidate = function(form, formObject) {
		cardNo = eval("form."+this.cardNo);
		if(cardNo==null) {
			throw new Error(-1, "找不到 " + this.cardNo + " 欄位");
		}
		//這邊可能需要判別長度是否大於0
		cardType = eval("form."+this.cardType);
		if(cardType==null) {
			throw new Error(-1, "找不到 " + this.cardType + " 欄位");
		}
		var cardType = new RadioPlugIn(cardType).getChecked();
				
		formObject.executeObject = cardNo;
		try {
			cardNoData =  cardNo.value;
			cardTypeData = cardType.value;
			return this.creditCardCheck(cardNoData, cardTypeData);
		} catch(e) { throw e; }
	}
}

function CreditCard(cardNo , cardType, errorMessage, isNecessary) {
	return new CreditCardConstruct(cardNo , cardType, errorMessage, isNecessary);
}

//檢查身份證字號
function PersonalIDConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	
	this.PersonalIDCheck = function(data) {
		//空值
		if (isEmpty(data)) {
			//表示必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			var UserID = data.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) {
				this.errorMessage += "格式不正確！";
				throw new Error(-1, this.errorMessage);
			}
			// 確定首碼在A-Z之間
			if ((AreaCode < "A") || (AreaCode > "Z")) {
				this.errorMessage += "格式不正確！";
				throw new Error(-1, this.errorMessage);
			}
			// 確定2-10碼是數字
			if (isNaN(parseInt(UserID.substring(1,10)))){
				this.errorMessage += "格式不正確！";
				throw new Error(-1, 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){
				this.errorMessage += "格式不正確！";
				throw new Error(-1, this.errorMessage);
			}
		}
		return true;
	}
	
	this.isValidate = function(form, formObject) {
		field = eval("form."+this.fieldName);
		
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		
		var data = field.value;
		try {
			return this.PersonalIDCheck(data);
		} catch(e) { throw e; }
		
		
	};

}
function PersonalID(fieldName, errorMessage, isNecessary){
	return new PersonalIDConstruct(fieldName, errorMessage, isNecessary);
}


//Select
function SelectConstruct(fieldName, errorMessage, isNecessary) { 
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;

	this.isValidate = function(form, formObject) {
		field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (field.options.length==0 || isEmpty( field.options[ field.selectedIndex ].value ) ) {
			this.errorMessage += "欄位未選";
			throw new Error(-1, this.errorMessage);
		}
		return true;
	}
}
function Select(fieldName, errorMessage) {
	return new SelectConstruct(fieldName, errorMessage);
}
	
//text field
function TextFieldConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.message="尚未填寫";
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {

		field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += this.message;
				throw new Error(-1, this.errorMessage);
			}
		}
		return true;
	};
}
function TextField(fieldName, errorMessage, isNecessary) {
	return new TextFieldConstruct(fieldName, errorMessage, isNecessary);
}


//Radio
function RadioConstruct(fieldName, errorMessage) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.maxSelection = null;
	this.count = 0;
	this.isValidate = function(form, formObject) {
		this.count = 0;
		field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		if(!field.length) {
			field = new Array(field);
		}
		formObject.executeObject = field[0];
		for(var i=0;i<field.length;i++) {
			if(field[i].checked) {
				this.count++;
			}
		}
		if(this.count == 0) {
			this.errorMessage += "欄位未選擇";
			throw new Error(-1, this.errorMessage);
		}
		if(this.maxSelection!=null && this.count > this.maxSelection) {
			this.errorMessage += "欄位最多只能選"+this.maxSelection+"項";
			throw new Error(-1, this.errorMessage);
		}
		return true;
	}
}
function Radio(fieldName, errorMessage, maxSelection) {
	return new RadioConstruct(fieldName, errorMessage);
}

function Checkbox(fieldName, errorMessage, maxSelection) {
	temp = new RadioConstruct(fieldName, errorMessage);
	if(maxSelection!=null) {
		temp.maxSelection = maxSelection;
	}
	return temp;
}

//password
function PasswordConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		var confirmField = eval("form."+this.fieldName+"2");
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		if(confirmField==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 的確認欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			var pattern = /^[a-zA-Z0-9]{4,}/;
			if( !pattern.test(field.value) ) { 
				this.errorMessage += "請輸入四碼以上的英文或數字!"; 
				throw new Error(-1, this.errorMessage); 
			} 
			if(field.value != confirmField.value) {
				this.errorMessage += "輸入的密碼不一致";
				throw new Error(-1, this.errorMessage);
			}
		}
		return true;
	};
}
function Password(fieldName, errorMessage, isNecessary) {
	return new PasswordConstruct(fieldName, errorMessage, isNecessary);
}


//Email
function EmailConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			var pattern = /^([a-zA-Z\.0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
			if( !pattern.test(field.value) ) { 
				this.errorMessage += "格式不正確!"; 
				throw new Error(-1, this.errorMessage);
			} 
		}
		return true;
	};
}
function Email(fieldName, errorMessage, isNecessary) {
	return new EmailConstruct(fieldName, errorMessage, isNecessary);
}


//Member
function MemberConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		
		field.value = field.value.trim();
		
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
/*暫時不檢查			
			//第一個字元用 a-z, A-Z, 0-9
			var pattern = new RegExp("^[a-zA-Z0-9]+", "g");
			if( !pattern.test(field.value) ) { 
				this.errorMessage += "第一個字母必須是a-z, A-Z, 0-9!"; 
				throw new Error(-1, this.errorMessage); 
			} 
			//不能有例外字元
			pattern = new RegExp("[^a-zA-Z0-9@._-]+", "g");
			if( pattern.test(field.value) ) { 
				this.errorMessage += "除了a-z, A-Z, 0-9 , -, _ 不能有其他字元!"; 
				throw new Error(-1, this.errorMessage);
			} */
		}
		return true;
	};
	this.test = function() {
		return this.errorMessage;
	}
	
}
function Member(fieldName, errorMessage, isNecessary) {
	return new MemberConstruct(fieldName, errorMessage, isNecessary);
}


//DateFormat
function DateFormatConstruct(fieldName, errorMessage, format, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.format = format;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			if(this.format=="YYYYMMDD") {
				var pattern = new RegExp("^[0-9]{8,8}$", "g");
				if( !pattern.test(field.value) ) { 
					this.errorMessage += "格式不正確(YYYYMMDD)"; 
					throw new Error(-1, this.errorMessage);
				}
				var yearFormat =  field.value.substring(0,4);
				var monthFormat =  field.value.substring(4,6);
				var dayFormat =  field.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(field.value) ) { 
					this.errorMessage += "格式不正確(YYYY/MM/DD)"; 
					throw new Error(-1, this.errorMessage);
				}
				formatArray = field.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);
			monthFormat = parseInt(monthFormat);
			dayFormat = parseInt(dayFormat);
			
			if(monthFormat==2 && (yearFormat % 4)==0 ) {
				dayArray[2] = 29;
			}
			if( dayFormat > dayArray[monthFormat]) {
				this.errorMessage += "格式不正確"; 
				throw new Error(-1, this.errorMessage);
			}
		}
		return true;
	};
}
function DateFormat(fieldName, errorMessage, format, isNecessary) {
	return new DateFormatConstruct(fieldName, errorMessage, format, isNecessary);
}

//URL
function WebsiteURLConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			pattern = new RegExp("^[http://]?[a-zA-Z0-9_-]+(\.+[a-zA-Z0-9_-]+){1,}", "g");
			if( !pattern.test(field.value) ) { 
				this.errorMessage += "格式不正確!"; 
				throw new Error(-1, this.errorMessage); 
			} 
		}
		return true;
	};
}
function WebsiteURL(fieldName, errorMessage, isNecessary) {
	return new WebsiteURLConstruct(fieldName, errorMessage, isNecessary);
}

//Invoice
function InvoiceConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			var cx = new Array(1,2,1,2,1,2,4,1);
			var SUM = 0;
			var pattern = /^(\d{8,8})$/;
			if( !pattern.test(field.value) ) { 
				this.errorMessage += "格式不正確!應該是八碼的數字"; 
				throw new Error(-1, this.errorMessage); 
			} 
			Num = field.value;
			//分割成字元陣列
			var cnum = Num.split("");
			for (i=0; i<8; i++) {
				var n = cnum[i] * cx[i];
				//若乘出來的值為二位數則將十位數和個位數相加, 並傳回
				if (n > 9) {
					n = parseInt(n/10) + n%10;
				}
				SUM += n;
			}
			if (SUM % 10 != 0) {
				//若上述演算不正確但是 G 為 7, 再加上 1 被 10 整除也為正確
				if( !(cnum[6] == 7 && (SUM + 1)%10 == 0) ) {
					this.errorMessage += "格式不正確!"; 
					throw new Error(-1, this.errorMessage); 
				}
			}
		}
		return true;
	};
}
function Invoice(fieldName, errorMessage, isNecessary) {
	return new InvoiceConstruct(fieldName, errorMessage, isNecessary);
}
//Integer
function IntegerConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			if (isNaN(field.value)) {
				this.errorMessage += "格式不正確!應該是數值"; 
				throw new Error(-1, this.errorMessage); 
			}
		}
		return true;
	};
}
function Integer(fieldName, errorMessage, isNecessary) {
	return new IntegerConstruct(fieldName, errorMessage, isNecessary);
}

//folder name
function FolderNameConstruct(fieldName, errorMessage, isNecessary) {
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {
		var field = eval("form."+this.fieldName);
		if(field==null) {
			throw new Error(-1, "找不到 " + this.fieldName + " 欄位");
		}
		formObject.executeObject = field;
		//空值
		if (isEmpty(field.value)) {
			//必填
			if(this.isNecessary != false) {
				this.errorMessage += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		} else {
			var pattern = /['|"]/g;
			if( pattern.test(field.value) ) { 
				this.errorMessage += "資料夾名稱請勿輸入[ \' 或 \"]字元"; 
				throw new Error(-1, this.errorMessage); 
			} 
		}
		return true;
	};
}
function FolderName(fieldName, errorMessage, isNecessary) {
	return new FolderNameConstruct(fieldName, errorMessage, isNecessary);
}


//for HTML
function FormObject(formName) {
	this.isNecessary = true;
	this.unnecessary = false;
	this.formName = formName;
	this.checkArray = new Array();
	//this.messageArray = new Array();
	this.executeObject = null;

	this.add = function(fieldObject) {
		this.checkArray[this.checkArray.length] = fieldObject;
	};
	this.checkData = function() {
		try {
			form = eval("document."+this.formName);
			if(form==null) {
				throw new Error(-1, "找不到取名為 " + this.formName + " 的Form");
			}
			for(var i=0;i<this.checkArray.length;i++) {
				this.checkArray[i].isValidate(form, this);
			}
			form.submit();
		} catch(e) {
			this.executeObject.focus();
			throw new Error(-1, e.description);
		}
	};
}

//for server
function ServerObject() {
	this.checkArray = new Array();
	this.messageArray = new Array();
	this.executeObject = null;

	this.add = function(fieldObject) {
		this.checkArray[this.checkArray.length] = fieldObject;
	};
	this.checkData = function() {
		this.messageArray.length = 0;
		try {
			for(var i=0;i<this.checkArray.length;i++) {
				this.checkArray[i].isValidate(form, this);
			}
			form.submit();
		} catch(e) {
			throw new Error(-1, "Error!!\n\n"+e.description);
		}
	};
}

//檢查起始日及結束日
function datecheckConstruct(start_date, end_date, errorMessage, isNecessary) {
	this.fieldName1 = start_date;
	this.fieldName2 = end_date;
	this.errorMessage = errorMessage;
	this.isNecessary = isNecessary;
	this.isValidate = function(form, formObject) {

		field1 = eval("form."+this.fieldName1);
		field2 = eval("form."+this.fieldName2);
		formObject.executeObject = field1;
		if(!isEmpty(field1.value) || !isEmpty(field2.value) ) {
			if (isEmpty(field1.value)) throw new Error(-1, this.errorMessage+"－起始日期欄位未填!!");
			if (isEmpty(field2.value)) throw new Error(-1, this.errorMessage+"－結束日期欄位未填!!");
		}else{
			if (field1.value > field2.value) throw new Error(-1, this.errorMessage+"－起始日期不得大於結束日期!!");
		}
		return true;
	};
}
function datecheck(start_date, end_date, errorMessage, isNecessary) {
	return new datecheckConstruct(start_date, end_date, errorMessage, isNecessary);
}