/**
* 网站信息管理类别验证
*/
var Validator = Validator || {
	/**
	* 数字
	*/
	number: /^\d+$/,
	/**
	* 数字和字母
	*/
	numberletter: /^[\da-zA-Z]+$/,
	/**
	* 浮点数
	*/
	floats: /^[+|-]?\d*\.?\d*$/,
	// 年龄
	age: /^\d{2}$/,
	/**
	* email
	*/
	email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	/**
	* 电话
	*/
	phone: /^(\d{3,4})?-?(\d{7,8})$/,
	mobile13: /^13\d{9}$/,
	mobile15: /^15\d{9}$/,
	mobile0:  /^0\d{10,11}$/,
	/**
	* 网址
	*/
	url: /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,

	/**
	* 不能为空elements为{elementID: "msg "}
	*
	* @author dengxh at 2009-3-18
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	notEmpty: function(elements){
		return this.each(elements, function(el, msg){
			if(Validator._valOf(el) == ''){
				Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		});
	},

	/**
	* 两次输入密码是否相同
	*
	* @author dengxh at 2009-3-18
	* @param string from 第一个密码输入框ID
	* @param string to 第二个密码输入框ID
	* @param object config 验证配置选项
	* @return boolean
	*/
	password: function(from, to, config){
		var c = {
			length: 0,
			notEmpty: false,
			empty: '密码不能为空',
			equal: '两次输入的密码不相同'
		};

		if(c.length && c.notEmpty && this._valOf(from).length < c.length){
			Tip.alert(c.empty, function(){$('#'+from).focus()});
		}
		else if(this._valOf(from) != this._valOf(to)){
			Tip.alert(c.equal, function(){$('#'+to).focus()});
		}
		else{
			return true;
		}
	},

	/**
	* 是否为浮点数
	*
	* @author dengxh at 2009-3-18
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	isFloat: function(elements){
		return this.each(elements, function(el, msg){
			if(!Validator.floats.test(Validator._valOf(el))){
				Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		});
	},

	/**
	* 判断是否为数字
	*
	* @author dengxh at 2009-3-18
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	isNumber: function(elements, callback){
		return this.each(elements, function(el, msg){
			var val = Validator;
			if(callback && !callback(el)){
				return true;
			}
			if(!val.number.test(val._valOf(el))){
				Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		});
	},

	/**
	* 是否为数字的字母的组合
	*
	* @author dengxh at 2009-3-18
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	isNumberletter: function(elements, config){
		return this.each(elements, function(el, msg){
			var val = Validator;
			if(!val.numberletter.test(val._valOf(el))){
				Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		}, config);
	},

	/**
	* 是否为数字的字母的组合
	*
	* @author dengxh at 2009-3-18
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	isEmail: function(elements, config){
		return this.each(elements, function(el, msg){
			var val = Validator;
			if(!val.email.test(val._valOf(el))){
				Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		}, config);
	},

	/**
	* 是否为正确的手机号码
	*
	* @author dengxh at 2009-4-16
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	isMobile: function(elements, config){
		return this.each(elements, function(el, msg){
			var val = Validator;
			if(!val.mobile13.test(val._valOf(el)) && !val.mobile15.test(val._valOf(el)) && !val.mobile0.test(val._valOf(el))){
				if(msg != '' && msg != null)
					Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		}, config);
	},
	/**
	* 是否为正确的电话号码
	*
	* @author dengxh at 2009-4-16
	* @param object elements 需要验证的对象集合
	* @return boolean
	*/
	isPhone: function(elements, config){
		return this.each(elements, function(el, msg){
			var val = Validator;
			if(!val.phone.test(val._valOf(el))){
				if(msg != '' && msg != null)
					Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		}, config);
	},
	/**
	* 用回调函数处理对象数组,如果回调函数返回false则返回false并结束运行
	*
	* @author dengxh at 2009-3-18
	* @param object elements 需要验证的对象集合
	* @param function fn 回调函数
	* @param object config 配置选项
	* @return boolean
	*/
	each: function(elements, fn, config){
		var c = {
			allowEmpty: false
		};
		if(config != undefined){
			$.extend(c, config);
		}

		for(var i in elements){
			if(c.allowEmpty == true && Validator._valOf(i) == ''){
				continue;
			}else{
				if(!fn(i, elements[i]))
				return false;
			}
		}
		return true;
	},

	/**
	* 得到HTML元素的value
	*
	* @author dengxh at 2009-3-18
	* @param string id 元素ID
	* @return string
	*/
	_valOf: function(el){
		if(typeof(el) == 'object')
		return el.value || el.val();
		else if(typeof(el) == 'string')
		return $('#'+el).val();
		else
		return el;
	},
	/**
	* 简单年龄验证
	*
	* @author panxp at 2009-4-15
	* @param int age
	* @return bool
	*/

	isAge:function(elements, config){
		return this.each(elements, function(el, msg){
			var val = Validator;
			if(!val.age.test(val._valOf(el))){
				if(msg != '' && msg != null)
					Tip.alert(msg, function(){$('#'+el).focus();});
				return false;
			}else{
				return true;
			}
		}, config);
	},
	/**
	* 简单电话验证不区分手机和座机
	*
	* @author panxp at 2009-4-15
	* @param int tel
	* @return bool
	*/
	_isPhone: function(tel){
		var pattern =  /^\d{4,12}$/;
		return pattern.test(tel);
	},
	/**
	* 简单邮编验证
	*
	* @author panxp at 2009-4-15
	* @param int postCode
	* @return bool
	*/
	isPostCode: function(postCode){
		var pattern =  /^\d{6}$/;
		return pattern.test(postCode);
	}
}
