// Updates form fields after selection change
function UpdateFields(form,fields)
	{
		
	var dayIndex = form.fd.selectedIndex;
	var monthIndex = form.fm.selectedIndex;
	var yearIndex = form.fy.selectedIndex;
	
	var fday = form.fd.options[dayIndex].value;
	var fmonth = monthIndex;
	var fyear = form.fy.options[yearIndex].value;
		
	GenerateDayOptions(form.fd,month,fyear);
	GenerateMonthOptions(form.fm,fmonth);
	
	var dayIndex = form.td.selectedIndex;
	var monthIndex = form.tm.selectedIndex;
	var yearIndex = form.ty.selectedIndex;
	
	var tday = form.td.options[dayIndex].value;
	var tmonth = monthIndex;
	var tyear = form.ty.options[yearIndex].value;
		
	GenerateDayOptions(form.td,month,tyear);
	GenerateMonthOptions(form.tm,tmonth);
	
	// converts the selected date to EPOCH to work with them
	var checkInDate = new Date(fyear,fmonth,fday);
	var checkOutDate = new Date(tyear,tmonth,tday);
	
	// updates check out date only if it is lower than check in date
	if ((fields == 'checkin') && (checkOutDate < checkInDate))
		{
			UpdateCheckOutDate(form,fday,fmonth,fyear);
			
			var dayIndex = form.td.selectedIndex;
			var monthIndex = form.tm.selectedIndex;
			var yearIndex = form.ty.selectedIndex;
	
			var tday = form.td.options[dayIndex].value;
			var tmonth = monthIndex;
			var tyear = form.ty.options[yearIndex].value;
			
			var checkOutDate = new Date(tyear,tmonth,tday);
		}
		
	document.getElementById('inWd').firstChild.nodeValue=wdArray[checkInDate.getDay()];
	document.getElementById('outWd').firstChild.nodeValue=wdArray[checkOutDate.getDay()];
	
	// Updates num nights
    var numNights = Math.round((checkOutDate - checkInDate) / 86400000)
		
    if (numNights < 1) document.getElementById('numNights').firstChild.nodeValue = "?????";
    else if (numNights == 1) document.getElementById('numNights').firstChild.nodeValue = numNights + " " + nightTxt;
    else document.getElementById('numNights').firstChild.nodeValue = numNights + " " + nightsTxt;
		
	}
	
// Updates weeks days and num nights
function UpdateWeekDays(form)
	{
				
	var dayIndex = form.fd.selectedIndex;
	var monthIndex = form.fm.selectedIndex;
	var yearIndex = form.fy.selectedIndex;
	
	var fday = form.fd.options[dayIndex].value;
	var fmonth = monthIndex;
	var fyear = form.fy.options[yearIndex].value;
	
	var dayIndex = form.td.selectedIndex;
	var monthIndex = form.tm.selectedIndex;
	var yearIndex = form.ty.selectedIndex;
	
	var tday = form.td.options[dayIndex].value;
	var tmonth = monthIndex;
	var tyear = form.ty.options[yearIndex].value;
	
	// updates weeks days
	var checkInDate = new Date(fyear,fmonth,fday);
	var checkOutDate = new Date(tyear,tmonth,tday);
	document.getElementById('inWd').firstChild.nodeValue=wdArray[checkInDate.getDay()];
	document.getElementById('outWd').firstChild.nodeValue=wdArray[checkOutDate.getDay()];
	
	// Updates num nights
    var numNights = Math.round((checkOutDate - checkInDate) / 86400000)
		
    if (numNights < 1) document.getElementById('numNights').firstChild.nodeValue = "?????";
    else if (numNights == 1) document.getElementById('numNights').firstChild.nodeValue = numNights + " " + nightTxt;
    else document.getElementById('numNights').firstChild.nodeValue = numNights + " " + nightsTxt;
		
	}
	

