function AjaxFlightSort(cache,carrier,stops,seg,itin,sortid,rsltsId,rsltPrefix,orderPrefix){
  (!rsltsId) ? rsltsId = "defaultResults" : ""; 
  (!rsltPrefix) ? rsltPrefix = "defaultResultId" : "";
  (!orderPrefix) ? orderPrefix = "defaultResult" : "";
  document.getElementById("sortImg").style.visibility = "visible";
  xajax_AjaxFlightSort(cache,carrier,stops,seg,itin,sortid,rsltsId,rsltPrefix,orderPrefix);
}


function showMatrixFlights(link,carrier,stops) {
  resetHighlight();
  var cell = link.parentNode;
  var flightBoxen = document.getElementsByName("mat[]");

  for(var h=0, len=flightBoxen.length; h<len; h++){
    var flightId = flightBoxen[h].id.substr(3,flightBoxen[h].id.length - 3);
    var arr = flightBoxen[h].value.split("|");
    arr[1] = (arr[1] > 2) ? 2 : arr[1];
    flightCompare = new Dynamic("defaultResultId" + flightId);

    if((arr[0] == carrier || carrier == -1) && (arr[1] == stops || stops == -1)) {
      flightCompare.showBlock();
      flightBoxen[h].checked = true;
    } else {
      flightCompare.collapse();
    }
  }
  Page.showAllCorner.showBlock();
  var cellid = cell.id.substr(1,cell.id.length);
  highlight(cellid,true);
}

function highlight(cellId,setid) {
  var cell = (cellId) ? document.getElementById("m" + cellId) : false;
  if(setid) {
    Page.hlCellId = cellId;
  }

  if(Page.cellHighlight) {
    stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(Page.cellHighlight,"#fff","#ffffe0");
  }
  if(Page.rowHighlight) {
    var hlString = Page.rowHighlight.id.substr(1,Page.rowHighlight.id.length);
    var hlArray = hlString.split(":");
    var hlRow = hlArray[0];
    for(var c=Page.Matrix.minCell; c<Page.Matrix.visCells; c++) {
      var rowCell = Page.Matrix.el.rows[hlRow].cells[c];
      stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(rowCell,"#fff","#ffffe0");
    }
  }
  if(Page.colHighlight) {
    var hlCol = false;
    for(var g=0; g<Page.Matrix.el.rows[0].cells.length; g++) {
      if(Page.Matrix.el.rows[0].cells[g] == Page.colHighlight) {
        hlCol = g;
      }
    }
    if(hlCol) {
			for(var u=1, len=Page.Matrix.el.rows.length; u<len; u++) {
        var colCell = Page.Matrix.el.rows[u].cells[hlCol];
        stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(colCell,"#fff","#ffffe0");
      }
    }
  }
  if(cell) {
    var hlArray = cellId.split(":");
    var hlRow = hlArray[0];
    var hlCol = hlArray[1];
    if(hlRow != 0 && hlCol != 0) {
      cell.style.backgroundColor="#ffffe0";
      cell.onmouseover = function(){ cell.style.backgroundColor='#ffffe0'; }
      cell.onmouseout = function(){ cell.style.backgroundColor='#ffffe0'; }
      Page.cellHighlight = cell;
    } else if(hlRow != 0 && hlCol == 0) {
      for(var c=Page.Matrix.minCell; c<Page.Matrix.visCells; c++) {
        var rowCell = Page.Matrix.el.rows[hlRow].cells[c];
        stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(rowCell,"#ffffe0","#ffffe0");
      }
      Page.rowHighlight = cell;
    } else if(hlRow == 0 && hlCol != 0) {
			for(var g=0, len=Page.Matrix.el.rows[0].cells.length; g<len; g++) {
        if(Page.Matrix.el.rows[0].cells[g] == cell) {
          hlCol = g;
        }
      }
      for(var u=1, len=Page.Matrix.el.rows.length; u<len; u++) {
				var colCell = Page.Matrix.el.rows[u].cells[hlCol];
        stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(colCell,"#ffffe0","#ffffe0");
      }
      Page.colHighlight = cell;
    }
  }
}

function stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(cel,normColor,overColor) {
  cel.style.backgroundColor = normColor;
  cel.onmouseover = function() { cel.style.backgroundColor = overColor; }
  cel.onmouseout = function() { cel.style.backgroundColor = normColor; }
}
  
