var __airportList =
'<result><contains>ALL</contains><airport><code>HAN</code><name>Hanoi</name><region>false</region><city>false</city><from>none</from><subairport/><level>0</level></airport><airport><code>01D</code><name>Deutschland - Nord</name><region>true</region><city>false</city><from>none</from><subairport><airport><code>BRE</code><name>Bremen</name></airport><airport><code>HAM</code><name>Hamburg</name></airport><airport><code>HAJ</code><name>Hannover</name></airport><airport><code>FMO</code><name>Münster</name></airport><airport><code>RLG</code><name>Rostock Laage</name></airport></subairport><level>0</level></airport><airport><code>BRE</code><name>Bremen</name><region>false</region><city>false</city><from>REGION</from><level>1</level><subairport/></airport><airport><code>HAM</code><name>Hamburg</name><region>false</region><city>false</city><from>REGION</from><level>1</level><subairport/></airport><airport><code>HAJ</code><name>Hannover</name><region>false</region><city>false</city><from>REGION</from><level>1</level><subairport/></airport><airport><code>FMO</code><name>Münster</name><region>false</region><city>false</city><from>REGION</from><level>1</level><subairport/></airport><airport><code>RLG</code><name>Rostock Laage</name><region>false</region><city>false</city><from>REGION</from><level>1</level><subairport/></airport></result>';

function AirportLoader(anURL)
{
  this.toBeUpdatedLists=new Array();
  this.airportList=new Array();
  this.codeList=new Array();
  this.result;
  this.url=anURL;
  this.listener=new Array();
}



AirportLoader.prototype.getAirportList=function()
{
  return this.airportList;
}

AirportLoader.prototype.addListToUpdate=function(anArray)
{
  this.toBeUpdatedLists[this.toBeUpdatedLists.length]=anArray;
}

AirportLoader.prototype.addLoadListener=function(aListener)
{
	if (this.listener)
  		this.listener[this.listener.length]=aListener;
}
/**
 * receives the map content. Implementation of the XML ResponseListener interface.
 * Used for initial loading of the map by an own RequestProcessor.
 **/
AirportLoader.prototype.responseReceived=function(aResponseEvent)
{
  this.result = aResponseEvent.response;
  if(this.result == null)
  {
    alert("AirportLoader: No result received");
    return;
  }
  //alert("AirportLoader: result received"+this.result);
  var theXmlReader = new XmlReader(this.result);
  var theAirportNodes=theXmlReader.getNodesByPath('result.airport');

  if (theAirportNodes != null)
  {
    //alert('No. of airportNodes: '+theAirportNodes.length);

    this.airportList=this.createList(theXmlReader, theAirportNodes, true);
    this.updateNameLists(this.airportList);
  }
  else
  {
    //alert("No airportNodes found");
    return;
  }
}

AirportLoader.prototype.createList=function(aReader, aNodeList, aLevel)
{
	var theList= new Array();
    var theNameArray=new Array();
    var i;
    var theName;
    var theCode;
    var theNameNode;
    var theCodeNode;

    for(i=0;i<aNodeList.length;i++)
    {
      theNameNode=aReader.getFirstChildNode(aNodeList[i],'name');
      theCodeNode=aReader.getFirstChildNode(aNodeList[i],'code');
      //alert('Name:'+theXmlReader.getText(theNameNode));
      theName=aReader.getText(theNameNode);
      theCode=aReader.getText(theCodeNode);
	  var theAirport = new Airport(theName, theCode);
	  if (aLevel)
	  	theAirport.setSub(this.createSubports(aReader, aNodeList[i]));
      //theNameArray[theNameArray.length]=theAirport;
      //theList[theName]=theAirport;
	  //this.airportList[theCode]=theAirport;
	  theList[theList.length]=theAirport;

    }
	if (theList.length==0)
	{
		return null;
	}
	return theList;
}

AirportLoader.prototype.createSubports=function(aReader, aNode)
{
	var theNode = aReader.getFirstChildNode(aNode, 'subairport');
	if (theNode)
	{
	  var theArray = aReader.getChildNodes(theNode, 'airport');
	  if (theArray != null && theArray.length>0)
	  return  this.createList(aReader, theArray, false);
	}
	return null;
}

AirportLoader.prototype.updateNameLists=function(aNameArray)
{
   for(var i = 0; i<this.listener.length; i++)
   {
   	this.listener[i].updateList(aNameArray);
   }
}

AirportLoader.prototype.load=function(aWord)
{

 var theParser = new DOMImplementation();
 try {
   var theDomObject = theParser.loadXML(__airportList);
   var theResponse = new ResponseEvent (this, theDomObject);
   this.responseReceived(theResponse);
 }
 catch(anException)
 {
  alert("Creation of Airport Loading XML failed: "+anException.toString());
 }

}// JavaScript Document


/**
 * Starts the initial loading of the map.
 */
AirportLoader.prototype.loadAsync=function(aKey)
{
  var theReq = new RequestProcessor();
  //theReq.setAsync(false);
  theReq.addResponseEventListener(this);
  theReq.setHttpType('GET');
  theReq.setResponseHandler(new TextXMLResponseHandler());
  var theURL = this.url;
  	if (aKey!=null)
  	{
  	  theURL=this.url+"?airportname="+aKey;
 	}
  //document.formular.keys.value="Sending URL "+theURL;
  //alert("Sending "+theURL);
  theReq.sendRequest(theURL,'');
  //alert("AirportLoader: started");
}// JavaScript Document



function Airport(aName, aCode)
{
	this.name=aName;
	this.code=aCode;
	this.subairports=null;
	this.subcount=-1;
}

Airport.prototype.toString=function()
{
	return this.name;
}

Airport.prototype.setSub=function(anAirportList)
{
	this.subairports=anAirportList;
	if(anAirportList)
	{
		this.subcount=anAirportList.length;
	}
}

Airport.prototype.addSub=function(anAirport)
{
 if (this.subairports==null)
 {
 	this.subairports=new Array();
 }
 this.subcount++;
 this.subairports[this.subcount]=anAirport;
}

Airport.prototype.hasSub=function()
{
 return (this.subcount>-1);
}

Airport.prototype.getSubports=function()
{
 return (this.subairports);
}


