/*##########################################################
 ## Creato da:    Nicola Gerotto                          ##
 ## Copyright By: Nicola Gerotto                          ##
 ## Email:        nicola.gerotto@gmail.com                ##
 ## Anno:         2006/2007                               ##
 ## Controllo e gestione istantaneo nei campi di un Form  ##
 ###########################################################
*/

// PERSONALIZZAZIONE MESSAGGI DI ERRORE IN BASE ALLA LINGUA. //

// ITALIANO :

var msg_nome_it = "Il campo Nome &egrave; obbligatorio.";
var msg_cognome_it	= "Il campo Cognome &egrave; obbligatorio.";
var msg_telefono_it = "Il campo Telefono &egrave; deve essere numerico";
var msg_email_it = "Il campo Email &egrave; obbligatorio, e deve contenere caratteri validi.";
var msg_N_Adulti_it = "Il campo Numero Adulti &egrave; obbligatorio e numerico compreso tra 0 e 4.";
var msg_N_Bambini_it = "Il campo Numero Bambini &egrave; obbligatorio e numerico compreso tra 0 e 4.";
var msg_dtd_it = "Il campo Data di arrivo &egrave; obbligatorio.";
var msg_dta_it = "Il campo Data di partenza &egrave; obbligatorio.";
var msg_Trattamento_it = "Il campo Trattamento &egrave; obbligatorio.";
var msg_data_contatto_it = "Il campo Data Contatto &egrave; obbligatorio.";
var msg_ora_contatto_it = "Il campo Ora Contatto &egrave; obbligatorio.";
var msg_nome_contatto_it = "Il campo Nome Contatto &egrave; obbligatorio.";
var msg_telefono_contatto_it = "Il campo telefono Contatto &egrave; obbligatorio e deve esere numerico.";
var msg_email_contatto_it = "Il campo Email Contatto &egrave; obbligatorio, e deve contenere caratteri validi.";

// INGLESE :

var msg_nome_en = "The Name field is obligatory";
var msg_cognome_en= "The Surname field is obligatory";
var msg_telefono_en = "The Telephone field is obligatory and must be written in numbers";
var msg_email_en = "The E-mail field is obligatory and must contain valid letters ";
var msg_N_Adulti_en = "The Number of Adults field  is obligatory and must contain a number between 0 and 4.";
var msg_N_Bambini_en = "The Number of Children field  is obligatory and must contain a number between 0 and 4.";
var msg_dtd_en = "The Arrival Date field is obligatory";
var msg_dta_en = "The Departure Date field is obligatory";
var msg_Trattamento_en = "The Services field is obligatory";
var msg_data_contatto_en = "Il campo Data Contatto &egrave; obbligatorio.";
var msg_ora_contatto_en = "Il campo Ora Contatto &egrave; obbligatorio.";
var msg_nome_contatto_en = "Il campo Nome Contatto &egrave; obbligatorio.";
var msg_telefono_contatto_en = "Il campo telefono Contatto &egrave; obbligatorio e deve esere numerico.";
var msg_email_contatto_en = "Il campo Email Contatto &egrave; obbligatorio, e deve contenere caratteri validi.";

// TEDESCO :

var msg_nome_de = "Der Name ist ein Pflichtfeld.";
var msg_cognome_de	= "Der Familienname ist ein Pflichtfeld.";
var msg_telefono_de = "Die Telefonnummer ist ein Pflichtfeld und muss numerisch eingegeben werden.";
var msg_email_de = "Die E-Mail-Adresse ist ein Pflichtfeld und muss gültige Zeichen enthalten.";
var msg_N_Adulti_de = "Die Anzahl der Erwachsenen muss ausgef&uuml;llt werden und erfolgt numerisch von 0 bis 4. ";
var msg_N_Bambini_de = "Die Anzahl der Kinder muss ausgef&uuml;llt werden und erfolgt numerisch von 0 bis 4. ";
var msg_dtd_de = "Der Ankuft ist ein Pflichtfeld.";
var msg_dta_de = "Der Abreise ist ein Pflichtfeld.";
var msg_Trattamento_de = "Der Verpflegung ist ein Pflichtfeld.";
var msg_data_contatto_de = "Il campo Data Contatto &egrave; obbligatorio.";
var msg_ora_contatto_de = "Il campo Ora Contatto &egrave; obbligatorio.";
var msg_nome_contatto_de = "Il campo Nome Contatto &egrave; obbligatorio.";
var msg_telefono_contatto_de = "Il campo telefono Contatto &egrave; obbligatorio e deve esere numerico.";
var msg_email_contatto_de = "Il campo Email Contatto &egrave; obbligatorio, e deve contenere caratteri validi.";

// FRANCESE :

