// Fonction d'ouverture des pop-up de plans
function openPlan(url){
	var myOpenWinPlan
	myOpenWinPlan = window.open(url,'Plan_APPI_Hotel','width=600,height=600,personalbar=no,toolbar=no,menubar=no,location=no,scrollbars=no,resize=no');
	myOpenWinPlan.focus();
}
/* //////////////////////////
Gestion des messages d'alerte
//////////////////////////// */
function alerteMsg(codeAlerte) {

	switch(codeAlerte) {
		// Formulaire reservation : resa
		case 'noRoom' :
			alert("Please select a room");
				switch(this.GET['lang']) {
				}
			break;
		case 'toMuchBreakfasts' :
			alert("The number of breafasts is higher than number of people");
			break;
		case 'wrongBreakfastNbr' :
			alert("The number of breafasts is not correct");
			break;
		case 'voidDate' :
			alert("Select a date");
			break;
		case 'negativDate' :
			alert("Date 1 is higher than date 2");
			break;
		case 'nullDate' :
			alert("You selected O day");
			break;
		// Formulaire reservation : coordonnées
		case 'noName' :
			alert("You must indicate your name");
			break;
		case 'noSurname' :
			alert("You must indicate your surname");
			break;
		case 'noMail' :
			alert("You must indicate your e-mail adress");
			break;
		case 'wrongMail' :
			alert("Your e-mail adress seems to be wrong");
			break;
		case 'retypeMail' :
			alert("To avoid typing errors, you must write down two times the same e-mail adress in the two e-mail fields.");
			break;
		case 'missTypeMail' :
			alert("To avoid typing errors, e-mail adresses typed in the two e-mail fields must be the same. Please check it");
			break;
		case 'emptyMsg' :
			return confirm("Your message is empty. Send it anyway ?");
			break;
		// Selection de date
	}
	return false;
}
/* ////////////////////////// */

/* //////////////////////////
Fonctions pour le calcul du cout et de la durée de la réservation
//////////////////////////// */
function init(){
//  window.document.calc.dannee.value=new Date().getFullYear();
//  window.document.calc.fannee.value=new Date().getFullYear();
}

function calcul(){

	// Tarifs de chambres (et petit dej) par catégorie de chambre
	var tj1
	tj1 = 30;
	var tj2
	tj2 = 40;
	var tj3
	tj3 = 50;
	var tj4
	tj4 = 65;
	var tj5
	tj5 = 80;
	var tjbk
	tjbk = 6;

	// Récupération du nombre de chambres par catégories
	nj1=window.document.calc.categ1_nbr.value;
	nj2=window.document.calc.categ2_nbr.value;
	nj3=window.document.calc.categ3_nbr.value;
	nj4=window.document.calc.categ4_nbr.value;
	nj5=window.document.calc.categ5_nbr.value;
	if(nj1+nj2+nj3+nj4+nj5==0) {
	  return alerteMsg("noRoom");
	}

	// Récupération du nombre total de petit dej / jour
	nbk=window.document.calc.breakfast_nbr.value;
	np=1.0*nj1+2.0*nj2+2.0*nj3+3.0*nj4+4.0*nj5;
	
	if(nbk == "") {
	  nbk = 0;
	  window.document.calc.breakfast_nbr.value = "0";
	}
	if(nbk != parseInt(nbk) || parseInt(nbk)<0) {
	  window.document.calc.breakfast_nbr.focus();
	  return alerteMsg("wrongBreakfastNbr");
	}
	nbk == parseInt(nbk);
	if(np<nbk) {
	  window.document.calc.breakfast_nbr.focus();
	  return alerteMsg("toMuchBreakfasts");
	}
  
	// Calcul de la durée du séjour
	nbj=calcDuree('date1','date2');
	if (nbj == "void") {
	  window.document.calc.duree.value="";
	  window.document.calc.montant.value="";	
	  return alerteMsg("voidDate");
	} else if (nbj == "neg") {
	  window.document.calc.duree.value="";
	  window.document.calc.montant.value="";
	  return alerteMsg("negativDate");
	} else if (nbj == "nul") {
	  window.document.calc.duree.value="";
	  window.document.calc.montant.value="";
	  return alerteMsg("nullDate");
	} else {
	  cout=nbj*(tj1*nj1+tj2*nj2+tj3*nj3+tj4*nj4+tj5*nj5+nbk*tjbk);
	  window.document.calc.duree.value=nbj;
	  window.document.calc.montant.value=cout;
	  }
  
}
/* //////////////////////////
Gestion des dates
//////////////////////////// */