//Set current dates on form load
function LoadDates(form) 
	{
  	curDate = new Date();
  	curDay = curDate.getDate();
  	curMonth = curDate.getMonth();
  	curYear = curDate.getFullYear();
	
  	GenerateDayOptions(form.fd,curMonth,curYear);
	GenerateMonthOptions(form.fm,curMonth);
	GenerateYearOptions(form.fy,curYear);
		
	// sets check in date to current day
	form.fd.selectedIndex=curDay-1;
	form.fm.selectedIndex=curMonth;
	form.fy.selectedIndex=0;
	
	GenerateDayOptions(form.td,curMonth,curYear);
	GenerateMonthOptions(form.tm,curMonth);
	GenerateYearOptions(form.ty,curYear);
	
  	if (curDay >= 28) 
		{
    	if (curMonth == 11) 
			{
      		form.fm.selectedIndex = 0;
      		form.fy.selectedIndex++;
			GenerateDayOptions(form.fd,0,curYear+1);
			}
    	else 
			{
			form.fm.selectedIndex++;
			GenerateDayOptions(form.fd,curMonth+1,curYear);			
			}		
    	form.fd.selectedIndex = 0;
  		}  
  	else {form.fd.selectedIndex = curDay+2;}
	
	var dayIndex = form.fd.selectedIndex;
	var monthIndex = form.fm.selectedIndex;
	var yearIndex = form.fy.selectedIndex;
	
	var day = form.fd.options[dayIndex].value;
	var month = monthIndex;
	var year = form.fy.options[yearIndex].value;
		
	UpdateCheckOutDate(form,day,month,year);
	
	var dayIndex = form.td.selectedIndex;
	var monthIndex = form.tm.selectedIndex;
	var yearIndex = form.ty.selectedIndex;
	
	var tday = form.td.options[dayIndex].value;
	var tmonth = monthIndex;
	var tyear = form.ty.options[yearIndex].value;
	
	// updates weeks days
	var checkInDate = new Date(year,month,day);
	var checkOutDate = new Date(tyear,tmonth,tday);
	document.getElementById('inWd').firstChild.nodeValue=wdArray[checkInDate.getDay()];
	document.getElementById('outWd').firstChild.nodeValue=wdArray[checkOutDate.getDay()];
	
}

// accepts as arguments the selected indexes of each of the check in form fields
function UpdateCheckOutDate(form,day,month,year)
	{
	
	if (
		( (day == 31) && ( (month == 0)||(month==2)||(month==4)||(month==6)||(month==7)||(month==9)||(month==11) ) )  ||
		( (day == 30) && ( (month == 3 )||(month == 5)||(month == 8)||(month == 10) ) ) ||
		( (day == 28) && (month == 1) && (year%4 != 0) ) ||
		( (day == 29) && (month == 1) && (year%4 == 0) )
	   )
		{ 
			if (month == 11)
				{
					form.tm.selectedIndex=0;
					form.ty.selectedIndex=form.fy.selectedIndex+1;
					GenerateDayOptions(form.td,0,year+1);
				}
			else 
				{
					form.tm.selectedIndex=form.fm.selectedIndex+1;
					form.ty.selectedIndex=form.fy.selectedIndex;
					GenerateDayOptions(form.td,month+1,year);
				}			
			form.td.selectedIndex=0;
		}
	else
		{
			GenerateDayOptions(form.td,month,year);
			form.tm.selectedIndex=form.fm.selectedIndex; 
			form.ty.selectedIndex=form.fy.selectedIndex; 
			form.td.selectedIndex=form.fd.selectedIndex+1; 
		}
					
	}
	

// accepts as an argument the complete field (document.form.field) where to generate day entries
function GenerateDayOptions(dayField,month,year)
	{
	var numDays=31;
	if ((month == 3 )||(month == 5)||(month == 8)||(month == 10))	{numDays=30;}
	if ((month == 1)&&(year % 4 == 0)) {numDays=29;}
	if ((month == 1)&&(year % 4 != 0)) {numDays=28;}
	
	// generates entries
	dayField.options.length=numDays;
	
	for (var i=0; i<numDays; i++)
		{
		dayField.options[i].value=i+1;
		dayField.options[i].text=i+1;
		}
	} // end function
	
// accepts as an argument the complete field (document.form.field) where to generate day entries
function GenerateMonthOptions(monthField)
	{
	
	// generates entries
	monthField.options.length=12;
	
	for (var i=0; i<12; i++)
		{
		monthField.options[i].value=i+1;
		monthField.options[i].text=month[i];
		}
	} // end function
	
// accepts as an argument the complete field (document.form.field) where to generate day entries, and the current year
function GenerateYearOptions(yearField,year)
	{
	
	// generates entries
	yearField.options.length=3;
	
	for (var i=0; i<3; i++)
		{
		yearField.options[i].value=year;
		yearField.options[i].text=year;
		year++;
		}
	} // end function
	


