function presearch(airline, flightNum, dept, dest){
  $("#airline").val(airline);
  $("#airlines").val(airline);
  $("#flightNum").val(flightNum);
  flightzoo(dept, dest);
}

function flightzoo(dept, dest){

  if ($("#airline").val() == ""){var tempAir = $("#airlines").val(); $("#airline").val(tempAir);}
  var airline = $("#airline").val();
  var flightNum = $("#flightNum").val();
  if (airline == "" || flightNum == "" || flightNum == "Flight Number"){alert("Please tell us the airline and flight number");}else{

  $("#submit").attr("src","/images/loading.gif");
  $("#submita").attr("href","#");
  $("#howMany").hide();
  $("#imWorking").show();
  $("#more").hide();

  var node = document.getElementById('results');
  while (node.hasChildNodes()) {node.removeChild(node.lastChild);}

  var more = document.getElementById('moreContent');
  while (more.hasChildNodes()) {more.removeChild(more.lastChild);}

  $("#results").hide();

  if (airline == "" || flightNum == "" || flightNum == "Flight Number"){alert("Please tell us the airline and flight number");}else{
    var params = "airline=" + airline + "&flightNum=" + flightNum + "&dept=" + dept + "&dest=" + dest;
    $.post("/search.php",params,function(data)
	{
	$("#results").show("slow");

	var d = data.split("|");

	//-- no results --
	if (d[0] < 1)
		{
		var main = document.getElementById("results");
		var div = document.createElement("div");
		div.setAttribute('style','padding:5px;');
		var p = document.createElement("p");
		p.setAttribute("style","font-weight:bold;font-size:14px;");
		p.appendChild(document.createTextNode("TripAnomaly is not currently tracking that flight."));
		div.appendChild(p);
		var p = document.createElement("p");
		p.appendChild(document.createTextNode("Sometimes flights operate under codeshare agreements. For example, you may have a Delta flight but it's operated by Pinnacle Airlines and sold as Delta Connection. Double check to see if your flight is being operated by another carrier."));
		div.appendChild(p);
		main.appendChild(div);
		}

	if (d[0] > 1)
		{
		var main = document.getElementById("results");
		var div = document.createElement("div");
		div.setAttribute('style','padding:5px;text-align:center;font-weight:bold;color:#3366CC;font-size:16px;margin-bottom:10px;');
		div.appendChild(document.createTextNode("TripAnomaly found multiple city pairs for that flight."));
		main.appendChild(div);

		for (i = 1; i < d.length; i++)
			{
			var p = d[i].split("*");
			var choice = document.createElement("div");
			choice.setAttribute("class","choice");

			var div = document.createElement("div");
			div.setAttribute('style','float:left;width:250px;text-align:center;padding:5px;font-weight:bold;font-size:14px;');
			div.appendChild(document.createTextNode(p[1] + " (" + p[0] + ") To "));
			choice.appendChild(div);

			var div = document.createElement("div");
			div.setAttribute('style','float:left;width:250px;text-align:center;padding:5px;font-weight:bold;font-size:14px;');
			div.appendChild(document.createTextNode(p[3] + " (" + p[2] + ")"));
			choice.appendChild(div);

			var div = document.createElement("div");
			div.setAttribute('style','float:left;width:140px;text-align:center;padding:5px;');
			var a = document.createElement("a");
			a.setAttribute("href","javascript:flightzoo('" + p[0] + "','" + p[2] + "');");
			var img = document.createElement("img");
			img.setAttribute("src","/images/blueSelect.gif");
			img.setAttribute("alt","Select");
			img.setAttribute("title","Select");
			a.appendChild(img);
			div.appendChild(a);
			choice.appendChild(div);

			var div = document.createElement("div");
			div.setAttribute('style','clear:both;margin-bottom:10px;');
			choice.appendChild(div);

			main.appendChild(choice);
			}
		}

	if (d[0] == 1)
		{
		//-- d[1] holds the  main data --
		var p = d[1].split("*");
		var score = parseFloat(p[6]);
		var flightDistance = p[4];
		var flightTime = p[5];
		var carrierName = p[7];

		var main = document.getElementById("results");

		var div = document.createElement("div");
		div.setAttribute('style','float:left;width:85px;');
		var img = document.createElement("img");
		img.setAttribute("src","/images/flightmonitor/big" + score + ".jpg");
		div.appendChild(img);
		main.appendChild(div);

		var div = document.createElement("div");
		div.setAttribute('style','float:left;width:360px;');
		var h2 = document.createElement("h2");
		h2.appendChild(document.createTextNode(carrierName + " Flight Number " + flightNum));
		div.appendChild(h2);
		var para = document.createElement("p");
		para.appendChild(document.createTextNode(p[1] + " (" + p[0] + ") to " + p[3] + " (" + p[2] + ")"));
		div.appendChild(para);
		var para = document.createElement("p");
		para.appendChild(document.createTextNode("Flight Time: " + flightTime + " Distance: " + flightDistance + " miles"));
		div.appendChild(para);
		main.appendChild(div);

		var div = document.createElement("div");
		div.setAttribute("style","float:left;width:63px;");
		var fblink = "http://tripanomaly.com/?f=" + airline + "|" + flightNum + "|" + p[0] + "|" + p[2];
		var fbtitle = "My TripAnomaly Flight Ranking";
		var fb = document.createElement("a");
		fb.setAttribute("href","http://www.facebook.com/sharer.php?u=" + encodeURIComponent(fblink) + "&t=" + encodeURIComponent(fbtitle));
		fb.setAttribute("target","_blank");
		fb.setAttribute("rel","nofollow");
		var img = document.createElement("img");
		img.setAttribute("src","/images/fbshare.gif");
		img.setAttribute("style","float:right;margin-top:30px;");
		img.setAttribute("alt","Share on Facebook");
		img.setAttribute("title","Share on Facebook");
		fb.appendChild(img);
		div.appendChild(fb);

		main.appendChild(div);

		var div = document.createElement("div");
		div.setAttribute('style','clear:both;margin-bottom:10px;');
		main.appendChild(div);

		var div = document.createElement("div");
		div.setAttribute("style","height:5px;border-bottom:0px solid #F77502;");
		main.appendChild(div);

		var para = document.createElement("p");
		para.setAttribute("style","font-weight:bold;color:red;");
		para.appendChild(document.createTextNode("The TripAnomaly ranking for your flight is a " + score + " out of 10."));
		main.appendChild(para);

		var para = document.createElement("p");

		if (score < 2){para.appendChild(document.createTextNode("Based upon our analysis, TripAnomaly does not recommend this flight. The likelihood of getting to your destination on time is very poor."));}
		if (score > 1 && score < 4){para.appendChild(document.createTextNode("Based upon our analysis, the likelihood of getting to your destination on time is poor. There are more reliable flights available on this route."));}
		if (score > 3 && score < 8){para.appendChild(document.createTextNode("Based upon our analysis, the likelihood of getting to your destination on time is average. This is not the most consistent option, but your flight should be uneventful which is a good thing!"));}
		if (score > 7){para.appendChild(document.createTextNode("Based upon our analysis, TripAnomaly highly recommends this flight. The likelihood of getting to your destination on time is very good. Based upon comparisons with other flights on this route, your flight ranks near the top. If you're satisfied with your score, click the blue link below to book a cheap ticket!"));}

		main.appendChild(para);

		$("#more").show("slow");

		var h3 = document.createElement("h3");
		h3.appendChild(document.createTextNode("How Does " + carrierName + " Rate Overall On This Route?"));
		more.appendChild(h3);

		//-- add carrier comparables --
		var allCarriers = d[2].split("%");
		for (i in allCarriers)
			{
			var comparables = document.createElement("div");
			comparables.setAttribute("class","comp");

			var c = allCarriers[i].split("*");
			var div = document.createElement("div")
			div.setAttribute("class","left");
			div.setAttribute("style","width:100px;");
			var img = document.createElement("img");
			img.setAttribute("src","/images/carriers/" + c[0] + ".gif");
			div.appendChild(img);
			comparables.appendChild(div);
			
			var div = document.createElement("div")
			div.setAttribute("class","left");
			div.setAttribute("style","width:230px;font-weight:bold;");
			div.appendChild(document.createTextNode(c[1]));
			comparables.appendChild(div);

			var div = document.createElement("div")
			div.setAttribute("class","left");
			div.setAttribute("style","width:120px;font-weight:bold;color:green;");
			div.appendChild(document.createTextNode("Avg Score: " + c[2]));
			comparables.appendChild(div);

			var div = document.createElement("div")
			div.setAttribute("class","clear");
			comparables.appendChild(div);

			more.appendChild(comparables);
			}
		
		var h3 = document.createElement("h3");
		h3.appendChild(document.createTextNode("Route Map"));
		more.appendChild(h3);

		var params = "dept=" + p[0] + "&dest=" + p[2];
		$.post("/coordinates.php",params,function(coords)
			{
			var c = coords.split("|");
			var iframeParams = "deptLat=" + c[0] + "&deptLong=" + c[1] + "&destLat=" + c[2] + "&destLong=" + c[3];
			var mp = document.getElementById("map_canvas");
			var iframe = document.createElement("iframe");
			iframe.setAttribute("src","/map.php?" + iframeParams);
			iframe.setAttribute("id","gmapfz");
			iframe.setAttribute("width","480");
			iframe.setAttribute("height","480");
			iframe.setAttribute("frameborder","0");
			iframe.setAttribute("scrolling","no");
			iframe.setAttribute("marginwidth","0");
			iframe.setAttribute("marginheight","0");

			more.appendChild(iframe);
			});

		}

	$("#submit").attr("src","/images/submit.gif");
	$("#submita").attr("href","javascript:flightzoo('','');");
	$("#imWorking").hide();
	})
  }
 var data = "";
 var d = "";
 }
}

function randomFZ(){
  var params = "r=1";
    $.post("/random.php",params,function(data){window.location="/?f=" + data; });
}