function resetHighlight() {
  if(Page.hlCellId) {
    if(Page.cellHighlight) {
      stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(Page.cellHighlight,"#fff","#e2e6f2");
      Page.cellHighlight = null;
    }
    if(Page.rowHighlight) {
      var hlString = Page.rowHighlight.id.substr(1,Page.rowHighlight.id.length);
      var hlArray = hlString.split(":");
      var hlRow = hlArray[0];
      for(var c=Page.Matrix.minCell; c<Page.Matrix.visCells; c++) {
        var rowCell = Page.Matrix.el.rows[hlRow].cells[c];
        stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(rowCell,"#fff","#e2e6f2");
      }
      Page.rowHighlight = null;
    }
    if(Page.colHighlight) {
      var hlCol = false;
      for(var g=0, len=Page.Matrix.el.rows[0].cells.length; g<len; g++) {
        if(Page.Matrix.el.rows[0].cells[g] == Page.colHighlight) {
          hlCol = g;
        }
      }
      if(hlCol) {
				for(var u=1, len=Page.Matrix.el.rows.length; u<len; u++) {
          var colCell = Page.Matrix.el.rows[u].cells[hlCol];
          stupidNecessityBecauseOfJavascriptsHorrifyinglyDaftScopingProblemsIHateThis(colCell,"#fff","#e2e6f2");
        }
      }
      Page.colHighlight = null;
    }
  }
}


