//
//	Created by Luca Orio
//	Copyright (c) 2009 t-immagine. All rights reserved.
//	ver. 0.1
// 


//#################################################################################################//
//------------------------------------------------------------------------------------------------//
//
//	VALIDATION & AJAX FORM                                                                        //
//
//------------------------------------------------------------------------------------------------//
//#################################################################################################//


//----------------------------------------------------------------------- validazione form contacts

function contactsValidation() {
	
	$(".error").hide();

	$(".button").click(function() {
		
		$(".error").hide();
		
		checkForm();
		
		function checkForm(){
			
			var thereIsAnError = false;
			
			var nomecognome = $("input#nomecognome").val();
			if (nomecognome == "" || nomecognome == "Inserisci il tuo nome e cognome") {
				$("label#nomecognome_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var email = $("input#email").val();
			if (email == "" || email == "Inserisci la tua mail") {
				$("label#email_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var messaggio = $("textarea#messaggio").val();
			if (messaggio == "" || messaggio == "Inserisci il messaggio") {
				$("label#messaggio_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var privacy = $('#privacy').attr('checked')?1:0;
			if (privacy == 0) {
				$("label#privacy_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var societa = $("input#societa").val();
			if (societa == "Inserisci la societa\'") societa = "Nessun dato inserito";

			var citta = $("input#citta").val();
			if (citta == "Inserisci la tua citta\'") citta = "Nessun dato inserito";
			
			var telefono = $("input#telefono").val();
			if (telefono == "Inserisci il tuo numero di telefono") telefono = "Nessun dato inserito";
			
			if (thereIsAnError == true) return false;
			else sendForm(nomecognome, email, messaggio, societa, citta, telefono);
		}
		
		
		function sendForm(nomecognome, email, messaggio, societa, citta, telefono){
			
			var dataString = 'nomecognome=' + nomecognome + '&email=' + email + '&messaggio=' + messaggio + '&societa=' + societa + '&citta=' + citta + '&telefono=' + telefono;

			$.ajax({
				type: "POST",
				url: "sendmail/sendmail.php",
				data: dataString,
				success: function() {
					$('#contactsForm').html("<div id='message'></div>");
					$('#message').html("<span class='redText'>Richiesta contatti spedita!</span>")
					.append("<p>Il nostro staff provveder&agrave; a ricontattarti non appena possibile..</p>")
					.hide()
					.fadeIn(500, function() {
						$('#message').append("<img id='checkmark' src='images/check.png' />");
					});
				}
			});
			return false;
		}
	});


}


//----------------------------------------------------------------------- validazione form newsletter


function newsletterValidation() {
	
	$(".error").hide();

	$(".buttonNewsletter").click(function() {
		
		$(".error").hide();
		
		checkNewsletterForm();
		
		function checkNewsletterForm(){
			
			var thereIsAnErrorNewsletter = false;
			
			var nomecognomeNewsletter = $("input#nomecognomeNewsletter").val();
			if (nomecognomeNewsletter == "" || nomecognomeNewsletter == "Inserisci il tuo nome e cognome") {
				$("label.nomecognomeNewsletter_error").fadeIn(500);
				thereIsAnErrorNewsletter = true;
			}
			
			var emailNewsletter = $("input#emailNewsletter").val();
			if (emailNewsletter == "" || emailNewsletter == "Inserisci la tua mail") {
				$("label.emailNewsletter_error").fadeIn(500);
				thereIsAnErrorNewsletter = true;
			}
			
			if (thereIsAnErrorNewsletter == true) return false;
			else subscribeNewsletter(nomecognomeNewsletter, emailNewsletter);
		}
		
		
		function subscribeNewsletter(nomecognomeNewsletter, emailNewsletter){
			
			var dataString = 'name=' + nomecognomeNewsletter + '&email=' + emailNewsletter + '&p=25&nlbox[1]=105&funcml=add';

			$('#contactsForm').html("<div id='message'></div>");
			$('#message').html("<span class='redText'>Richiesta iscrizione spedita con successo!</span>")
			.append("<p>Grazie per esserti iscritto... a presto con nuove notizie!</p>")
			.hide()
			.fadeIn(500, function() {
				$('#message').append("<img id='checkmark' src='images/check.png' />");
			});

		
		//--------------------------------------------------------------da implementare con lo script di manessi 
		
			$.ajax({
				dataType: 'jsonp',
				url: "http://www.timmagine.com/12all-mailing/box.php",
				data: dataString
			});
			return false;
		}
	});
}


//----------------------------------------------------------------------- validazione form ecard


function ecardValidation() {
	
	
	
	$(".error").hide();

	$(".button").click(function() {
		
		$(".error").hide();
		
		checkFormEcard();
		
		function checkFormEcard(){
			
			var thereIsAnError = false;
			
			var nomecognomemitt = $("input#nomecognomemitt").val();
			if (nomecognomemitt == "" || nomecognomemitt == "Inserisci il tuo nome e cognome") {
				$("label#nomecognomemitt_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var nomecognomedest = $("input#nomecognomedest").val();
			if (nomecognomedest == "" || nomecognomedest == "Inserisci il nome del destinatario") {
				$("label#nomecognomedest_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var emailmitt = $("input#emailmitt").val();
			if (emailmitt == "" || emailmitt == "Inserisci la tua mail") {
				$("label#emailmitt_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var emaildest = $("input#emaildest").val();
			if (emaildest == "" || emaildest == "Inserisci la mail del destinatario") {
				$("label#emaildest_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var messaggio = $("textarea#messaggio").val();
			if (messaggio == "" || messaggio == "Inserisci un eventuale messaggio") messaggio = "";
			
			var privacy = $('#privacy').attr('checked')?1:0;
			if (privacy == 0) {
				$("label#privacy_error").fadeIn(500);
				thereIsAnError = true;
			}
			
			var imgUrl = $("input#imgUrl").val();
			
			if (thereIsAnError == true) return false;
			else sendFormEcard(nomecognomemitt, nomecognomedest, emailmitt, emaildest, messaggio, imgUrl);
		}
		
		
		function sendFormEcard(nomecognomemitt, nomecognomedest, emailmitt, emaildest, messaggio, imgUrl){
			
			//	passo in ajax
			var dataString = 'nomecognomemitt=' + nomecognomemitt + '&nomecognomedest=' + nomecognomedest +'&emailmitt=' + emailmitt + '&emaildest=' + emaildest + '&imgUrl=' + imgUrl + '&messaggio=' + messaggio;
			$.ajax({
				type: "POST",
				url: "sendmail/sendecard.php",
				data: dataString,
				success: function() {
					$('#contactsForm').html("<div id='message'></div>");
					$('#message').html("<br/><br/><span class='redText'>E-card spedita correttamente!</span>")
					.append("<p>Controlla la casella mail per visualizzarla in anteprima.</p>")
					.hide()
					.fadeIn(500, function() {
						$('#message').append("<img id='checkmark' src='images/check.png' />");
					});
				}
			});
			
			//	pucio l'address del mittente della ecard nella newsletter
			var dataString2 = 'name=' + nomecognomemitt + '&email=' + emailmitt + '&p=25&nlbox[1]=105&funcml=add';
			$.ajax({
				dataType: 'jsonp',
				url: "http://www.timmagine.com/12all-mailing/box.php",
				data: dataString2
			});
			
			return false;
		}
	});


}