﻿function RadioPlugIn(radio) {
	this.radio = radio;
	this.getChecked = function() {
		for(i=0;i<this.radio.length;i++) {
			if(this.radio[i].checked) {
				return this.radio[i];
			}
		}
		return null;
	}
}

//判斷特定欄位值是否有填
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 = "11";
	this.MASTER = "12";
	this.JCB = "13";

	
	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.setRequired = function(isNecessary) {
		this.isNecessary = isNecessary;
		return this;
	}
	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) {
			this.errorMessage += "欄位未選";
			throw new Error(-1, this.errorMessage);
		}
		if (field.selectedIndex==-1 || 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.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 += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		}
		return true;
	};
	this.setRequired = function(isNecessary) {
		this.isNecessary = isNecessary;
		return this;
	}
}
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.minSelection = 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);
		}
		if(this.minSelection!=null && this.count < this.minSelection) {
			this.errorMessage += "欄位最少要選"+this.minSelection+"項";
			throw new Error(-1, this.errorMessage);
		}
		return true;
	}
	this.setMaxSelection = function(maxSelection) {
		this.maxSelection = maxSelection;
		return this;
	}
	this.setMinSelection = function(minSelection) {
		this.minSelection = minSelection;
		return this;
	}
	this.setSelection = function(selection) {
		this.maxSelection = selection;
		this.minSelection = selection;
		return this;
	}
}
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;
}
//textarea
function TextArea(fieldName, errorMessage) {
	
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.isNecessary = true;
	//預設長度100
	this.maxLength = 1500;
	this.setMaxLength = function(maxLength) {
		this.maxLength = maxLength;
		return this;
	}
	//設定顯示的DIV
	this.setShowDIV = function(showDIV) {
		this.showDIV = showDIV;
	}
	this.setRequired = function(isNecessary) {
		this.isNecessary = isNecessary;
		return this;
	}
	this.showCount = function(form) {
		field = eval("form."+this.fieldName);
		this.showDIV.innerHTML = "目前字數："+field.value.length;
	}
	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 += "欄位未填";
				throw new Error(-1, this.errorMessage);
			}
		}
		if (field.value.length > this.maxLength){
			this.errorMessage += "欄位超過"+this.maxLength+"字元限制，請重新修改";
			throw new Error(-1, this.errorMessage);
		}
		return true;
	}
}


//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.setRequired = function(isNecessary) {
		this.isNecessary = isNecessary;
		return this;
	}
	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;
		
		
		
		//空值
		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.setFormat = function(format) {
		this.format = format;
		return this;
	}
	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,10);
			monthFormat = parseInt(monthFormat,10);
			dayFormat = parseInt(dayFormat,10);
			
			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); 
			} 
			//分割成字元陣列
			var cnum = field.value.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);
}


function EmptyCheckConstruct(nameList, errorMessage) {
	this.nameList = nameList;
	this.errorMessage = errorMessage;

	this.isValidate = function(form, formObject) {
		for(i=0;i<this.nameList.length;i++) {
			field = eval("form."+nameList[i]);
			if(!isEmpty(field.value)) {
				break;
			}
		}
		if(i==this.nameList.length) {
			formObject.executeObject = field;
			throw new Error(-1, this.errorMessage);
		}
		return true;
	};
}
function EmptyCheck(fieldName, errorMessage) {
	return new EmptyCheckConstruct(fieldName, errorMessage);
}


//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);
}

function IntegerFields(fieldName, errorMessage) {
	return new IntegerConstruct(fieldName, errorMessage);
}




//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.getDocumentForm = function() {
		return eval("document."+this.formName);
	}
	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, "Error!!\n\n"+e.description);
		}
	}
	this.checkField = 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);
			}
		} catch(e) {
			this.executeObject.focus();
			throw new Error(-1, "Error!!\n\n"+e.description);
		}
	}
}
