var Browser = '';
Browser	= ''
var sector1_o				= ''
var sector1_d				= ''
var sector2_o				= ''
var sector2_d				= ''

var oDay;
var oMonth;
var oYear;
var rDay;
var rMonth;
var rYear;

function setPassengerTypes(form) {
  var paxTypes = "";
  var paxTypeCHILDNum	= document[form].CHILD.value;
  var paxTypeCHILDCode	= "CHILD";
  paxTypes = paxTypes + paxTypeCHILDNum + paxTypeCHILDCode;
  var paxTypeADULTNum	= document[form].ADULT.value;
  var paxTypeADULTCode	= "ADULT";
  paxTypes = paxTypes + paxTypeADULTNum + paxTypeADULTCode;
  document[form].pT.value = paxTypes;
  }

function submit_WCI_Form(form)
{
  var error = validateNoSubmit(form);
  var theForm = document.forms[form];
  if (!error)
  {
    SetSabreWCIFields(form);
    theForm.action = theForm.action + 
      "#RecordLocator=" + document[form].RecordLocator.value +
      "&FlightOrigin=" + document[form].FlightOrigin.value +
      "&FirstName=" + document[form].FirstName.value +
      "&LastName=" + document[form].LastName.value +
      "&Language=" + document[form].Language.value;

    theForm.submit();
  }
  return false;
}

function submit_SB_Form(form)
{
/** ALL THIS FIRST STUFF IS TO SUPPORT LEGACY VALUES:
 **
 */
  captureDates();
  var urlVars = getURLParameters();
 
  // update values using url parameters if applicable
  if ($("sector1_o").value == "" && urlVars.from) {
    $("sector1_o").value = urlVars.from;
    if ($("sector1_d").value == "" && urlVars.to)
      $("sector1_d").value = urlVars.to;
  }
  sector1_o	= $("sector1_o").value;
  sector1_d	= $("sector1_d").value;
  numAdts = $("ADULT").value;
  numChds = $("CHILD").value;
  numInfs = $("INFANT").value;
  fromdt = $("MARKET1_DATE").value;
  todt =  $("MARKET2_DATE").value;

  var cal1Hidden = $("sector_2_d");

  var tripTypeFix = "0";	
  for (var i=0; i<document.bBaf.TRIP_TYPE.length; i++)
    if (document.bBaf.TRIP_TYPE[i].checked)
      tripTypeFix = i;

  if ( sector1_o.length > 3 )	
  { 
    sector1_o	= sector1_o.substring(1,4); 
  }

  if (tripTypeFix == 0)
    { 
    sector2_o = sector1_d;
    sector2_d = sector1_o;
  }
  else
  { 
    sector2_o = "XXX";
    sector2_d = "XXX";
  }

  if (checkCities() && checkDate(form) && checkPassenger(form) && enforceMaxPax(form) )
  {
    document[form].m1.value = "" + oYear + oMonth + oDay + sector1_o + sector1_d;
    if (document[form].departDate1)
      document[form].departDate1.value = "" + oYear + oMonth + oDay;

    if( (sector2_o == "XXX") || (sector2_d == "XXX") )
    {
      document[form].m2.value="";
      document[form].nom.value="1";
      if (document[form].departDate2)
        document[form].departDate2.value = "";
    }
    else
    {
      document[form].m2.value = "" + rYear + rMonth + rDay + sector2_o + sector2_d;
      document[form].nom.value="2";
      if (document[form].departDate2)
        document[form].departDate2.value = "" + rYear + rMonth + rDay;
    }

    setPassengerTypes(form);
    document.dest1Form.dIndex.value = document[form].sector1_d.selectedIndex;

    var tripTypeFix = (document[form].nom.value == "1") ? "1" : "0";	

    setTracking(trackingCookie0, tripTypeFix + "," + sector1_o + "," + sector1_d + "," + numAdts + "," + numChds + "," + numInfs + ",'" + fromdt + "','" + todt + "'",$("browserBack").value);
	
    var fare_cat = document[form].booker_fare_cat.value;
    document[form].fare_cat.value = fare_cat;
    if (fare_cat == "TrueBlue") {
        document[form].mode.value = 'OAR';
    } else {
        document[form].mode.value = '';
    }

    if ( tripTypeFix == 1 ) {
      if ( OWAlert( sector1_d ) )
        return true;
    }



    SetSabreFields(form);

	
    document[form].submit();
    $("ADULT").value = "0";
    $("MARKET1_DATE").value = "";
    $("MARKET2_DATE").value = "";
  }
  
  return false;
}

