// JavaScript Document

function ResponseHandlerFactory()
{
    
}

ResponseHandlerFactory.prototype.createResponseHandler=function(aRequestor)
{

   if (window.ActiveXObject)
   {
    return new XMLResponseHandler(aRequestor);
   }
   else
   {
    return new XMLResponseHandler(aRequestor);
    //return new TextXMLResponseHandler(aRequestor);
   }
}





// -------------- TExt XML Response Handler --------------------------



function TextXMLResponseHandler(aRequestor)
{
  this.flightRequestor=aRequestor;
  this.requestStart=null;
  if(this.flightRequestor)
    this.flightRequestor.addListener(this);
  this.xStat=new StatisticFactory().createStatistic('TextXMLResponseHandler');
}

TextXMLResponseHandler.prototype.checkStatus=function(aHttpRequest){
  if (aHttpRequest.status == 200) {
    return true;
  } else {
    
    return false;
    // die Anfrage enthielt Fehler;
    // die Antwort war z.B. 404 (nicht gefunden)
    // oder 500 (interner Server-Fehler)
  }
}
 

TextXMLResponseHandler.prototype.requestPerformed=function(anEvent)
{
  if (anEvent.action =='STARTED')
    this.xStat.incCall('requestPerformed');
}

TextXMLResponseHandler.prototype.handleResponse=function(aFlightRequestor, aHttpRequest, aResponseEventListener)
{
  var theXML=null;
  
  
  if (aHttpRequest.readyState == 4) {
       this.xStat.endCall('requestPerformed');
       this.xStat.incCall('handleResponse_DOMCreation');
      if (this.checkStatus(aHttpRequest)){
  	 	//alert("XML Text Handler");
          theXML = aHttpRequest.responseText;
      aFlightRequestor.xml=theXML;
  		if(theXML == null || !theXML)
  		{
  			alert("theXML = null");
  		}
  		//alert('xml created? - ['+theXML+']');
  		// ResponseHeaders:"+aHttpRequest.getAllResponseHeaders());
  		//alert("xml created? - ["+theXML+"] ResponseHeaders:"+aHttpRequest.statusText);
  		var theDomObject; 
  			
  	  try {
   		  //alert("Trying to create ActiveXObject: "+aHttpRequest.getAllResponseHeaders());
  		  //theDomObject =new ActiveXObject("Microsoft.XMLDOM");
  		  var theParser = new DOMImplementation();
        theDomObject = theParser.loadXML(theXML);
        this.xStat.endCall('handleResponse_DOMCreation');
        this.xStat.incCall('handleResponse_ViewCreation');
        //var theXMLTree = theDomObject.getDocumentElement();
  		  //alert("ActiveXObject created ");
  		  //theDomObject.async="false";
  		  //alert("ActiveXObject loadingXML ");
  		  //theDomObject.loadXML(theXML);
  		  
        
        if(!theDomObject.documentElement)
        {
          alert("null - XML["+aHttpRequest.getAllResponseHeaders()+"]");
          //document.write('<p>['+theXML+']</p>');
          
          return;
         } 
  		  }
  		  catch(anException)
  		  {
  		   alert("Creation of XML failed: "+anException.toString()+" "+aHttpRequest.getAllResponseHeaders());
  		   alert(theXML);
  		   if (this.flightRequestor)
          this.flightRequestor.responseReceived('END');
  		   //GetValues(anException);
  		   return;
  		  }
    		
        if (aResponseEventListener == null || aResponseEventListener.length==0)
        {
          alert("No ResponseEventListener set!");          
        }
        else
        {
          var theResponseEvent = new ResponseEvent(this, theDomObject);
          var i;
          for (i= 0;i<aResponseEventListener.length; i++)
          {
            aResponseEventListener[i].responseReceived(theResponseEvent);
          }
        }
      }
      else
      {
       alert("HttpRequest.status="+aHttpRequest.status);
      }
      
      this.xStat.endCall('handleResponse_ViewCreation');
      
      if (this.flightRequestor)
      {
        this.flightRequestor.responseReceived('END');
      }
      
    } else {
      //doing nothing
      //alert("NOT READY");
    }
    
  	
}

// -------------- XML Response Handler --------------------------


function XMLResponseHandler(aRequestor)
{
  this.flightRequestor=aRequestor;
  if (this.flightRequestor)
    this.flightRequestor.addListener(this);
  this.xStat=new StatisticFactory().createStatistic('XMLResponseHandler');
}

XMLResponseHandler.prototype.checkStatus=function(aHttpRequest){
  if (aHttpRequest.status == 200) {
    return true;
  } else {
    
    return false;
  }
}


XMLResponseHandler.prototype.requestPerformed=function(anEvent)
{
  if (anEvent.action =='STARTED')
    this.xStat.incCall('requestPerformed');
}

