//各种格式的正则
var patterns = {
	int: /^[\d]+$/,
	varName: /^[\w]+$/,
	email: /^[\w-\.]+@([\w-]+\.){1,3}[a-z]{2,6}$/i,
	phone: /^[\d\-\_\ \#]{7,15}$/,
	cn: /^[u4E00-u9FA5]+$/,	//中文
	ip: /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/g,
	url: /(?:https?:\/\/)?\w+\.\w+/
};
//默认错误信息
var errors = {
	int: '[label] must be an available integer',
	varName: '[label] just include letter、numeral and underline',
	email: '[label] must be an availabel email',
	phone: '[label] must be an availabel telephone',
	cn: '[label]只能包含中文',
	ip: '[label]不是有效的IP',
	url: '[label]不是有效的url',
	noEmpty: '[label] cannot empty',
	minlength: '[label] cannot less than [value]',
	eq: '[label] 与 [value] 不匹配'
};
//与变量名规则相同
$.fn.extend({
	checkForm: function (){
		$(this).submit(function (){
			errorCount = 0;
			$(this).find('.error').remove();
			$(this).find('[format]').checkFormat();
			$(this).find('[noEmpty]').noEmpty();
			$(this).find('[minlength]').minlength();
			$(this).find('[eq]').eq();
			if(errorCount) return false;
			//如果cb不是函数会报错,所以用try和catch模拟if和else
			cb = $.formCb;
			
			try {
				if($(this).attr('cb') != 'undefined' && typeof(eval($(this).attr('cb')))=="function"){
					var cb = eval($(this).attr('cb'));
				}
			}catch(e){
				//默认回调函数
				var to = $(this).attr('cb');
			};
			//提交
			return $(this).formSubmit(cb);
		});
		$(this).find('input[type=text]:first').focus();
		//表单帮助
		$('a.help').mouseover(function (){
			$(this).tip($(this).next().html(),0);
		}).mouseout(function (){
			$('.jqMsg').remove();
		});
	},
	//提交表单,默认采用ajax方式,当有<input type="file">控件时,改用隐藏iframe方式提交.
	formSubmit: function (cb){
		if($(this).find(':file:visible').length) {
			//隐藏iframe方式提交
			if(!$('iframe[name=uploadFrame]').length) {
				$(this).before('<iframe name="uploadFrame" style="width:400px;height:100px;display:none;"></iframe>')
					.attr('target', 'uploadFrame')
					.attr('action',$(this).attr('url'));
			}
		}else{
			$.post($(this).attr('url'), $(this).getPost(), cb);
			return false;
		}
	},
	checkFormat: function (){
		var pattern = /^[\d]+$/;
		this.each(function (){
			var type = $(this).attr('format');
			var pattern = patterns[type];
			if(this.value.length && !pattern.exec(this.value)) {
				$.errorNotify(this,type);
			}
		});
	},
	noEmpty: function (){
		this.each(function (){
			if(!this.value.length) {
				if($(this).attr('msg')) {
					var error = $(this).attr('msg');
				}else if($(this).attr('label')){
					var error = $(this).attr('label') + " " + errors.noEmpty;
				}else{
					var error = this.name + " " + errors.noEmpty;
				}
				$.errorNotify(this,'noEmpty');
			}
		});
	},
	minlength: function (){
		this.each(function (){
			if(this.value.length < $(this).attr('minlength')) {
				$.errorNotify(this,'minlength');
			}
		});
	},
	eq: function (){
		this.each(function (){
			if(this.value != $('[name='+$(this).attr('eq')+']').val()) {
				$.errorNotify(this,'eq');
			}
		});
	}
});
$.extend({
	//提示方法
	errorNotify: function (e,type){
		errorCount++;
		if($(e).attr('error')) {
			var error = $(e).attr('error');
		}else {
			if($(e).attr('label')){
				var error = errors[type].replace('[label]', $(e).attr('label')); 
			}else{
				var error = errors[type].replace('[label]', e.name);
			}
			error = error.replace('[value]', $(e).attr(type));
		}
		if(errorCount==1) $(e).focus();
		$(e).parent().append('<span class="error"><br/>'+error+'</div>');
		//$(e).tip(error);
	},
	formCb: function (data)
	{
		var to = $(this).attr('cb');
		if(data==1) {
			if(to == 'reload' || to == undefined) {
				location.reload();
			}else if(to == 'none') {
				$.dialog('close');
			}else{
				location.href = to;
			}
		}else{
			$.tip(data);
		}
	}
});
//错误数目
var errorCount = 0;
$(function (){
	$('.xyForm').checkForm();
});