/**Validation plugins
*Function Sample: jQuery('form[name=reg_form]').setValidation('a', 'images/loading.gif') ;
*Input Sample: <input type="password" name="password2" required="1" field="password" flags="repeat" />
*Author: Shi Yaxiong
*Date: 2010-6-1
*/

jQuery.fn.setValidation = function(remark_tag, loadPic)
{
	var form = jQuery(this) ;
	var ret = true ;
	
	//失去焦点验证
	form.find('input[required=1], textarea[required=1], select[required=1]').blur(function(){
		if(isEmpty(jQuery(this).val())){
			ShowErrorMsg(jQuery(this), '不能为空') ;
		}else if(jQuery(this).attr('flags') == 'email'){
			if(!isEmail(jQuery(this).val())){
				ShowErrorMsg(jQuery(this), 'Email格式错误') ;
			}else{
				ShowRightMsg(jQuery(this), 'Email格式正确') ;
			}
			
		}else if(jQuery(this).attr('flags') == 'http'){
			if(!isHttp(jQuery(this).val())){
				ShowErrorMsg(jQuery(this), 'Http格式错误') ;
			}else{
				ShowRightMsg(jQuery(this), 'Http格式正确') ;
			}
			
		}else if(jQuery(this).attr('flags') == 'file'){
			if(!isFile(jQuery(this).val(), jQuery(this).attr('extension'))){
				ShowErrorMsg(jQuery(this), '文件格式错误') ;
			}else{
				ShowRightMsg(jQuery(this), '文件格式正确') ;
			}
			
		}else if(jQuery(this).attr('flags') == 'repeat'){
			if(jQuery(this).val() != form.find('input[name='+jQuery(this).attr('field')+']').val()){
				ShowErrorMsg(jQuery(this), '重复密码错误') ;
			}else{
				ShowRightMsg(jQuery(this), '重复密码正确') ;
			}
			
		}else{
			ShowRightMsg(jQuery(this), '正确') ;
		}
	}) ;
	form.find('input[flags=email]').blur(function(){
		if(!isEmail(jQuery(this).val())){
			ShowErrorMsg(jQuery(this), 'Email格式错误') ;
		}else{
			ShowRightMsg(jQuery(this), 'Email格式正确') ;
		}
	}) ;
	form.find('input[flags=http]').blur(function(){
		if(!isHttp(jQuery(this).val())){
			ShowErrorMsg(jQuery(this), 'Http格式错误') ;
		}else{
			ShowRightMsg(jQuery(this), 'Http格式正确') ;
		}
	}) ;
	form.find('input[flags=file]').blur(function(){
		if(!isFile(jQuery(this).val(), jQuery(this).attr('extension'))){
			ShowErrorMsg(jQuery(this), '文件格式错误') ;
		}else{
			ShowRightMsg(jQuery(this), '文件格式正确') ;
		}
	}) ;
	form.find('input[flags=repeat]').blur(function(){
		if(jQuery(this).val() != form.find('input[name='+jQuery(this).attr('field')+']').val()){
			ShowErrorMsg(jQuery(this), '重复密码错误') ;
		}else{
			ShowRightMsg(jQuery(this), '重复密码正确') ;
		}
	}) ;
	form.find('input[unique=1]').blur(function(){
		var obj = jQuery(this) ;
		if(isEmpty(obj.val())){
			return ;
		}
		if(obj.attr('flags') == 'email' && !isEmail(obj.val())){
			return ;
		}
		
		obj.next(remark_tag).html('<img src="'+loadPic+'" align="absmiddle" /> 正在验证...') ;
		jQuery.get(obj.attr('ajaxURL'), {field: obj.val()}, function(data){
																			  if(data != 'OK'){
																				  obj.attr('ajaxValue', '0') ;
																				  ShowErrorMsg(obj, '已存在') ;
																			  }else{
																				  obj.attr('ajaxValue', '1') ;
																				  ShowRightMsg(obj, '正确') ;
																			  }
																			}) ;
	}) ;
	
	
	//提交验证
	form.submit(function(){
		ret = true ;
		
		//required
		form.find('input[required=1], textarea[required=1], select[required=1]').each(function(){
			if(isEmpty(jQuery(this).val())){
				ShowErrorMsg(jQuery(this), '不能为空') ;
				
				return false;
			}else if(jQuery(this).attr('flags') == 'email'){
				if(!isEmail(jQuery(this).val())){
					ShowErrorMsg(jQuery(this), 'Email格式错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), 'Email格式正确') ;
				}
				
			}else if(jQuery(this).attr('flags') == 'http'){
				if(!isHttp(jQuery(this).val())){
					ShowErrorMsg(jQuery(this), 'Http格式错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), 'Http格式正确') ;
				}
				
			}else if(jQuery(this).attr('flags') == 'file'){
				if(!isFile(jQuery(this).val(), jQuery(this).attr('extension'))){
					ShowErrorMsg(jQuery(this), '文件格式错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), '文件格式正确') ;
				}
				
			}else if(jQuery(this).attr('flags') == 'repeat'){
				if(jQuery(this).val() != form.find('input[name='+jQuery(this).attr('field')+']').val()){
					ShowErrorMsg(jQuery(this), '重复密码错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), '重复密码正确') ;
				}
				
			}else{
				ShowRightMsg(jQuery(this), '正确') ;
			}
			
		}) ;
		
		if(!ret){
			return false ;
		}
		
		//email
		if(ret){
			form.find('input[flags=email]').each(function(){
				if(!isEmail(jQuery(this).val())){
					ShowErrorMsg(jQuery(this), 'Email格式错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), 'Email格式正确') ;
				}
			}) ;
		}
		
		//http
		if(ret){
			form.find('input[flags=http]').each(function(){
				if(!isHttp(jQuery(this).val())){
					ShowErrorMsg(jQuery(this), 'Http格式错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), 'Http格式正确') ;
				}
			}) ;
		}
		
		//file
		if(ret){
			form.find('input[flags=file]').each(function(){
				if(!isFile(jQuery(this).val(), jQuery(this).attr('extension'))){
					ShowErrorMsg(jQuery(this), '文件格式错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), '文件格式正确') ;
				}
			}) ;
		}
		
		//repeat
		if(ret){
			form.find('input[flags=repeat]').each(function(){
				if(jQuery(this).val() != form.find('input[name='+jQuery(this).attr('field')+']').val()){
					ShowErrorMsg(jQuery(this), '重复密码错误') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), '重复密码正确') ;
				}
			}) ;
		}
		
		//unique
		if(ret){
			form.find('input[unique=1]').each(function(){
				if(jQuery(this).attr('ajaxValue') != '1'){
					ShowErrorMsg(jQuery(this), '已存在') ;
					
					return false;
				}else{
					ShowRightMsg(jQuery(this), '正确') ;
				}
			}) ;
		}
		
		return ret ;
	}) ;
	
	function ShowErrorMsg(obj, msg){
		obj.next(remark_tag).html(' × '+msg) ;
		if(!jQuery.browser.msie){
			obj.focus() ;
		}
		ret = false ;
	}
	function ShowRightMsg(obj, msg){
		obj.next(remark_tag).html(' √ '+msg) ;
	}
	
	function isEmpty(value){
		var tmp = jQuery.trim(value) ;
		if( tmp == '' || tmp == '0'){
			return true ;
		}
	 
		return false ;
	}
	
	function isEmail(value){
		if( value.match( /^[A-z0-9-_\.]+@[A-z0-9-_\.]+$/i ) ){
			return true ;
		}
	
		return false ;
	}
	
	function isFile(value, extension){
		if(isEmpty(value) || value.match(RegExp("^[^\.]+.*\.+("+extension+")+$", "i"))){
			return true ;
		}
	
		return false ;
	}
	
	function isHttp(value){
		if( value.match( /^http:\/\/.*/i) ){
			return true ;
		}
	
		return false ;
	}
}
