//
// global declarations and initialization
//

var htmlBuffer = '';
var FirstAvailDay = new Date(2011,11,13);
var LastAvailDay = new Date(2037,11,31);
var FirstDay = FirstAvailDay;
var LastDay = LastAvailDay;



function init () 
{
  theDate         = FirstDay;
  today           = FirstDay;
  currMonth       = today.getMonth();
  msPerDay        = 24*60*60*1000;

  dispMonth = currMonth;
  dispYear = GetTheYear(today);
  
  monthName     = new makeArray(12);
  monthName[1]  = "Janvier";
  monthName[2]  = "Février";
  monthName[3]  = "Mars";
  monthName[4]  = "Avril";
  monthName[5]  = "Mai";
  monthName[6]  = "Juin";
  monthName[7]  = "Juillet";
  monthName[8]  = "Août";
  monthName[9]  = "Septembre";
  monthName[10] = "Octobre";
  monthName[11] = "Novembre";
  monthName[12] = "Décembre";

}


//
// entry point
//



function OpenCalendar (dateFldname , formNumber) 
{

  if (dateFldname == 'begindate')
    {
      FirstDay = FirstAvailDay;
      LastDay = LastAvailDay;
    }
  else
    {
        setDate('DMY4');
	sBeginDate = String(eval("document.forms[" + formNumber + "].begindate.value")+"/");
//	sBeginDate = String(eval("document.forms[" + formNumber + "].elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value
debut=0;idx = sBeginDate.indexOf('/',debut);
numday=parseInt(sBeginDate.substr(debut,idx-debut),10);
debut=idx+1;
idx = sBeginDate.indexOf('/',debut);
nummonth=parseInt(sBeginDate.substr(debut,idx-debut),10);
debut=idx+1;
idx = sBeginDate.indexOf('/',debut);
numyear=parseInt(sBeginDate.substr(debut,idx-debut),10);
debut=idx+1;
      numday=numday+1;
      nummonth=nummonth-1;
      FirstDay = new Date(numyear,nummonth,numday);
      LastDay = LastAvailDay;
    }
 
     

  init();

  // champ de la page de depart
  formNum  = formNumber;
  dateFld  = dateFldname;

  windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=275,height=290";
  calendarWindow = this.open("","calendarWindow",windowOptions);

  calendarWindow.callingForm = this;

  redraw();
}

//
// repaint the calendar
//
function redraw() 
{
  calendarWindow.callingForm = this;
  if (dispYear < 1900)
    firstOfMonth  = new Date(dispYear+1900,dispMonth,1);
  else
    firstOfMonth  = new Date(dispYear,dispMonth,1);

  calendarWindow.document.open();
  calTitle = "\n";
  calendarWindow.document.write(calTitle);
  drawCalendar(firstOfMonth);
  calendarWindow.document.write(htmlBuffer);
  calendarWindow.document.close();
  calendarWindow.callingForm = this;
  calendarWindow.focus();
}


//
// fill the calling forms date and month
//
function fillDate(day, month, year) 
{
  eval("document.forms[" + formNum + "]." + dateFld  + ".value="+"''+day+'/'+month+'/'+year+''");
  eval("document.forms[" + formNum + "]." + dateFld  + "day"+".value=\""+day+"\"");
  eval("document.forms[" + formNum + "]." + dateFld  + "month"+".value=\""+month+"\"");
  eval("document.forms[" + formNum + "]." + dateFld  + "year"+".value=\""+year+"\"");

   day--;
   year -= 2011;
  
   eval("document.forms[" + formNum + "]." + dateFld  + "day.selectedIndex=\""+day+"\"");
   eval("document.forms[" + formNum + "]." + dateFld  + "month.selectedIndex=\""+month+"\"");
   eval("document.forms[" + formNum + "]." + dateFld  + "year.selectedIndex=\""+year+"\"");


   if (dateFld != "enddate")
     {
       updateEndDate(document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
		     document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
		     document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value,
		     document.availinfo.nbnight.value);
     }
   else
     {
       updateNbNight(
		     document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value,
		     document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value,
		     document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value,
		     document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
		     document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
		     document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value);
     }

  calendarWindow.close();
}


function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
	return true;
    } 
    else {
	return false;
    }
}

function updateNbNight(day1,month1,year1,day2,month2,year2){
    
    date1 = new Date(year1,month1-1,day1);
    date2 = new Date(year2,month2-1,day2);

     var difference =
         Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
       - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);

     diff = difference/1000/60/60/24;
     if(diff > 0) eval("document.availinfo.nbnight.value=\""+diff+"\"");
}

