
function convertMonthInLetters(sqldate)
{
tabtomorrow = splitEnTroislaDate(sqldate);
//alert("duchangeen entier :"+ tabtomorrow[0]  + "|" + MonTableau[tabtomorrow[1]-1] + tabtomorrow[2]  +"\n");
return MonTableau[tabtomorrow[1]-1];
}


var tab=new Array();
var tabIndexDate = new Array();
var tabIndexPrice = new Array(); 
var toto = new Array();
var tabfromdate = new Array();
var tabtodate = new Array();
var curcode = "";

function resetField() {
  <!-- Reset des input champs -->
    if (document.TabAvailForm.fromdateSQL.value != "") {
      document.getElementById(document.TabAvailForm.fromdateSQL.value).style.backgroundColor='';
      document.getElementById(document.TabAvailForm.fromdateSQL.value).style.color='';
      document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("div").item(0).style.color = '';
      if (document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("span")!="" && document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("span").length>0)
	document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("span").item(0).style.color = '';
    }
  if (document.TabAvailForm.todateSQL.value != "")
    {
      document.getElementById(document.TabAvailForm.todateSQL.value).style.backgroundColor='';
      document.getElementById(document.TabAvailForm.todateSQL.value).style.color='';
      document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("div").item(0).style.color = '';
      if (document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("span")!="" && document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("span").length>0)
	document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("span").item(0).style.color = '';
    }
		  
  document.TabAvailForm.fromdateSQL.value = "";
  document.TabAvailForm.fromdate.value = "";                        
  document.getElementById("begindate").innerHTML = "";                    
  document.getElementById("total").innerHTML = "";
		  
  document.TabAvailForm.todateSQL.value = "";
  document.TabAvailForm.todate.value = "";                        
  document.getElementById("enddate").innerHTML = "";
}
        
function validDate(fromdate,todate,dateaffiche) {
  for(i=0; i<tab.length;i++){
    if(tab[i] > "#"+fromdate+"#" && tab[i] < "#"+todate+"#") {        
      alert(textvalidDateImpResa);            
      fromdate = document.TabAvailForm.fromdate.value;
      document.getElementById("errormsg").innerHTML = errDateClosed.replace("{fromdate}", "["+fromdate+"]").replace("{todate}", "["+dateaffiche+"]");
      resetField();
      document.TabAvailForm.choix.value = 0;    
      return false;
    }
  }
  fillPrice(fromdate,todate);
  tabfromdate = splitEnTroislaDate(fromdate);
  tabtodate = splitEnTroislaDate(todate);
                
  return true;
}
            
function interditDate(fromdate) {
  // cas où l'utilisateur saisit une date fermée
  for(i=0; i<tab.length;i++){
    if("#"+fromdate+"#" == tab[i]) {
      alert(textvalidDateInterdit);
      resetField();
      return true;
    }
  }
  return false;
}
            
function splitEnTroislaDate(fromdate){
  toto = fromdate.split('-');
  return toto;
}
			
function constructUrl(httphost,langcode,hotelid,addParameter){
  
  url = 'http://'+httphost+'/reservit/reserhotel.php?hotelid='+hotelid+'&lang='+langcode+'&curcode='+curcode+'&fday='+tabfromdate[2]+'&fmonth='+tabfromdate[1]+'&fyear='+tabfromdate[0]+'&tday='+tabtodate[2]+'&tmonth='+tabtodate[1]+'&tyear='+tabtodate[0]+addParameter;

  if (window.opener)
    {
      window.opener.location = url;
      window.close();
    }
  else if (window.parent.document)
    {
      window.parent.document.location = url;
    }
  else
    {
      window.location = url;
    }


}
            
function fillPrice(fromdate,todate){
  iFirstDate = 0;
  iLastDate = 0;
  price = 0;
  for (i=0; i<=tabIndexDate.length; i++) {
    if (tabIndexDate[i] == "#"+fromdate+"#") { iFirstDate = i; }
    if (tabIndexDate[i] == "#"+todate+"#") { iLastDate = i; }
  }
  for(j=iFirstDate; j< iLastDate;j++) {
    price += tabIndexPrice[j]
      }
                
  document.TabAvailForm.prix.value = FormatString(price)+" "+curcode;
  document.getElementById("total").innerHTML = FormatString(price)+" "+curcode;
}
            
