

Validator = {

Require : /.+/,

//Email : /^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/,

Email : /^([_A-Za-z0-9]+.)+@([_A-Za-z0-9]+.)+[A-Za-z0-9]{2,3}$/,

Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{5,7}$/,

Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}|15\d{9}|18[8|9]\d{8}$/,

Vipcode : /^[0-9]\d{9,10}$/,

Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,

IdCard : "this.IsIdCard(value,document.getElementsByName(getAttribute('birth_text'))[0].value)",

Currency : /^\d+(\.\d+)?$/,

Number : /^\d+$/,

Zip : /^[0-9]\d{5}$/,

QQ : /^[1-9]\d{4,8}$/,

Integer : /^[-\+]?\d+$/,

Double : /^[-\+]?\d+(\.\d+)?$/,

English : /^[A-Za-z]+$/,

Chinese : /^[\u0391-\uFFE5]+$/,

RegionCode : /^[0-9]\d{1,4}$/,

ChineseName : "this.IsChineseName(value)",

UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,

IsSafe : function(str){return !this.UnSafe.test(str);},

SafeString : "this.IsSafe(value)",

Date : "this.IsDate(value)",

Limit : "this.limit(value.length,getAttribute('min'), getAttribute('max'))",

LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",

Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",

Range : "if(getAttribute('min')){getAttribute('min') <= parseInt(value)};if(getAttribute('max')){getAttribute('max') >= parseInt(value)};",

Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",

Custom : "this.Exec(value, getAttribute('regexp'))",

Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",

ErrorItem : [document.forms[0]],

ErrorMessage : ["ΜαΚΎ:"],

Validate : function(theForm, mode){

	var obj = theForm || event.srcElement;
	
	var count = obj.elements.length;
	
	this.ErrorMessage.length = 1;
	
	this.ErrorItem.length = 1;
	
	this.ErrorItem[0] = obj;
	
	for(var i=0;i<count;i++){
	
		with(obj.elements[i]){
	
			var _dataType = getAttribute("dataType");
	
			if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined") 
			continue;
	
			this.ClearState(obj.elements[i]);
	
			if(getAttribute("require") == "false" && value == "")
			continue;
	
			switch(_dataType){
	
				case "IdCard" :
				
				case "ChineseName" :
				
				case "Repeat" :
				
				case "Range" :
				
				case "Compare" :
				
				case "Custom" :
				
				case "Group" : 
				
				case "Limit" :
				
				case "LimitB" :
				
				case "Date" :
				
				case "SafeString" :
	
					if(!eval(this[_dataType])) {
					
					this.AddError(i, getAttribute("msg"));
					
					}
	
					break;
	
			default :
	
				if(!this[_dataType].test(value)){
				
				this.AddError(i, getAttribute("msg"));
				
				}
	
				break;
	
			}
	
		}
	
	}

	if(this.ErrorMessage.length > 1){

		mode = mode || 1;
		
		var errCount = this.ErrorItem.length;

		switch(mode){

		case 2 :

			for(var i=1;i<errCount;i++)

			this.ErrorItem[i].style.color = "red";

		case 1 :

			alert(this.ErrorMessage.join("\n"));

			if(this.ErrorItem[1].name!="tempDate" && this.ErrorItem[1].type!="hidden"){
				this.ErrorItem[1].focus();
			}

			break;

		case 3 :

			for(var i=1;i<errCount;i++){

				try{

					var span = document.createElement("SPAN");

					span.id = "__ErrorMessagePanel";

					span.style.color = "#E31C45";

					this.ErrorItem[i].parentNode.appendChild(span);

					span.innerHTML = this.ErrorMessage[i].replace(/\d+:/,"*");

				}

				catch(e){alert(e.description);}

			}

			if(this.ErrorItem[1].name!="tempDate" && this.ErrorItem[1].type!="hidden"){
				this.ErrorItem[1].focus();
			}

			break;

		default :

			alert(this.ErrorMessage.join("\n"));

			break;

	}

	return false;

}

return true;

},

limit : function(len,min, max){

min = min || 0;

max = max || Number.MAX_VALUE;

return min <= len && len <= max;

},

LenB : function(str){

return str.replace(/[^\x00-\xff]/g,"**").length;

},

ClearState : function(elem){

with(elem){

if(style.color == "red")

style.color = "";

var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];

if(lastNode.id == "__ErrorMessagePanel")

parentNode.removeChild(lastNode);

}

},

AddError : function(index, str){

this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];

this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" 
+ str;

},

Exec : function(op, reg){

return new RegExp(reg,"g").test(op);

},

compare : function(op1,operator,op2){

switch (operator) {

case "NotEqual":

return (op1 != op2);

case "GreaterThan":

return (op1 > op2);

case "GreaterThanEqual":

return (op1 >= op2);

case "LessThan":

return (op1 < op2);

case "LessThanEqual":

return (op1 <= op2);

default:

return (op1 == op2); 

}

},

MustChecked : function(name, min, max){

	var groups = document.getElementsByName(name);

	var hasChecked = 0;

	min = min || 1;

	max = max || groups.length;

	for(var i=groups.length-1;i>=0;i--)

	if(groups[i].checked) hasChecked++;

	return min <= hasChecked && hasChecked <= max;

},

IsIdCard : function(str,birth) {
	var re;
	if (str.length!=15 && str.length != 18) {
		return false;
	}

	birth = birth.split("-")
	if (birth[0].length==1) birth[0] = "0" + birth[0];
	if (birth[1].length==1) birth[1] = "0" + birth[1];
	if (birth[2].length==1) birth[2] = "0" + birth[2];

	birth = birth[0] + birth[1] + birth[2];
	re = /-/g;
	birth = birth.replace(re, "");    
		
	if (str.length == 15){
		birth = birth.substring(2, birth.length);
		if (birth != str.substring(6,12)) return false;
	}else{
		if (birth != str.substring(6,14)) return false;		
	}
	return str;
},

IsChineseName : function(str){
	var m = str.match(new RegExp("^[\u0391-\uFFE5]+$"));
	if(m == null ) return false;
	if (str.length<2) return false;
	
	return str;
},

IsDate : function(str){
	
	var nd = new Date();
	str = str.replace(/\s+/g,""); 
	if(str.search(/^\d{4}-\d{1,2}-\d{1,2}$/) == 0){ 
		var y = parseInt(str.split("-")[0]); 
		var m = parseInt(str.split("-")[1]); 
		var d = parseInt(str.split("-")[2]); 
		if (y==nd.getYear()){
			if(m==nd.getMonth()+1){
				if(d>nd.getDate()) return false;
			}
			if(m>nd.getMonth()+1) return false;
		}
		if (y>nd.getYear()) return false;

		switch(m){ 
			case 1: 
			case 3: 
			case 5: 
			case 7: 
			case 8: 
			case 10: 
			case 12: 
				if(d>31){ 
					return false; 
				}else{ 
					return true; 
				} 
				break; 
			case 2: 
				if((y%4==0 && d>29) || ((y%4!=0 && d>28))){ 
					return false; 
				}else{ 
					return true; 
				}
				break; 
			case 4: 
			case 6: 
			case 9: 
			case 11: 
				if(d>30){ 
					return false; 
				}else{ 
					return true; 
				} 
				break; 
			default: 
				return false; 
			} 
	}else{ 
	   return false; 
	} 
}
}
