function changeDestinations(ids,names){
  var selValue = (document.getElementById("dest1").value) ? document.getElementById("dest1").value : "";
  selbox = document.getElementById("dest1");
  selbox.options.length = 0;
  selbox.options[selbox.options.length] = new Option("Please select one","-1");
  for(var i=0; i<ids.length; i++){
	var choose = (ids[i] == selValue) ? true : false;
    selbox.options[selbox.options.length] = new Option(names[i],ids[i],"",choose);
  }
}


function showAges(){
	kids = document.getElementById("children").value;
	if(kids == 0){
		Page.ageBox0.collapse();
		Page.ageBox1.collapse();
		Page.ageBox2.collapse();
		Page.ageBox3.collapse();
	}
	if(kids == 1){
		Page.ageBox0.showBlock();
		Page.ageBox1.collapse();
		Page.ageBox2.collapse();
		Page.ageBox3.collapse();
	}
	if(kids == 2){
		Page.ageBox0.showBlock();
		Page.ageBox1.showBlock();
		Page.ageBox2.collapse();
		Page.ageBox3.collapse();
	}
	if(kids == 3){
		Page.ageBox0.showBlock();
		Page.ageBox1.showBlock();
		Page.ageBox2.showBlock();
		Page.ageBox3.collapse();
	}
	if(kids == 4){
		Page.ageBox0.showBlock();
		Page.ageBox1.showBlock();
		Page.ageBox2.showBlock();
		Page.ageBox3.showBlock();
	}
}


