/*##########################################################
 ## 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.";
var msg_indirizzo_it = "Il campo Indirizzo &egrave; obbligatorio.";
var msg_cap_it = "Il campo Cap &egrave; obbligatorio.";
var msg_provincia_it = "Il campo provincia &egrave; obbligatorio.";
var msg_stato_it = "Il campo Stato &egrave; obbligatorio.";
var msg_richieste_it = "Il campo Richieste &egrave; obbligatorio.";


// 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.";
var msg_indirizzo_en = "The Address field is obligatory";
var msg_cap_en = "The Cap field is obligatory";
var msg_provincia_en = "The  Province field is obligatory";
var msg_stato_en = "The city is obligatory";
var msg_richieste_en = "The Request field is obligatory";

// 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.";
var msg_indirizzo_de = "Der Addresse ist ein Pflichtfeld.";
var msg_cap_de = "Der PLZ ist ein Pflichtfeld.";
var msg_provincia_de = "Der Land ist ein Pflichtfeld.";
var msg_stato_de = "Der Stadt ist ein Pflichtfeld.";
var msg_richieste_de = "Der Anfrage ist ein Pflichtfeld.";

// 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.";
var msg_indirizzo_fr = "Le champ Addresse est obligatoire.";
var msg_cap_fr = "Le champ Cp est obligatoire.";
var msg_provincia_fr = "Le champ Pays est obligatoire.";
var msg_stato_fr = "Le champ Ville est obligatoire.";
var msg_richieste_fr = "Le champ Demande est obligatoire.";

// CROATO:

var msg_nome_cr = "The Name field is obligatory";
var msg_cognome_cr= "The Surname field is obligatory";
var msg_telefono_cr = "The Telephone field is obligatory and must be written in numbers";
var msg_email_cr = "The E-mail field is obligatory and must contain valid letters ";
var msg_N_Adulti_cr = "The Number of Adults field  is obligatory and must contain a number between 0 and 4.";
var msg_N_Bambini_cr = "The Number of Children field  is obligatory and must contain a number between 0 and 4.";
var msg_dtd_cr = "The Arrival Date field is obligatory";
var msg_dta_cr = "The Departure Date field is obligatory";
var msg_Trattamento_cr = "The Services field is obligatory";
var msg_data_contatto_cr = "Il campo Data Contatto &egrave; obbligatorio.";
var msg_ora_contatto_cr = "Il campo Ora Contatto &egrave; obbligatorio.";
var msg_nome_contatto_cr = "Il campo Nome Contatto &egrave; obbligatorio.";
var msg_telefono_contatto_cr = "Il campo telefono Contatto &egrave; obbligatorio e deve esere numerico.";
var msg_email_contatto_cr = "Il campo Email Contatto &egrave; obbligatorio, e deve contenere caratteri validi.";
var msg_indirizzo_cr = "The Address field is obligatory";
var msg_cap_cr = "The Cap field is obligatory";
var msg_provincia_cr = "The  Province field is obligatory";
var msg_stato_cr = "The city is obligatory";
var msg_richieste_cr = "The Request field is obligatory";

// 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 == 'indirizzo2')
			return  eval("msg_indirizzo_" + lingua);
		if (object.id == 'cap')
			return  eval("msg_cap_" + lingua);
		if (object.id == 'provincia')
			return  eval("msg_provincia_" + lingua);
		if (object.id == 'stato')
			return  eval("msg_stato_" + lingua);
		if (object.id == 'richieste')
			return  eval("msg_richieste_" + lingua);
		if (object.id == 'email')
			return  eval("msg_email_" + 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 indirizzo = formTag.indirizzo;
var cap = formTag.cap;
var provincia = formTag.provincia;
var stato = formTag.stato;
var richieste = formTag.richieste;
var email = formTag.email;

//var Trattamento = formTag.Trattamento;


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

// Controllo campi
errore = (!validate(nome)) || errore;
errore = (!validate(cognome)) || errore;
errore = (!validate(indirizzo)) || errore;
errore = (!validate(cap)) || errore;
errore = (!validate(provincia)) || errore;
errore = (!validate(stato)) || errore;
errore = (!validate(richieste)) || errore;





errore = (!validateEmail(email)) || errore;
//errore = (!validate(dta)) || errore;
//errore = (!validate(dtd)) || 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 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 = (!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>");
	}
}
