var monthNames=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var monthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	date = new Date();
	day = date.getDate();
	month = date.getMonth();
	year = date.getFullYear();
	
	function getElem(elemname) {
 		return document.getElementById(elemname)
  }
	// set departure day field with tomorrow's date
	function fillDay() {
	 document.search.calendar_day.options.selectedIndex = day;
	}
	// check for invalid date
	function CheckDate() {
		formDay=getElem('calendar_day').options[getElem('calendar_day').selectedIndex].value;
	formMonthYear=getElem('calendar_month_year').options[getElem('calendar_month_year').selectedIndex].value;
		splitMonthYear=formMonthYear.split("/");
		dayNumber = parseInt(formDay);
		monthNumber = parseInt(splitMonthYear[0])-1;
		yearNumber = parseInt(splitMonthYear[1]);
	  if (dayNumber==day && monthNumber==month && yearNumber==year) { 	// check date selected is not today
    				alert ("It is not possible to search for holidays departing today.  Please select another date.");
						return false;
    } else if (year==yearNumber && month==monthNumber && dayNumber<day) { 	// check that date selected is not in the past
   		 alert("Your selected departure date is in the past.  Please select a valid date.");
			 return false;
	 	}	else if (monthNumber==1 && dayNumber>28) { // check if february and day more than 28 selected
    				if (yearNumber%4==0 && (yearNumber%400==0 || yearNumber%100!=0) ) { // check if leap year selected
								if (dayNumber>29) {
    							  alert("There are only 29 days in February in "+yearNumber+". Please select a valid date.");
										return false;
    						}
						} else {
    					 alert("There are only 28 days in February in "+yearNumber+". Please select a valid date.");
								return false;
    				}
    } else if (dayNumber>monthDays[monthNumber]) {	// check that valid date selected ie not 31 Apr
    					alert("There are only "+monthDays[monthNumber]+" days in "+monthNames[monthNumber]+". Please select a valid date.");
							return false;
		}
		return true;
	}
	// Shows/Hides Children Ages and FREE child place option
function RedrawChildren() {
  no_of_children = parseInt(getElem('NoChildren').value)
	// No children selected > 0
  if (no_of_children > 0) {
		strHTML = '';
		getElem('childagesrow').style.display = 'block';
 		// Loop round all children
  	for (i=1;i<no_of_children+1;i++) {
			var childage = parseInt(document.getElementsByName("searchCriteria.childAges["+(i-1)+"]")[0].value,10)
			strHTML += '<div class="childage">Child '+ i +'<select id="child'+ (i-1) +'" name="child'+ (i-1) +'">'
    	// Generate Options for Dropdowns - select current age
  	  for (age=2;age<17;age++) {
    	 strHTML += '<option '
  		 if (childage == age) strHTML += ' selected '
			 strHTML += ' value="'+age+'">'+age+'</option>'
  	  }
			strHTML += '</select></div>'
    }
		//strHTML += '</table>'
		getElem('childages').innerHTML = strHTML
	} else {
	  // No children selected is 0
	  getElem('childagesrow').style.display = 'none'
  }
}

// Checks for Maximum party size and no. of adults >= no. of infants
function CheckPassengerQty()
{
   NoAdults   = parseInt(getElem('NoAdults').value,10)
   NoChildren = parseInt(getElem('NoChildren').value,10)
   NoInfants  = parseInt(getElem('NoInfants').value,10)
   if ((NoAdults + NoChildren) > 9 ) {
	   alert('Your total party size must be 9 passengers or fewer')
	   return false;
	}
	tooManyInfants = false;
	if (NoInfants > NoAdults)
	{
	  grownUps = NoAdults
	  no_of_children = parseInt(getElem('NoChildren').value)
	  if( no_of_children > 0 )
	  {
	    for (i=1; i < no_of_children+1;i++)
	    {
	       age = parseInt(getElem('child' + (i-1)).value)
	       if( age > 15 )
	       {
	          grownUps++
	       }
	    }
	  }
     tooManyInfants = ( grownUps < NoInfants )
	}
	if( tooManyInfants )
	{
	   alert('There must be at least one passenger aged 16 or over for each infant travelling')
	   return false;
	}
   return true
}
// Set children ages from hidden fields and update free child place field
function ChildrenInit()
{
  RedrawChildren();
  a = document.getElementById("showLargeFamilyRooms")
  b = document.getElementById("familyroom")  
	b.checked = false;
  if (a.value == 'true') { b.checked = true }
}
// set country and destination codes
function setDestination() {
	//splitLocation = (getElem('SearchLocation').value).split(",");
	country = (getElem('SearchLocation').value).slice(0,3);
	if ((getElem('SearchLocation').value).length>3) {
		destination = (getElem('SearchLocation').value).slice(4,10);
	}
	else {
		destination = 'Any';
	}
	//splitLocation=locationCode.split(",");
	//country = splitLocation[0];
	//destination = splitLocation[1];
	getElem('SearchCountry').value = country;
	getElem('SearchDestination').value = destination;
}
// submit search
function doSearch() {
	if (!CheckDate()) { return; }
	if (!CheckPassengerQty()) { return; }
	// Copy children ages into hidden fields
	no_of_children = parseInt(getElem('NoChildren').value,10);
  	for (var i=0; i < no_of_children; i++) {
 		var age=document.getElementById("child"+i).selectedIndex+2;
 		document.getElementsByName("searchCriteria.childAges["+i+"]")[0].value=age;
	}
 	// Copy Family Checkbox value
  	a = document.getElementById("showLargeFamilyRooms");
  	b = document.getElementById("familyroom");
  	a.value = b.checked;
 	// set destination and country code
	setDestination();
	
	document.forms[0].submit();
}