/*
 * Created on Jan 10, 2006
 * Author Marek Olszewski m.olszewski@wsisiz.edu.pl
 * 
 */
 
 // Class JSHttpConn
 
 function JSHttpConn ()
  {
    this.running = false;
    this.complete = false;
    this.state = 0;
    this.get = JSHttpConnGet;
    this.onStateChange = JSHttpConnOnStateChange;
    this.onTransferEnd = JSHttpConnOnTransferEnd;
    this.responseText="";
    this.evalOnTransferEnd="";
    
    this.assignXmlHttpObject=JSHttpConnAssignXmlHttpObject;
    this.assignXmlHttpObject();
	
    this.qURL = new Array();
    this.qExpr = new Array();
	
  }
  function JSHttpConnAssignXmlHttpObject ()
  {
  	if (window.XMLHttpRequest)
	  {
	  this.xmlhttp=new XMLHttpRequest();
	  
	  	if (this.xmlhttp)
		    this.xmlhttp.onreadystatechange=this.onStateChange;
		else
			alert("xmlhttp error");
	  }
	else if (window.ActiveXObject)
	  {
	  this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	    if (this.xmlhttp)
		    this.xmlhttp.onreadystatechange=this.onStateChange;
		else
			alert("xmlhttp error");
	  }
  }
  function JSHttpConnOnTransferEnd ()
  {
  	
  	this.assignXmlHttpObject();
  	eval(this.evalOnTransferEnd);
  	
  	if (this.qURL.length>0)
  		this.get(this.qURL.shift(),this.qExpr.shift()); 	
  	
  }
  
  function JSHttpConnGet(url , expression)
	{
	if (this.running)	
	{
		//alert('request already running !\nCannot get ' + url +'\nProcessing ' + this.currentUrl);
		this.qURL.push(url);
		this.qExpr.push(expression);
		
		return false;
	}
	this.currentUrl = url;
	this.evalOnTransferEnd = expression;
	this.running  = true;
	this.complete = false;
	this.xmlhttp.open("GET",url,true);
	
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	  this.xmlhttp.send(null);
	// code for IE
	else if (window.ActiveXObject)
	  this.xmlhttp.send();
	
	
	} 
	
	function JSHttpConnOnStateChange()
	{
	
	
	// if xmlhttp shows "loaded"
    if (httpConn.xmlhttp.readyState==4)
    {
	    // if "OK"
	    if (httpConn.xmlhttp.status==200)
	    {
	   
		  	httpConn.responseText = httpConn.xmlhttp.responseText;
		  	httpConn.complete = true;
		  	httpConn.running = false;
		  	httpConn.onTransferEnd();
	    }
		else
		{
			alert("Problem retrieving XML data")
		}
    }
    	
	}