var FormValidate = {
	version: "2.0",
	software: "FormValidate"
};
var fromValidate = Class.create({
	initialize: function(A,B,C){
		this.A=A;
		this.C=C;
		this.AA=false;
		this.B=B;
		Event.observe(window,'load',this.load.bind(this));
	},
	load:function(){
		if(Object.isUndefined(this.B)||this.B==null){alert("请定义form。");return;}
		Event.formobserve(this.B,(this.B.tagName=="FORM"?"submit":"click"),function(){return this.A_D();}.bind(this));
		this.A.each(function(A,B){
			if(!this.A_A(A))return;
			A.pass=false;
			var self=A.self;
			if(Object.isArray(self)){
				self.each(function(A){
					A.observe('blur',function(){this.A_B(B,true)}.bind(this));
				},this);
			}else
				if(!new WebeditInstance(A).isEdit())self.observe('blur',function(){this.A_B(B,true)}.bind(this));
			this.A_C(A,false);
		},this);
	},
	A_A: function (A){
		if(Object.isUndefined(A.fieldname)){alert("请定义参数属性：fieldname。");return false;}
		if(A.self == null){alert(A.fieldname+"不是已经存在的对象！");return false;}
		if(Object.isArray(A.self)){
			var AA=true;
			A.self.each(function(A){
				if(A.tagName!='INPUT')AA=false;
			},AA);
			if(!AA){alert(A.fieldname+"不是表单控件！");return false;}
		}else{
			if(Object.isString(A.self))return false;
			var tname=A.self.tagName;
			if(tname!='INPUT'&&tname!='TEXTAREA'&&tname!="SELECT"){alert(A.fieldname+"不是表单控件！");return false;}
		}
		return true;
	},
	A_B: function(A,B){
		this.A_C(this.A[A],B);
	},
	A_C: function(A,B){
		if(Object.isString(A)&&!B)return;
		if(!Object.isUndefined(A.contrast)&&A.contrast!=null)this.A_E(A);
		var excute = Class.create({
			initialize: function(A,B,C){
				this.A=A;
				this.B=B;
				this.G=new WebeditInstance(this.A.self);
				this.C=this.A.self;
				if(!Object.isUndefined(this.A.contrast)&&this.A.contrast!=null){
					if(!this.A.contrast.judgment&&C){this.A_O(this.A.contrast.fieldname+"没有通过验证！",false,this.A.contrast.show,this.A.contrast.info);this.C.value='';return;}
				}
				this.D=this.A_A();
				if(this.D){
					this.A.pass=false;
					if(!Object.isUndefined(this.A.initiative)){this.A.initiative.pass=false;this.A.initiative.self.value='';}
					if(C)this.A_O(this.A.fieldname+this.D,false,this.A.show,this.A.info);
					return;
				}else{
					if(!Object.isUndefined(this.A.contrast)&&this.A.contrast!=null){
						var E=this.A_Q(this.C.value,this.A.contrast.compObj.value,this.A.contrast.dataType,this.A.contrast.level);
						if(E){
							this.A.pass=false;
							this.A_O(this.A.fieldname+E+this.A.contrast.fieldname+"!",false,this.A.show,this.A.info);
						}else{
							this.A.pass=true;
							this.A_O(this.A.fieldname+"通过验证！",true,this.A.show,this.A.info);
						}
						return;
					}
					if(!Object.isUndefined(this.A.service)&&this.A.service){
						var json=this.C.name+"="+this.B_F();
						new Ajax.Request(this.B,{method: 'post',parameters: json , onComplete: this.A_P.bind(this),encoding:'UTF-8'});
					}else{
						this.A.pass=true;
						if(C)this.A_O(this.A.fieldname+"通过验证！",true,this.A.show,this.A.info);
						if(C&&!Object.isUndefined(this.A.initiative)){this.A.initiative.contrast.judgment=this.A.pass;new excute(this.A.initiative,this.B,true);}
					}
				}
			},
			B_F: function(){
				if(!Object.isUndefined(this.A.association)&&Object.isArray(this.A.association)){
					var AA=this.A.association;
					var BB="{";
					for(var i=0;i<AA.length;i++){
						if(AA[i][0]=="input"){
							BB+=$(AA[i][1]).name+":\""+this.B_G($(AA[i][1]).value)+"\",";
						}
						if(AA[i][0]=="select"){
							var CC=$(AA[i][1]);
							var DD="";
							BB+=CC.name+":\"";
							for(var j = 0 ,length = CC.options.length ; j < length; j++ ){
								if(CC.options[j].selected)DD+=(DD==""?"":",")+this.B_G(CC.options[j].value);
							}
							BB+=DD+"\",";
						}
						if(AA[i][0]=="radio" || AA[i][0]=="checkbox"){
							var EE=$$R(AA[i][1]);
							var FF="";
							BB+=AA[i][1]+":\"";
							for(var j = 0 ,length = EE.length ; j < length; j++ ){
								if(EE[j].checked)FF+=(FF==""?"":",")+this.B_G(EE[j].value);
							}
							BB+=FF+"\",";
						}
					}
					BB+=this.C.name+":\""+this.C.value+"\"}";
					return BB;
				}else{
					return this.B_G(this.C.value);
				}
			},
			B_G: function(value){
				return value.replace(/\@/g,"@64;").replace(/\\/g,"@92;").replace(/\"/g,"@34;").replace(/\&/g,"@38;").replace(/\#/g,"@35;").replace(/\?/g,"@63;").replace(/\%/g,"@37;").replace(/\n/g,"@13;").replace(/\r/g,"@10;");
			},
			A_A: function(){
				var AA=this.A.handlemode;
				if(!this.G.isEdit()&& (Object.isArray(this.C)||this.C.tagName=='SELECT'))return this.A_B(AA);
				var AB=(this.G.isEdit()?this.G.getData():this.C.value);
				if((Object.isUndefined(AA)||AA==null||!AA)&&AB=='')return false;
				var AC=this.A.dataType;
				var AE=false;
				var AF=false;
				if(this.B_A(AC,0)&&AB.empty())return "请输入数据！";
				if(this.B_A(AC,1)&&(AF=this.A_I(AB)))return AF;
				if(this.B_A(AC,2)&&(AF=this.A_J(AB)))return AF;
				if(this.B_A(AC,3)&&(AF=this.A_J(AB,2)))return AF;
				if(this.B_A(AC,4)&&(AF=this.A_J(AB,3)))return AF;
				if(this.B_A(AC,5)&&(AF=this.A_L(AB)))return AF;
				if(this.B_A(AC,6)&&!this.A_E(AB,/^[0-9A-Za-z]+$/))return "请输入英文字母和数字！";
				if(this.B_A(AC,7)&&!this.A_E(AB,/^[\u4e00-\u9fa5]+$/))return "请输入汉字！";
				if(this.B_A(AC,8)&&!this.A_E(AB,/^[A-Za-z]+$/))return "请输入英文字母！";
				if(this.B_A(AC,9)&&!this.A_E(AB,/^[0-9a-zA-Z\u4e00-\u9fa5]+$/))return "请输入汉字、字母或数字！";
				if(this.B_A(AC,10)&&!this.A_E(AB,/^[a-z]+$/))return "请输入小写英文字母！";
				if(this.B_A(AC,11)&&!this.A_E(AB,/^[A-Z]+$/))return "请输入大写英文字母！";
				if(this.B_A(AC,12)&&!this.B_D(AB))return "请输入整数！";
				if(this.B_A(AC,13)&&!this.A_E(AB,/^[0-9]+$/))return "请输入自然数！";
				if(this.B_A(AC,14)&&!this.A_E(AB,/^([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/))return "请输入有效电子邮件地址！";
				if(this.B_A(AC,15)&&!this.A_E(AB,/(^[1][3,5,8][0-9]{9}$)|(^0[1][3,5][0-9]{9}$)/))return "请输入合法的手机号码！";	
				if(this.B_A(AC,16)&&!this.A_E(AB,/(^([0][1-9]{2,3}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0][1-9]{2,3}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)/))return "请输入有效的电话号码！";
				if(this.B_A(AC,17)&&!this.A_E(AB,/^(http(s)?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/))return "请输入合法网址！";
				if(this.B_A(AC,18)&&(AF=this.A_M(AB)))return AF;
				if((AF=this.A_N(AB)))return AF;
				return false;
			},
			B_A: function(A,B){
				return (A&this.A_D(B))==this.A_D(B);
			},
			A_B: function(AA){
				var AB=0;
				var AC=0;
				if(Object.isArray(this.C)){
					for(var i = 0, length = this.C.length; i < length; i++){
						if((this.C[i].type=='radio')&&this.C[i].checked)return false;
						if((this.C[i].type=='checkbox')&&this.C[i].checked){AC=1;AB++;}
					}
					
					if(AC==0)return "请选择！";
					if(AC==1&&((Object.isUndefined(AA)||AA==null||!AA)&&AB==0))return false;
				}else{
					var AD=!Object.isUndefined(this.C.multiple);
					for(var i = 0 ,length = this.C.options.length ; i < length; i++ ){
						if(AD&&this.C.options[i].selected&&(this.C.options[i].value!=""))return false;
						if(!AD&&this.C.options[i].selected)AB++;
					}
					if((!Object.isUndefined(AA)&&AA!=null||AA)&&AD)return "请选择！";
					if((Object.isUndefined(AA)||AA==null||!AA)&&AB==0)return false;
				}
				return this.A_C(AB);
			},
			A_C: function(A){
				var AA=this.A.smalls;
				var AB=this.A.bigs;
				if(!Object.isUndefined(AA)&&AA>A)return "最少选择"+AA+"项！";
				if(!Object.isUndefined(AB)&&AB<A)return "最多选择"+AB+"项！";
				return false;
			},
			A_D: function(A){
				return Math.pow(2,A);
			},
			A_E: function(A,B){
				return (new RegExp(B)).test(A);
			},
			A_F: function(A){
				return this.A_E(A,/^\d+$/);
			},
			A_G: function(A,B){
				if(Object.isUndefined(A))return "没有需要检测的对象！";
				var AA = A.trim();
				var AB,AC,AD,AE,AF,AG,AH,AI;
				if(Object.isUndefined(B))B=1;
				if(B==1)AH = /^(\d{1,4})-(\d{1,2})-(\d{1,2})$/;
				if(B==2)AH = /^(\d{1,4})-(\d{1,2})-(\d{1,2})\s(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
				if(B==3)AH = /^(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
				AI = AA.match(AH);   
				if(AI == null)return "日期长度不对,或日期中有非数字符号！";
				if(B==1||B==2){
					AB = AI[1];   
					AC = AI[2];   
					AD = AI[3];
					if (AB<1000 || AB>9999) return "年份应该为1000-9999的整数！"; 
					if (AC < 1 || AC > 12)return "月份应该为1到12的整数！";  
					if (AD < 1 || AD > 31)return "每个月的天数应该为1到31的整数！";
					if ((AC==4 || AC==6 || AC==9 || AC==11) && AD==31)return "该月不存在31号！";
					if (AC==2){   
						var isleap=(AB % 4==0 && (AB % 100 !=0 || AB % 400==0));   
						if (AD>29)return "2月最多有29天！";   
						if ((AD==29) && (!isleap))return "闰年2月才有29天！"; 
					}
				}
				if(B==2||B==3){
					if(B==2){
						AE=AI[4];
						AF=AI[5];
						AG=AI[6];
					}
					if(B==3){
						AE=AI[1];
						AF=AI[2];
						AG=AI[3];
					}
					if(AE<0||AE>23)return "小时请输入0-23之间的整数！";
					if(AF<0||AF>59)return "分钟请输入0-59之间的整数！";
					if(AG<0||AG>59)return "秒请输入0-59之间的整数！";
				}
				if(B==1)return new Date(AB,AC-1,AD);
				if(B==2)return new Date(AB,AC-1,AD,AE,AF,AG);
				if(B==3){
					var AJ=new Date();
					return new Date(AJ.getYear(),AJ.getMonth(),AJ.getDay(),AE,AF,AG);
				}
			},
			A_H: function(A,B){
				return A==B?0:(A>B?1:-1);
			},
			A_I: function(A){
				if(!this.A_F(A))return "请输入数字！";
				var B=this.B_B();
				if(B&&Object.isNumber(B.lt)&&this.A_H(parseInt(A),parseInt(B.lt))!=1)return "输入数值必须大于"+B.lt+"！";
				if(B&&Object.isNumber(B.lteq)&&this.A_H(parseInt(A),parseInt(B.lteq))==-1)return "输入数值应大于等于"+B.lteq+"！";
				if(B&&Object.isNumber(B.gt)&&this.A_H(parseInt(A),parseInt(B.gt))!=-1)return "输入数值必须小于"+B.gt+"！";
				if(B&&Object.isNumber(B.gteq)&&this.A_H(parseInt(A),parseInt(B.gteq))==1)return "输入数值应小于等于"+B.gteq+"！";
				return false;
			},
			A_J: function(A,B){
				var C=false;
				if((C=this.A_G(A,B))&&Object.isString(C))return C;
				var D=this.B_B();
				var E="";
				if(D&&!Object.isString((E=this.A_G(D.lt,B)))&&this.A_H(Date.parse(C),Date.parse(E))!=1)return "输入日期或时间必须大于"+D.lt+"！";
				if(D&&!Object.isString((E=this.A_G(D.lteq,B)))&&this.A_H(Date.parse(C),Date.parse(E))==-1)return "输入日期或时间应大于等于"+D.lteq+"！";
				if(D&&!Object.isString((E=this.A_G(D.gt,B)))&&this.A_H(Date.parse(C),Date.parse(E))!=-1)return "输入日期或时间必须小于"+D.gt+"！";
				if(D&&!Object.isString((E=this.A_G(D.gteq,B)))&&this.A_H(Date.parse(C),Date.parse(E))==1)return "输入日期或时间应小于等于"+D.gteq+"！";
				return false;
			},
			B_B: function(){
				if(Object.isUndefined(this.A.compare))return false;
				return this.A.compare;
			},
			B_C: function(){
				if(Object.isUndefined(this.A.dataLength))return false;
				return this.A.dataLength;
			},
			B_D: function(A){
				return this.A_E(A,/^(\+|-)?\d+$/);
			},
			A_K: function(A){
				return this.A_E(A,/^(\+|-)?\d+($|\.\d+$)/);
			},
			A_L: function(A){
				if(!this.A_K(A))return "请输入实数！";
				var B=this.B_B();
				if(B&&this.A_K(B.lt)&&this.A_H(parseFloat(A),parseFloat(B.lt))!=1)return "输入数值必须大于"+B.lt+"！";
				if(B&&this.A_K(B.lteq)&&this.A_H(parseFloat(A),parseFloat(B.lteq))==-1)return "输入数值应大于等于"+B.lteq+"！";
				if(B&&this.A_K(B.gt)&&this.A_H(parseFloat(A),parseFloat(B.gt))!=-1)return "输入数值必须小于"+B.gt+"！";
				if(B&&this.A_K(B.gteq)&&this.A_H(parseFloat(A),parseFloat(B.gteq))==1)return "输入数值应小于等于"+B.gteq+"！";
				return false;
			},
			A_M: function(A){
				var AF={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
				var AG = 0;
				var AH = A;
				var AI = AH.length;
				var AJ="";
				if(!this.A_E(AH,/^\d{17}(\d|x)$/i)&&!this.A_E(AH,/^\d{15}$/i))return "长度非法！";
				AH = AH.replace(/x$/i,"a");
				if(AF[parseInt(AH.substr(0,2))]==null)return "非法地区！";
				if (AI==18){   
					AJ=AH.substr(6,4)+"-"+Number(AH.substr(10,2))+"-"+Number(AH.substr(12,2));   
					var d = new Date(AJ.replace(/-/g,"/"))   
					if(AJ!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "非法生日！";  
					for(var i = 17;i>=0;i--)   
						AG += (Math.pow(2,i) % 11) * parseInt(AH.charAt(17 - i),11);
					if(AG%11!=1)return "非法！";
				}   
				else if (AI==15){   
					AJ = "19" + AH.substr(6,2) + "-" + Number(AH.substr(8,2)) + "-" + Number(AH.substr(10,2));
					var d = new Date(AJ.replace(/-/g,"/"))   
					var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate();      
					if(AJ != dd)return "非法生日！";
				}
				return false;
			},
			A_N: function(A){
				var AA=false;
				if((AA=this.B_C())){
					if(Object.isNumber(AA)&&A.length!=AA)
						return "输入长度为"+AA+"个字符！";
					else{
						var AB=AA.max;
						var AC=AA.min;
						if((Object.isNumber(AC)&&A.length<AC)&&(Object.isNumber(AB)&&A.length>AB))
							return "输入长度在"+AC+"-"+AB+"字符之间！";
						else{
							if(Object.isNumber(AC)&&A.length<AC)return "输入长度不能少于"+AC+"个字符！";
							if(Object.isNumber(AB)&&A.length>AB)return "输入长度不能多于"+AB+"个字符！";
						}
					}
				}
				return false;
			},
			A_O: function(A,B,C,D){
				if(!this.B_E(A,B,C,D))alert(A);
			},
			B_E: function(A,B,C,D){
				
				var show=C;
				var info=D;
				if(!Object.isUndefined(info)&&info!=null){
					if(Object.isUndefined(show)&&show!=null)show.style.display="";
					if(!B)A="<font color='red'>"+A+"</font>";
					info.innerHTML=A;
					return true;
				}
				return false;
			},
			A_P: function(A){
				if(A.readyState == 4 && A.status == 200){
					var B=A.responseText.evalJSON();
					if(B.error){
						this.A.pass=false;
						if(!Object.isUndefined(this.A.initiative)){this.A.initiative.pass=false;this.A.initiative.self.value='';}
						this.A_O(this.A.fieldname+B.error,false,this.A.show,this.A.info);
					}else{
						this.A.pass=true;
						this.A_O(this.A.fieldname+"通过验证！",true,this.A.show,this.A.info);
						if(!Object.isUndefined(this.A.initiative))this.A.initiative.contrast.judgment=this.A.pass;new excute(this.A.initiative,this.B,true);
					}
				}else{
					this.B_E("正在验证信息...",false,this.A.show,this.A.info);
				}
			},
			A_Q: function(A,B,C,D){
				switch (D){
					case 1:
						return this.A_R(A,B,C)?false:"必须大于等于";
					case 2:
						return this.A_S(A,B,C)?false:"必须大于";
					case 3:
						return this.A_T(A,B,C)?false:"必须小于等于";
					case 4:
						return this.A_U(A,B,C)?false:"必须小于";
					case 5:
						return this.A_V(A,B,C)?false:"必须等于";
				}
				return false;
			},
			A_R: function(A,B,C){
				if(C=="number")return (this.A_H(parseInt(A),parseInt(B))>=0);
				if(C=="double")return (this.A_H(parseFloat(A),parseFloat(B))>=0);
				if(C=="date")return (this.A_H(Date.parse(this.A_G(A)),Date.parse(this.A_G(B)))>=0);
				if(C=="datetime") return (this.A_H(Date.parse(this.A_G(A,2)),Date.parse(this.A_G(B,2)))>=0);
				if(C=="time") return (this.A_H(Date.parse(this.A_G(A,3)),Date.parse(this.A_G(B,3)))>=0);
				return A>=B;
			},
			A_S: function(A,B,C){
				if(C=="number")return (this.A_H(parseInt(A),parseInt(B))>0);
				if(C=="double")return (this.A_H(parseFloat(A),parseFloat(B))>0);
				if(C=="date")return (this.A_H(Date.parse(this.A_G(A)),Date.parse(this.A_G(B)))>0);
				if(C=="datetime") return (this.A_H(Date.parse(this.A_G(A,2)),Date.parse(this.A_G(B,2)))>0);
				if(C=="time") return (this.A_H(Date.parse(this.A_G(A,3)),Date.parse(this.A_G(B,3)))>0);
				return A>B;
			},
			A_T: function(A,B,C){
				if(C=="number")return (this.A_H(parseInt(A),parseInt(B))<=0);
				if(C=="double")return (this.A_H(parseFloat(A),parseFloat(B))<=0);
				if(C=="date")return (this.A_H(Date.parse(this.A_G(A)),Date.parse(this.A_G(B)))<=0);
				if(C=="datetime") return (this.A_H(Date.parse(this.A_G(A,2)),Date.parse(this.A_G(B,2)))<=0);
				if(C=="time") return (this.A_H(Date.parse(this.A_G(A,3)),Date.parse(this.A_G(B,3)))<=0);
				return A<=B;
			},
			A_U: function(A,B,C){
				if(C=="number")return (this.A_H(parseInt(A),parseInt(B))<0);
				if(C=="double")return (this.A_H(parseFloat(A),parseFloat(B))<0);
				if(C=="date")return (this.A_H(Date.parse(this.A_G(A)),Date.parse(this.A_G(B)))<0);
				if(C=="datetime") return (this.A_H(Date.parse(this.A_G(A,2)),Date.parse(this.A_G(B,2)))<0);
				if(C=="time") return (this.A_H(Date.parse(this.A_G(A,3)),Date.parse(this.A_G(B,3)))<0);
				return A<B;
			},
			A_V: function(A,B,C){
				if(C=="number")return (this.A_H(parseInt(A),parseInt(B))==0);
				if(C=="double")return (this.A_H(parseFloat(A),parseFloat(B))==0);
				if(C=="date")return (this.A_H(Date.parse(this.A_G(A)),Date.parse(this.A_G(B)))==0);
				if(C=="datetime") return (this.A_H(Date.parse(this.A_G(A,2)),Date.parse(this.A_G(B,2)))==0);
				if(C=="time") return (this.A_H(Date.parse(this.A_G(A,3)),Date.parse(this.A_G(B,3)))==0);
				if(C=="string")return A==B;
				return A==B;
			}
		});
		new excute(A,this.C,B);
	},
	A_D: function(e){
		this.AA=false;
		this.A.each(function(A){
			if(!A.pass||!Object.isUndefined(A.contrast))this.A_C(A,true);
			if(!A.pass)this.AA=true;
		},this);
		if (this.AA && e && e.preventDefault) e.preventDefault();
		return !this.AA;
	},
	A_E: function(A){
		this.A.each(function(B){
			if(A.contrast.compObj==B.self){
				A.dataType=B.dataType;
				A.contrast.judgment=B.pass;
				A.contrast.fieldname=B.fieldname;
				A.contrast.show=B.show;
				A.contrast.info=B.info;
				B.initiative=A;
			}					 
		},A);
	}
});

var WebeditInstance = Class.create({
	initialize: function(D){
		this.B=false;
		this.C=false;
		if(!Object.isString(D))return;
		try {
			this.A=FCKeditorAPI.GetInstance(D);
		} catch (e) { }
		if(Object.isFunction(this.A.GetInstanceObject)){
			this.C="FCKeditor";
			this.B=true;
		}
	},
	isEdit: function(){
		return this.B;
	},
	getData: function(){
		if(this.C=="FCKeditor")return this.A.GetData().stripTags().replace(/\&nbsp\;/g,"").replace(/\&(.[a-z0-9]+)\;/g,"*");
		return false;
	}
});