function SetSabreWCIFields (form)
{
  // FirstName, LastName is already set in the form.
  document[form].FlightOrigin.value = document[form].CityCode.value;
  document[form].RecordLocator.value = document[form].RL.value;
  document[form].Language.value = "en_US";
}

function SetSabreFields(form)
{
  document[form].searchType.value = "NORMAL";
  document[form].journeySpan.value = document[form].nom.value == 1 ? "OW" : "RT";
  document[form].departureDate.value = "" + oYear + "-" + oMonth + "-" + oDay;
  if (document[form].journeySpan.value == "RT")
    document[form].returnDate.value = "" + rYear + "-" + rMonth + "-" + rDay;
  else
    document[form].returnDate.value = "";
  
  document[form].origin.value = sector1_o;
  document[form].destination.value = sector1_d;
  document[form].numAdults.value = numAdts;
  document[form].numChildren.value = numChds;
  document[form].numInfants.value = numInfs;
  document[form].lang.value = "EN";

  var fare_cat = document[form].fare_cat.value.toLowerCase();
  if ( fare_cat == "lowest" )
    document[form].fareFamily.value = "LOWESTFARE";
  else if ( fare_cat == "trueblue" )
    document[form].fareFamily.value = "TRUEBLUE";
  else
    document[form].fareFamily.value = "REFUNDABLE";

  document[form].cabinClass.value = "A";
}

function captureDates()
  {
  var cal0Hidden = $("sector_1_d");
  var cal0HiddenMonthYear = $("sector_1_m");

  var cal1Hidden = $("sector_2_d");
  var cal1HiddenMonthYear = $("sector_2_m");

  oDay 	= cal0Hidden.value;
  oMonth	= cal0HiddenMonthYear.value.substring(0,2);
  oYear	= cal0HiddenMonthYear.value.substring(2, 6);
  rDay	= cal1Hidden.value;
  rMonth	= cal1HiddenMonthYear.value.substring(0,2)
  rYear	= cal1HiddenMonthYear.value.substring(2,6)
  }



function enforceMaxPax(form)
  {		
  var paxTotal = 0 
  paxTotal = eval(paxTotal) + eval(document[form].CHILD.value) 
  paxTotal = eval(paxTotal) + eval(document[form].ADULT.value) 
  if (paxTotal > 7){
    alert("\nYou are allowed a maximum of seven passengers per online booking. If your party is larger than this, please call 1-888-JETBLUE (538-2583), option 2 or go to www.jetblue.com/groups.\n")
    return false 
  }
  else{
    return true 
    }
  }


function checkDate(form){
  var theForm = document[form];
  var leaveSet = cal0Hidden.value != "" ? true : false;
  var returnSet = cal1Hidden.value != "" ? true : false;
  var todayDate = new Date(); 
  var tMonth = todayDate.getMonth() + 1; 
  var tDay = todayDate.getDate(); 
  var tYear = todayDate.getFullYear();
  var todayStr = tMonth + "/" + tDay + "/" + tYear;

  var trip_type = "";
  for(var i=0; i<theForm.length; i++) {
    if(theForm[i].name == "TRIP_TYPE" && theForm[i].checked) trip_type = theForm[i].value;
  }
  
  if(!leaveSet) {
    alert("Please select a date to leave.");
    return false;
    }
  else if(trip_type == "RoundTrip" && !returnSet) {
    alert("Please select a date to return.");
    return false;
    }
  else {
    if(cal0Hidden.value == cal1Hidden.value) {
      if(!confirm("Your return date is the same as your departure date. Is such a short trip intentional?"))
        return false;  
      }
    if(todayStr == cal0Hidden.value) 
      alert("Because you've selected today's date, only flights that leave at least 1 hour and 30 minutes from the current time will be shown on the next page.");
    return true;
    }
  }	// checkDate


