
function validateForm(form, lang, printAlert)
{
	if (!document.forms) return false;
	var formObj = null;
	for (var i=0; i<document.forms.length; i++){
		if (document.forms[i]==form) formObj = form;
		else if (document.forms[i].name==form) formObj = document.forms[i];
	}
	var doAlert = validateForm.arguments.length==3?validateForm.arguments[2]:false;
	if (formObj==null) {
		if (confirm("Il modulo da validare non esiste in questa pagina\nI campi non possono essere controllati preventivamente\nInviare comunque i dati?")) return true;
		else return false;
	}
	var obligs = document.getElementById("obligatory")?document.getElementById("obligatory").value:null;
	if (obligs==null) {
		if (confirm("Non e' stato possibile controllare preventivamente i campi di questo modulo\nInviare comunque i dati?")) return true;
		else return false;
	}
	var res = true;
	var element, elementName, elementVal, elementType;
	for (var i=0; i<formObj.elements.length; i++){
		element = formObj.elements[i];
		element.style.backgroundColor = "";// ripristina lo sfondo di default per tutti
		if (element.type=="checkbox"){
			element.parentNode.style.backgroundColor = "";
		}
	}
    document.getElementById("error_1").style.visibility = 'hidden';
	document.getElementById("error_2").style.visibility = 'hidden';
	document.getElementById("error_3").style.visibility = 'hidden';
	document.getElementById("error_4").style.visibility = 'hidden';
	document.getElementById("error_5").style.visibility = 'hidden';

	for (var i=0; i<formObj.elements.length; i++){
		element = formObj.elements[i];
		elementName = element.name!=null?element.name:element.id;
		elementType = (element.type==null || element.type=="")?"text":element.type;
		if (elementType!="checkbox")
		   elementVal = element.value!=null?element.value:element.innerHTML;
		
		if (obligs.indexOf(elementName+",")!=-1){// il campo è censito come obbligatorio 
			if (elementType.indexOf("text")!=-1 || elementType.indexOf("select")!=-1 || elementType=="password" || elementType=="file"){
				if (elementVal=="") { res = false; break; }
			}else if (elementType=="checkbox"){
				if (element.checked==false) { res = false; break; }
			}
		}
	}
	if (!res) { 
		element.style.backgroundColor = "#cc0000"; 
		if (elementType=="checkbox"){
			element.parentNode.style.backgroundColor = "#cc0000";
		}
	}
	if (doAlert && !res) {
	     document.getElementById("error_1").style.visibility = 'visible';
         if (lang == 'it') { 
            alert("Per cortesia inserire tutte le informazioni richieste. Grazie!");
	      }
         else { 
            alert("Please fill all mandatory fields before sending the form. Thank you!");
	     }
     }

	if (res) {
		
		if (!document.visibile.email.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\..{2,2}))$)\b/gi)) {
		document.getElementById("error_2").style.visibility = 'visible';
		document.visibile.email.style.backgroundColor = "#cc0000"; 
		return false;
		}
	    
		if (document.visibile.checkin_y.value > document.visibile.checkout_y.value){
			document.getElementById("error_3").style.visibility = 'visible';
			res=false;
		}
		else if (document.visibile.checkin_y.value == document.visibile.checkout_y.value){
			 if (document.visibile.checkin_m.value > document.visibile.checkout_m.value){
				document.getElementById("error_4").style.visibility = 'visible';
				res=false;
			 }
			 else if (document.visibile.checkin_m.value == document.visibile.checkout_m.value){
				if (document.visibile.checkin_d.value > document.visibile.checkout_d.value){
					document.getElementById("error_5").style.visibility = 'visible';
					res=false;
				}
			 }
		}

	}
	document.sendform.nome.value = document.visibile.nome.value;
	document.sendform.cognome.value = document.visibile.cognome.value;
	document.sendform.realname.value = document.visibile.nome.value + " " + document.visibile.cognome.value;
	document.sendform.email.value = document.visibile.email.value;
	document.sendform.telefono.value = document.visibile.telefono.value;
	document.sendform.stato.value = document.visibile.stato.value;
	document.sendform.checkin_date.value = document.visibile.checkin_d.value+"/"+document.visibile.checkin_m.value+"/"+document.visibile.checkin_y.value;
	document.sendform.checkout_date.value = document.visibile.checkout_d.value+"/"+document.visibile.checkout_m.value+"/"+document.visibile.checkout_y.value;
	document.sendform.adulti.value = document.visibile.adulti.value;
	document.sendform.bambini_6_12.value = document.visibile.bambini_6_12.value;
	document.sendform.bambini_0_5.value = document.visibile.bambini_0_5.value;
	document.sendform.camere.value =  "1) Tipo camera: "+document.visibile.tipologia.value+"; Numero camere: "+document.visibile.quantity.value+"\n"+
		"\t2) Tipo camera: "+document.visibile.tipologia2.value+"; Numero camere: "+document.visibile.quantity2.value+"\n"+
		"\t3) Tipo camera: "+document.visibile.tipologia3.value+"; Numero camere: "+document.visibile.quantity3.value+"\n";
	document.sendform.informazioni_aggiuntive.value = document.visibile.informazioni_aggiuntive.value;
	
	if (res)
	  document.sendform.submit();

	return res;
}// --- validateForm


function mail() {
	var dominio = "canziane.it";
	var utente = "info";
	var destinatario = utente + "@" + dominio;
	var indirizzo = "mailto:" + destinatario;
	document.write(destinatario.link(indirizzo));
}

// Funzione di cancellazione messaggi di errore
function erase( form ) {
    if (!document.forms) return false;
	var formObj = null;
	for (var i=0; i<document.forms.length; i++){
		if (document.forms[i]==form) formObj = form;
		else if (document.forms[i].name==form) formObj = document.forms[i];
	}
	for (var i=0; i<formObj.elements.length; i++){
		element = formObj.elements[i];
		element.style.backgroundColor = "";// ripristina lo sfondo di default per tutti
		if (element.type=="checkbox"){
			element.parentNode.style.backgroundColor = "";
		}
	}
	document.getElementById("error_1").style.visibility = 'hidden';
	document.getElementById("error_2").style.visibility = 'hidden';
	document.getElementById("error_3").style.visibility = 'hidden';
	document.getElementById("error_4").style.visibility = 'hidden';
	document.getElementById("error_5").style.visibility = 'hidden';
}