function recup(elem, dateaffiche , sqldate )
{

  document.getElementById("errormsg").innerHTML = "";

  if(
     document.TabAvailForm.fromdateSQL.value != "" &&
     document.TabAvailForm.choix.value == 1 &&
     sqldate < document.TabAvailForm.fromdateSQL.value
     ) {
    document.TabAvailForm.choix.value = 0;
//	alert("type de choix0 :"+document.TabAvailForm.choix.value+"\n");
  }
                  
  if(
     document.TabAvailForm.todateSQL.value != "" &&
     document.TabAvailForm.choix.value == 0 &&
     sqldate > document.TabAvailForm.todateSQL.value
     ) {
    document.TabAvailForm.choix.value = 1;
//	alert("type de choix1 :"+document.TabAvailForm.choix.value+"\n");
  }


                  
  // Cas de modification du todate => Choix = 1
  if(document.TabAvailForm.choix.value == 1) {
    // 1/ on sort si le futur todate = fromdate
    // 2/ on sort si les dates sont pas compatibles
    if (
	sqldate == document.TabAvailForm.fromdateSQL.value  ||
	!validDate(document.TabAvailForm.fromdateSQL.value,sqldate,dateaffiche)
	)
      return;
                  
    // déselection le précédent todate
    if (document.TabAvailForm.todateSQL.value != "") {
      document.getElementById(document.TabAvailForm.todateSQL.value).style.backgroundColor='';
      document.getElementById(document.TabAvailForm.todateSQL.value).style.color='';
      document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("div").item(0).style.color = '';
      if (document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("span")!="" && document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("span").length>0)
	document.getElementById(document.TabAvailForm.todateSQL.value).getElementsByTagName("span").item(0).style.color = '';
    }
    
    // Color configuration
    var bgcolor = '#AAA8BD';
    var color = 'white';
    
    if (elem != null)
    {
		if (document.getElementById("hotelid").innerHTML == "10")
		{
			bgcolor='#B19999';
			color='white';
		}
		else if (document.getElementById("hotelid").innerHTML == "4243")
		{
			bgcolor='#DBD5D5';
			color='black';
		}
		else if (document.getElementById("hotelid").innerHTML == "203")
		 {
			bgcolor='#988F86';
			color='black';
		 }
		else if (document.getElementById("custid").innerHTML == "158")
		 {
			bgcolor='#008FD6';
			color='white';
		 }
		
		elem.style.backgroundColor=bgcolor;
	    elem.style.color=color;
	    elem.getElementsByTagName("div").item(0).style.color = color;
	    if (elem.getElementsByTagName("span")!="" && elem.getElementsByTagName("span").length>0)
	      elem.getElementsByTagName("span").item(0).style.color = color;
    }

    // Fill duration
    fillDuration(document.TabAvailForm.fromdateSQL.value,sqldate,bgcolor,color);
    
    document.TabAvailForm.todateSQL.value = sqldate;
    document.TabAvailForm.todate.value = dateaffiche;                        
    document.TabAvailForm.choix.value = 0;    
    document.getElementById("enddate").innerHTML = dateaffiche; 
  }
  // Cas de modification du fromdate => Choix = 0
  else 
  {
    if (
	!interditDate(sqldate) &&
	(
	 (document.TabAvailForm.fromdateSQL.value == "")  
	 ||
	 (
	  document.TabAvailForm.fromdateSQL.value != "" &&
	  document.TabAvailForm.todateSQL.value != "" &&
	  validDate(sqldate,document.TabAvailForm.todateSQL.value,null)
	  )
	 ||
	 !(document.TabAvailForm.fromdateSQL.value < sqldate)
	 )
	) {
      // déselection le précédent fromdate
    if (document.TabAvailForm.fromdateSQL.value != "") 
	{
	  document.getElementById(document.TabAvailForm.fromdateSQL.value).style.backgroundColor='';
	  document.getElementById(document.TabAvailForm.fromdateSQL.value).style.color='';
	  document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("div").item(0).style.color = '';
	  if (document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("span")!="" && document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("span").length>0)
	    document.getElementById(document.TabAvailForm.fromdateSQL.value).getElementsByTagName("span").item(0).style.color = '';
	}
                    
      // Color configuration
      var bgcolor = '#AAA8BD';
      var color = 'white';
      
      if (elem != null)
      {
  		if (document.getElementById("hotelid").innerHTML == "10")
  		{
  			bgcolor='#B19999';
  			color='white';
  		}
  		else if (document.getElementById("hotelid").innerHTML == "4243")
  		{
  			bgcolor='#DBD5D5';
  			color='black';
  		}
  		else if (document.getElementById("hotelid").innerHTML == "203")
  		 {
  			bgcolor='#988F86';
  			color='black';
  		 }
  		
  		elem.style.backgroundColor=bgcolor;
  	    elem.style.color=color;
  	    elem.getElementsByTagName("div").item(0).style.color = color;
  	    if (elem.getElementsByTagName("span")!="" && elem.getElementsByTagName("span").length>0)
  	      elem.getElementsByTagName("span").item(0).style.color = color;
      }
      
    fillDuration(sqldate,document.TabAvailForm.todateSQL.value,bgcolor,color);
      
    document.TabAvailForm.fromdate.value = dateaffiche;
    document.TabAvailForm.fromdateSQL.value = sqldate;
    document.TabAvailForm.choix.value = 1;        
    document.getElementById("begindate").innerHTML = dateaffiche;
//	alert("sqldate date séléctionné :"+sqldate +"\n");
	tabtomorrow = splitEnTroislaDate(sqldate);
	tomorrow = new Date(tabtomorrow[0], tabtomorrow[1]-1, tabtomorrow[2], 10, 0, 0);
	tomorrow.setTime(tomorrow.getTime()+(24*60*60*1000));
//alert("Steph = "+tomorrow.getTime().toLocaleString());
	
	monthTomorrow = tomorrow.getMonth()+1;
	sqlTomorrow = ((tomorrow.getYear() < 1900) ? 1900 + tomorrow.getYear() : tomorrow.getYear())+"-"+((monthTomorrow < 10) ? "0"+monthTomorrow : monthTomorrow)+"-"+((tomorrow.getDate() < 10) ? "0" + tomorrow.getDate() : tomorrow.getDate());
	sqlTomorrow2= ((tomorrow.getDate() < 10) ? "0" + tomorrow.getDate() : tomorrow.getDate())+" "+convertMonthInLetters(sqlTomorrow)+" "+((tomorrow.getYear() < 1900) ? 1900 + tomorrow.getYear() : tomorrow.getYear());

//alert("date ajoutée avec un jour "+sqlTomorrow+"\n");
//alert("trace sert pour l'affichage"+sqlTomorrow2+"\n");

	if(sqlTomorrow.length > 0 && document.getElementById(sqlTomorrow) != null)
   	recup(document.getElementById(sqlTomorrow), sqlTomorrow2, sqlTomorrow);
    }    
  }

  displayBkgButton();
}

