/*
	Autore: Danilo Cicognani
	Script: richiesta.js
	Applicazione: Stella Cadente Vacanze
	Versione: 1.00
	Data: 08/07/2008
	Scopo: Funzionalita' di controllo del form di richiesta
	Copyright (c) 2008 CicoNET di Danilo Cicognani
*/
$('document').ready(function() {
	$('input[@title]').each(function() {
		var strTitle = $(this).attr('title');
		$(this).addClass('placeHolder').val(strTitle).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 = '';
		$('input.obbl').each(function() {
			var strTitle = $(this).attr('title');
			if (this.value == strTitle) {
				strMsg += '<li>Il campo ' + strTitle + ' &egrave; obbligatorio</li>';
				intErrors++;
			}
		});
		$('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) {
			$('<span class="obbl" id="submit-message">Per favore correggi gli errori seguenti:</span>').append('<ul>' 
			+ strMsg + '</ul>').insertBefore('#invia');
			return false;
		}
		else {
			$('input[@title]').each(function() {
				var strTitle = $(this).attr('title');
				if (this.value == strTitle)
					this.value = '';
			});
		}
	});
});
