/*
	Autore: Danilo Cicognani
	Script: richiesta.js
	Applicazione: www.fieraesagradeigusti.it
	Versione: 2.00
	Data: 08/06/2011
	Scopo: Funzionalita' di controllo del form di richiesta
	Copyright (c) 2009-2011 CicoNET di Danilo Cicognani
*/
$('document').ready(function() {
	$('input[title]').each(function() {
		var strTitle = $(this).attr('title');
		if (this.value == '')
			$(this).addClass('placeHolder').val(strTitle);
		$(this).focus(function() {
			strTitle = $(this).attr('title');
			if (this.value == strTitle)
				$(this).removeClass('placeHolder').val('');
		}).blur(function() {
			strTitle = $(this).attr('title');
			if (this.value == '')
				$(this).addClass('placeHolder').val(strTitle);
		});
	});
	$('textarea[title]').each(function() {
		var strTitle = $(this).attr('title');
		if (this.value == '')
			$(this).addClass('placeHolder').val(strTitle);
		$(this).focus(function() {
			strTitle = $(this).attr('title');
			if (this.value == strTitle)
				$(this).removeClass('placeHolder').val('');
		}).blur(function() {
			strTitle = $(this).attr('title');
			if (this.value == '')
				$(this).addClass('placeHolder').val(strTitle);
		});
	});
	$('span#conditional').hide();
	$('span.obbl2').text('(*)');
	$('input.conditional').each(function() {
		var $thisInput = $(this);
		var $thisFlag = $('span#text-' + $thisInput.attr('id'));
		if (! this.checked)
			$thisFlag.hide();
		else
			$('input[name = "' + $thisInput.attr('id') + '"]').addClass('obbl');
		$thisInput.click(function() {
			$('span.conditional').hide();
			$('input.obbl2').removeClass('obbl');
			$thisFlag.show();
			$('input[name = "' + $thisInput.attr('id') + '"]').addClass('obbl');
		});
	});
	$('form').submit(function() {
		$('.submit-message').remove();
		intErrors = 0;
		strMsg = '';
		$(this).find('input.obbl').each(function() {
			var strTitle = $(this).attr('title');
			if (this.value == strTitle) {
				strMsg += '<li>Il campo ' + strTitle + ' &egrave; obbligatorio</li>';
				intErrors++;
			}
		});
		$(this).find('input.int').each(function() {
			var strTitle = $(this).attr('title');
			if ((this.value != strTitle) && ($.trim(this.value).length > 0) && ((this.value != parseInt(this.value)) || (parseInt(this.value) <= 0))) {
				strMsg += '<li>Il campo ' + strTitle + ' deve essere un numero maggiore di 0</li>';
				intErrors++;
			}
		});
		$(this).find('input#accettare').each(function() {
			if (! this.checked) {
				strMsg += '<li>Impossibile inviare la richiesta senza ACCETTARE l\'uso e il trattamento dei dati personali</li>';
				intErrors++;
			}
		});
		if (intErrors > 0) {
			$(this).parent().find('.invia').before('<div class="submit-message">Per favore correggi gli errori seguenti:' + '<ul>' + strMsg + '</ul></div>');
			return false;
		}
		else {
			$('input[title]').each(function() {
				var strTitle = $(this).attr('title');
				if (this.value == strTitle)
					this.value = '';
			});
		}
	});
});
