/* JAVASCRIPT DOCUMENT: */
/* Copyright &copy; 2006 Stefano Fiori - STEO82
 * AUTHOR: Stefano Fiori - STEO82   (steo82.matrix@tiscali.it)
 * Use this code for FREE, but leave this message!
 */

var alert_msg_ita = "\t\t *** Attenzione ***\n\n * Per favore inserite dei valori validi nei campi segnalati:\n\n";
var alert_msg_eng = "\t\t *** Attention ***\n\n * Please insert valid value into marked fields:\n\n";
var _alert_msg_ita = "\t\t *** Attenzione ***\n\n * Per favore inserite dei valori validi nei campi segnalati:\n\n";
var _alert_msg_eng = "\t\t *** Attention ***\n\n * Please insert valid value into marked fields:\n\n";
var ok_msg_ita = "La sua prenotazione online è stata spedita con successo!\n   Grazie !";
var ok_msg_eng = "Your online reservation was successfully sent!\n   Thank You!";

var almenoTelOrEMail = true;
var obbligoPresenzaNote = false;

function check_and_send() {
	this.almenoTelOrEMail = false;  // vera inizializzazione ma serve così prima di premere INVIA per la prima volta!
	var finded_errors = false;

	var bool = this.checkName_and_Surname();
	if(!finded_errors)
		finded_errors = bool;

	bool = this.tel_or_mail();
	if(!finded_errors)
		finded_errors = !bool;

	this.checkAdulti_and_Bambini();
	if (this.obbligoPresenzaNote)
		if (!this.checkNote()) {
			finded_errors = true;
			this.alert_msg_ita = this.alert_msg_ita + "- " + "Inserite le informazioni riguardanti N° Adulti e/o N° Bambini" + ";\n";
			this.alert_msg_eng = this.alert_msg_eng + "- " + "Insert information about N° Adults and/or N° Children" + ";\n";
	}


	if (!finded_errors)
		document.getElementById("Form_Prenota").submit();

	this.allertaUtente(finded_errors);
	this.alert_msg_ita = this._alert_msg_ita;
	this.alert_msg_eng = this._alert_msg_eng;
}


function checkName_and_Surname() {
	var finded_errors = false;
	var input2Check = new Array(new String("Nome"), new String("Cognome"), new String("Name"), new String("Surname"));
	var obj = null;
	for ( var i in input2Check ) {
		obj = getObj(input2Check[i]);
		if ( obj != null && obj.value.length <= 1 ) {
			var style = new String();
			style = style.concat("background-color: red; font-size: large;");
			obj.setAttribute("style", style);
			obj.focus();
			obj.select();
			finded_errors = true;
			this.alert_msg_ita = this.alert_msg_ita + "- " + input2Check[i] + ";\n";
			this.alert_msg_eng = this.alert_msg_eng + "- " + input2Check[i] + ";\n";
		}
		else if ( obj != null )
			obj.removeAttribute("style");
	}
	return finded_errors;
}

function tel_or_mail() {
	var almeno1 = false;

	var style = new String();
	style = style.concat("background-color: red; font-size: large;");

	var obj = null;
	obj = getObj("Telefono");
	if ( obj != null && obj.value.length > 6 )
		almeno1 = true;
	else
		obj.setAttribute("style", style);

	obj = getObj("Email");
	if ( obj != null ) {
		if (obj.value.indexOf('.') != -1 && obj.value.indexOf('@') != -1 & obj.value.length > 8)
			almeno1 = true;
		else
			obj.setAttribute("style", style);
	}
	if (!almeno1) {
		this.alert_msg_ita = this.alert_msg_ita + "- " + "Inserite almeno un Telefono o una E-Mail per poterVi ricontattare" + ";\n";
		this.alert_msg_eng = this.alert_msg_eng + "- " + "Provide an E-Mail address or a Telephone number to enable Us to contact You" + ";\n";
	} else {
		removeStyle("Telefono");
		removeStyle("Email");
	}

	return almeno1;
}

function checkAdulti_and_Bambini() {
	var formObj = getObj("Form_Prenota");
	var list = formObj.Adulti.options;
	for (var i = 0; i < list.length; i++) {
		if ( list[i].selected && this.compare(">4", list[i].value) )
			this.obbligoPresenzaNote = true;
	}
	var list = formObj.Bambini.options;
	for (var i = 0; i < list.length; i++) {
		if ( list[i].selected && this.compare(">4", list[i].value) )
			this.obbligoPresenzaNote = true;
	}
}


function checkNote() {
	var bool = false;
	var formObj = getObj("Form_Prenota");
	var note = formObj.Note.value;
	if ( note.length > 5 )
		bool = true;
	return bool;
}

function getObj(obj_name)  {
	var obj = null;
	var obj = document.getElementById(obj_name);
	return obj;
}

function allertaUtente(errors) {
	var lang = document.getElementById("PAGE_LANG").value;
	if (errors) {
		if (this.compare(lang,"ITA"))
			alert(this.alert_msg_ita);
		else if (this.compare(lang,"ENG"))
			alert(this.alert_msg_eng);
	} else {
		if (this.compare(lang,"ITA"))
			alert(this.ok_msg_ita);
		else if (this.compare(lang,"ENG"))
			alert(this.ok_msg_eng);
	}
}

function compare(a, b) {
	var idem = true;
	
	if (a.length == b.length) {
		a = a.toUpperCase();
		b = b.toUpperCase();

		for(var i = 0; i < a.length && idem; i++) {  if ( a.charAt(i) != b.charAt(i) )  idem = false;  }

	} else idem = false;

	return idem;
}

function removeStyle(nameObj) {
	var obj = getObj(nameObj);
	if(obj != null && !this.almenoTelOrEMail)
		obj.removeAttribute("style");
}


/** Funzioni non più usate */
function autoCheckTelOrEMail() {
	var objT = null;
	var objEM = null;
	var style = "background-color: red; font-size: large";
	if(!this.almenoTelOrEMail) {
		objT = getObj("Telefono");
		objEM = getObj("Email");
		if(this.focusTel) {
			objT.setAttribute("style", style);
			objEM.removeAttribute("style");
			objT.select();
		}
		if(this.focusEMail) {
			objEM.setAttribute("style", style);
			objT.removeAttribute("style");
			objEM.select();
		}
	}
}