// Function to fill tab Duration (color)
function fillDuration(fromdate,todate,bgcolor,color)
{
	// New dates
	var arrayFromDate = fromdate.split('-');
	var fromDate = new Date(arrayFromDate[0],arrayFromDate[1]-1,arrayFromDate[2],6,0,0);
	var arrayToDate = todate.split('-');
	var toDate = new Date(arrayToDate[0],arrayToDate[1]-1,arrayToDate[2],6,0,0);
	
	// Old dates
	var arrayOldToDate = (document.TabAvailForm.todateSQL.value).split('-');
	var oldToDate = new Date(arrayOldToDate[0],arrayOldToDate[1]-1,arrayOldToDate[2],6,0,0);
	var arrayOldFromDate = (document.TabAvailForm.fromdateSQL.value).split('-');
	var oldFromDate = new Date(arrayOldFromDate[0],arrayOldFromDate[1]-1,arrayOldFromDate[2],6,0,0);
	
	// Remove color from previous fromdate
	if (oldFromDate.getTime() < fromDate.getTime())
	{
		while (oldFromDate.getTime() < fromDate.getTime())
		{
			var CurDay = oldFromDate.getDate();
		    var CurMonth = oldFromDate.getMonth();
		    var CurYear = y2k(oldFromDate.getYear()); 
		    CurMonth = CurMonth +1;
	
		    if (CurMonth<10) CurMonth = "0"+CurMonth;
		    if (CurDay<10) CurDay = "0"+CurDay;
		    
			var strCurrentDate = CurYear + '-' + CurMonth + '-' + CurDay;
			if (document.getElementById(strCurrentDate))
			{
				var elemDate = document.getElementById(strCurrentDate);
				elemDate.style.backgroundColor='';
				elemDate.style.color='';
				elemDate.getElementsByTagName("div").item(0).style.color = '';
				 if (elemDate.getElementsByTagName("span")!="" && elemDate.getElementsByTagName("span").length>0)
					  elemDate.getElementsByTagName("span").item(0).style.color = '';
			}
			oldFromDate = new Date(oldFromDate.getTime() + 24*60*60*1000);
		}
	}
	
	// Remove color from previous todate
	if (oldToDate.getTime() > toDate.getTime())
	{
		while (oldToDate.getTime() > toDate.getTime())
		{
			var CurDay = oldToDate.getDate();
		    var CurMonth = oldToDate.getMonth();
		    var CurYear = y2k(oldToDate.getYear()); 
		    CurMonth = CurMonth +1;
		    
		    if (CurMonth<10) CurMonth = "0"+CurMonth;
		    if (CurDay<10) CurDay = "0"+CurDay;
		    
			var strCurrentDate = CurYear + '-' + CurMonth + '-' + CurDay;
			if (document.getElementById(strCurrentDate))
			{
				var elemDate = document.getElementById(strCurrentDate);
				elemDate.style.backgroundColor='';
				elemDate.style.color='';
				elemDate.getElementsByTagName("div").item(0).style.color = '';
				 if (elemDate.getElementsByTagName("span")!="" && elemDate.getElementsByTagName("span").length>0)
					  elemDate.getElementsByTagName("span").item(0).style.color = '';
			}
			oldToDate = new Date(oldToDate.getTime() - 24*60*60*1000);
		}
	}
	
	// Color new dates
	while (fromDate.getTime() < toDate.getTime())
	{
		var CurDay = fromDate.getDate();
	    var CurMonth = fromDate.getMonth();
	    var CurYear = y2k(fromDate.getYear()); 
	    CurMonth = CurMonth +1;
	    
	    if (CurMonth<10) CurMonth = "0"+CurMonth;
	    if (CurDay<10) CurDay = "0"+CurDay;
	    
		var strCurrentDate = CurYear + '-' + CurMonth + '-' + CurDay;
		if (document.getElementById(strCurrentDate))
		{
			var elemDate = document.getElementById(strCurrentDate);
			elemDate.style.backgroundColor=bgcolor;
			elemDate.style.color=color;
			elemDate.getElementsByTagName("div").item(0).style.color = color;
		    if (elemDate.getElementsByTagName("span")!="" && elemDate.getElementsByTagName("span").length>0)
		    	elemDate.getElementsByTagName("span").item(0).style.color = color;
		}
		fromDate = new Date(fromDate.getTime() + 24*60*60*1000);
	}
}


            
function ajax_parseJs(inputObj)
{
  var jsTags = inputObj.getElementsByTagName('SCRIPT');
  for(var no=0;no<jsTags.length;no++){
    eval(jsTags[no].innerHTML);
  }
}

function displayBkgButton()
{
  if (document.getElementById("begindate") && document.getElementById("begindate").innerHTML != "" && 
      document.getElementById("enddate") && document.getElementById("enddate").innerHTML != "")
    {
      document.getElementById("buttons").style.display = 'block';
    }


}

