function jbAppendCityPair(from, to, date)
  {
  var track = "0,"+from +","+to+","+date;
  setTracking(track);
  }

function jbAppendFlightNo(number, date)
  {
  var track = "1,"+number+","+date;
  setTracking(track);
  }

function setTracking(track)
  {
  var tracked = getCookie(trackingCookie);
  var trackedParts = new Array();

  // if a cookie exists, remove the last tracked item and slide the new one onto
  // the front
  if (tracked)
    {
    deleteCookie(trackingCookie);
    trackedParts = tracked.split("//")
    trackedParts.pop();
    trackedParts.unshift(track);
    newTracked = trackedParts.join("//");
    }
  else 
    {
    newTracked = track + "//";
    }
  
  setCookie(trackingCookie, newTracked, 48);

  }

function getDate(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].type == "radio" && theForm[i].checked)
    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;
        }
      }
    }
  return (theDate.getMonth() + 1) + "/" + theDate.getDate() + "/" + theDate.getFullYear();
  }


function trackFlight(form)
  {
  var theForm = document.forms[form];
  var fromSet = theForm.depCity0.value != "" ? true : false;
  var toSet = theForm.arrCity0.value != "" ? true : false;
  var numSet = theForm.flightNumber.value != "" ? true : false;
  
  // the city section is filled out, track using it.
  if(theForm.depCity0.value != "" && theForm.arrCity0.value != "" )
    {
    jbAppendCityPair(theForm.depCity0.value, 
                     theForm.arrCity0.value, 
                     getDate(form, "trackDate1"));
    return true;
    }
  else if(theForm.flightNumber.value != "" && isANumber(theForm.flightNumber.value))
    {
    jbAppendFlightNo(theForm.flightNumber.value, getDate(form, "trackDate2"));
    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;
    }
  }

// call it trackFlightFlightView
function trackFlightFlightView(form)
  {
  var theForm = document.forms[form];
  var fromSet = theForm.depCity0.value != "" ? true : false;
  var toSet = theForm.arrCity0.value != "" ? true : false;
  var numSet = theForm.flightNumber.value != "" ? true : false;
  
  // the city section is filled out, track using it.
  if(theForm.depCity0.value != "" && theForm.arrCity0.value != "" )
    {    
    // 
    theForm.depart.value = theForm.depCity0.value;
    theForm.arrive.value = theForm.arrCity0.value;
    theForm.date.value = getDateFlightView(form, "trackDate1");
    theForm.submit();
    return true;
    }
  else if(theForm.flightNumber.value != "" && isANumber(theForm.flightNumber.value))
    {
    // set the fields to work the "flighttrack.com"
    theForm.date.value = getDateFlightView(form, "trackDate2");
    theForm.flight.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;
  }

function trackInit()
  {
  var tracked = getCookie(trackingCookie);
  var trackedText = "<div style='padding-bottom:5px;'>Recently tracked: ";
  
  if (tracked)
    {
    // change the classes to show this correctly

    track = tracked.split("//");
    for (var i=0; i<track.length; i++)
      {
      trackParts = track[i].split(",");

      if (trackParts.length > 1)
        {
        if (trackParts[0] == 0)
          {
          trackedText += "<a href='./?from=" + trackParts[1] + "&to=" + trackParts[2] + "&trackDate=" + trackParts[3] + "'>" + 
                            trackParts[1] + " to " + trackParts[2] + 
                          "</a> ";
          }
        else if (trackParts[0] == 1)
          {
          trackedText += "<a href='./?flightNumber=" + trackParts[1] + "&trackDate=" + trackParts[2] + "'>" + 
                            "Flight " + trackParts[1] +
                          "</a> ";
          }
        }      
      }
    $("fsContentTrackedWrapper").innerHTML = trackedText.substr(0, trackedText.length-2) + "</div>";
    $("fsControlTop").className = "tracked";

    }
  else
    {
    // must remove the div - ie bug
    var d = document.getElementById("cookieJar");
    var olddiv = document.getElementById("fsContentTrackedWrapper");
    d.removeChild(olddiv);
    $("fsControlTop").className="";
    }
  }

var trackingCookie = "jbHpFnTracked";