var msg_nome_fr = "Le champ Nom est obligatoire.";
var msg_cognome_fr	= "Le champ Pr&eacute;nom est obligatoire.";
var msg_telefono_fr = "Le champ T&eacute;l&eacute;phone est obligatoire et doit &ecirc;tre num&eacute;rique.";
var msg_email_fr = "Le champ Email est obligatoire et doit contenir des caract&eacute;res valables.";
var msg_N_Adulti_fr = "Le champ Nombre Adultes est obligatoire et num&eacute;rique entre 0 et 4.";
var msg_N_Bambini_fr = "Le champ Nombre Enfants est obligatoire et num&eacute;rique entre 0 et 4.";
var msg_dtd_fr = "Le champ Arriv&eacute;e est obligatoire.";
var msg_dta_fr = "Le champ D&eacute;part est obligatoire.";
var msg_Trattamento_fr = "Le champ Conditions est obligatoire.";
var msg_data_contatto_fr = "Il campo Data Contatto &egrave; obbligatorio.";
var msg_ora_contatto_fr = "Il campo Ora Contatto &egrave; obbligatorio.";
var msg_nome_contatto_fr = "Il campo Nome Contatto &egrave; obbligatorio.";
var msg_telefono_contatto_fr = "Il campo telefono Contatto &egrave; obbligatorio e deve esere numerico.";
var msg_email_contatto_fr = "Il campo Email Contatto &egrave; obbligatorio, e deve contenere caratteri validi.";


// FINE PERSONALIZZAZIONE MESSAGGI DI ERRORE IN BASE ALLA LINGUA. //


// Funzione di controllo per i campi del form.
function messages(object){
		
	jQuery.noConflict();
	var lingua = jQuery('#lingua').val();
	
		if (object.id == 'nome')
			return eval("msg_nome_" + lingua); // ottengo la variabile msg_nome_it se sono in italiano;
		if (object.id == 'cognome')
			return eval("msg_cognome_" + lingua);
		if (object.id == 'telefono')
			return eval("msg_telefono_" + lingua);
		if (object.id == 'email')
			return  eval("msg_email_" + lingua);
		if (object.id == 'N_Adulti1' || object.id == 'N_Adulti2' || object.id == 'N_Adulti3' || object.id == 'N_Adulti4' || object.id == 'N_Adulti5')
			return  eval("msg_N_Adulti_" + lingua);
		if (object.id == 'N_Bambini1' || object.id == 'N_Bambini2' || object.id == 'N_Bambini3' || object.id == 'N_Bambini4' || object.id == 'N_Bambini5')
			return  eval("msg_N_Bambini_" + lingua);
		if (object.id == 'dtd')
			return  eval("msg_dtd_" + lingua);
		if (object.id == 'dta')
			return  eval("msg_dta_" + lingua);
		if (object.id == 'Trattamento')
			return  eval("msg_Trattamento_" + lingua);
			
		/* CALLBACK */
		
		if (object.id == 'data_contatto')
			return eval("msg_data_contatto_" + lingua);
		if (object.id == 'ora_contatto')
			return eval("msg_ora_contatto_" + lingua);
		if (object.id == 'nome_contatto')
			return eval("msg_nome_contatto_" + lingua);
		if (object.id == 'telefono_contatto')
			return eval("msg_telefono_contatto_" + lingua);
		if (object.id == 'email_contatto')
			return  eval("msg_email_contatto_" + lingua);	
			
}

