// formValidate v.2 - 2010
// cody by luca garbin
// www.ritzwebmaster.com

/* funzionamento
jQuery(document).ready(function($){
	$('#contact').formValidate({
		campi : new Array(
					new Array('#nome','Nome','Nome: campo obbligatorio','text',true), // tipi campi: text, email, privacy
					new Array('#mail','Email','Email: campo obbligatorio','email',true),
					new Array('#msg','Messaggio','Messaggio: campo obbligatorio','text',true),
					new Array('#privacy','Privacy','Autorizzare il trattamento dei dati','privacy',true)
				),
		textIn : true, // campi di testo con il nome dentro
		alertt: new Array(true,''), // new Array(false,'#idBoxPerTesto')
		ajax: false,
		ajaxResponse: ''
	})
})


*/

(function($){  
	$.fn.formValidate = function(options) {  
	
		  var defaults = {  
			campi : new Array(),
			textIn : false,
			alertt: new Array(true,''), // alert o insert txt
			ajax : false,
			ajaxURL :'',
			ajaxResponse : ''
		}

 		var options = $.extend(defaults, options);  
	
		return this.each(function(){
			var obj = $(this);  
			var alertt = options.alertt
			var campi = options.campi
			var _textIn = options.textIn
	
			if(!alertt[0])
				$(alertt[1]).css('animate',0)
			
			// copio le variabili passate
	
			// funzione per controllo email
			function isValidEmailAddress(emailAddress)
			{
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				return pattern.test(emailAddress);
			}
			
			if(_textIn) {
				$(this).find('input, textarea').not('input[type=submit]').each(function(){
					$(this).focus(function(){
					if($(this).val()==this.defaultValue)
						$(this).val('')
					else if($(this).val()!=this.defaultValue)
						$(this).select()
					})
			
					$(this).blur(function() {
						if ($.trim(this.value) == ''){
							this.value = (this.defaultValue ? this.defaultValue : '');
						}
					})
				})
			}
	
			// azioni sui campi
	
			obj.submit(function(){
				/******* controllo campi ******/
				for(i=0;i<campi.length; i++) {
					if($(campi[i][4])) {
						var current = $(campi[i][0])
						var currentVal = current.val()
		
						switch(campi[i][3]) {
							case 'email':
								if(currentVal=='' || (currentVal==campi[i][1] && _textIn) || !isValidEmailAddress(currentVal)) {
									
									if(alertt[0])
										alert(campi[i][2])
									else
										$(alertt[1]).html(campi[i][2]).animate({'opacity':1})
		
									return false
								}
							break;
							
							case 'privacy':
								if(!$(campi[i][0]).attr('checked')) {
									if(alertt[0])
										alert(campi[i][2])
									else
										$(alertt[1]).html(campi[i][2]).animate({'opacity':1})
		
								return false
								}
							break;
		
							case 'text':
								if(currentVal=='' || (currentVal==campi[i][1] && _textIn)) {
									if(alertt[0])
										alert(campi[i][2])
									else
										$(alertt[1]).html(campi[i][2]).animate({'opacity':1})
		
									return false
								}
							break;
						}
					}
				}
				
				if(!alertt[0])
					$(alertt[1]).html('')
			
				// invio tramite ajax
				if(options.ajax){
					// INIZIO AJAX
					var url = options.ajaxURL ? options.ajaxURL : obj.attr('action')
					// stringa
				
					var str = $(this).serialize()
					
					$.ajax({  
						type: 'POST',  
						url: options.ajaxURL,
						data: str, 
						success: function(response) { 
							$(options.ajaxResponse).html(response)
							$('#'+obj.attr('id'))[0].reset()
						},
						error: function(response){
							$(options.ajaxResponse).html('Si e\' verificato un errore. Riprovi piu\' tardi.')
						}
					})
	
					// FINE AJAX
					return false
				}else{
					return true
				}
			})
		})
	};
})(jQuery);  
