var fn = 'form1';

function updateDepartureDate() {
	hform=document.forms[fn];

	var numNights = hform[fn+':numNights'].options[hform[fn+':numNights'].selectedIndex].value;

	var day = hform[fn+':arrivalDay'].options[hform[fn+':arrivalDay'].selectedIndex].value;
	var month = hform[fn+':arrivalMonth'].options[hform[fn+':arrivalMonth'].selectedIndex].value;
	var year = hform[fn+':arrivalYear'].options[hform[fn+':arrivalYear'].selectedIndex].value;
 
	var milliseconds = Date.UTC(year, month-1, day);
	var depDate = new Date(milliseconds + (86400000 * numNights));
	updateDateSelects(depDate, "departure");
	// updateWeekday("dept");
}

function autofitIframe(id) {
  if (!window.opera && !document.mimeType && document.all && document.getElementById) {
   var myElement = parent.document.getElementById(id)
   if (myElement != null) { // prevent faults on pages were id is unknown
    myElement.style.height=this.document.body.offsetHeight+"px";
   }
  }
  else if(document.getElementById) {
   var myElement = parent.document.getElementById(id)
   if (myElement != null) {// prevent faults on pages were id is unknown
    myElement.style.height=this.document.body.scrollHeight+"px";
   }
  }
} 


function arrivalDateChanged(offSetArr) {
// if arrival date changed, use current numnights to update departure date
	hform=document.forms[fn];

	var arrDay = hform[fn+':arrivalDay'].options[hform[fn+':arrivalDay'].selectedIndex].value;
	var arrMonth = hform[fn+':arrivalMonth'].options[hform[fn+':arrivalMonth'].selectedIndex].value;
	var arrYear = hform[fn+':arrivalYear'].options[hform[fn+':arrivalYear'].selectedIndex].value;
 
	var depDay = hform[fn+':departureDay'].options[hform[fn+':departureDay'].selectedIndex].value;
	var depMonth = hform[fn+':departureMonth'].options[hform[fn+':departureMonth'].selectedIndex].value;
	var depYear = hform[fn+':departureYear'].options[hform[fn+':departureYear'].selectedIndex].value;

  var millisecondsArrival = Date.UTC(arrYear, arrMonth-1, arrDay);
  var arrDate = new Date(millisecondsArrival);
  // check if arrival date is in the past. If so, use today
  var today = new Date();
  today = new Date(today.getTime() + offSetArr*1000*60*60*24);
//  if (Math.round((today-arrDate)/(1000*60*60*24)) > 0) {
//	  arrDate = today;
//  }
  
//  alert(arrDay+'-a-'+arrDate.getDate()+'-b-'+(arrMonth-1)+'-c-'+arrDate.getMonth()+'-d-'+arrDate.getTime());
  if (arrDay != arrDate.getDate() || arrMonth-1 != arrDate.getMonth()) {
  //  alert('update');
    updateDateSelects(arrDate.getTime(), "arrival");  // update arrival date if user puts e.g. 31/2/2005
  }
  
	var numNights = hform[fn+':numNights'].options[hform[fn+':numNights'].selectedIndex].value;
	var millisecondsDeparture = arrDate.getTime() + (numNights * 86400000);
//  alert('millisecondsDeparture: '+millisecondsDeparture);
  updateDateSelects(millisecondsDeparture, "departure");
}




function departureDateChanged() {
// if departure date changed, update nights
// if nights results to more than 30, put 30 and change departure date accordingly
	hform=document.forms[fn];

	var arrDay = hform[fn+':arrivalDay'].options[hform[fn+':arrivalDay'].selectedIndex].value;
	var arrMonth = hform[fn+':arrivalMonth'].options[hform[fn+':arrivalMonth'].selectedIndex].value;
	var arrYear = hform[fn+':arrivalYear'].options[hform[fn+':arrivalYear'].selectedIndex].value;
 
	var depDay = hform[fn+':departureDay'].options[hform[fn+':departureDay'].selectedIndex].value;
	var depMonth = hform[fn+':departureMonth'].options[hform[fn+':departureMonth'].selectedIndex].value;
	var depYear = hform[fn+':departureYear'].options[hform[fn+':departureYear'].selectedIndex].value;
 
  var millisecondsDeparture = Date.UTC(depYear, depMonth-1, depDay);
  var depDate = new Date(millisecondsDeparture);
  if (depDay != depDate.getDate() || depMonth-1 != depDate.getMonth()) {
    updateDateSelects(millisecondsDeparture, "departure");  // update departure date if user puts e.g. 31/2/2005
  }

	var millisecondsArrival = Date.UTC(arrYear, arrMonth-1, arrDay);

	var numNights = (millisecondsDeparture - millisecondsArrival) / 86400000;
//	alert ('numNights: '+numNights);
	
	if (numNights <= 0) {  // arrival is igual or later than departure -> set departure date to arrival date + 1
		updateNumNights(1);
		updateDateSelects(millisecondsArrival + 86400000, "departure");
	}
	else {
		if (numNights > 30) { // set departure date to arrival date + 30
			updateNumNights(30);
			updateDateSelects(millisecondsArrival + 86400000 * 30, "departure");
		}
		else {
			updateNumNights(numNights);
		}
	}
}

function updateNumNights(numNights) {
  if (numNights < 10) {
    numNights = '0' + numNights
  }
//  alert('updateNumNights '+numNights);
	hform=document.forms[fn];
	var form = hform[fn+':numNights'];
	for(var x = 0; x < eval(form.options.length); x++){
		if(eval(form.options[x].value) == numNights){
  //    alert('x: '+x);
			eval(form.selectedIndex = x);
		}
	}
}

function updateDateSelects(dateMilliSecs, dayType) {
	var theDate = new Date(dateMilliSecs);
	var day = theDate.getUTCDate();
	var month = theDate.getUTCMonth()+1;
	var year = theDate.getUTCFullYear();
	//alert("Date is " + day + "/" + month + "/" + year);

	var form = hform[fn+':'+dayType+'Day'];
	for(var x = 0; x < eval(form.options.length); x++){
		if(eval(form.options[x].value) == day){
			eval(form.selectedIndex = x);
		}
	}

	form = hform[fn+':'+dayType+'Month'];
	for(var x = 0; x < eval(form.options.length); x++){
		if(eval(form.options[x].value) == month){
			eval(form.selectedIndex = x);
		}
	}

	form = hform[fn+':'+dayType+'Year'];
	for(var x = 0; x < eval(form.options.length); x++){
		if(eval(form.options[x].value) == year){
			eval(form.selectedIndex = x);
		}
	}

}

/** To insert the dimensions in the page
 */
function setDimensions() {
	document.getElementById("inputHeight").value = this.document.body.scrollHeight
	document.getElementById("inputWidth").value = parseInt(this.document.body.scrollWidth, 10) + 20
}