function controllo(form){
var errore = false;
//Inizializzo le variabili
var formTag = document.getElementById(form);
if (form=='form'){
var nome = formTag.nome;
var cognome = formTag.cognome;
var telefono = formTag.telefono;
var email = formTag.email;
//var Trattamento = formTag.Trattamento;


var N_Adulti1 = formTag.N_Adulti1;
var N_Adulti2 = formTag.N_Adulti2; 
var N_Adulti3 = formTag.N_Adulti3; 
var N_Adulti4 = formTag.N_Adulti4; 
var N_Adulti5 = formTag.N_Adulti5;

var N_Bambini1 = formTag.N_Bambini1; 
var N_Bambini2 = formTag.N_Bambini2; 
var N_Bambini3 = formTag.N_Bambini3; 
var N_Bambini4 = formTag.N_Bambini4; 
var N_Bambini5 = formTag.N_Bambini5;

var dta = formTag.dta;
var dtd = formTag.dtd;
//var email = formTag.Email;
//var username = formTag.Username;
//var password = formTag.Password;

// Controllo campi
errore = (!validate(nome)) || errore;
errore = (!validate(cognome)) || errore;

if ((N_Adulti1 != 'undefined') && (N_Adulti1 != null) && (N_Adulti1 != '')){
errore = (!validatePattern(N_Adulti1,"(^[0-4]{1}$)")) || errore;
}
if ((N_Adulti2 != 'undefined') && (N_Adulti2 != null) && (N_Adulti1 != '')){
errore = (!validatePattern(N_Adulti2,"(^[0-4]{1}$)")) || errore;
}
if ((N_Adulti3 != 'undefined') && (N_Adulti3 != null) && (N_Adulti1 != '')){
errore = (!validatePattern(N_Adulti3,"(^[0-4]{1}$)")) || errore;
}
if ((N_Adulti4 != 'undefined') && (N_Adulti4 != null) && (N_Adulti1 != '')){
errore = (!validatePattern(N_Adulti4,"(^[0-4]{1}$)")) || errore;
}
if ((N_Adulti5 != 'undefined') && (N_Adulti5 != null) && (N_Adulti1 != '')){
errore = (!validatePattern(N_Adulti5,"(^[0-4]{1}$)")) || errore;
}

if ((N_Bambini1 != 'undefined') && (N_Bambini1 != null) && (N_Adulti1 != '')){
errore = (!validatePattern(N_Bambini1,"(^[0-4]{1}$)")) || errore;
}
if ((N_Bambini2 != 'undefined') && (N_Bambini2 != null) && (N_Bambini2 != '')){
errore = (!validatePattern(N_Bambini2,"(^[0-4]{1}$)")) || errore;
}
if ((N_Bambini3 != 'undefined') && (N_Bambini3 != null) && (N_Bambini3 != '')){
errore = (!validatePattern(N_Bambini3,"(^[0-4]{1}$)")) || errore;
}
if ((N_Bambini4 != 'undefined') && (N_Bambini4 != null) && (N_Bambini4 != '')){
errore = (!validatePattern(N_Bambini4,"(^[0-4]{1}$)")) || errore;
}
if ((N_Bambini5 != 'undefined') && (N_Bambini5 != null) && (N_Bambini5 != '')){
errore = (!validatePattern(N_Bambini5,"(^[0-4]{1}$)")) || errore;
}

errore = (!validateEmail(email)) || errore;
//errore = (!validate(dta)) || errore;
//errore = (!validate(dtd)) || errore;
errore = (!validatePattern(dta,"(^[0-9]{2}/[0-9]{2}/[0-9]{2}$)")) || errore;
errore = (!validatePattern(dtd,"(^[0-9]{2}/[0-9]{2}/[0-9]{2}$)")) || errore;
//errore = (!validatePattern(dtd, "(^(\d){2}/(\d){2}/(\d){2})")) || errore;
//errore = (!validate(Trattamento)) || errore;
}

else if (form=='formCallback'){
var data_contatto = formTag.data_contatto;
var ora_contatto = formTag.ora_contatto;
var nome_contatto = formTag.nome_contatto;
var telefono_contatto = formTag.telefono_contatto;
var email_contatto = formTag.email_contatto;

errore = (!validate(data_contatto)) || errore;
errore = (!validatePattern(ora_contatto,"(^[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$)")) || errore;
errore = (!validate(nome_contatto)) || errore;
errore = (!validateNumber(telefono_contatto)) || errore;
errore = (!validateEmail(email_contatto)) || errore;
}

// Se tutto va bene
if (!errore) {
   hideError();
   //formTag.action = "../form.php";        //?action=invio
   formTag.submit();
}
else{
	showError();
}
return errore;

}

//----------------------------------//

function nocolor(form){
	jQuery.noConflict();
    var formTag = document.getElementById(form);
    hideError();
	jQuery("#error_list").empty();
	for(var i=0;i<formTag.length;++i){
		var element = formTag.elements[i];
		if(element.type=="text" || element.type=="checkbox" || element.type=="radio" || 
		   element.type=="textarea" || element.type=="file" || element.type=="select"||
		   element.type=="password"){
		   element.className="default";
		    }
	}
}

function showError(){
	jQuery.noConflict();
	jQuery("#error").addClass("errorBox");
	jQuery("#error").show();
}

function hideError(){
	jQuery.noConflict();
	jQuery("#error").hide();
}
/*-------------------------------------------------------------------------*/
function validate(object){
	var error = false;
	if (object.value.length<1 || object.value==""){
		error = true;
		sbagliato(object);
	}else{
		giusto(object);		
	}
	return !error;
}

function validateNumber(object){
	var error = false;
	if (object.value.length<1 || isNaN(object.value) || (object.value == "") ){
		error = true;
		sbagliato(object);
	}else{
		giusto(object);
	}
	return !error;
}

function validateEmail(object){
	var expreg= new RegExp("(.+)@(.+)\\.(.+)");
	var error = false;
	if (object.value.length<1 || !expreg.test(object.value) ){
		error = true;
		sbagliato(object);
	}else{
		giusto(object);
	}
	return !error;
}

function validatePattern(idObj, pattern){
	var expreg= new RegExp(pattern);
	var error = false;
	//var val = $("#"+idObj).val();
	if (!expreg.test(idObj.value) ){
		error = true;
		sbagliato(idObj);
	}else{
		giusto(idObj);
	}
	return !error;
}

function validateNone(object){
	var error = false;
	object.className = "giusto";
	return !error;
}

function wait(object){
	object.className="wait";
}

function cleanError(){
	jQuery.noConflict();
	if (jQuery('#error_list').children().length==0){		
		jQuery("#error").hide();	
    }
}
	
function giusto(object){
	jQuery.noConflict();
	object.className = "giusto";
	jQuery("#error_"+object.id).remove();
	cleanError();
}
function sbagliato(object){
	jQuery.noConflict();
	object.className = "sbagliato";	
	showError();
    if ( jQuery('#error_list').children("li[@id='error_"+object.id+"']").length == 0 ){
	  jQuery('#error_list').append("<li class='liError' id='error_"+object.id+"'>" + messages(object) + "</li>");
	}
}