function valVacationSearch(form){
  var Search = new FormField(form.search1);
  var Dept = new FormField(form.dept1);
  var Dest = new FormField(form.dest1);
  var DeptDate = new FormField(form.deptdate1);
  var DestDate = new FormField(form.destdate1);
  var Children = new FormField(form.children);
  var now = new Date();
  var today = new Date(now.getFullYear(),now.getMonth(),now.getDate()).getTime();
  
  if(!Search.valNotEmpty("Please enter a departure airport.")) return false;
  if(!Search.valMaxLength(99,"Please limit your search to 99 characters in the departure airport field.")) return false; 
  if(!Dept.valNotEmpty("Please enter a departure airport.")) return false;
  if(!Dept.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false; 
  if(!Dest.valAlpha("Please select a destination.")) return false;
  if(!DeptDate.valDate("Please enter departing dates in MM/DD/YYYY format.")) return false;
  if(!DestDate.valDate("Please enter return dates in MM/DD/YYYY format.")) return false;
  
  var deptDateArray = DeptDate.fieldValue.split("/");
  var destDateArray = DestDate.fieldValue.split("/");
  var deptStamp = new Date(deptDateArray[2],deptDateArray[0]-1,deptDateArray[1]).getTime();
  var destStamp = new Date(destDateArray[2],destDateArray[0]-1,destDateArray[1]).getTime();
  
  if(deptStamp > destStamp) {
    alert("You cannot return before you depart on the vacation."); return false;
  }
  if(deptStamp < today) {
    alert("Please pick a departure date that is in the future."); return false;
  }
  if(Children.fieldValue == 1){
	var age0 = new FormField(form.age0);
	if(!age0.valPosNum("Please select the age of child #1.")) return false;
  }
  if(Children.fieldValue == 2){
	var age0 = new FormField(form.age0);
	var age1 = new FormField(form.age1);
	if(!age0.valPosNum("Please select the age of child #1.")) return false;
	if(!age1.valPosNum("Please select the age of child #2.")) return false;
  }
  if(Children.fieldValue == 3){
	var age0 = new FormField(form.age0);
	var age1 = new FormField(form.age1);
	var age2 = new FormField(form.age2);
	if(!age0.valPosNum("Please select the age of child #1.")) return false;
	if(!age1.valPosNum("Please select the age of child #2.")) return false;
	if(!age2.valPosNum("Please select the age of child #3.")) return false;
 }
 if(Children.fieldValue == 4){
	var age0 = new FormField(form.age0);
	var age1 = new FormField(form.age1);
	var age2 = new FormField(form.age2);
	var age3 = new FormField(form.age3);
	if(!age0.valPosNum("Please select the age of child #1.")) return false;
	if(!age1.valPosNum("Please select the age of child #2.")) return false;
	if(!age2.valPosNum("Please select the age of child #3.")) return false;
	if(!age3.valPosNum("Please select the age of child #4.")) return false;
  }
  return true;
}


function AjaxVacationHotelSort(cache,sortid,rsltsId,rsltPrefix,orderPrefix){
  (!rsltsId) ? rsltsId = "defaultResults" : ""; 
  (!rsltPrefix) ? rsltPrefix = "defaultResultId" : "";
  (!orderPrefix) ? orderPrefix = "defaultResult" : "";
  document.getElementById("sortImg").style.visibility = "visible";
  xajax_AjaxVacationHotelSort(cache,sortid,rsltsId,rsltPrefix,orderPrefix);
}


function AjaxVacationFlightSort(cache,sortid,rsltsId,rsltPrefix,orderPrefix){
  (!rsltsId) ? rsltsId = "defaultResults" : ""; 
  (!rsltPrefix) ? rsltPrefix = "defaultResultId" : "";
  (!orderPrefix) ? orderPrefix = "defaultResult" : "";
  document.getElementById("sortImg").style.visibility = "visible";
  xajax_AjaxVacationFlightSort(cache,sortid,rsltsId,rsltPrefix,orderPrefix);
}


function changeInsurance(grandTotal,depositTotal,avgTotal,paxTotals,insChecked){
	try {
	  for(i=1; i<=paxTotals.length; i++){
		paxTotal = new Dynamic("paxTotal" + i);
		paxTotal.el.innerHTML = "$" + paxTotals[i-1];
		paxInsurance = new Dynamic("paxInsurance" + i);
		(insChecked) ? paxInsurance.showBlock() : paxInsurance.collapse();
	  }
	} catch(e) {}
	(insChecked) ? Page.insuranceTotal.showBlock() : Page.insuranceTotal.collapse();
	Page.grandTotal.el.innerHTML = "$" + grandTotal;
	Page.avgTotal.el.innerHTML = "(Avg. per person: $" + avgTotal + ")";
	try {
	  Page.depositAmount.el.innerHTML = "Deposit Amount $" + depositTotal;
	} catch(e) {}
	try {
	  Page.fullPayment.el.innerHTML = "Full Payment $" + grandTotal;
	} catch(e) {}
}


function valVacationCheckout(i) {
	var tripName = new FormField(document.getElementById("tripName"));
	if(!tripName.valNotEmpty("Please enter a valid trip name.")) return false;
	if(!tripName.valMaxLength(125,"The trip name you entered is longer than the 125 characters allowed.")) return false;

	var Email = new FormField(document.getElementById("email"));
	if(!Email.valEmail("Please enter a valid email address.")) return false;
	if(!Email.valMaxLength(80,"The email address you entered is longer than the 80 characters allowed")) return false;

	if(!valName(i)) return false;
	if(!valUSPhone(i)) return false;
	if(!valCard(i)) return false;
	if(!valBillAddress(i)) return false;
	if(document.getElementById("shipAddressLink").checked == true){
		if(!valShipAddress(i)) return false;
	}
	if(!valAgree()) return false;
    return true;
}


function selectAirport() {
		selectedItem = this.currResults[this.focusKey] ? this.currResults[this.focusKey] : false;
		if(selectedItem) {
				this.Input.el.value = selectedItem.code + ": " + selectedItem.airport + " - " + selectedItem.city + ", " + selectedItem.loc;
				document.getElementById(this.outputIds.hiddenInput).value = selectedItem.code;
		}
}
