
//校验表单
function verifyForm(oForm) {
	if (oForm == null) {
		return true;
	}
	var els = oForm.elements;
	if (els.length == null) {
		return false;
	}
	var oElement = null;
	try {
		for (var i = 0; i < els.length; i++) {
			oElement = new FormElement();
			if (!oElement.verify(els[i])) {
				return false;
			}
		}
	}
	catch (e) {
		alert(e.description);
		return false;
	}
	return true;
}

//表单元素，即表单域
function FormElement() {
	this.objs = null;
	this.obj = null;
	this.vMax = null;
	this.vMin = null;
	this.vNotNull = false;
	this.vReg = null;
	this.vType = null;
	this.vName = null;
	this.vMsg = null;
	this.msg = null;
	this.id = null;
	this.name = null;
	this.tagName = null;
	this.type = null;
	this.form = null;
	this.value = "";
	this.clear = function () {
		this.objs = null;
		this.obj = null;
		this.vMax = null;
		this.vMin = null;
		this.vNotNull = false;
		this.vReg = null;
		this.vType = null;
		this.vName = null;
		this.vMsg = null;
		this.msg = null;
		this.id = null;
		this.name = null;
		this.tagName = null;
		this.type = null;
		this.form = null;
		this.value = "";
	};
	
	//初始化
	this.init = function (obj) {
		this.clear();
		if (obj == null) {
			return;
		}
		if (obj.tagName == null) {
			var oForm = obj.form;
			if (oForm == null) {
				return false;
			}
			this.objs = oForm.elements[obj.name];
			if (this.objs == null || this.objs.length == 0) {
				return false;
			}
			this.obj = this.objs[0];
		} else {
			this.obj = obj;
		}
		this.tagName = this.obj.tagName.toUpperCase();
		this.type = this.obj.type ? obj.type.toLowerCase() : null;
		this.form = this.obj.form;
		this.vNotNull = this.isTrue(this.obj.style.vNotNull);
		var vMax = this.obj.style.vMax;
		if (vMax != null && vMax.length > 0 && !isNaN(vMax)) {
			this.vMax = parseInt(vMax);
		}
		var vMin = this.obj.style.vMin;
		if (vMin != null && vMin.length > 0 && !isNaN(vMin)) {
			this.vMin = parseInt(vMin);
		}
		this.vReg = this.obj.style.vReg;
		if (this.vReg != null && this.vReg.length > 0) {
			this.vReg = this.vReg.replace(/^('|")|('|")$/g, "");
			this.vReg = new RegExp(this.vReg, "g");
		} else {
			this.vReg = null;
		}
		var vType = this.obj.style.vType;
		if (vType != null) {
			this.vType = vType.replace(/^\s+|\s+$/g, "");
		}
		var vName = this.obj.style.vName;
		if (vName == null || vName.length == 0) {
			this.vName = obj.name;
		} else {
			this.vName = vName;
		}
		this.vMsg = this.obj.style.vMsg;
		return true;
	};
	
	//校验
	this.verify = function (obj) {
		if (obj == null && this.obj == null) {
			return true;
		}
		if (obj != null) {
			this.init(obj);
			if (this.obj == null) {
				return true;
			}
		}
		//不检查
		if (this.isTrue(this.obj.style.uncheck)) {
			return true;
		}
		
		//sets the value
		this.value = "";
		if (this.objs != null) {
			if (type == "checkbox") {
				var values = [];
				for (var i = 0; i < this.objs.length; i++) {
					if (this.objs[i].checked) {
						values[values.length] = this.objs[i].value;
					}
				}
				this.value = values.join(",");
			} else {
				if (type == "radio") {
					for (var i = 0; i < this.objs.length; i++) {
						if (this.objs[i].checked) {
							this.value = this.objs[i].value;
							break;
						}
					}
				} else {
					this.initValue();
				}
			}
		} else {
			this.initValue();
		}
		
		//format min or max
		if (this.vMin && !isNaN(this.vMin)) {
			if (this.value.length < this.vMin) {
				this.alert("\u4e0d\u80fd\u5c11\u4e8e" + this.vMin + "\u4e2a\u5b57\u7b26");
				try {
					this.obj.focus();
				}
				catch (e) {
				}
				return false;
			}
		}
		if (this.vMax && !isNaN(this.vMax)) {
			if (this.value.length > this.vMax) {
				this.alert("\u4e0d\u80fd\u591a\u4e8e" + this.vMax + "\u4e2a\u5b57\u7b26");
				try {
					this.obj.focus();
				}
				catch (e) {
				}
				return false;
			}
		}
		
		//verify not null
		if (this.vNotNull) {
			if (this.value == "") {
				this.alert("\u4e0d\u80fd\u4e3a\u7a7a");
				try {
					this.obj.focus();
				}
				catch (e) {
				}
				return false;
			}
		}
		
		//verify reg
		if (this.vReg != null && this.value.length > 0) {
			if (!this.vReg.test(this.value)) {
				this.alert("\u683c\u5f0f\u9519\u8bef");
				try {
					this.obj.focus();
				}
				catch (e) {
				}
				return false;
			}
		}
		if (this.vType != null && this.value.length > 0) {
			if (!this.verifyForType()) {
				this.alert();
				try {
					this.obj.focus();
				}
				catch (e) {
				}
				return false;
			} else {
				this.msg = null;
			}
		}
		return true;
	};
	this.initValue = function () {
		if (this.tagName == "SELECT") {
			if (this.obj.multiple) {
				var values = [];
				var options = this.obj.options;
				for (var i = 0; i < options.length; i++) {
					if (options[i].selected) {
						values[values.length] = options[i].value;
					}
				}
				this.value = values.join(",");
			} else {
				var options = this.obj.options;
				for (var i = 0; i < options.length; i++) {
					if (options[i].selected) {
						this.value = options[i].value;
						break;
					}
				}
			}
		} else {
			this.value = this.obj.value;
		}
	};
	this.alert = function (msg) {
		if (this.vMsg != null && this.vMsg.length > 0) {
			alert(this.vMsg);
		} else {
			if (msg != null && msg.length > 0) {
				alert("\"" + this.vName + "\" " + msg);
			} else {
				if (this.msg != null) {
					alert("\"" + this.vName + "\" " + this.msg);
				}
			}
		}
	};
	this.verifyForType = function () {
		this.value = this.value.replace(/^\s+|\s+$/g, "");
		switch (this.vType) {
		  case "number":
			var r = /^(|-|\+)\d+(|\.\d+)$/g;
			var b = r.test(this.value);
			if (!b) {
				this.msg = "\u8f93\u5165\u503c\u4e0d\u4e3a\u6570\u503c";
			}
			return b;
		  case "positive":
			var b = parseInt(this.value);
			if (isNaN(b) || b <= 0) {
				this.msg = "\u8f93\u5165\u503c\u4e0d\u4e3a\u6b63\u6570";
				return false;
			}
			this.obj.value = b;
			return true;
		  case "digit":
			var r = /^\d+$/g;
			var b = r.test(this.value);
			if (!b) {
				this.msg = "\u8f93\u5165\u503c\u4e0d\u5168\u4e3a\u6570\u5b57";
			}
			return b;
		  case "date":
			var r = /^\d{4}(-\d{1,2}){0,2}\s*.*$/g;
			if (!r.test(this.value)) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			this.value = this.value.replace(/\s.*$/g, "");
			var arr = this.value.split("-");
			var y = new Number(arr[0]);
			var m = arr.length > 1 ? new Number(arr[1]) : 1;
			var d = arr.length > 2 ? new Number(arr[2]) : 1;
			if (m == 0 || d == 0) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			if (m == 2) {
				if (d > 29) {
					this.msg = "\u65e5\u671f\u65e0\u6548";
					return false;
				}
				if (y % 4 == 0 && y % 400 != 0 && d == 29) {
					this.msg = "\u95f0\u5e74\u4e0d\u80fd\u670929\u65e5";
					return false;
				}
				return true;
			} else {
				if (m == 4 || m == 6 || m == 9 || m == 11) {
					if (d > 30) {
						this.msg = "\u65e5\u671f\u65e0\u6548";
						return false;
					}
				} else {
					if (d > 31) {
						this.msg = "\u65e5\u671f\u65e0\u6548";
						return false;
					}
				}
			}
			return true;
		  case "time":
			//(((|[01])[0-9])|2[0-3])(:(|[0-5])[0-9]){0,2}
			var r = /^\d{4}-\d{1,2}-\d{1,2}\s*(|\d{1,2}(:\d{1,2}){0,2})$/g;
			if (!r.test(this.value)) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			this.value = this.value.replace(/\s+/g, " ");
			var datetime = this.value.split(" ");
			var arr = datetime[0].split("-");
			var y = new Number(arr[0]);
			var m = arr.length > 1 ? new Number(arr[1]) : 1;
			var d = arr.length > 2 ? new Number(arr[2]) : 1;
			var h = 0, n = 0, s = 0;
			if (datetime.length > 1) {
				arr = datetime[1].split(":");
				h = new Number(arr[0]);
				if (arr.length > 1) {
					n = new Number(arr[1]);
				}
				if (arr.length > 2) {
					s = new Number(arr[2]);
				}
			}
			if (m == 0 || d == 0) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			if (m == 2) {
				if (d > 29) {
					this.msg = "\u65e5\u671f\u65e0\u6548";
					return false;
				}
				if (y % 4 == 0 && y % 400 != 0 && d == 29) {
					this.msg = "\u95f0\u5e74\u4e0d\u80fd\u670929\u65e5";
					return false;
				}
				return true;
			} else {
				if (m == 4 || m == 6 || m == 9 || m == 11) {
					if (d > 30) {
						this.msg = "\u65e5\u671f\u65e0\u6548";
						return false;
					}
				} else {
					if (d > 31) {
						this.msg = "\u65e5\u671f\u65e0\u6548";
						return false;
					}
				}
			}
			if (h > 23) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			if (n > 59) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			if (s > 59) {
				this.msg = "\u65e5\u671f\u65e0\u6548";
				return false;
			}
			return true;
		  case "email":
			var r = /^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/;
			if (!r.test(this.value)) {
				this.msg = "\u8f93\u5165\u503c\u4e0d\u7b26\u5408Email\u683c\u5f0f";
				return false;
			}
			return true;
		  case "idcard":
			return this.checkIdCard();
		  case "zipcode":
			var r = /^[1-9]\d{5}$/g;
			var b = r.test(this.value);
			if (!b) {
				this.msg = "\u65e0\u6548\u7684\u90ae\u653f\u7f16\u7801";
			}
			return b;
		}
		return true;
	};
	this.checkIdCard = function () {
		idcard = this.value;
		var area = {11:"\u5317\u4eac", 12:"\u5929\u6d25", 13:"\u6cb3\u5317", 14:"\u5c71\u897f", 15:"\u5185\u8499\u53e4", 21:"\u8fbd\u5b81", 22:"\u5409\u6797", 23:"\u9ed1\u9f99\u6c5f", 31:"\u4e0a\u6d77", 32:"\u6c5f\u82cf", 33:"\u6d59\u6c5f", 34:"\u5b89\u5fbd", 35:"\u798f\u5efa", 36:"\u6c5f\u897f", 37:"\u5c71\u4e1c", 41:"\u6cb3\u5357", 42:"\u6e56\u5317", 43:"\u6e56\u5357", 44:"\u5e7f\u4e1c", 45:"\u5e7f\u897f", 46:"\u6d77\u5357", 50:"\u91cd\u5e86", 51:"\u56db\u5ddd", 52:"\u8d35\u5dde", 53:"\u4e91\u5357", 54:"\u897f\u85cf", 61:"\u9655\u897f", 62:"\u7518\u8083", 63:"\u9752\u6d77", 64:"\u5b81\u590f", 65:"\u65b0\u7586", 71:"\u53f0\u6e7e", 81:"\u9999\u6e2f", 82:"\u6fb3\u95e8", 91:"\u56fd\u5916"};
		var Y, JYM;
		var S, M;
		var idcard_array = new Array();
		idcard_array = idcard.split("");
		//地区检验
		if (area[parseInt(idcard.substr(0, 2))] == null) {
			this.msg = "\u5730\u533a\u975e\u6cd5!";
			return false;
		}
		//身份号码位数及格式检验
		switch (idcard.length) {
		  case 15:
			if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0)) {
				ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
			} else {
				ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
			}
			if (ereg.test(idcard)) {
				return true;
			} else {
				this.msg = "\u51fa\u751f\u65e5\u671f\u8d85\u51fa\u8303\u56f4\u6216\u542b\u6709\u975e\u6cd5\u5b57\u7b26!";
				return false;
			}
			break;
		  case 18:
				//18位身份号码检测
				//出生日期的合法性检查
				//闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
				//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
			if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard.substr(6, 4)) % 4 == 0)) {
				ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
			} else {
				ereg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
			}
			if (ereg.test(idcard)) {//测试出生日期的合法性
					//计算校验位
				S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3;
				Y = S % 11;
				M = "F";
				JYM = "10X98765432";
				M = JYM.substr(Y, 1);//判断校验位
					//检测ID的校验位
				if (M == idcard_array[17]) {
					return true;
				} else {
					this.msg = "\u6821\u9a8c\u4f4d\u9519\u8bef!";
					return false;
				}
			} else {
				this.msg = "\u51fa\u751f\u65e5\u671f\u8d85\u51fa\u8303\u56f4\u6216\u542b\u6709\u975e\u6cd5\u5b57\u7b26!";
				return false;
			}
			break;
		  default:
			this.msg = "\u4f4d\u6570\u4e0d\u5bf9!";
			return false;
		}
		return true;
	};
	
	//判断boolean值
	this.isTrue = function (p) {
		if (p == null) {
			return false;
		}
		var t = typeof (p);
		if (t == "boolean") {
			return p;
		}
		if (!isNaN(p)) {
			return new Number(p) != 0;
		}
		if (t == "string") {
			var lp = p.replace(/^\s+|\s+$/g, "").toLowerCase();
			return "" == lp || "y" == lp || "true" == lp || "yes" == lp;
		}
		if (t == "object") {
			return true;
		}
		return false;
	};
}
function Memento(oForm) {
	this.oForm = oForm;
	this.objs = [];
	this.add = function (name) {
		try {
			this.objs[name] = oForm.elements[name].value;
		}
		catch (e) {
		}
	};
	this.addAllSelect = function () {
		var els = oForm.getElementsByTagName("SELECT");
		if (els != null) {
			var el;
			for (var i = 0; i < els.length; i++) {
				el = els[i];
				if (el.name == null || el.name == "") {
					continue;
				}
				try {
					this.objs[el.name] = el.value;
				}
				catch (e) {
				}
			}
		}
	};
	this.set = function (names) {
		for (var i = 0; i < names.length; i++) {
			this.add(names[i]);
		}
	};
	this.recover = function () {
		for (var name in this.objs) {
			try {
				oForm.elements[name].value = this.objs[name];
			}
			catch (e) {
			}
		}
		this.objs = [];
	};
}
/**
* 1 : number 接收数字
* 2 : char  接收字母
* 4 : enter 接收回车
*/
function filterKeyDown(type) {
	var e = window.event;
	if (e == null || type == null || type == "" || isNaN(type)) {
		return;
	}
	var keyCode = e.keyCode;
	if (keyCode == 16 || keyCode == 17 || keyCode == 8 || keyCode == 46 || keyCode == 9 || keyCode == 189 || keyCode == 110 || keyCode == 190) {
		return;
	}
	if ((type & 1) == 1) {
		if (!(event.shiftKey || event.leftShift) && keyCode >= 48 && keyCode <= 57) {
			return;
		} else {
			if (keyCode >= 96 && keyCode <= 105) {
				return;
			}
		}
	}
	if ((type & 2) == 2) {
		if (keyCode >= 65 && keyCode <= 90) {
			return;
		}
	}
	if ((type & 4) == 4) {
		if (keyCode == 13) {
			return;
		}
	}
	else {
		if (keyCode == 13) {
			event.keyCode = 9;
			return;
		}
	}
	//event.keyCode = 0;
	event.returnValue = false;
}