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 = "es_MX";
}

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("Selecciona una fecha de salida.");
    return false;
    }
  else if(trip_type == "RoundTrip" && !returnSet) {
    alert("Selecciona una fecha de regreso.");
    return false;
    }
  else {
    if(cal0Hidden.value == cal1Hidden.value) {
      if(!confirm("Tu fecha de regreso es la misma que tu fecha de salida. \277Es intencional este viaje tan corto?"))
        return false;  
      }
    if(todayStr == cal0Hidden.value) 
      alert("Debido a que has seleccionado la fecha de hoy, s\363lo se mostrar\341n en la pr\363xima p\341gina los vuelos que partan al menos 1 hora y 30 minutos despu\351s de la hora actual.");
    return true;
    }
  }	// checkDate


function checkCities()
  {
  // Make sure cities are entered, and aren't same

  if (sector1_o == '')
    {
    alert ("\nElije una ciudad de salida.\n");
    okay = false;
    }
  else if (sector1_d == '')
    {
    alert ("\nElije una ciudad de llegada.\n");
    okay = false;
    }
  else if (((sector2_o == 'XXX')&&(sector2_d != 'XXX'))||((sector2_o != 'XXX')&&(sector2_d == 'XXX')))
    {
    alert ("\nTu par de ciudades de regreso est\341 incompleto. Selecciona\ndos ciudades o elimina la que has seleccionado.\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("\nDebes seleccionar al menos un pasajero.\n");
    return false;
  }

  if(parseInt(adults) == 0 && parseInt(children) > 0) {
    alert("n\nLos ni\361os de entre 5 a\361os de edad y menores de 14 a\361os que viajen solos se consideran menores sin acompa\361ante.\n\nLos menores sin acompa\361ante solamente pueden viajar en vuelos sin escala o vuelos directos (vuelos que hacen escala pero no requieren cambio de avi\363n). No se admiten en vuelos con conexi\363n.\n\nLos vuelos reservados antes del 1 de Mayo de 2008, se encuentran sujetos a un cargo de \04450 por persona, por cada viaje de una v\355a para menores sin acompa\361ante. A partir del 1 de Mayo de 2008, el cargo ser\341 de \04475.\n\nUna vez realizada la reservaci\363n, llama al 1-800-JETBLUE (538-2583) para suministrar el nombre, direcci\363n y n\372mero de tel\351fono, tal como aparece en la identificaci\363n con foto de la persona que llevar\341 y recoger\341 al ni\361o. La tarifa tambi\351n ser\341 calculada en ese momento.\n");
  }
  
  if (parseInt(infants) > parseInt(adults)) {
    alert("\nNo puedes tener m\341s Infantes que Adultos.\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("Elije una ciudad de salida.");
    return false;
    }
  else if(!toSet && !numSet)
    {
    alert("Elije una ciudad de llegada.");
    return false;
    }
  else if(!numSet || !isANumber(theForm.flightNumber.value))
    {
    alert("Ingresa un n\372mero de vuelo v\341lido (uno a cuatro d\355gitos).");
    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;
  }
