var xmlHttp;
var boolVar;

gloubi ="";

function showEl(a) {
	document.getElementById(a).style.display = 'block';
}

function hideEl(a) {
	document.getElementById(a).style.display = 'none';
}

function dispos(e,f,g,h) { 
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  }
  gloubi = h;
  boolVar = g;
  if(h=='datesDep') { 
	if(!(isValidDate(document.roomForm.datesArr.value))) {
		alert("Incorrect date format in date of arrival field.");
		//document.getElementById('datesDep').style.display = 'none';
		return;
	}
  }
  //document.getElementById("datesArr").style.display = "block";
  var url="dispo-chambre.php";
  url=url+"?m="+e;
  url=url+"&y="+f;
  url=url+"&bv="+g;
  url=url+"&swi="+h;
  if(h == "datesDep")  url=url+"&dateArr="+document.roomForm.datesArr.value;
  xmlHttp.onreadystatechange=stateChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
  	//document.getElementById(gloubi).innerHTML = "<p style='text-align:center;'><img src='img/wait30.gif' alt='Loading...' /></p>";
}

function isValidDate(a) {
	result = false;
	if(a.length == 10) {
		//alert(a[2]);
		if(a.indexOf("/")) {
		//if(a[2] == "/" && a[5] == "/") {
			ubhdz = a[0]+a[1]+a[3]+a[4]+a[6]+a[7]+a[8]+a[9];
			if(isNumeric(ubhdz)) result = true;
		}
	}
	return result;
}

function validResa() {
	a = document.roomForm.datesArr.value;
	b = document.roomForm.datesDep.value;
	if(a != "" && b != "") {
		if(isValidDate(a) && isValidDate(b)) document.roomForm.submit();
		else alert("Please fill in both arrival & departure dates.");
	}
	else alert("Please fill in both arrival & departure dates.");
}

//Vérifie si une var est composée uniquement de chiffres
function isNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 // if(boolVar == 1) {
	  document.getElementById(gloubi).innerHTML=xmlHttp.responseText 
	  document.getElementById(gloubi).style.display = 'block';
  /*}
  else {
	  document.getElementById("datesArr").innerHTML=xmlHttp.responseText 
  }*/
 } 
}

function fillArr(a,b) {
	document.roomForm[b].value = a;
	if(b == 'datesArr') document.roomForm['datesDep'].value = "";
	document.getElementById(b).style.display = "none";
}