function selectDate1(){
	window.dateField = document.calc.date1;
	calendar = window.open('ut_jav12b_new.html','cal','WIDTH=300,HEIGHT=300,top=80,left=500');
}
function selectDate2(){
	window.dateField = document.calc.date2;
	calendar = window.open('ut_jav12b_new.html','cal','WIDTH=300,HEIGHT=300,top=80,left=500');
}

function formatDate(chaine) {
  var ctl = /^(\d\d?)\/(\d\d?)\/(\d\d)(\d\d)?$/;
  var tab = ctl.exec(chaine);
  if (tab.length) {
     this.jour = tab[1];
     this.mois = tab[2] - 1;
     if (tab.length > 4) this.an = (tab[4]) ? tab[3] + tab[4] : 2000 + eval(tab[3]);
     else this.an = 2000 + eval(tab[3]);
     this.OK = true;
  }
  else this.OK = false;
}

function nombreJours(autreDate) {
  if (this.OK) {
     var date1 = new Date(this.an, this.mois, this.jour);
     if (autreDate.OK)
        var date2 = new Date(autreDate.an, autreDate.mois, autreDate.jour);
     else return "erreur";
  }
  else return "erreur";
  if (date1.getTime() < date2.getTime())
//     return Math.floor((date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24);
     return Math.round((date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24);
  else if (date1.getTime() > date2.getTime()) {
//     return Math.round((date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24);
	return "neg"
  }
  else if (date1.getTime() == date2.getTime()) {
//     return Math.round((date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24);
	return "nul"
  }
}

function nombreJoursOld(autreDate) {
  if (this.OK) {
     var date1 = new Date(this.an, this.mois, this.jour);
     if (autreDate.OK)
        var date2 = new Date(autreDate.an, autreDate.mois, autreDate.jour);
     else return "erreur";
  }
  else return "erreur";

nbjdate2=date2.getTime() / 1000 / 60 / 60 / 24;
nbjdate1=date1.getTime() / 1000 / 60 / 60 / 24;

if (date1.getTime() < date2.getTime())
//     return Math.floor(  nbjdate2 - nbjdate1 );
     return Math.round(  nbjdate2 - nbjdate1 );
  else
     // ce else, cas où date2 est antérieure à date1, ne sert à rien
     return "erreur date posterieure";
}




formatDate.prototype.nombreJours = nombreJours;

function calcDuree(case1, case2){
  if ((document.calc[case1].value == "") || (document.calc[case2].value == ""))
		return "void";
  else {
     d1 = new formatDate(document.calc[case1].value);
     d2 = new formatDate(document.calc[case2].value);
	 return d1.nombreJours(d2);
  }
}
/* ////////////////////////// */




/* //////////////////////////
Coordonnées du formulaire
//////////////////////////// */

initialValues = new Array();
initialValues["nom"] = "";
initialValues["prenom"]= "";
initialValues["email"] = "Please write your e-mail adress";
initialValues["email2"]= "Please write your e-mail adress again";

function empty(o){ 
	if (o.value == initialValues[o.name]) {
		o.value = "";
		}
}

function verif(){
	// empty nom
	if (document.calc.nom.value=="") {
	   document.calc.nom.focus();
	   return alerteMsg("noName");
	   }
	 
	// empty prenom
	if (document.calc.prenom.value=="") {
	   document.calc.prenom.focus();
	   return alerteMsg("noSurname");
	   }
	 	   	   
	 // mauvais email
	 if (document.calc.email.value=="" || document.calc.email.value==initialValues["email"]) {
	   document.forms[0].email.focus();
	   return alerteMsg("noMail");
	   } else {// malformed email 
	  			v=document.calc.email.value;				
				if(!(v.indexOf('@')>0 && v.indexOf('@')<v.length-2 && v.lastIndexOf('.')>v.indexOf('@')+1 && v.lastIndexOf('.')<v.length-2)) {
			    	document.calc.email.focus();
			   		return alerteMsg("wrongMail");
			   }
			   if (document.calc.email2.value=="" || document.calc.email2.value==initialValues["email2"]) {
			    	document.calc.email2.focus();
			   		return alerteMsg("retypeMail");
			   }
			   if (document.calc.email.value!=document.calc.email2.value) {
			    	document.calc.email2.focus();
			   		return alerteMsg("missTypeMail");
			   }
		}
	   
	// empty message
	if (document.calc.comment.value=="") {
		document.calc.comment.focus();
		return alerteMsg("emptyMsg");
	}
	 	   	   
	document.calc.sendok.value=="envoye";
}


/* ////////////////////////// */






