$.fn.validate = function(options)
{
	
	var $this 					= $(this);
	var defaults 				= { form : "formNews" , field : $this.attr("id") , key : "required" , error: "Enter value", inline:"0" } ;
	var custom_options  		= $.extend(defaults,options);
	var curr_form				= custom_options.form;
	var curr_field				= custom_options.field;
	var curr_value				= $this.val();
	var curr_key				= custom_options.key;
	var curr_error				= custom_options.error;
	var curr_inline				= custom_options.inline;
	var curr_error_div			= "div_err_"+curr_form+"_"+curr_field;
	var curr_error_field		= "hid_err_"+curr_form+"_"+curr_field;
	var checked_count 	  		= 0;
	var checked_started  		= 0;
	var curr_field_class    	= "";
	var curr_field_name			= $this.attr("name");
	
	if (curr_field_name	== null){
		curr_field_name	= "";
	}
	var curr_field_name_array   = curr_field_name.split("[");
														  
	if((curr_field_name_array[1])=="]" && checked_count==0) {													 
		curr_field_name        	= curr_field_name_array[0];
		curr_field_class		= curr_field_name+"_class";
		$('.'+curr_field_class).each(function() {
			checked_started=1;	
			if($(this).attr("checked"))	{
				checked_count++;
				curr_value = curr_value+$(this).val()+"~*";
			}
		});
		
		
	} 
	
	var curr_error_field_value 	= $("#"+curr_error_field).val();
	curr_value					= (checked_count==0 && checked_started>0)?"":curr_value;
	curr_value					= (curr_value==null)?"":curr_value;
	
	var dataArray 				= new Array();
	dataArray[0] 				= new Array();
	dataArray[0][0] 			= curr_form;
	dataArray[0][1] 			= curr_field;
	dataArray[0][2] 			= curr_value;
	dataArray[0][3] 			= curr_key;
	dataArray[0][4] 			= curr_error;
	
	var curr_key_array   = curr_key.split("~*");
	var curr_error_array = curr_error.split("~*");
	
	if(curr_key_array.length>1) 	{
		
		var curr_key_array_item_array_item_value  		= "";
		var curr_value  								= "";
		for(var ii=0;ii<curr_key_array.length;ii++)	{
			
			var curr_key_array_item 					= curr_key_array[ii];
			var curr_key_array_item_array 				= curr_key_array_item.split("->");
			var curr_key_array_item_array_item       	= (curr_key_array_item_array.length>1)?curr_key_array_item_array[0]:curr_key_array_item;
			var curr_key_array_item_array_item_name    	= (curr_key_array_item_array.length>1)?curr_key_array_item_array[0]:curr_field;
			var curr_key_array_item_type 				= $("#"+curr_key_array_item_array_item_name).attr("type");
			var curr_key_array_item_array_item_value 	= $("#"+curr_key_array_item_array_item_name).val();
			if(curr_key_array_item_type=="checkbox" ||  curr_key_array_item_type=="radio")
			{
				var curr_key_array_item_type_checked 	= $("#"+curr_key_array_item_array_item_name).attr("checked");
				if(curr_key_array_item_type_checked != true) {
					curr_key_array_item_array_item_value = "";
				}
				
				
				if(curr_key_array_item_type=="radio") {
					
					curr_key_array_item_type_checked = $('input[name='+curr_key_array_item_array_item_name+']:checked', '#'+curr_form+'').val();
					curr_key_array_item_array_item_value = curr_key_array_item_type_checked;
				}
				
				
				
					if(curr_key_array_item_array.length==3)
					{
						if(curr_field!=$this.attr("id")) {
							if(curr_error_field_value=="") {
								return true;
							}
						
						}
						
					}
			}
			
			
			var curr_key_array_item_array_key        	= (curr_key_array_item_array.length>1)?curr_key_array_item_array[1]:curr_key_array_item;
			curr_value									= curr_value+curr_key_array_item_array_item_value+"~*";
		}
		
	} else {
		
		 if(checked_count==0) {
			 var curr_type = $(this).attr("type");
			 if(curr_type=="checkbox") {
				var curr_checked 	= $(this).attr("checked");
					if(curr_checked != true) {
						curr_value = "";
					}
			 }
		 }
	}
	dataArray[0][2] 	= curr_value;
	$.ajax({
		   
		  url: GLOBAL_BASE_PATH+"/validate/",
		  type: "POST",
		  cache: false,
		  async: false,
		  global: false,
		  data: { ajax_request: dataArray },
		  success: function(data) {
			 
			  if(data=="") {
				 // $("#"+curr_error_div).hide();
				  $("#"+curr_error_div).addClass("hide");
				  var curr_height= $("#"+curr_error_div).height();
				  $("#"+curr_error_field).val(0);
			  } else {
				 
				  $("#"+curr_error_div).html(data+'<span></span>');
				  //$("#"+curr_error_div).show();
				  if(curr_inline==0) {
				 	 $("#"+curr_error_div).removeClass("hide");
				  }
				   $("#"+curr_error_field).val(1);
				  $('#'+curr_form+'_errors').val(1);
			  }
		  }
		  
	});
	
};



$.fn.validateForm = function(options)
{
	
	
	 var defaults 			= { form : "formNews" } ;
	 var custom_options  	= $.extend(defaults,options);
	 var curr_form			= custom_options.form;
	 var curr_form_result	= 0;
	 var curr_field_value   = "";
	 
	 
	 
	 $('.hid_err_'+curr_form+'_validations').each(function() {
			curr_field_value  = $(this).val();	
			
			
			if(parseInt(curr_field_value)==1)
			{
				var curr_error_div  = $(this).attr("rel");	
				//$('#'+curr_error_div).show();
				$("#"+curr_error_div).removeClass("hide");
			}
			
			if(parseInt(curr_field_value)==1 && curr_form_result==0) {
				curr_form_result=1;
			}
													   
	 });
	 
	 if(curr_form_result==0) {
		 $('#'+curr_form+'_errors').val(0);
		 return true;
	 }
	 return false;
}

function validateReset(curr_form,curr_field)
{
	var dataArray 			= new Array();
	dataArray[0] 			= curr_form;
	dataArray[1]	 		= curr_field;
	var curr_error_div		= "div_err_"+curr_form+"_"+curr_field;
	var curr_error_field	= "hid_err_"+curr_form+"_"+curr_field;
	
	$.ajax({
		  url: GLOBAL_BASE_PATH+"/validate/destroy/",
		  type: "POST",
		  cache: false,
		  async: false,
		  global: false,
		  data: { ajax_request: dataArray },
		  success: function(data) {
			  $("#"+curr_error_div).addClass("hide");
			  $("#"+curr_error_field).val(0);
		  }
		  
	});
}


