/**
copy from common.js 2006-12-9
*/

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.dbleBytelength = function() {
	var dbleByte=this.match(/[^\x00-\xff]/ig);
	//return this.length+(dbleByte==null?0:dbleByte.length);
	return this.length;
}

Array.prototype.indexOf = function(object) {
    for (var i = 0; i < this.length; i++)
      if (this[i] == object) return i;
    return -1;
  }

/**
 *	Validator 0.4
 *	(c) 2001 - 2006 Cocoon Studio
 *	required:common.js
 *	Create by tao.jie,2006-3-6
 *	Modify by tao.jie,2006-3-7,selected();radio();checkbox()
 *	Modify by tao.jie,2006-3-8,pluging();date()
 * Modify by tao.jie,2006-4-19,listSelected(),bug fixed
 * Modify by tao.jie,2006-11,render()
 * Modify by tao.jie,2006-12-27,release():hidden input can't focus bug fixed
*/


var Validator = function() { };
Validator = {
	REQUIRED: "{0} 不能为空",
	NUMBER_ONLY: "{0} 不是一个有效数字",
	INT_ONLY: "{0} 不是一个有效数字",
	LENGTH_RANGE_MIN: "{0} 的长度不能小于 {1} 字",
	LENGTH_RANGE_MAX: "{0} 的长度不能大于 {1} 字",
	LENGTH_RANGE: "{0} 的长度必须在 {1} 到 {2} 之间",
	NUMBER_RANGE_MIN: "{0} 不能小于 {1}",
	NUMBER_RANGE_MAX: "{0} 不能大于 {1}",
	NUMBER_RANGE: "{0} 必须在 {1} 到 {2} 之间",
	EMAIL: "{0} 不是一个有效的 E-Mail 格式",
	DATE_MATCH: "{0} 不是一个有效的日期，日期格式应为 {1}",
	EQUALS: "{0} 必须与 {1} 一致",
	MUTI_SELECTED: "{0} 必须选择 {1} 项",
	SELECTED: "{0} 必须选择",
	CHECKBOX_MIN: "{0} 至少选择 {1} 项",
	CHECKBOX_MAX: "{0} 至多选择 {1} 项",
	CHECKBOX: "{0} 必须选择 {1} 项 到 {2} 项之间",
	messager: [],
	focusElement: null,


	getElement: function(name) {	//private
		if(typeof(name)=="object") return name;
		return document.getElementsByName(name);
	},

	setFocus: function(focus) {	//private
		this.focusElement = this.focusElement==null?focus:this.focusElement;
	},

	validatorBase: function(element,argNames,method) {	//private
		var elements = this.getElement(element);
		var failedCount = 0;
		for(var i=0;i<elements.length;i++) {	
			if(!method(elements[i])) {
				if(argNames.length > 0) this.setFocus(elements[i]);
				failedCount++;
			}
		}
		if(failedCount > 0 && argNames.length > 0) this.addMessager(argNames);
		return failedCount;
	},

	addMessager: function(argNames) {	//private
		if(argNames.length == 0) return; 
		var msg = argNames[0];
		for(var i=1;i<argNames.length;i++) {
			msg = msg.replace(new RegExp("\\{"+(i-1)+"\\}","g"),argNames[i]);
		}
		this.insertMessager(msg);
	},

	insertMessager: function(str) {	//private
		this.messager[this.messager.length++] = str;
	},


	required: function(element,argName) {
		this.validatorBase(element,Array(this.REQUIRED,argName),function(e) {
			return e.value.trim()!="";
		});
	},


	numberOnly: function(element,argName) {
		this.validatorBase(element,Array(this.NUMBER_ONLY,argName),function(e) {
			var value = e.value.trim();
			if(value=="") return true;
			else return /^-?\d+(\.\d+)?$/.test(value);
		});
	},


	intOnly: function(element,argName) {
		this.validatorBase(element,Array(this.INT_ONLY,argName),function(e) {
			var value = e.value.trim();
			if(value=="") return true;
			else return !isNaN(value);
		});
	},


	lengthRange: function(element,argName,min,max) {
		if(min!=-1 && max==-1) {
			this.validatorBase(element,Array(this.LENGTH_RANGE_MIN,argName,min),function(e) {
				var value = e.value.trim();
				return !(value.dbleBytelength() < min);
			});
		}else if(max!=-1 && min==-1) {
			this.validatorBase(element,Array(this.LENGTH_RANGE_MAX,argName,max),function(e) {
				var value = e.value.trim();
				return !(value.dbleBytelength() > max);
			});
		}else if(min!=-1 && max!=-1) {
			this.validatorBase(element,Array(this.LENGTH_RANGE,argName,min,max),function(e) {
				var value = e.value.trim();
				return !(value.dbleBytelength() < min) && !(value.dbleBytelength() > max);
			});
		}
	},


	numberRange: function(element,argName,min,max) {
		this.numberOnly(element,argName);
		if(min!=-1 && max==-1) {
			this.validatorBase(element,Array(this.NUMBER_RANGE_MIN,argName,min),function(e) {
				var value = e.value.trim();
				if(value=="") return true;
				else return !(value < min);
			});
		}else if(max!=-1 && min==-1) {
			this.validatorBase(element,Array(this.NUMBER_RANGE_MAX,argName,max),function(e) {
				var value = e.value.trim();
				if(value=="") return true;
				else return !(value > max);
			});
		}else if(min!=-1 && max!=-1) {
			this.validatorBase(element,Array(this.NUMBER_RANGE,argName,min,max),function(e) {
				var value = e.value.trim();
				if(value=="") return true;
				else return !(value < min) && !(value > max);
			});
		}
	},

	email: function(element,argName,min,max) {
		this.validatorBase(element,Array(this.EMAIL,argName),function(e) {
			var value = e.value.trim();
			if(value=="") return true;
			else return /([\-\_\w]+@[\-\_\w]+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i.test(value);
		});
	},

	dateMatch: function(element,argName,pattern) {
		this.validatorBase(element,Array(this.DATE_MATCH,argName,pattern),function(e) {
			var value = e.value.trim();
			if(value=="") return true;
			else {
				var p = pattern.replace(/(dd)|(mm)/gi,"\\d{1,2}");
				p = p.replace(/y/gi,"\\d");
				if(!new RegExp(p).test(value)) return false;
				var inputDate = new Date(value.replace(/\D/gi,"/"));
				if(inputDate=="NaN") return false;
				else {
					var year=inputDate.getFullYear();
					var month=inputDate.getMonth()+1;
					var day=inputDate.getDate();
					var dMatch = value.split(/\D/);

					pattern = pattern.toLowerCase();
					var y = pattern.indexOf("y");
					var m = pattern.indexOf("m");
					var d = pattern.indexOf("d");
					var sort = new Array(y,m,d);
					sort = sort.sort();
					if(parseInt(year)!=dMatch[sort.indexOf(y)] 
						|| parseInt(month)!=dMatch[sort.indexOf(m)] 
						|| parseInt(day)!=dMatch[sort.indexOf(d)])
						return false;
				}
				return true;
			}
		});
	},

	selected: function(element,argName) {
		this.validatorBase(element,Array(this.SELECTED,argName),function(e) {
			if(e.options.length>1) {
				return e.selectedIndex != 0;
			} else return true;
			
		});
	},

	listSelected: function(element,argName,min,max) {
		var count = this.validatorBase(element,Array(),function(e) {
			return !e.selected;
		});
		 if(min == max) {
			if(count < min || count > max) this.addMessager(Array(this.MUTI_SELECTED,argName,min,max));
		} else if(min!=-1 && max==-1) {
			if(count < min) this.addMessager(Array(this.CHECKBOX_MIN,argName,min));
		} else if(min==-1 && max!=-1) {
			if(count > max) this.addMessager(Array(this.CHECKBOX_MAX,argName,max));
		} else if(min!=-1 && max!=-1) {
			if(count < min || count > max) this.addMessager(Array(this.CHECKBOX,argName,min,max));
		}
	},


	radio: function(element,argName) {
		var count = this.validatorBase(element,Array(),function(e) {
			return !e.checked;
		});
		if(count==0) this.addMessager(Array(this.SELECTED,argName));
	},
	
	checkbox: function(element,argName,min,max) {
		var count = this.validatorBase(element,Array(),function(e) {
			return !e.checked;
		});
		if(min!=-1 && max==-1) {
			if(count < min) this.addMessager(Array(this.CHECKBOX_MIN,argName,min));
		} else if(min==-1 && max!=-1) {
			if(count > max) this.addMessager(Array(this.CHECKBOX_MAX,argName,max));
		} else if(min!=-1 && max!=-1) {
			if(count < min || count > max) this.addMessager(Array(this.CHECKBOX,argName,min,max));
		}
	},

	pluging: function(str,focus,method) {
		if(method()) {
			this.insertMessager(str);
			this.setFocus(focus);
		}
	},



	render: function(oId) {
		if(arguments.length==1) {
			if(this.messager.length!=0) {
				var o = document.getElementById(oId);
				o.style.display = "block";
				o.innerHTML = "請注意：<br>"+this.messager.join("</br>");
				this.release();
				return false;
			}
		} else {
			if(this.messager.length!=0) {
				alert(this.messager.join("\n"));
				this.release();
				return false;
			}
		}
		return true;
	},


	release: function() {	//private
		if(this.focusElement!=null && this.focusElement.type!="hidden")
			this.focusElement.focus();
		this.messager = [];
		this.focusElement = null;
	}
} 