function commonValidate(form) {
  var Adults = new FormField(form.adults);
  var Seniors = new FormField(form.seniors);
  var Children = new FormField(form.children);
  var Infants = new FormField(form.infants);
  var NonStop = new FormField(form.nonstop);
  var COS = new FormField(form.classofservice);

  if(!Adults.valNum("Invalid input. The system administrator has been notified")) return false;
  if(!Adults.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!Seniors.valNum("Invalid input. The system administrator has been notified")) return false;
  if(!Seniors.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!Children.valNum("Invalid input. The system administrator has been notified")) return false;
  if(!Children.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!Infants.valNum("Invalid input. The system administrator has been notified")) return false;
  if(!Infants.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(parseInt(Adults.fieldValue) + parseInt(Seniors.fieldValue) + parseInt(Children.fieldValue) + parseInt(Infants.fieldValue) > 7) {
    alert("You may not have more than seven passengers");
    return false;
  }
  if(parseInt(Adults.fieldValue) + parseInt(Seniors.fieldValue) < 1) {
    alert("We're sorry, you must have at least one adult or senior");
    return false;
  }
  if(parseInt(Adults.fieldValue) + parseInt(Seniors.fieldValue) < parseInt(Infants.fieldValue)) {
    alert("You may not have more infants than adults");
    return false;
  }
  if(!NonStop.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!COS.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
}

function roundTripValidate(form) {
  var Search1 = new FormField(form.search1);
  var Dept = new FormField(form.dept1);
  var AltDept = new FormField(form.altdept1);
  var Search7 = new FormField(form.search7);
  var Dest = new FormField(form.dest1);
  var AltDest = new FormField(form.altdest1);
  var DeptDate = new FormField(form.deptdate1);
  var DeptTime = new FormField(form.depttime1);
  var DestDate = new FormField(form.destdate1);
  var DestTime = new FormField(form.desttime1);

  if(!Search1.valNotEmpty("Please select an option for the \"Leaving From\" field.")) return false;
  if(!Search1.valMaxLength(99,"Please limit your search to 99 characters in the \"Leaving From\" field.")) return false; 
  if(!Dept.valNotEmpty("Please select an option for the \"Leaving From\" field")) return false;
  if(!Dept.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false;
  if(!AltDept.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!Search7.valNotEmpty("Please select an option for the \"Going To\" field.")) return false;
  if(!Search7.valMaxLength(99,"Please limit your search to 99 characters in the \"Going To\" field.")) return false; 
  if(!Dest.valNotEmpty("Please select an option for the \"Going To\" field")) return false;
  if(!Dest.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false;
  if(!AltDest.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!DeptDate.valDate("Please enter dates in MM/DD/YYYY format")) return false;
  if(!DeptTime.valMaxLength(4,"Invalid input. The system administrator has been notified")) return false;
  if(!DestDate.valDate("Please enter dates in MM/DD/YYYY format")) return false;
  if(!DestTime.valMaxLength(4,"Invalid input. The system administrator has been notified")) 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();
  var now = new Date();

  var today = new Date(now.getFullYear(),now.getMonth(),now.getDate()).getTime();
  var now = null;
  if(deptStamp > destStamp) {
    alert("You cannot come back before you leave");
    return false;
  }

  if(deptStamp < today) {
    alert("Please pick a departure date that is in the future");
    return false;
  }

  return commonValidate(form);
}

function oneWayValidate(form) {
  var Search1 = new FormField(form.search1);
  var Dept = new FormField(form.dept1);
  var AltDept = new FormField(form.altdept1);
  var Search7 = new FormField(form.search7);
  var Dest = new FormField(form.dest1);
  var AltDest = new FormField(form.altdest1);
  var DeptDate = new FormField(form.deptdate1);
  var DeptTime = new FormField(form.depttime1);
  var TimeChoice = new FormField(form.timechoice1);

  if(!Search1.valNotEmpty("Please select an option for the \"Leaving From\" field.")) return false;
  if(!Search1.valMaxLength(99,"Please limit your search to 99 characters in the \"Leaving From\" field.")) return false; 
  if(!Dept.valNotEmpty("Please select an option for the \"Leaving From\" field")) return false;
  if(!Dept.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false;
  if(!AltDept.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!Search7.valNotEmpty("Please select an option for the \"Going To\" field.")) return false;
  if(!Search7.valMaxLength(99,"Please limit your search to 99 characters in the \"Going To\" field.")) return false; 
  if(!Dest.valNotEmpty("Please select an option for the \"Going To\" field")) return false;
  if(!Dest.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false;
  if(!AltDest.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
  if(!DeptDate.valDate("Please enter dates in MM/DD/YYYY format")) return false;
  if(!DeptTime.valMaxLength(4,"Invalid input. The system administrator has been notified")) return false;
  if(!TimeChoice.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;

  return commonValidate(form);
}

function multiLegValidate(form) {
  var currDeptStamp = false;
  for(var h=1; h<=Page.multiCounter; h++) {
    var Search = new FormField(eval("form.search" + h));
	var Dept = new FormField(eval("form.dept" + h));
    var AltDept = new FormField(eval("form.altdept" + h));
    var Dest = new FormField(eval("form.dest" + h));
    var AltDest = new FormField(eval("form.altdest" + h));
    var DeptDate = new FormField(eval("form.deptdate" + h));
    var DeptTime = new FormField(eval("form.depttime" + h));
    var TimeChoice = new FormField(eval("form.timechoice" + h));

    if(!Search.valNotEmpty("Please select an option for the \"Leaving From\" field.")) return false;
    if(!Search.valMaxLength(99,"Please limit your search to 99 characters in the \"Leaving From\" field.")) return false; 
    if(!Dept.valNotEmpty("Please select an option for the \"Leaving From\" field")) return false;
    if(!Dept.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false;
    if(!AltDept.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
    if(!Dest.valNotEmpty("Please select an option for the \"Going To\" field")) return false;
    if(!Dest.valMaxLength(3,"Invalid input. The system administrator has been notified")) return false;
    if(!AltDest.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;
    if(!DeptDate.valDate("Please enter dates in MM/DD/YYYY format")) return false;
    if(!DeptTime.valMaxLength(4,"Invalid input. The system administrator has been notified")) return false;
    if(!TimeChoice.valMaxLength(1,"Invalid input. The system administrator has been notified")) return false;

    var deptDateArray = DeptDate.fieldValue.split("/");
    var prevDeptStamp = (currDeptStamp) ? currDeptStamp : new Date();
    var currDeptStamp = new Date(deptDateArray[2],deptDateArray[0]-1,deptDateArray[1]).getTime();

    if(prevDeptStamp > currDeptStamp) {
      alert("All legs must be in chronological order");
      return false;
    }
  }

  return commonValidate(this);
}


function valFarewatchForm(form) {
	var FarewatchDept = new FormField(form.farewatchDept);
	try{
	  var FarewatchDest = new FormField(form.farewatchDest);
	} catch(e) {}
	if(!FarewatchDept.valAlphaExtra("Please select a departure city.")) return false;
	if(FarewatchDest.isNotEmpty()) {
    if(!FarewatchDest.valAlphaExtra("Please select a destination city.")) return false;
	}
  return true;
}


function valAirportCodesForm(form) {
	var codeRegion = new FormField(form.codeRegion);
	try{
	  var codeLocation = new FormField(form.codeLocation);
	} catch(e) {}
    if(!codeRegion.valAlpha("Please select a region.")) return false;
	if(codeLocation.isNotEmpty()) {
	  switch(codeRegion.fieldValue) {
		case "US" :
		  var msg = "state";
		  break;
		case "CA" :
		  var msg = "province";
		  break;
		case "INTL" :
		  var msg = "country";
		  break;
	  }
	  if(!codeLocation.valAlpha("Please select a "+ msg +".")) return false;
	}
  return true;
}


function valFlightsCheckout(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(!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 airSearchValidate(form) {
  //form.flighttype.value always undefined WHY??? - So I have to do this for now
  var elms = document.getElementsByName("flighttype");
  for(var i=0, len=elms.length; i<len; i++) {
    if(elms[i].checked == true) {
      var flightTypeValue = elms[i].value;
    }
  }

  switch(flightTypeValue) {
    case "roundtrip" :
      return roundTripValidate(form);
      break;
    case "oneway" :
      return oneWayValidate(form);
      break;
    case "multileg" :
      return multiLegValidate(form);
      break;
  }
}


function showInfChoices() {
  if(Page.infantDrop.el.value != 0) {
    Page.infantChoices.showBlock();
  } else {
    Page.infantChoices.collapse();
  }
}


function showDest() {
  Page.destBox.showBlock();
  Page.timeChoiceBox.collapse();
}


function hideDest() {
  Page.destBox.collapse();
  Page.timeChoiceBox.showInline();
}


function multiHandler(adv) {
  if(Page.multiCounter < 6 && adv == 1) {
    Page.multis[Page.multiCounter + 1].showBlock();
    Page.multiCounter = (Page.multiCounter + 1 < 7 ? Page.multiCounter + 1 : Page.multiCounter);
  }
  if(Page.multiCounter > 1 && adv == -1) {
    Page.multis[Page.multiCounter].collapse();
    Page.multiCounter--;
  }
  if(Page.multiCounter == 6) {
    Page.AddLeg.collapse();
  } else {
    Page.AddLeg.showInline();
  }
  if(Page.multiCounter == 2) {
    Page.RemoveLeg.collapse();
  } else {
    Page.RemoveLeg.showInline();
  }
  document.getElementById("multiCounter").value = Page.multiCounter;
}


function switchForm(form) {
  switch(form) {
    case "roundTrip":
      Page.multi.collapse();
      showDest();
      Page.Boss.monitor(true,false,Page.DeptCal,Page.RetCal);
      break;
    case "oneWay":
      Page.multi.collapse();
      hideDest();
      Page.Boss.monitor(true,false,Page.DeptCal);
      break;
    case "multiLeg":
      hideDest();
      Page.multi.showBlock();
      Page.Boss.monitor(true,false,Page.DeptCal,Page.MultiCal2,Page.MultiCal3,Page.MultiCal4,Page.MultiCal5,Page.MultiCal6);
      break;
  }
}


function changeFarewatchDest(names){
	try {
	  var selValue = document.getElementById("farewatchDest").value;
	} catch(e) {}
	var frag = document.createDocumentFragment();
	var label = frag.appendChild(document.createElement("label"));
	label.appendChild(document.createTextNode("Destination city"));
	var selbox = frag.appendChild(document.createElement("select"));
	selbox.setAttribute("id","farewatchDest");
	selbox.setAttribute("name","farewatchDest");
	selbox.options.length = 0;
	selbox.options[selbox.options.length] = new Option("Please select one","-1");
	for(var i=0; i<names.length; i++){
	  var choose = (names[i] == selValue) ? true : false;
	  selbox.options[selbox.options.length] = new Option(names[i],names[i],"",choose);
	}
	Page.farewatchDestBox.empty();
	Page.farewatchDestBox.el.appendChild(frag);
}


function changeAirportCodes(ids,names){
	try {
	  var selValue = document.getElementById("codeLocation").value;
	} catch(e) {}
	var frag = document.createDocumentFragment();
	var label = frag.appendChild(document.createElement("label"));
	switch(document.getElementById("codeRegion").value) {
	  case "CA": var inpLbl = "Province"; break;
  	  case "INTL": var inpLbl = "Country"; break;
	  default : var inpLbl = "State"; break;
    }	
	label.appendChild(document.createTextNode(inpLbl));
	var selbox = frag.appendChild(document.createElement("select"));
	selbox.setAttribute("id","codeLocation");
	selbox.setAttribute("name","codeLocation");
	selbox.options.length = 0;
	selbox.options[selbox.options.length] = new Option("Please select one","-1");
	for(var i=0, len=ids.length; i<len; i++){
	  var choose = (names[i] == selValue) ? true : false;
	  selbox.options[selbox.options.length] = new Option(names[i],ids[i],"",choose);
	}
	Page.codeLocationBox.empty();
	Page.codeLocationBox.el.appendChild(frag);
}

function selectAirport() {
		var 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;
		}
}

