// JavaScript Document

function show(idLayer) {document.getElementById(idLayer).style.display='block';}
function hide(idLayer) {document.getElementById(idLayer).style.display='none';}

function change(id, newClass) {
identity=document.getElementById(id);
identity.className=newClass;
}


var intPlode = 0;

function divPlode(objDv,strType,strOn,strOff)
{
	var childDiv = objDv.getElementsByTagName("div");
	var strImg = "";
	//explosion
	if(intPlode==1)
	{
		for(var i=0;i<childDiv.length;i++)
		{
			if(childDiv[i].getAttribute("name")=="divX")
			{
				if(childDiv[i].style.display=='none')
				{
					childDiv[i].style.display = 'block';
					intPlode = 11;
				}
				else
				{
					childDiv[i].style.display = 'none';
					intPlode = 12;
				}
				i = childDiv.length;
			}
		}
	}
	else if(intPlode==2)
	{
		for(var i=0;i<childDiv.length;i++)
		{
			if(childDiv[i].name=="divX")
			{
				childDiv[i].style.display = 'block';
				i = childDiv.length;
			}
		}
		intPlode = 11;
	}
	else if(intPlode==3)
	{
		for(var i=0;i<childDiv.length;i++)
		{
			if(childDiv[i].name=="divX")
			{
				childDiv[i].style.display = 'none';
				i = childDiv.length;
			}
		}
		intPlode = 12;
	}
	//displaying
	if(strType!=undefined)
	{
		if(strOn!=undefined&&strOff!=undefined)
		{
			if(intPlode==11)
			{
				strImg = strOff;
			}
			else if(intPlode==12)
			{
				strImg = strOn;
			}
		}
		if(strType=="img")
		{
			var childImg = objDv.getElementsByTagName("img");
			for(var i=0;i<childImg.length;i++)
			{
				if(childImg[i].getAttribute("name")=="imgX")
				{
					childImg[i].src = strImg;
					i = childImg.length;
				}
			}
		}
	}
}


