// JavaScript Document
var validList = [];
var validResult = false;
$.fn.validForm = function(options)
{
	var defaults = {
	};    
	var opts = $.extend(defaults, options);
	
	$(this).submit(function(){
		validResult = true;
		for(i=0;i<validList.length;i++)
		{
			$(validList[i]).blur();
		}
		return validResult;
	});

}
$.fn.valid = function(options)
{
	var defaults = {
		name: null,
		IsNull: false,
		minLen: null,
		maxLen: null,
		type: null,
		path: null,
		depend: null,
		depend_val: null,
		ajaxUrl: null
	};    
	var opts = $.extend(defaults, options);
	validList[validList.length] = '#'+$(this).attr('id');
 	$(this).blur(function(){
		var val = $(this).val();
		var len = strlen(val);
		if(opts.depend!=null)
		{
			if($('#'+opts.depend).attr(opts.depend_attr)!= opts.depend_val)
			{
				Passed(this);
				return true;
			}
		}
		if(opts.IsNull==false)
		{
			if(len==0)
			{
				ShowErrMsg(this,opts.name+'不能为空');
				return false;
			}
		}
		if(opts.minLen!=null)
		{
			if(len<opts.minLen)
			{
				ShowErrMsg(this,opts.name+'不能小于'+opts.minLen+'个字符');
				return false;
			}
		}
		if(opts.maxLen!=null)
		{
			if(len>opts.maxLen)
			{
				ShowErrMsg(this,opts.name+'不能大于'+opts.maxLen+'个字符');
				return false;
			}
		}
		if(opts.path!=null)
		{
			var obj = this;
			var result = true
			$.ajax({url:'../uploadify/check-exists.php',type: 'POST',async: false, data:'path='+opts.path,success: function(data){
				if(data!='1')
				{
					ShowErrMsg(obj,'请先上传'+opts.name);
					result = false;
				}
			}})
			if(!result)
				return false;
		}
		if(opts.type!=null)
		{
			switch(opts.type)
			{
				case 'url':
					if(len==0)
					{
						ShowErrMsg(this,opts.name+'不能为空');
						return false;
					}
					if(val=='http://'||!is_url(val))
					{
						ShowErrMsg(this,opts.name+'填写不正确');
						return false;
					}
				break;
				case 'email':
				
				break;
				case 'ip':
				
				break;
				case 'phone':
				
				break;
				case 'mobile':
				
				break;
				case 'ajax':
				
				break;
			}
		}
		Passed(this);
	});
	function ShowErrMsg(obj,str)
	{
		$(obj).parent().parent().attr('class','input_area input_area_error');
		$(obj).parent().parent().children('.input_info:eq(0)').html(str);
		validResult = false;
	}
	function Passed(obj)
	{
		$(obj).parent().parent().attr('class','input_area input_area_passed');
		$(obj).parent().parent().children('.input_info:eq(0)').html('填写正确');
	}
	function strlen(s)
	{ 
		var l = 0; 
		var a = s.split(""); 
		for (var i=0;i<a.length;i++)
		{ 
			if (a[i].charCodeAt(0)<299)
				l++; 
			else
				l+=2; 
		} 
		return l; 
	}
	function is_url(str_url)
	{
		var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
		+
		"(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
		+
		"|" // 允许IP和DOMAIN（域名）
		+
		"([0-9a-z_!~*'()-]+.)*" // 域名- www. 
		+
		"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名 
		+
		"[a-z]{2,6})" // first level domain- .com or .museum 
		+
		"(:[0-9]{1,4})?" // 端口- :80 
		+
		"((/?)|" // a slash isn't required if there is no file name 
		+
		"(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
		var re = new RegExp(strRegex);
		//re.test()
		if (re.test(str_url)) {
		   return (true);
		} else {
		   return (false);
		}
	}
}
