/**
* jQuery plugin form validation.
* Version 1.0 
*/

jQuery.fn.checkform = function(options){

	
	var options = jQuery.extend({
		rules : {}, //object rules
		viewErrors : {
						required: {
							
						}
						
					}
	}, options);
	
	var objectMain = this;
	
	var counterErrors = 0;
	
	this.validate = function(){
	
		this.cleanErrors();
		
		jQuery.each(options.rules, function(field, fieldRules){
		
		objectField = jQuery('#'+field);
		
		jQuery.each(fieldRules, function(name, values){
			objectMain[name](objectField, values);
			
		});
		
	});
		result = (counterErrors>0) ? false : true;
		return result;
	}
	
	
	this.required = function(object, objectValues){
	
		
		if(!object.val() || object.val()==0)
		{
			this.getErrors(object, objectValues.message);
			counterErrors++;
		}
	
	}
	
	this.compareWithCurrentDate = function(object, objectValues){
		
			
		var date = new Date();
		currentDate = Date.parse(date);
		
	
		objectDate = object.val().split('.');
		changeDate = Date.parse(objectDate[1]+'/'+objectDate[0]+'/'+objectDate[2]);
		
		
		if(changeDate < currentDate)
		{
			
			this.getErrors(object, objectValues.message);
			counterErrors++;
		}
	}
	
	this.compareWithOlderDate = function(object, objectValues){
		
		objectDate = object.val().split('.');
		changeDate = Date.parse(objectDate[1]+'/'+objectDate[0]+'/'+objectDate[2]);
	
		objectPrev = jQuery('#'+objectValues.value).val().split('.');
	
		prevDate = Date.parse(objectPrev[1]+'/'+objectPrev[0]+'/'+objectPrev[2]);
		
	
		if(changeDate < prevDate)
		{
			this.getErrors(object, objectValues.message);
			counterErrors++;
		}
	}
	
	this.typeField = function(object, objectValues){
		
		switch (objectValues.value) {
  			case 'digit':
    			if(!(/^[0-9]+$/i).test(object.val()))
				{
					this.getErrors(object, objectValues.message);
					counterErrors++;
				}
    		break
			case 'email': 
				if(!(/^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}$/i).test(object.val()))
				{
					this.getErrors(object, objectValues.message);
					counterErrors++;
				}
			break
			case 'date':			
				if(!(/\d{2}\.\d{2}\.\d{4}/).test(object.val()))
				{
						this.getErrors(object, objectValues.message);
						counterErrors++;
				}
    		break			
		}
	}
	
	this.getErrors = function(object, message)
	{
		if(message && !object.prev('.errors').length )
		{
		
			object.closest('tr').find('td:last').append('<p class="errors">'+message+'</p>');
			
		}
	
		object.css('border-color', options.viewErrors.required.color);
		object.closest('tr').find('td:first').css('color', options.viewErrors.required.color);
		jQuery(options.viewErrors.required.block).html(options.viewErrors.required.message);
	}
	
	this.cleanErrors = function()
	{
		this.find('p.errors').remove();
		this.find('tr').find('td:first').css('color', 'black');
		this.find('tr').find('input, textarea').css('border-color', '#5C5C5C');
		countErrors = 0;
	}
	
	return this;
};