function setDate(dateformat){

    if(dateformat == 'DMY4'){
	
	eval("document.availinfo.begindate.value=\""+
	     document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value
+"/"+ 
	     document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value
+"/"+
	     document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value

	     +"\"");

	eval("document.availinfo.enddate.value=\""+
	     document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value
+"/"+ 
	     document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value
+"/"+
	     document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value

	     +"\"");
    }
    else{
	eval("document.availinfo.begindate.value=\""+
	     document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value
+"/"+
	     document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value
+"/"+ 
	     document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value
	     +"\"");
	
	eval("document.availinfo.enddate.value=\""+
	     document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value
+"/"+
	     document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value
+"/"+ 
	     document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value

	     +"\"");
    } 
    
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function updateYear(fieldmonth, fieldyear)
{
  month = document.availinfo.elements[fieldmonth].options[document.availinfo.elements[fieldmonth].selectedIndex].value;
  year = document.availinfo.elements[fieldyear].options[document.availinfo.elements[fieldyear].selectedIndex].value;

  if (fieldmonth == "fmonth")
    {
      if (month < parseInt('12',10) && year == parseInt('2011',10) ) 
	{
	  document.availinfo.elements[fieldyear].selectedIndex++;
	  
	  updateEndDate(document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
			document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
			document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value,
			document.availinfo.nbnight.value);
	  updateNbNight( document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value,
			 document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value,
			 document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value,
			 document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
			 document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
			 document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value);
	}
    }
}

function updateEndDate(myDay,myMonth,myYear,daysToAdd) {

  //savemyYear = myYear;
  //savemyYear = 
  savemyYear = document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value;

  myMonth = myMonth - 1;
    convertDate = new Date(myYear,myMonth,myDay,8,0,0);
    newDate = new Date(convertDate.getTime() + daysToAdd*24*60*60*1000);
    
    if (convertDate.getDay() == newDate.getDay() && 
	convertDate.getMonth() == newDate.getMonth() && 
	convertDate.getFullYear() == newDate.getFullYear())
      {
	newDate = new Date(convertDate.getTime() + ((daysToAdd*24)+1)*60*60*1000);
      }

    CurDay = newDate.getDate();
    CurMonth = newDate.getMonth();
    CurYear = y2k(newDate.getYear()); 

    CurMonth = CurMonth +1;

    if (CurMonth<10)
	CurMonth = "0"+CurMonth;
    if (CurDay<10)
	CurDay = "0"+CurDay;

    if ((CurDay > 0) && (CurMonth > 0) && (CurYear >0)) 
      {
	// DEV-#253: Todate = Fromdate + 1
	document.availinfo.elements['enddateday'].selectedIndex=(CurDay - 1);
	document.availinfo.elements['enddatemonth'].selectedIndex=(CurMonth-1);
	if (savemyYear != CurYear)
	  {
	    if (savemyYear <= CurYear)
	      document.availinfo.elements['enddateyear'].selectedIndex=(document.availinfo.elements['enddateyear'].selectedIndex +1);
	    else
	      
	      document.availinfo.elements['enddateyear'].selectedIndex=(document.availinfo.elements['enddateyear'].selectedIndex -1);
	  }
      }
}


//
// set the month
//
function changeMonth (increment) 
{
  nextMonth = dispMonth;
  nextYear = dispYear;

  if (increment == 1) 
    {
      nextMonth++ ;
      if (nextMonth>11)
	{
	  nextYear++;
	  nextMonth = 0;
	}
    } 
  else 
    {
      nextMonth-- ;
      if (nextMonth<0)
	{
	  nextMonth = 11;
	  nextYear--;
	}
    }

  if ((Date2_sup_Date1 (1,nextMonth,nextYear,1,LastDay.getMonth(),GetTheYear(LastDay))) && (Date2_sup_Date1 (1,FirstDay.getMonth() ,GetTheYear(FirstDay),1,nextMonth,nextYear)))
    {
      dispMonth = nextMonth;
      dispYear = nextYear;
      redraw();
    }

//  document.cookie="dispMonth="+nextMonth;
}


// compare 2 dates:
// vrai si date2 >= date1;

function Date2_sup_Date1 (Day1, Month1, Year1, Day2, Month2, Year2)
{

  if (Day1<=9)
    Day1 = '0'+Day1;
  if (Day2<=9)
    Day2 = '0'+Day2;
  if (Month1<=9)
    Month1 = '0'+Month1;
  if (Month2<=9)
    Month2 = '0'+Month2;

  if (Year2>Year1)
    return true;
  else
    {
      if (Year2 == Year1)
	{
	  if (Month2>Month1)
	    return true;
	  else
	    {
	      if (Month2 == Month1)
		{
		  if (Day2>=Day1)
		    return true;
		  else
		    return false;
		}
	      else 
		return false;
	    }
	}
      else 
	return false;
    }

}


function drawCalendar (theDate) 
{
  monthNum = theDate.getMonth() + 1;

  htmlBuffer  = "<HTML>\n";
  htmlBuffer += "<HEAD>\n";
  htmlBuffer += '';
  htmlBuffer += "\n<TITLE>Calendrier</TITLE></HEAD>\n";
  htmlBuffer += "</HEAD>\n\n";
  htmlBuffer += "<BODY bgcolor=\"white\">\n";
  htmlBuffer += "<FORM>\n";
  htmlBuffer += "<CENTER>\n<TABLE>\n";
  htmlBuffer += "<TH COLSPAN=\"2 \"class=\"backgroundtitle\">\n";
  htmlBuffer += "  <INPUT TYPE=BUTTON NAME=\"monthDn\" VALUE=\"<<\" onClick=callingForm.changeMonth(-1)>\n";
  htmlBuffer += "<TH COLSPAN=3 class=\"calendar_month\"> ";
  htmlBuffer += monthName[monthNum];
  tempYear = GetTheYear(theDate);
  htmlBuffer += " " + tempYear;
  htmlBuffer += "<TH COLSPAN=2 class=\"backgroundtitle\">\n<INPUT TYPE=\"button\" NAME=\"monthUp\" VALUE=\">>\" onClick=callingForm.changeMonth(1)>\n";

  htmlBuffer += "<TR class=\"calendar_date\">\n";
  htmlBuffer += "<TH WIDTH = 14.29% class=\"background\"><FONT class='calendar_date'><b>D</b></FONT>\n";
  htmlBuffer += "<TH WIDTH = 14.29%><FONT class='calendar_date'><b>L</b></FONT>\n";
  htmlBuffer += "<TH WIDTH = 14.29%><FONT class='calendar_date'><b>Ma</b></FONT>\n";
  htmlBuffer += "<TH WIDTH = 14.29%><FONT class='calendar_date'><b>Me</b></FONT>\n";
  htmlBuffer += "<TH WIDTH = 14.29%><FONT class='calendar_date'><b>J</b></FONT>\n";
  htmlBuffer += "<TH WIDTH = 14.29%><FONT class='calendar_date'><b>V</b></FONT>\n";
  htmlBuffer += "<TH WIDTH = 14.29% class=\"background\"><FONT class='calendar_date'><b>S</b></FONT><TR>\n";
  drawBody(theDate);
  htmlBuffer += "</TABLE>\n";
  htmlBuffer += "</BODY>\n</HTML>\n";
}


function drawBody (theDate) 
{
  thisYear = GetTheYear(theDate);
  thisMonth = theDate.getMonth();
  thisDate  = theDate.getDate();

  firstY = GetTheYear(FirstDay);
  firstM = FirstDay.getMonth();
  firstD = FirstDay.getDate();

  lastY = GetTheYear(LastDay);
  lastM = LastDay.getMonth();
  lastD = LastDay.getDate();
  
  firstSunday(theDate);

  for (w=0; w<6; w++) 
    {
      if ((theDate.getMonth() == thisMonth) || (w==0)) // Avoid the last line
	{
	  for (d=0; d<7; d++) 
	    {
	      date = theDate.getDate();

	      htmlBuffer   += "<TD ALIGN=CENTER HEIGHT = 30";
	      if ((d==0) || (d==6))
		htmlBuffer += " class=\"background\"";
	      htmlBuffer += "><FONT class='calendar_day'>";

	      // skip previous month
	      if (theDate.getMonth() != thisMonth) 
		{
		  htmlBuffer += "<BR>";
		} 
	      else 
		{
		  if (Date2_sup_Date1(firstD, firstM, firstY, date,thisMonth,thisYear) && Date2_sup_Date1(date,thisMonth,thisYear,lastD, lastM, lastY))
		    {
		      htmlBuffer += "<INPUT TYPE=BUTTON NAME=";
		      htmlBuffer += date;
		      htmlBuffer += " VALUE=";
		      if ( date < 10 ) 
			{
			  htmlBuffer += "0";
			}
		      htmlBuffer += date;
		      htmlBuffer += " onClick=callingForm.fillDate("+date+","+thisMonth+","+thisYear+")>";
		    }
		  else
		    {
		      if ( date < 10 ) 
			{
			  htmlBuffer += "0";
			}
		      htmlBuffer += date;
		    }
		}
	      htmlBuffer += "</TD>\n";
	      // increment the date
	      newTime = theDate.getTime() + msPerDay;
	      //theDate.setTime(newTime);

	      // check for DST 
	      theDate.setTime(newTime + 60*60*24);
	    }
	  htmlBuffer += "</TR>\n<TR HEIGHT = 40>\n";
	}
    }
  htmlBuffer += "</FORM>";
}

function makeArray(n) 
{
  this.length = n;
  return this;
}
function firstSunday (fromDate) 
{
  fromDate.setHours(22);
  while (fromDate.getDay() != 0) 
    {
      newTime = fromDate.getTime() - msPerDay;
      //fromDate.setTime(newTime);
      
      	fromDate.setTime(newTime + 60*60*24);
    }
}

function ExtractDate (field, portion)
{
  var date = new String(eval('document.availinfo.'+field+'.value'));
  var datearray = date.split('/');
  
  if (datearray.length<3)
    return 0;

  if (portion == 'day')
    return datearray[0];
  if (portion == 'month')
    return datearray[1];
  if (portion == 'year')
    return datearray[2];
    
}


function checkdate(d,m,y)
 {
   if (y < 100)
     {
       y = y+1900;
     }
   var yl=1990;   // least year to consider
   var ym=2037;   // most year to consider
   if (m<1 || m>12)  return(false);
   if (d<1 || d>31)  return(false);
   if (y<yl || y>ym) return(false);
   if (m==4 || m==6 || m==9 || m==11)
     if (d==31) return(false);
   if (m==2)
   {
     var b=parseInt(y/4);
     if (isNaN(b)) return(false);
     if (d>29)     return(false);
     if (d==29 && ((y/4)!=parseInt(y/4))) return(false);
   }
   return(true);
 }


function Verify()
{
  if (document.availinfo.currency && document.availinfo.currency.type == "select")
    {
      if (document.availinfo.currency.options[document.availinfo.currency.selectedIndex].value == "???")
	document.availinfo.currency.selectedIndex=0;
    }

 

var numRoom = 2;
var numAdults = parseInt(document.availinfo.numadult.options[document.availinfo.numadult.selectedIndex].value);
if (document.availinfo.numsenior) { numAdults += parseInt(document.availinfo.numsenior.options[document.availinfo.numsenior.selectedIndex].value); }
var numtotal = parseInt(document.availinfo.numadult.options[document.availinfo.numadult.selectedIndex].value);if (document.availinfo.numsenior) { numtotal += parseInt(document.availinfo.numsenior.options[document.availinfo.numsenior.selectedIndex].value); }
numtotal += parseInt(document.availinfo.nbchilds.options[document.availinfo.nbchilds.selectedIndex].value);
var roomid = parseInt(document.availinfo.currentroomid.value);
      

      
if (numtotal>4){alert ('Le nombre maximum de personne est: 4');return false;}   
	  var beginday = ExtractDate ('begindate', 'day');
	  var beginmonth = ExtractDate ('begindate', 'month');
	  var beginyear = ExtractDate ('begindate', 'year');
	  var endday = ExtractDate ('enddate', 'day');
	  var endmonth = ExtractDate ('enddate', 'month');
	  var endyear = ExtractDate ('enddate', 'year');
   
  

   if (!checkdate(beginday,beginmonth,beginyear))
     {
       alert ("Date d'arrivée erronée");
       return false;
     }

   if (!checkdate(endday,endmonth,endyear))
     {
       alert ("Date de départ erronée");
       return false;
     }


   if (!Date2_sup_Date1 (FirstAvailDay.getDate(),FirstAvailDay.getMonth()+1,GetTheYear(FirstAvailDay),beginday ,beginmonth ,beginyear ))
     {
       alert ("La date d'arrivée n'est pas disponible");
       return false;
     }

   if (!Date2_sup_Date1 (endday, endmonth ,endyear,LastAvailDay.getDate(),LastAvailDay.getMonth()+1,GetTheYear(LastAvailDay)))
     {
       alert ("Cet établissement est indisponible pour les dates demandées");       
       return false;
     }


   if (Date2_sup_Date1 (endday, endmonth ,endyear,beginday,beginmonth,beginyear))
     {
       alert ("La date de départ doit être supérieure à celle d'arrivée.");
       return false;
     }
   
   if (document.availinfo.nbnight.value < 1)
     {
       alert ("Vous devez réservez au minimum pour 1 nuit(s)");
       return false;
     }

  if (!availinfo_Validator(document.availinfo))
    {
      return false;
    }

  return true;
}







function Vide()
{
  for (j = 0; j < document.availinfo.length; j++)
    {
      switch (document.availinfo.elements[j].type)
	{
	case 'radio':
	    document.availinfo.elements[j].checked = false;
	    break;
	case 'text':
	    document.availinfo.elements[j].value = '';
	    break;
	case 'select-one':
	    document.availinfo.elements[j].value = '-1';
	    break;
	case 'checkbox':
	    document.availinfo.elements[j].checked = false;
	    break;
	}
    }

}


function UpdateSelectDateByCalendar(fieldname, pattern)
 {
   Today = new Date();       
   currentYear = y2k(Today.getYear());
   
   year = ExtractDate(fieldname,'year');
   month = ExtractDate(fieldname,'month');
   day = ExtractDate(fieldname,'day');
   
   document.availinfo.elements[fieldnameday].selectedIndex = day-1;
   document.availinfo.elements[fieldnamemonth].selectedIndex = month-1;
   document.availinfo.elements[fieldnameyear].selectedIndex = year-currentYear;
   document.availinfo.elements[pattern].value = document.availinfo.elements[fieldname].value;
   document.availinfo.nbnight.value=1; 

   if (pattern)
   updateEndDate(document.availinfo.elements[pattern+'day'].options[document.availinfo.elements[pattern+'day'].selectedIndex].value,
		 document.availinfo.elements[pattern+'month'].options[document.availinfo.elements[pattern+'month'].selectedIndex].value,
		 document.availinfo.elements[pattern+'year'].options[document.availinfo.elements[pattern+'year'].selectedIndex].value,
		 document.availinfo.nbnight.value) ;
   updateNbNight( document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value,
		  document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value,
		  document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value,
		  document.availinfo.elements[pattern+'day'].options[document.availinfo.elements[pattern+'day'].selectedIndex].value,
		  document.availinfo.elements[pattern+'month'].options[document.availinfo.elements[pattern+'month'].selectedIndex].value,
		  document.availinfo.elements[pattern+'year'].options[document.availinfo.elements[pattern+'year'].selectedIndex].value);
 }
function UpdateSelectBeginDate(fid, item, fieldname)
    {
      Today = new Date();       
      currentYear = y2k(Today.getYear());

            year = ExtractDate(fieldname,'year');
      month = ExtractDate(fieldname,'month');
      day = ExtractDate(fieldname,'day');
            
      if (item == "begindate")
	{
	  document.availinfo.fday.selectedIndex = day-1;
	  document.availinfo.fmonth.selectedIndex = month-1;
	  document.availinfo.fyear.selectedIndex = year-currentYear;


	  document.availinfo.nbnight.value=1; 
	  updateEndDate(document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
			document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
			document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value,
			document.availinfo.nbnight.value) ;
	  updateNbNight( document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value,
			 document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value,
			 document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value,
			 document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
			 document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
			 document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value)
	    
	}
      else
	{
	  document.availinfo.enddateday.selectedIndex = day-1;
	  document.availinfo.enddatemonth.selectedIndex = month-1;
	  document.availinfo.enddateyear.selectedIndex = year-currentYear;

	  updateNbNight( document.availinfo.elements['enddateday'].options[document.availinfo.elements['enddateday'].selectedIndex].value,
			 document.availinfo.elements['enddatemonth'].options[document.availinfo.elements['enddatemonth'].selectedIndex].value,
			 document.availinfo.elements['enddateyear'].options[document.availinfo.elements['enddateyear'].selectedIndex].value,
			 document.availinfo.elements['fday'].options[document.availinfo.elements['fday'].selectedIndex].value,
			 document.availinfo.elements['fmonth'].options[document.availinfo.elements['fmonth'].selectedIndex].value,
			 document.availinfo.elements['fyear'].options[document.availinfo.elements['fyear'].selectedIndex].value);
	    }
    }




  function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function updateEndDateStay(fname,myDay,myMonth,myYear,daysToAdd) 
    {
      savemyYear = document.forms[fname].elements['enddateyear'].value;
      
      myMonth = myMonth - 1;
      convertDate = new Date(myYear,myMonth,myDay,8,0,0);
      newDate = new Date(convertDate.getTime() + daysToAdd*24*60*60*1000);
      
      if (convertDate.getDay() == newDate.getDay() && 
	  convertDate.getMonth() == newDate.getMonth() && 
	  convertDate.getFullYear() == newDate.getFullYear())
	{
	  newDate = new Date(convertDate.getTime() + ((daysToAdd*24)+1)*60*60*1000);
	}
      
      CurDay = newDate.getDate();
      CurMonth = newDate.getMonth();
      CurMonth = CurMonth +1;
      CurYear = y2k(newDate.getYear()); 
      
      if (CurMonth<10)
	CurMonth = "0"+CurMonth;
      if (CurDay<10)
	CurDay = "0"+CurDay;
      
      if ((CurDay > 0) && (CurMonth > 0) && (CurYear >0)) 
	{
	  // DEV-#253: Todate = Fromdate + 1
	  document.forms[fname].elements['enddateday'].value=CurDay;
	  document.forms[fname].elements['enddatemonth'].value=CurMonth;
	  if (savemyYear != CurYear)
	    {
	      document.forms[fname].elements['enddateyear'].value=CurYear;
	    }
	}
    }

function ExtractDateStay (fid, fname, field, portion)
{ 
  var elemDate = document.getElementsByName(field);
  if (elemDate != null)
    {
      var datearray = elemDate[fid].value.split('/');
      
      if (datearray.length<3)	return 0;
      
      if (portion == 'day')
	return datearray[0];
      if (portion == 'month')
	return datearray[1];
      if (portion == 'year')
	return datearray[2];
    }
  else return 0;
}

function SelectForfait(fname, fid, begindate, nbnights)
{
  if (fid > 0) fid = fid-1;

  document.forms[fname].begindate.value=begindate;
  var beginday = ExtractDateStay (fid, fname, 'begindate', 'day');
  var beginmonth = ExtractDateStay (fid, fname, 'begindate', 'month');
  var beginyear = ExtractDateStay (fid, fname, 'begindate', 'year');

  document.forms[fname].nbnight.value=nbnights;
  updateEndDateStay(fname,beginday,beginmonth,beginyear,nbnights);

eval("document.forms[fname].enddate.value='"+document.forms[fname].enddateday.value+"/"+document.forms[fname].enddatemonth.value+"/"+document.forms[fname].enddateyear.value+"';");
    
  document.forms[fname].fday.value=beginday;
  document.forms[fname].fmonth.value=beginmonth;
  document.forms[fname].fyear.value=beginyear;
}

function ChangeVisibility(namefield, type)
{
  var nava = (document.layers);
  var dom = (document.getElementById);
  var iex = (document.all);
  var myelem = null;

  if (nava) { myelem = document.namefield; }
  else if (iex) 
    { 
      myfield = document.all[namefield];
      if (myfield != "null" && myfield!=null && typeof(myfield) != "undefined") 
	myelem = myfield.style; 
    }
  else if (dom) 
    { 
      myfield = document.getElementById(namefield);
      if (myfield != "null" && myfield!=null && typeof(myfield) != "undefined") myelem = myfield.style; 
    }

  if (myelem != null && myelem != "null")
    {
      myelem.position='relative';
      if (type == 'hidden') myelem.position='absolute';
      myelem.visibility=type;
    }
}

function HideMyStay(idform)
{
  for (idx=1; idx<=document.forms.length; idx++)
    {
      if (idx != idform)
	{
	  if (document.forms["availinfo"+idx] && document.forms["availinfo"+idx].chsejour)
	    {
	      if (document.forms["availinfo"+idx].chsejour.checked)
		{
		  ShowCalc("mystay"+idx,'none');
		}
	      document.forms["availinfo"+idx].chsejour.checked = false;
	    }
	}

      ShowCalc("mystay"+idform,'block'); 

      if (idform > 0)
	{
	  if (document.forms["availinfo"+idform] && document.forms["availinfo"+idform].chsejour)
	    document.forms["availinfo"+idform].chsejour.checked = true;
	  else if (document.forms["availinfo"] && document.forms["availinfo"].chsejour)
	    document.forms["availinfo"].chsejour.checked = true;

	  if (document.forms["availinfo"].chnight)
	    document.forms["availinfo"].chnight.checked = false;
	}
    }
}


