/*
 * Title:
 * Description:
 * Aucthor: 曾宪斌
 * Email: zengxianbin@gmail.com
 * Create Date:2009-06-18
 * Copyright 2009
 */ 
(function($){
	$.fn.verify = function(verify){
		var msg = [];
		var sqlFlag = true;
		var Features = verify.split("\&\&");		
		var value = this.val();
		if(this.attr("ztype")&&this.attr("ztype").toLowerCase()=="select"){
				//value = '';
		}
		for(var i = 0; i < Features.length; i++) {
			var arr = Features[i].split("\|");
			var name = "";
			var rule;
			if(arr.length==2){
				name = arr[0];
				rule = arr[1];
			}else{
				rule = Features[i];
			}
			var op = "=";
			if(rule.indexOf('>') > 0) {
				op = ">";
			}else if (rule.indexOf('<') > 0) {
				op = "<";
			}
			var f = rule.split(op);
			var fName = f[0];
			var fValue = null;
			if(f.length > 1) {
				fValue = f[1];
			}
			if(fName=="Any") {
				sqlFlag = false;
			}else if (fName=="Regex") {
				fValue = rule.substring(6);
				if (value==null||value==""||!fValue) {continue;}
				var reg = fValue;
				if(!reg.startWith("^")){
					reg = "^"+reg;
				}
				if(!reg.endWith("$")){
					reg += "$";
				}
				if(!new RegExp(reg).test(value)){
					msg.push(name);
				}
			}else if (fName=="Script") {
				fValue = rule.substring(7);
				if (value==null||value==""||!fValue) {continue;}
				if(!eval(fValue)){
					msg.push(name);
				}
			}else if (fName=="NotNull") {
				if (value==null||value=="") {
					if(this.attr("ztype")&&this.attr("ztype").toLowerCase()=="select"){
						msg.push("必须选择"+name);
					}else{
						msg.push(name+"不能为空");
					}
				}
			}else if (fName=="Number") {
				if (value==null||value=="") {continue;}
				if(!Z.isNumber(value)){
					msg.push(name+"必须是数字");
				}
			}else if (fName=="Time") {
				if (value==null||value=="") {continue;}
				var timearr = value.split(":");
				if(timearr.length!=3){
						msg.push(name+"的值"+value+"不是正确的时间!");
				}else{
						if(!Z.isInt(timearr[0])||timearr[0]<0||timearr[0]>23){
							msg.push(name+"的值"+value+"错误，小时数"+timearr[0]+"不正确!");
							ele.focusEx();
						}else	if(!Z.isInt(timearr[1])||timearr[1]<0||timearr[1]>59){
							msg.push(name+"的值"+value+"错误，分钟数"+timearr[1]+"不正确!");
						}
				}
			}else if (fName=="Int") {
				if (value==null||value=="") {continue;}
				if(!Z.isInt(value)){
					msg.push(name+"必须是整数");
				}
			}else if (fName=="Date") {
				if (value==null||value=="") {continue;}
				if(!/\d{4}\-\d{2}\-\d{2}/.test(value)){
					msg.push(name+"必须是正确的日期");
				}else{
			  var ts = value.split("-");
				  var d = new Date(ts[0],--ts[1],ts[2]);
				  if(d.getMonth()!=ts[1]||d.getDate()!=ts[2]){
					msg.push(name+"必须是正确的日期");
				  }
				}
			}else if(fName=="Email") {
				if (value==null||value=="") {continue;}
				var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				if(value&&value.match(pattern)==null){
				  msg.push(name+"不是正确的电子邮箱地址");
				}
			}else if(fName=="Tel") {
				if (value==null||value=="") {continue;}
				var pattern = /^(0\d{2,3}\-)?\d{7,8}(\-\d{3,4})?$/;
				if(value&&value.match(pattern)==null){
				  msg.push(name+"不是正确的电话号码");
				}
			}else if(fName=="Mobile") {
				if (value==null||value=="") {continue;}
				var pattern = /^(?:13\d|15[89]|189)-?\d{5}(\d{3}|\*{3})$/;
				if(value&&value.match(pattern)==null){
				  msg.push(name+"不是正确的手机号码");
				}
			}else if(fName=="Length") {
				if (value==null||value=="") {continue;}
				if(isNaN(fValue)) {
					msg.push("校验规则错误，Length后面必须是数字");
				}else{
					try{
						var len = parseInt(fValue);
						if(op=="="&&value.length!=len) {
						  msg.push(name+"长度必须是" + len);
						}else if (op==">"&&value.length<=len) {
							msg.push(name+"长度必须大于" + len);
						}else if (op=="<"&& value.length>=len) {
							msg.push(name+"长度必须小于" + len);
						}
					} catch (ex) {
						msg.push("校验规则错误，Length后面必须是整数"+ex.message);
					}
				}
			}
		}
		if(msg.length>0){
			$(this).tip().close();
			var txt = msg.join("<br>");
			$(this).tip(txt,10).show();
			return false;
		}else{
			$(this).tip().close();
			return true
		}
	};
	$.fn.isValid = function(){
		var rs = true;
		if($(this).attr('verify'))
			rs = rs&&$(this).verify($(this).attr('verify'));
		$(this).find('$[verify]').each(function(){
			v = $(this).verify($(this).attr('verify'));
			rs = rs&&v;
		});
		return rs;
	};
	$.fn.clearValid = function(){
		if($(this).attr('verify'))
			$(this).tip().close();
		$(this).find('$[verify]').each(function(){
			$(this).tip().close();
		});
	};
	
	$(function(){
		$('$[verify]').each(function(){
			$(this).bind("focus", function(){
			  $(this).verify($(this).attr('verify'));
			}); 
			$(this).bind("blur", function(){
				$(this).verify($(this).attr('verify'));
			}); 
		});
		$('form').each(function(){
			this.onsubmit = function(){
				return $(this).isValid();
			}
		});
		
	/*	$('form').submit(function(){
			alert($(this).isValid())
			return $(this).isValid();
		});*/
	})
})(jQuery);	