// JavaScript Document
var blnDone = false;

function init() {
   // quit if this function has already been called
   if (blnDone) return;

   // flag this function so we don't do the same thing twice
   blnDone = true;
	    
   // Gestion du formulaire de réservation
   // Français
   var objFormReservation = document.getElementById("frmReservation");
   
   if (objFormReservation) {

	   objFormReservation.onsubmit = function () {

			if (validateFormReservation(this, "fr")) {
				return true;
			}
			else {
				return false;	
			};	
		}
   } 
   
   // Anglais
   var objFormReservation = document.getElementById("frmReservationEn");
   
   if (objFormReservation) {

	   objFormReservation.onsubmit = function () {

			if (validateFormReservation(this, "en")) {
				return true;
			}
			else {
				return false;	
			};	
		}
   } 
   
};


/* Ce bout de code est ajout? afin d'appeler init seulement lorsque le DOM est charg? compl?tement */
/* for Mozilla */
if (document.addEventListener) {
   document.addEventListener("DOMContentLoaded", init, null);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
   document.write("<script defer src=ie_onload.js><"+"/script>");
/*@end @*/

/* for other browsers */
window.onload = init;

function validateFormReservation(form, strLangue) {
	
	var blnValide = true;
	
	var strLabel = "";

	var listInput = form.getElementsByTagName("input");
	var listSelect = form.getElementsByTagName("select");
	var listLabel = form.getElementsByTagName("label");
		
	// Tous les champs text sont obligatoires
	for (i = 0; blnValide && i < listInput.length;i++) {
		if (listInput[i].type == "text" && (listInput[i].title.indexOf("Obligatoire") > 0 || listInput[i].title.indexOf("Required") > 0))  {
			if (listInput[i].value == "") {
				
				for (y = 0; y < listLabel.length; y++) {
					if (listLabel[y].htmlFor == listInput[i].id) {
						if (strLangue == "fr") {
						alert("Le champ " + listLabel[y].innerHTML + " est obligatoire.");	
					} else {
						alert("The field " + listLabel[y].innerHTML + " is required.");	
					}
					
					listInput[i].focus();
					
					}
				}
				blnValide = false;
			}
		}
	}
	
	// Tous les champs textarea sont obligatoires

	for (i = 0; blnValide && i < listSelect.length;i++) {		
		if (listSelect[i].value == "0" || listSelect[i].value == "") {
			
			for (y = 0; y < listLabel.length; y++) {
				if (listLabel[y].htmlFor == listSelect[i].id) {
					if (strLangue == "fr") {
						alert("Le champ " + listLabel[y].innerHTML + " est obligatoire.");	
					} else {
						alert("The field " + listLabel[y].innerHTML + " is required.");	
					}
				}
				
				listSelect[i].focus();
			}
			blnValide = false;
		}
		
	}
	
	return blnValide;
}
