var selectedFlightDetails;

function filterArrivalAirports(form) {
	if(form == "bookFlightsSidebar") {
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				var arrivalBoxLeft = document.getElementById("arrivalBoxLeft");
				arrivalBoxLeft.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.open("POST","ajax/booking.php",true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send("dep=" + document.getElementById("departureBoxLeft").value);
	} else {
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				var arrivalBoxRight = document.getElementById("arrivalBoxRight");
				arrivalBoxRight.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.open("POST","ajax/booking.php",true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send("dep=" + document.getElementById("departureBoxRight").value);
	}
}

function changeRowStyles(rowId) {
	if(selectedFlightDetails != rowId) {
		var row = document.getElementById(rowId);
		row.style.backgroundColor = "#336699";
		row.style.color = "#FFF";
	}
}

function changeRowStylesBack(rowId,style) {
	if(selectedFlightDetails != rowId) {
		var row = document.getElementById(rowId);
		if(style=="lightTableRow") {
			row.style.backgroundColor = "#DDD";
			row.style.color = "#000";
		} else {
			row.style.backgroundColor = "#FFF";
			row.style.color = "#000";
		}
	}
}

function showFlightDetails(rowId,flightNum,pid) {
	if(selectedFlightDetails && selectedFlightDetails != "row_" + rowId) {
		document.getElementById(selectedFlightDetails + "_details").style.display = "none";
		document.getElementById(selectedFlightDetails).style.backgroundColor = "";
		document.getElementById(selectedFlightDetails).style.color = "#000";
	}
	selectedFlightDetails = "row_" + rowId;
	document.getElementById("row_" + rowId + "_details").style.display = "";
	document.getElementById("row_" + rowId).style.backgroundColor = "#003366";
	if(flightNum != 0) {
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				document.getElementById("row_" + rowId + "_details_box").innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.open("POST","ajax/scheduleFlightDetails.php",true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send("flightID=" + flightNum + "&pid=" + pid);
	}
}

function bookFlight(flightNum,pid) {
	document.getElementById("linkStrip_" + flightNum).innerHTML = "Loading...";
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			showFlightDetails(selectedFlightDetails.substr(4,10),flightNum,pid);
		}
	}
	xmlhttp.open("POST","ajax/booking.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("book=true&flightID=" + flightNum + "&pid=" + pid);
}

function unbookFlight(flightNum,pid) {
	document.getElementById("linkStrip_" + flightNum).innerHTML = "Loading...";
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			showFlightDetails(selectedFlightDetails.substr(4,10),flightNum,pid);
		}
	}
	xmlhttp.open("POST","ajax/booking.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("unbook=true&flightID=" + flightNum + "&pid=" + pid);
}

function bookFlightFull(flightNum,pid) {
	document.getElementById("linkStrip_" + flightNum).innerHTML = '<span style="font-size:15px;font-weight:bold">Loading...</span>';
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var span = '<span style="font-size:15px;text-decoration:underline;font-weight:bold">';
			document.getElementById("linkStrip_" + flightNum).innerHTML = 
			'<a href="#booking" onClick="unbookFlightFull(\'' + flightNum + '\',\'' + pid + '\')">' + 
			span + 'Unbook Flight</span></a>' +
			' | <a href="?page=briefing&id=' + flightNum + '">' + 
			span + 'Pilot Briefing</span></a>';
		}
	}
	xmlhttp.open("POST","ajax/booking.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("book=true&flightID=" + flightNum + "&pid=" + pid);
}

function unbookFlightFull(flightNum,pid) {
	document.getElementById("linkStrip_" + flightNum).innerHTML = '<span style="font-size:15px;font-weight:bold">Loading...</span>';
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var span = '<span style="font-size:15px;text-decoration:underline;font-weight:bold">';
			document.getElementById("linkStrip_" + flightNum).innerHTML = 
			'<a href="#booking" onClick="bookFlightFull(\'' + flightNum + '\',\'' + pid + '\')">' + span + 'Book Flight</span></a>';
		}
	}
	xmlhttp.open("POST","ajax/booking.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("unbook=true&flightID=" + flightNum + "&pid=" + pid);
}
