// JavaScript Document
/**
  *	Form Validation - Cartel Internet & Marketing
  *
  * (c) 2011 Raymond Veldhuizen
  *
  **/
  
  
(function($) {
	$.fn.cValidation = function(options) {
		var optionObj = $.extend({}, $.fn.cValidation.defaults, options);		
		var form 				= this;
		
		var url 				= optionObj.action;
		var formData 			= '';
		var inputFields 		= form.find('input[type=hidden], input[type=text], input[type=radio]:checked, input[type=checkbox], select, textarea');
		
		var __init = function() {
			form.find('input[type=button]').click(function() {
				inputFields.each(function(i)
				{
					/* If checkbox is not checked... give it an empty value */
					(checkCheckbox(this) == false) ? inputValue = '' : inputValue = this.value;
					(this.alt == undefined) ? this.alt = this.title : null;
					
					formData = formData+"name["+i+"]="+this.name+"&inputValue["+i+"]="+inputValue+"&validate["+i+"]="+this.alt+"&";
				});	
				
				$.ajax({
					type: 'POST',
					url: url,
					data: formData,
					dataType: 'json',
					cache: false,
					success: function(data) {
						if(data.status == 'true')
						{
							form.slideUp();
							$('#formSucces').html(data.message);
						} else {
							/* Clear all error-classes (needed for re-check) */
							inputFields.each(function(i)
							{
								(optionObj.highlightErrors) ? $('input[name="'+this.name+'"]').removeClass('error') : null;
								(optionObj.highlightErrors) ? $('textarea[name="'+this.name+'"]').removeClass('error') : null;
								(optionObj.showErrorMessage) ? $('span[title="'+this.name+'"]').html('') : null;
								(optionObj.showErrorMessage) ? $('span[title="'+this.name+'"]').removeClass('clearError') : null;
								(optionObj.showErrorMessage) ? $('span[title="'+this.name+'"]').addClass('clear') : null;
								/* For checkboxes and select fields, we always used text error instead of highlights, so clear them always */
								var selectField = $('select[name="'+this.name+'"]').attr('class');
								(this.type == 'checkbox' || selectField == 'formSelect') ? $('span[title="'+this.name+'"]').html('') : null;
							});
							/* Add error-classes from recieved data */
							$.each(data,function(i) {
								if(i.substring(0,6) == 'error_')
								{
									var field = i.substring(6);
									(optionObj.highlightErrors) ? $('input[name="'+field+'"]').addClass('error') : null;
									(optionObj.highlightErrors) ? $('textarea[name="'+field+'"]').addClass('error') : null;
									(optionObj.showErrorMessage) ? $('span[title="'+field+'"]').removeClass('clear') : null;
									(optionObj.showErrorMessage) ? $('span[title="'+field+'"]').addClass('clearError') : null;
									(optionObj.showErrorMessage) ? $('span[title="'+field+'"]').html(data[i]) : null;
									/* For checkboxes and select fields, show always an text error instead of highlights */
									var type = $('input[name="'+field+'"]').attr('type');
									var selectField = $('select[name="'+field+'"]').attr('class');
									(type == 'checkbox' || selectField == 'formSelect') ? $('span[title="'+field+'"]').html(data[i]) : null;
								}
							});
							/* Set default form message */
							$('#formMessage').html(data.message);
						}
					}
				});c
			});
		};
		
		var checkCheckbox = function(object) {
			if(object.type == 'checkbox' && object.alt == 'validateCheckbox')
			{
				if(!object.checked) 
				{
					return false;	
				} else {
					return object.value;	
				}
			}				
			return null;
		};
		
		__init();
		return this;
	};
})(jQuery);

$.fn.cValidation.defaults = {
	showErrorMessage	: false,
	highlightErrors		: true
};