XMLResponseHandler.prototype.toString=function()
{
  return 'XMLRequestHandler';
}

XMLResponseHandler.prototype.handleResponse=function(aFlightRequestor, aHttpRequest, aResponseEventListener)
{
  var theXML;
  if (aHttpRequest.readyState == 4) {
    if (this.checkStatus(aHttpRequest)){
       this.xStat.endCall('requestPerformed');
      try {        
        this.xStat.incCall('handleResponse_DOMCreation');
	 	//alert("XML Handler");
        theXML = aHttpRequest.responseXML;
        this.xStat.endCall('handleResponse_DOMCreation');   
        aFlightRequestor.xml=theXML;
      	if(theXML == null || !theXML)
      	{
      	  alert("theXML = null");
      		return;
      	}
		//alert("xml created? - ["+theXML+"] ResponseHeaders:"+aHttpRequest.getAllResponseHeaders());
		//alert("xml created? - ["+theXML+"] ResponseHeaders:"+aHttpRequest.statusText);
		//alert("responseText:\n"+aHttpRequest.responseText);
		   }
   	   catch(anException)
  	   {
  		   alert("Creation of XML failed: "+anException.toString()+" "+aHttpRequest.getAllResponseHeaders());
  		   alert(theXML);
  		   if (this.flightRequestor)
          this.flightRequestor.responseReceived('END');
  		   //GetValues(anException);
  		   return;
  		 }

		   this.xStat.incCall('handleResponse_ViewCreation');
        if (aResponseEventListener == null || aResponseEventListener.length==0)
        {
          //alert("No ResponseEventListener set!");          
        }
        else
        {
          var theResponseEvent = new ResponseEvent(this, theXML);
          var i;
          for (i= 0;i<aResponseEventListener.length; i++)
          {
            aResponseEventListener[i].responseReceived(theResponseEvent);
          }
        }
        this.xStat.endCall('handleResponse_ViewCreation');
        if (this.flightRequestor)
          this.flightRequestor.responseReceived('END');
      }
      else
      {
       alert("HttpRequest.status="+aHttpRequest.status);
      }
    } else {
      //doing nothing
      //alert("NOT READY");
    }
}

// ------------- Html Request Handler ---------------------

function HtmlRequestHandler (aContentContainer,aRequestor)
{
  this.flightContainer = aContentContainer;
  this.flightRequestor=aRequestor;
  if (this.flightRequestor)
    this.flightRequestor.addListener(this);
  this.xStat=new StatisticFactory().createStatistic('HTMLRequestHandler');
}

HtmlRequestHandler.prototype.checkStatus=function(aHttpRequest){
  if (aHttpRequest.status == 200) {
    return true;
  } else {
    
    return false;
    // die Anfrage enthielt Fehler;
    // die Antwort war z.B. 404 (nicht gefunden)
    // oder 500 (interner Server-Fehler)
  }
}

HtmlRequestHandler.prototype.toString=function()
{
  return 'HtmlRequestHandler';
}
 
HtmlRequestHandler.prototype.setContentContainer=function(aContainer)
{
  this.flightContainer = aContainer;
}

HtmlRequestHandler.prototype.handleResponse=function(aFlightRequestor, aHttpRequest, aResponseListener)
{

if (aHttpRequest.readyState == 4) {
     if (this.checkStatus(aHttpRequest)){
	 	  this.xStat.endCall('requestPerformed');
	 	  try {        
        this.xStat.incCall('handleResponse_HTMLCreation');
         theHtmlCode = aHttpRequest.responseText;
        if (aResponseListener == null || aResponseListener.length==0)
        {
         //alert("ResponseListener NOT found");

          theContent = document.getElementById(this.flightContainer);
          theContent.innerHTML=theHtmlCode;
        }
        else
        {
          var theResponseEvent = new ResponseEvent(this, theHtmlCode);
          var i;
          for (i= 0;i<aResponseListener.length; i++)
          {
            aResponseListener[i].responseReceived(theResponseEvent);
          }
        }
        this.xStat.endCall('handleResponse_HTMLCreation');
       }
       catch(anException)
       {
        this.xStat.endCall('handleResponse_HTMLCreation');
        alert("Creation of HTML failed: "+anException.toString()+" "+aHttpRequest.getAllResponseHeaders());
        
  		   alert(theHtmlCode);
  		   if (this.flightRequestor)
          this.flightRequestor.responseReceived('END');
  		   //GetValues(anException);
  		   return;
       }
        
      }
      else
      {
       alert("HttpRequest.status="+aHttpRequest.status);
      }
    } else {
      //doing nothing
      //alert("NOT READY");
    }
}
