﻿//************************//
//Author 		: ldc
//Date			: 2008-04-25
//LastModify	: 2008-04-25
//E-Mail		: ldc003@163.com
function $(o){
	return document.getElementById(o);
}
function getA(o,name){
	return o.getAttribute(name);
}
String.prototype.inc = function(s){
	if(this.indexOf(s)!=-1)
		return true;
	return false;
}

function Validator(){
	me = this,
	this.errMsg = null,
	this.form = null,
	this.setCheck = function(form){
		me.form = document.forms[form];
		var os = me.form.elements;
		var o = null;
		for(var i=0;i<os.length;i++){
			o = os[i];
			if(getA(o,"chkRule")){
				o.onblur = me.setblur;
				o.onfocus = me.setfocus;
			}
		}
	},
	this.setblur = function(){
		if(me.check(this,false)){
			this.className = "";
			if($(this.id+"_span")){
				$(this.id+"_span").className = "chkok";
				$(this.id+"_span").innerHTML = "OK！";
			}
		}
	},
	this.setfocus = function(){
		this.className = "focus";
		if($(this.id+"_span")){
			$(this.id+"_span").className = "chknormal";
			$(this.id+"_span").innerHTML = getA(this,"baktext");
		}
	},
	this.check = function(o,chkall){
		var rule = getA(o,"chkRule");
		var result = /\/([0-9]{1,8})-([0-9]{1,8})\//.exec(rule);
		if(rule.inc("/null/")){
			if(o.value==""){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"不能为空！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"不能为空！");
				}
				return false;
			}
		}
		if(rule.inc("/^a/")){
			if(!(/^[a-zA-Z]{1}$/.test(o.value.substring(0,1)))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"第一个字符必须是字母！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"第一个字符必须是字母！");
				}
				return false;
			}
		} 
		if(result){
			var nmin = result[1];
			var nmax = result[2];
			if(o.value.length<nmin||o.value.length>nmax){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"长度必须是"+nmin+"-"+nmax+"个字符！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"长度必须是"+nmin+"-"+nmax+"个字符！");
				}
				return false;
			}
		}
		if(rule.inc("/compare/")){
			var cop = $(getA(o,"compareto"));
			if(o.value!=cop.value){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"与"+getA(cop,"tname")+"输入的不一致！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"与"+getA(cop,"tname")+"输入的不一致！");
				}
				return false;
			}
		} 
		if(rule.inc("/mail/")){
			if(!(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"格式有误！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"格式有误！");
				}
				return false;
			}
		} 
		if(rule.inc("/date/")){
				
			if(!(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/.test(o.value))&&!(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"格式有误！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"格式有误！");
				}
				return false;
			}
		} 
		if(rule.inc("/num/")){
			if(!(/^[0-9]{1,8}$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"只能为数字！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"只能为数字！");
				}
				return false;
			}
		} 
		if(rule.inc("/abc/")){
			if(!(/^[a-zA-Z]+$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"只能为英文字母！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"只能为英文字母！");
				}
				return false;
			}
		} 
		if(rule.inc("/abc1/")){
			if(!(/^[a-zA-Z0-9]+$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"只能为英文或者数字！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"只能为英文或者数字！");
				}
				return false;
			}
		}
		if(rule.inc("/custom/")){
		    var r = new RegExp(getA(o,"exp"));
			if(!r.test(o.value)){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tips");
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tips"));
				}
				return false;
			}
		}
		if(rule.inc("/exist/")){
			if(chkall==false){
				$(o.id+"_span").className = "chknormal";
				$(o.id+"_span").innerHTML = "正在验证是否可用..";
				var callback = getA(o,"fn");
				try{
					eval("var fn = "+callback);
					fn(o.value,function(res){
						if(res.value==1){
							o.className = "";
							$(o.id+"_span").className = "chkok";
							$(o.id+"_span").innerHTML = "该用户名可以注册！";
						}else{
							o.className = "waring";
							$(o.id+"_span").className = "chkwaring";
							$(o.id+"_span").innerHTML = "用户名 "+o.value+" 已经被注册！";
						}
					});return;
				}catch(e){alert(e.message)};
			}
		}
		return true;	
	},
	this.checkall = function(){
		me.errMsg = new Array();
		var os = me.form.elements;
		var o = null;
		for(var i=0;i<os.length;i++){
			o = os[i];
			if(getA(o,"chkRule")){
				me.check(o,true);
			}
		}
		var msg = "";
		if(me.errMsg.length>0){
			for(var i=0;i<me.errMsg.length;i++){
				if(i>0){
					msg+= "\n";
				}
				msg+= "- "+ me.errMsg[i];
			}
			alert(msg);
			return false;
		}
		return true;
	}
}