/* START DESTINATION PAGE LOCATION BREAKDOWN */
$(document).ready(function() {

	var destinations = new Array();

	destinations[0] = new Array();
	destinations[0]["code"] = "BFI";
	destinations[0]["name"] = "Seattle's Boeing Field";
	destinations[0]["area"] = "Northwest";
	destinations[0]["valid_arrivals"] = new Array("PDX","PDT","ONP","AST");

	destinations[1] = new Array();
	destinations[1]["code"] = "PDX";
	destinations[1]["name"] = "Portland Intl. Airport";
	destinations[1]["area"] = "Northwest";
	destinations[1]["valid_arrivals"] = new Array("PDT","BFI","ONP","AST");

	destinations[2] = new Array();
	destinations[2]["code"] = "PDT";
	destinations[2]["name"] = "Eastern Oregon Regional Airport At Pendleton";
	destinations[2]["area"] = "Northwest";
	destinations[2]["valid_arrivals"] = new Array("PDX","BFI");

	destinations[3] = new Array();
	destinations[3]["code"] = "ONP";
	destinations[3]["name"] = "Newport Municipal Airport";
	destinations[3]["area"] = "Northwest";
	destinations[3]["valid_arrivals"] = new Array("PDX","BFI","AST");

	destinations[4] = new Array();
	destinations[4]["code"] = "AST";
	destinations[4]["name"] = "Astoria Regional Airport";
	destinations[4]["area"] = "Northwest";
	destinations[4]["valid_arrivals"] = new Array("PDX","BFI","ONP");

	destinations[5] = new Array();
	destinations[5]["code"] = "YKM";
	destinations[5]["name"] = "Yakima Air Terminal / Mcallister Field";
	destinations[5]["area"] = "Northwest";
	destinations[5]["valid_arrivals"] = new Array("PDX");

	destinations[6] = new Array();
	destinations[6]["code"] = "MEM";
	destinations[6]["name"] = "Memphis Intl. Airport";
	destinations[6]["area"] = "Mid-South";
	destinations[6]["valid_arrivals"] = new Array("JBR","HRO","HOT","ELD","SLN","MCI");

	destinations[7] = new Array();
	destinations[7]["code"] = "HOT";
	destinations[7]["name"] = "Hot Springs Memorial Field Airport";
	destinations[7]["area"] = "Mid-South";
	destinations[7]["valid_arrivals"] = new Array("MEM","HRO","ELD","JBR","MCI");

	destinations[8] = new Array();
	destinations[8]["code"] = "HRO";
	destinations[8]["name"] = "Harrison Boone County Airport";
	destinations[8]["area"] = "Mid-South";
	destinations[8]["valid_arrivals"] = new Array("ELD","HOT","JBR","MEM","MCI","SLN");

	destinations[9] = new Array();
	destinations[9]["code"] = "JBR";
	destinations[9]["name"] = "Jonesboro Municipal Airport";
	destinations[9]["area"] = "Mid-South";
	destinations[9]["valid_arrivals"] = new Array("ELD","HRO","HOT","MCI","MEM");

	destinations[10] = new Array();
	destinations[10]["code"] = "ELD";
	destinations[10]["name"] = "El Dorado South Arkansas Regional Airport At Goodwin Field";
	destinations[10]["area"] = "Mid-South";
	destinations[10]["valid_arrivals"] = new Array("HRO","HOT","JBR","MCI","MEM");
	
	destinations[11] = new Array();
	destinations[11]["code"] = "MCI";
	destinations[11]["name"] = "Kansas City International Airport";
	destinations[11]["area"] = "Mid-South";
	destinations[11]["valid_arrivals"] = new Array("ELD","HRO","HOT","JBR","MEM","SLN");
	
	destinations[12] = new Array();
	destinations[12]["code"] = "SLN";
	destinations[12]["name"] = "Salina Municipal Airport";
	destinations[12]["area"] = "Mid-South";
	destinations[12]["valid_arrivals"] = new Array("HRO","MCI","MEM");
	

	$("select#origin").change(function() {
		for (var selected_destination = 0;selected_destination < destinations.length;selected_destination++)
		{
			if (destinations[selected_destination]["code"] == $(this).val())
			{
				$("select#dest optgroup,select#dest option").remove();
				$(document.createElement("option")).attr("value","").text("").appendTo($("select#dest"));

				for (var valid_destination = 0;valid_destination < destinations[selected_destination]["valid_arrivals"].length;valid_destination++)
				{
					for (var add_destination = 0;add_destination < destinations.length;add_destination++)
					{
						if (destinations[add_destination]["code"] == destinations[selected_destination]["valid_arrivals"][valid_destination])
						{
							if ($("select#dest optgroup[label=" + destinations[add_destination]["area"] + "]").length == 0)
							{
								$(document.createElement("optgroup")).attr("label",destinations[add_destination]["area"]).appendTo($("select#dest"));
							}

							$(document.createElement("option")).attr("value",destinations[add_destination]["code"]).text(destinations[add_destination]["name"] + " - " + destinations[add_destination]["code"]).appendTo($("select#dest optgroup[label=" + destinations[add_destination]["area"] + "]"));
						}
					}
				}
			}
		}
	});

});
/* END DESTINATION PAGE LOCATION BREAKDOWN */


	function refillDestList(destBox, labelDest, unusedOption)
	{
		var destValue = destBox.options[ destBox.selectedIndex ].value
		destBox.length = 1
		destBox.options[0] = new Option(labelDest)
		destBox.options[0].value = ""
		var i = 1
		var selIndex = 0

		if ( unusedOption )
		{
			destBox.length += 1
			var newOption = new Option( unusedOption )
			destBox.options[i] = newOption
			destBox.options[i].value = ""
			i++
		}

		for (var j = 0; j < Dests.length-1; j++)
		{
			destBox.length += 1
			var newOption = new Option(eval('s' + Dests[j]))
			destBox.options[i+j] = newOption
			destBox.options[i+j].value = Dests[j]
			if (destValue == Dests[j]) {selIndex = i+j}
		}

		destBox.selectedIndex = selIndex

	}	// end refillDestList

	function correctDatePadding(date){		
		year_end = date.indexOf("-", 0);
		year = date.substr(0, year_end);		
		date = date.substr(year_end+ 1);
		
		
		month_end = date.indexOf("-", 0);		
		month = date.substr(0, month_end);
		date = date.substr(month_end);

		day = date.substr(1);

		
		if(month.length == 1)
			month = "0" + month;
		if(day.length == 1)
			day = "0" + day;
			
		return year + "-" + month + "-" + day;
	}

	function toggleReturn() {		
		elem = document.getElementById('ret_date_box');		
		if(document.search.ret_check.checked){
			elem.style.display = "block";
			
		}
		else{
			elem.style.display = "none";	
		}
		elem.blur();
	}
	
	function checkSearchForm()
	{
		okay = true;

		// Make sure cities are entered

		if (document.search.origin.value=='')
		{
			alert ("\nPlease select the city you will be departing from.\n")
			okay = false
		}
		else if (document.search.dest.value=='')
		{
			alert ("\nPlease choose your destination.\n")
			okay = false
		}

		else if (document.search.dep.value == 'yyyy/mm/dd')
		{
			alert ("\nPlease select a valid departure date.\n")
			okay = false
		}
		else if(document.search.ret_check.checked){
			if(document.search.ret.value == 'yyyy/mm/dd'){
				alert("\nPlease select a valid return date.\n")
				okay = false;
			}
		}

		
		if(okay) {		
			//gonna build me a form here!!!
			//stub = document.getElementById('stub');
			
			new_form = document.createElement('form');
			new_form.setAttribute('method', 'get');
			new_form.setAttribute('action', 'http://seaportair.tikaero.com/Default.aspx');
			new_form.setAttribute('id', 'new_form');
										
			ori = document.createElement("input");
			ori.setAttribute("type", "hidden");
			ori.setAttribute("name", "ori");
			ori.setAttribute("value", document.search.origin.value);
			new_form.appendChild(ori);
													
			des = document.createElement("input");
			des.setAttribute("type", "hidden");
			des.setAttribute("name", "des");
			des.setAttribute("value", document.search.dest.value);
			new_form.appendChild(des);
													
			dep = document.createElement("input");
			dep.setAttribute("type", "hidden");
			dep.setAttribute("name", "dep");
			dep.setAttribute("value", correctDatePadding(document.search.dep.value));
			new_form.appendChild(dep);
			
			if(document.search.ret_check.checked){
				ret = document.createElement("input");
				ret.setAttribute("type", "hidden");
				ret.setAttribute("name", "ret");
				ret.setAttribute("value", correctDatePadding(document.search.ret.value));
				new_form.appendChild(ret);
			}			
			
			adt = document.createElement("input");
			adt.setAttribute("type", "hidden");
			adt.setAttribute("name", "adt");
			adt.setAttribute("value", document.search.adt.value);
			new_form.appendChild(adt);
			
			chd = document.createElement("input");
			chd.setAttribute("type", "hidden");
			chd.setAttribute("name", "chd");
			chd.setAttribute("value", "0");
			new_form.appendChild(chd);
			
			inf = document.createElement("input");
			inf.setAttribute("type", "hidden");
			inf.setAttribute("name", "inf");
			inf.setAttribute("value", "0");
			new_form.appendChild(inf);										
			
			document.getElementById('frm').appendChild(new_form); // add the new form to the div
			
			document.getElementById('new_form').submit(); // submit the form we just created
			
		}

		return false;

	}  // end checkSearchForm