function verifFormBook() {
	isOk = 1;
	messErreur = "";
	//Dates
	if(document.formEnq.arrDate.value =="" || document.formEnq.depDate.value =="" ) {
		isOk = 0;
		messErreur = messErreur + "Please enter arrival & departure dates<br>";
	}
	//Details
	if(document.formEnq.name.value =="" || document.formEnq.phone.value =="" || document.formEnq.address.value =="" || document.formEnq.email.value =="" ) {
		isOk = 0;
		messErreur = messErreur + "Please enter full details<br>";
	}
	//Email
	var mailOk = 1;
	var a = document.formEnq.email.value;
	var invChars = new Array("#","$","%","&","/","^","[","!","#","$","%","&","'","*","+","=","?","^","`","{","|","}","~","]","$");
	for(i=0;i<invChars.length;i++) {
			if(a.indexOf(invChars[i]) != -1) {
				mailOk = 0;
				break;
			}
		}
		if (a.indexOf("@") == -1 || a.indexOf(".",a.indexOf("@")) == -1) {
			mailOk = 0;
		}
		else {
			arro = a.indexOf("@");
			dot = a.indexOf(".",arro);
			if (dot - arro < 5 || arro < 3) {
				mailOk = 0;
			}
			if (a.length - dot < 3) {
				mailOk = 0;
			}
		}
	if(mailOk == 0) {
		isOk = 0;
		messErreur = messErreur + "Please enter a valid email address<br>";
	}
	//Adultes
	if(document.formEnq.adSing.value ==0 && document.formEnq.adCoup.value ==0 ) {
		isOk = 0;
		messErreur = messErreur + "Please enter at least 1 adult<br>";
	}
	//Enfants
	if(document.formEnq.children.value ==1) {
		if(document.formEnq.noChildren.value =="" || document.formEnq.ageChildren.value =="" ) {
			isOk = 0;
			messErreur = messErreur + "Please specify number/age of children<br>";
		}
	}
	//Chiens
	if(document.formEnq.dogs.value ==1) {
		if(document.formEnq.noDogs.value =="") {
			isOk = 0;
			messErreur = messErreur + "Please specify number of pet dogs<br>";
		}
	}
	if(isOk == 1) {
		document.getElementById('erreurs').style.display = 'none';
		document.formEnq.submit();
		messErreur = "";
	}
	else {
		document.getElementById('erreurs').innerHTML = messErreur;
		document.getElementById('erreurs').style.display = 'block';
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

var jour1 = 1;
var mois1 = 1;
var annee1 = 1;
var jour2 = jour1 + 1;
var mois2 = mois2 + 1;
var annee2 = annee2 + 1;
var goDate1 = "";
var goDate2 = "";
isBusy = new Array();
cptBusy = 0;
isMonthPlus = 0;

function Recommencer(e) {
  if (e == 1) {
    for (i=1;i<=jour1;i++) {
      document.getElementById(i).style.opacity = 1;
      //document.getElementById(i).style.backgroundColor = '#ccc';
      document.getElementById('Date1').innerHTML = 'Arrival date'
      document.getElementById('Date1').style.opacity = 0.2;
      document.getElementById('CacheBouton').style.display = 'block';
    }
  }
  else {
    for (i=jour1+1;i<=jour2;i++) {
      document.getElementById(i).style.opacity = 1;
      document.getElementById(i).style.backgroundColor = '#ccc';
      document.getElementById('Date2').innerHTML = 'Departure date'
      document.getElementById('Date2').style.opacity = 0.2;
      document.getElementById('CacheBouton').style.display = 'block';
    }
  }
}

function fillDispos(e,f,g,h) {
  bOk = 1;
  idchambre = h;
  //Date d'arrivée
	if (document.getElementById('Date1').innerHTML == 'Arrival date') {
          if (document.getElementById(e).style.backgroundColor == 'rgb(255, 0, 0)') {
            bOk = 0;
            msgErreur = "Can only be chosen as a departure date";
          }
          if (document.getElementById('Date2').innerHTML != 'Departure date') {
            if (e < jour2 || f < mois2 || g < annee2) {
              bOk = 1;
            }
            else {
              bOk = 0;
            }
            for (i=e;i<jour2;i++) {
              if (document.getElementById(i).style.backgroundColor == 'rgb(255, 0, 0)') {
                bOk=0;
                isBusy[cptBusy] = i+"/"+f+"/"+g;
                cptBusy ++;
                msgErreur = "This room is not available during your whole stay.";
              }
            }
          }
          if (bOk == 1) {
            document.getElementById('Date1').style.opacity = 1;
            document.getElementById('Date1').innerHTML = e+'/'+f+'/'+g;
            for (i=1;i<e;i++) {
              document.getElementById(i).style.opacity = 0.2;
            }
            document.getElementById(e).style.backgroundColor = '#cccfff';
            jour1 = e;
            mois1 = f;
            annee1 = g;
            goDate1 = jour1+"/"+mois1+"/"+annee1;
              document.getElementById('toNextStep').innerHTML = "<a href='reservation-form.php?idchambre="+idchambre+"&Date1="+encodeURIComponent(goDate1)+"&Date2="+encodeURIComponent(goDate2)+"'>Confirm dates and go to reservation form</a>";
            if (document.getElementById('Date2').innerHTML != 'Departure date') {
              for (i=e;i<=jour2;i++) {
                document.getElementById(i).style.backgroundColor = '#cccfff';
              }
              document.getElementById('CacheBouton').style.display = 'none';
            }
          }
	}
  // Date de départ
	else {
          if (g < annee1) {
            bOk = 0;
            msgErreur = "Please choose an appropriate year";
          }
          if (e < jour1 || e == jour1) {
            if (f < mois1 || f == mois1) {
              bOk = 0;
              msgErreur = "Please choose an appropriate month";
            }
            else {
              isMonthPlus = 1;
            }
          }
          if (f < mois1 && g == annee1) {
            bOk = 0;
            msgErreur = "Please choose an appropriate month";
          }
          for (i=jour1;i<e;i++) {
            if (document.getElementById(i).style.backgroundColor == 'rgb(255, 0, 0)') {
              bOk=0;
              isBusy[cptBusy] = i+"/"+f+"/"+g;
              cptBusy ++;
              msgErreur = "This room is not available during your whole stay.";
            }
          }
          if (bOk == 1) {
            if (document.getElementById('Date2').innerHTML == 'Departure date') {
              document.getElementById('Date2').style.opacity = 1;
              document.getElementById('Date2').innerHTML = e+'/'+f+'/'+g;
              for (i=jour1;i<=e;i++) {
                document.getElementById(i).style.backgroundColor = '#cccfff';
              }
              document.getElementById('CacheBouton').style.display = 'none';
              jour2 = e;
              mois2 = f;
              annee2 = g;
              goDate2 = jour2+"/"+mois2+"/"+annee2;
              document.getElementById('toNextStep').innerHTML = "<a href='reservation-form.php?idchambre="+idchambre+"&Date1="+encodeURIComponent(goDate1)+"&Date2="+encodeURIComponent(goDate2)+"'>Confirm dates and go to reservation form</a>";
            }
          }
	}
    if(bOk == 0) {
      msg = "";
      for(i=0;i<isBusy.length;i++) {
        msg = msg + isBusy[i] + "<br>";
      }
      document.getElementById('Status').innerHTML = msgErreur + ""+ msg;
    }
}

function showRoomPic(a,b) {
	if(a == 'cooked-breakfast') x = 'cooked-breakfast';
	else if(a == 'cont-breakfast') x = 'cont-breakfast';
	else x = 'room-'+a+'-'+b;
	document.getElementById('showRoomPic').innerHTML = '<img src="img/'+x+'.jpg" />';
}

function changeRoomId(a) {
	if(a) {
		document.roomForm.datesArr.disabled = false;
		document.roomForm.datesDep.disabled = false;
	}
	else {
		document.roomForm.datesArr.disabled = true;
		document.roomForm.datesDep.disabled = true;
	}
	clearAll();
	document.roomForm.datesArr.value = "";
	document.roomForm.datesDep.value = "";
	//if(a == 6 || a == 7) {
		document.getElementById('addPers').style.display = "block";
		if(a == 3) document.roomForm.nbMax.value = 4;
		else document.roomForm.nbMax.value = 2;
	//}
	/*else {
		document.getElementById('addPers').style.display = "none";
		document.roomForm.nbAds.value = '2';
		document.roomForm.nbChi.value = '0';
		document.getElementById('nbPers').innerHTML = '<img src="img/pic-defgrey.gif" /><img src="img/pic-defgrey.gif" />';
	}*/
}

function addPers(x,y) {
	a = parseInt(document.roomForm.nbAds.value);
	b = parseInt(document.roomForm.nbChi.value);
	c = y;
	if(a+b < c) {
		document.roomForm[x].value++;
		document.getElementById('nbPers').innerHTML = document.getElementById('nbPers').innerHTML+"<img src='img/"+x+".gif' />";
	}
	else alert("You cannot add another person. Please choose the Forest room if you want accommodation more than 2 people.");	
}

function verifBas(a,b,c) {
	for(i=c;i<(document[a].length-b);i++) {
		if(document[a][i].value == "") {
			alert('Please fill in all mandatory fields marked with an *.');
			return false;
		}
	}
	return true;
}

function verifEmail(a) {
	if (a.indexOf("@") == -1 || a.indexOf(".",a.indexOf("@")) == -1) {
		return false;
	}
	else {
		arro = a.indexOf("@");
		dot = a.indexOf(".",arro);
		if (dot - arro < 5 || arro < 2) {
			return false;
		}
		if (a.length - dot < 2) {
			return false;
		}
		return true;
	}
}

function isRead() {
	if(document.bookForm.policy.value == 0) document.bookForm.policy.value = 1;
	else document.bookForm.policy.value = 0;
}

function confirmBooking() {
	if(document.bookForm.policy.value == 1) {
		if(verifBas('bookForm',11,0) && verifBas('bookForm',1,14)) {
			if(isNumeric(document.bookForm.cardNumber.value) && isNumeric(document.bookForm.cardSec.value) && (document.bookForm.cardSec.value.length==3) && (document.bookForm.cardNumber.value.length==16)) {
				if(verifEmail(document.bookForm.email.value)) {
					document.bookForm.submit();
				}
				else alert("Please enter valid email address");
			}
			else alert("Please insert correct card number and/or security code");
		}
	}
	else alert("Please agree to the terms and conditions");
}

function clearAll() {
	document.getElementById('nbPers').innerHTML = '<img src=\'img/nbAds.gif\' />';
	nbMax = 2;
	if(document.roomForm.roomId.selectedIndex == 3) nbMax = 4;
	document.roomForm.nbMax.value = nbMax;
	document.roomForm.nbAds.value = 1;
	document.roomForm.nbChi.value = 0;
}