// <![CDATA[

var bdPlanes = [];

function BDAddPlane(planeName,runwayName)
{
	
	// Check if runaway exists
	if ( !function_exists(runwayName) ) {
		return false;
	}
	
	// Check if plane exists
	oPlane = BDGetPlane(planeName);
	if ( oPlane != false ) {
		bdPlanes = bdPlanes.remove(oPlane[0]);
		bdPlanes = bdPlanes.insert([planeName,runwayName],oPlane[0]);
	} else {
		bdPlanes[bdPlanes.length] = [planeName,runwayName];
	}
	
	return true;
	
}

function BDControlTower(someXML)
{
	
	var planeName = someXML.getElementsByTagName('plane');
	if ( planeName.length < 1 ) { BDView(someXML); return false; };
	planeName = planeName[0].firstChild.nodeValue;
	
	var plane = BDGetPlane(planeName);
	if ( plane != false ) {
		eval(plane[1][1] + '(someXML)');
	} else {
		BDView(someXML); return false;
	}
	
	return true;
	
}

function BDGetPlane(planeName)
{
	
	for ( key in bdPlanes ) {
		
		if ( bdPlanes[key][0] == planeName ) {
			return [key,bdPlanes[key]];
		}
		
	}
	
	return false;
	
}

function function_exists( function_name ) {
	// http://kevin.vanzonneveld.net
	// +	original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +	improved by: Steve Clay
	// +	improved by: Legaev Andrey
	// *	example 1: function_exists('isFinite');
	// *	returns 1: true

	if ( typeof function_name == 'string' ) {
		return (typeof window[function_name] == 'function');
	} else {
		return (function_name instanceof Function);
	}
	
}

// ----- From http://drakware.com/?e=3

var xmlreqs = new Array();

function CXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function xmlreqGET(url) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("GET",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
}

function xmlreqPOST(url,data) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("POST",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-length", data.length);
		xmlreqs[pos].xmlhttp.setRequestHeader("Connection", "close");
		xmlreqs[pos].xmlhttp.send(data);
	}
}

function xmlhttpChange(pos) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
			BDControlTower(xmlreqs[pos].xmlhttp.responseXML);
		} else {
			//handle_error();
		}
		xmlreqs[pos].freed = 1;
	}
}

// -----

function URLencode(sStr) {
	return encodeURIComponent(sStr);
}

// ]]>
