/************************/
/*  功能：数据验证    	*/
/*  version：v1.0     	*/
/*  create：2009-6-25	*/
/*  update：2009-6-25	*/
/************************/


var validate = {
	
}


/**************/
/* 数据类型检测 */
/**************/
validate.datatype = {
	/***
	 * 功能：返回数据类型
	 * 参数说明：mix 被检测对象
	 * 返回值：tring 数据类型
	*/
	objectType: function(obj){
		try{
			var oType = typeof obj;
				oType = oType.toString().toLowerCase();
			return oType;
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为对象
	 * 参数说明：string 被检测对象
	 * 返回值：boolean True/False
	*/
	isObject: function(obj){
		try{
			if(this.objectType(obj) == "object"||obj.constructor == Object){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为函数
	 * 参数说明：string 被检测对象
	 * 返回值：boolean True/False
	*/
	isFunction: function(obj){
		try{
			if(obj instanceof Function === true||this.objectType(obj) == "function"){//||obj.constructor == Function
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为字符串
	 * 参数说明：string 被检测对象
	 * 返回值：boolean True/False
	*/
	isString: function(obj){
		try{
			if(obj instanceof String === true||this.objectType(obj) == "string"||obj.constructor === String){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为数组
	 * 参数说明：string 被检测对象
	 * 返回值：boolean True/False
	*/
	isArray: function(obj){
		try{
			if(obj instanceof Array === true||this.objectType(obj) == "object"&&obj.constructor === Array&&"splice" in obj === true&&"join" in obj === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为数字
	 * 参数说明：tring 被检测对象
	 * 返回值：boolean True/False
	*/
	isNumeric: function(obj){
		try{
			if(obj instanceof Number === true||this.objectType(obj) == "number"||obj.constructor === Number){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为Boolean
	 * 参数说明：tring 被检测对象
	 * 返回值：boolean True/False
	*/
	isBoolean: function(obj){
		try{
			if(obj instanceof Boolean === true||this.objectType(obj) == "boolean"||obj.constructor === Boolean||obj === true||obj === false){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为NULL
	 * 参数说明：tring 被检测对象
	 * 返回值：boolean True/False
	*/
	isNull: function(obj){
		try{
			if(this.objectType(obj) == "null"){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：检测是否为undefined
	 * 参数说明：tring 被检测对象
	 * 返回值：boolean True/False
	*/
	isUndefined: function(obj){
		try{
			if(this.objectType(obj) == "undefined"){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	}
}

/***********/
/* 数据验证 */
/***********/
validate.checkData = {
	/***
	 * 功能：验证是否为空
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isNull: function(str){
		try{
			if(str == ""||str === null||str.length === 0){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为英文字母、数字、横线、下划线
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isEnSign: function(str){
		try{
			var pattern = /^[\w-]+$/;
			if(pattern.test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为汉字
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isChinese: function(str){
		try{
			var pattern = /^[\u0391-\uFFE5]+$/;
			if(pattern.test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为英文
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isEnglish: function(str) {
		try{
			var pattern = /^[A-Za-z]+$/i;
			if(pattern.test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为数字
	 * 参数说明：string 进行检测的字符串，string 数字格式
	 * 返回值：boolean True/False
	*/
	isNumeric: function(str, format){
		try{
			format = validate.datatype.isUndefined(format) === true?"both":format.toLowerCase();
			var pattern = new Array();
				pattern["both"] = /^[+-]?\d+(.\d+)?$/;
				pattern["int"] = /^[+-]?\d+$/;
				pattern["float"] = /^[+-]?\d+.\d+$/;
			if(isNaN(str) === false&&pattern[format].test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为手机号码
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isMobile: function(str){
		try{
			var pattern = /^1[3|5]\d{9}$/;
			if(pattern.test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为IP地址
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isIP: function(str){
		try{
			var pattern = /^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3,5}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/;
			var IP = str.toArray(".");
			if(pattern.test(str) === true&&(IP.length === 4||IP.length === 6)){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为电子邮箱
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isMail: function(str){
		try{
			if(str.length < 6||str.length > 50){
				return false;
			}else{
				var pattern = new Array();
					pattern[0] = /^[\w\.\-]+@([A-Za-z\d]+([\-_][A-Za-z\d])?\.)+((com)|(net)|(gov)|(org))(\.[A-Za-z]{2})?$/i;
					pattern[1] = /^[\w\.\-]+@([A-Za-z\d]+([\-_][A-Za-z\d])?\.)+((biz)|(asia)|(name)|(mobi)|(cc)|(tv)|(me)|(info))$/i;
					pattern[2] = /^[\w\.\-]+@([A-Za-z\d]+([\-_][A-Za-z\d])?\.)+[A-Za-z]{2}$/i;
				for(var p = 0; p < pattern.size(); p++){
					if(pattern[p].test(str) === true){
						return true;
						break;
					}
				}
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为QQ号码
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isQQ: function(str){
		try{
			var pattern = /^[1-9]\d{4,9}$/;
			if(pattern.test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为支付宝账号
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isAlipay: function(str){
		try{
			if(this.isMail(str) === true||this.isMobile(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	},

	/***
	 * 功能：验证是否为邮政编码
	 * 参数说明：string 进行检测的字符串
	 * 返回值：boolean True/False
	*/
	isPc: function(str){
		try{
			var pattern = /^[1-9]\d{5}$/;
			if(pattern.test(str) === true){
				return true;
			}else{
				return false;
			}
		}catch(e){
		}
	}//,
}