function checkCities()
  {
  // Make sure cities are entered, and aren't same

  if (sector1_o == '')
    {
    alert ("\nPlease choose a departure city.\n");
    okay = false;
    }
  else if (sector1_d == '')
    {
    alert ("\nPlease choose an arrival city.\n");
    okay = false;
    }
  else if (((sector2_o == 'XXX')&&(sector2_d != 'XXX'))||((sector2_o != 'XXX')&&(sector2_d == 'XXX')))
    {
    alert ("\nYour returning city pair is incomplete. Please\nchoose two cities or clear the selected one.\n");
    okay = false;
    }
  else
    okay = true;

  return okay;

}  // end checkCities


// -------------------------------------------------

function checkPassenger(form){
  var adults = document[form].ADULT.value;
  var children = document[form].CHILD.value;
  var infants = document[form].INFANT.value;

  if(adults == "00")
    adults = 0;
 
  if (( adults + children ) <= 0 ) {
    alert("\nYou must select at least one passenger.\n");
    return false;
  }

  if(parseInt(adults) == 0 && parseInt(children) > 0) {
    alert("\n\nChildren between the ages of 5 and under 14 years who will be traveling alone are considered unaccompanied minors.\n\nUnaccompanied minors may only travel on nonstop or direct flights (flights that make a stop but do not change aircraft). No connecting flights.\n\nFlights booked are subject to an \04485 fee per person each way for unaccompanied minors.\n\nOnce booked, please call 1-800-JETBLUE (538-2583) to provide us with the name, address and phone number, as it appears on the photo ID, of the person dropping off and picking up the child. The fee will also be assessed at that time.\n");
  }
  
  if (parseInt(infants) > parseInt(adults)) {
    alert("\nYou cannot have more Lap infants than Adults.\n");
    return false;
  }

  return true;
}



// call it trackFlightFlightView
function trackFlightFlightView(form)
  {
  var theForm = document.forms[form];
  var fromSet = theForm.depCity2.value != "" ? true : false;
  var toSet = theForm.arrCity2.value != "" ? true : false;
  var numSet = theForm.flightNumber.value != "" ? true : false;
  
 // the city section is filled out, track using it.
  if(theForm.depCity2.value != "" && theForm.arrCity2.value != "" )
    {    
    // 
    theForm.FlightOrgn.value = theForm.depCity2.value;
    theForm.FlightDestn.value = theForm.arrCity2.value;

	// return which one is checked
	var chkValue;
	for(var i = 0; i < theForm.ytt0.length; i++) {
		if(theForm.ytt0[i].checked == true) { 
			chkValue = theForm.ytt0[i].value; 
		}		
	}
	
    theForm.FlightDate.value = chkValue;
	theForm.FlightNum.value = '';
    theForm.submit();
    return true;
    }
  else if(theForm.flightNumber.value != "" && isANumber(theForm.flightNumber.value))
    {
    // return which one is checked
	var chkValue;
	for(var i = 0; i < theForm.ytt1.length; i++) {
		if(theForm.ytt1[i].checked == true) { 
			chkValue = theForm.ytt1[i].value; 
		}		
	}
	
    theForm.FlightDate.value = chkValue;
    theForm.FlightNum.value = theForm.flightNumber.value;
    theForm.submit();

    return true;
    }
  else if(!fromSet && !numSet)
    {
    alert("Please choose a departure city.");
    return false;
    }
  else if(!toSet && !numSet)
    {
    alert("Please choose an arrival city.");
    return false;
    }
  else if(!numSet || !isANumber(theForm.flightNumber.value))
    {
    alert("Please enter a valid flight number (one to four digits).");
    return false;
    }
  }


function getDateFlightView(form, dateField)
  {
  // all this to calculate a date from the radio boxes...

  var theForm = document.forms[form];
  var today = new Date();
  for (i=0; i<theForm.length; i++)
    {
    if (theForm[i].name == dateField && theForm[i].checked)
      {
      // millseconds in a day 
      // 60*60*24*1000
      // 86400000
      switch (theForm[i].value)
        {
        case "-1":
          var theDate = new Date(today.valueOf() - 86400000);
          break;
        case "0":
          var theDate = today;
          break;
        case "1":
          var theDate = new Date(today.valueOf() + 86400000);
          break;
        }
      }
    }

  var month = (theDate.getMonth() + 1);
  month = month < 10 ? "0" + month : month;
  var day = theDate.getDate() < 10 ? "0" + theDate.getDate() : theDate.getDate();

  return theDate.getFullYear() + month + day;
  }
