//OUR GLOBAL AJAX MODULE WHICH HANDLES THE ACTUAL REQUESTS
var ajaxModuleObj=null;

//FUNCTION WHICH IS CALLED IN RESPONSE TO A STATE CHANGE OF THE XMLHTTP OBJECT
function ajaxReadyStateCallback()
{
    if (ajaxModuleObj.xmlHttp.readyState==4)
      {
          if (ajaxModuleObj.awaitingProcessing=true)
            {
                ajaxModuleObj.awaitingProcessing=false;
                
                if (ajaxModuleObj.currentRequest.onResponse!=null)
                  ajaxModuleObj.currentRequest.onResponse(ajaxModuleObj.xmlHttp);
                  
                ajaxModuleObj.xmlHttp.abort();
                
                ajaxModuleObj.activeRequest=false;
                
                ajaxModuleObj.processQueue();
            }
      }        
}

function ajaxRequest(callerObj)
{
//CROSS BROWSER FUNCTION FOR CREATING THE XMLHTTP OBJECT
this.create=function(callerObj)
        {  
               this.callerObj=callerObj; 
               this.headers=new Array();
               this.request=null;
               this.awaitingProcessing=false;
         }
        
this.clearRequestHeaders=function()
        {
            this.headers=new Array();
        }
        
this.setRequestHeader=function(header,value)
        {
            this.headers.push(new Array(header,value));
        }
                 
//SENDS A REQUEST TO A SERVER
this.sendRequest=function(method,url,data)
        {
            this.request=new Array(method,url,data);
            //ADD THE REQUEST TO THE GLOBAL AJAX MODULE
            ajaxModuleObj.addRequest(this);
        }
                 
//OUR RESPONSE HANDLER - TRIGGERED WHEN A RESPONSE HAS BEEN SUCESSFULLY RECEIVED 
this.onResponse=null;

this.create(callerObj);

return this;
}

function ajaxModule()
{
//CROSS BROWSER FUNCTION FOR CREATING THE XMLHTTP OBJECT
this.create=function()
        {
  		    this.xmlHttp=null;

            if (window.XMLHttpRequest)
         	    this.xmlHttp=new XMLHttpRequest();
            else if (window.ActiveXObject)
                this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            else
               alert("Your browser does not support XMLHTTP!");
			     
			this.requestQueue=new Array();
            this.awaitingProcessing=false;
            this.activeRequest=false;
            this.currentRequest=null;
         }
						 
//ADDS A REQUEST TO THE QUEUE
this.addRequest=function(request)
				 {
				 this.requestQueue.push(request);
                 
                 this.processQueue();
				 }
				 
//PROCESSES NEXT REQUEST IN THE QUEUE
this.processQueue=function()
				 {
				 if (this.requestQueue.length==0)
				   return;
				   
                 if (this.activeRequest==true)
                   return;
                 
                 this.activeRequest=true;
                 this.awaitingProcessing=true;
                   
				 this.currentRequest=this.requestQueue.shift();
				 
				 this.xmlHttp.abort();						//TRASH ANY OLD REQUESTS FOR THIS OBJECT
				 
                 for (var i=0; i<this.currentRequest.headers.length; i++)
                   this.xmlHttp.setRequestHeader(this.currentRequest.headers[i][0],this.currentRequest.headers[i][1]);
                 
         	     this.xmlHttp.onreadystatechange=ajaxReadyStateCallback;
				 this.xmlHttp.open(this.currentRequest.request[0],this.currentRequest.request[1],true);
  			     this.xmlHttp.send(this.currentRequest.request[2]);
                 }

this.create();

return this;
}

ajaxModuleObj=new ajaxModule();

function XMLParser(xmlstr)
{
this.parse=function(xmlstr)
							{
							if (window.ActiveXObject)
                {
                this.doc=new ActiveXObject("Microsoft.XMLDOM");
                this.doc.async="false";
                this.doc.loadXML(xmlstr);
                }
              // code for Mozilla, Firefox, Opera, etc.
              else
                {
                var parser=new DOMParser();
                this.doc=parser.parseFromString(xmlstr,"text/xml");
                }
							}
							
this.parse(xmlstr);

return this;
}

function Node2Str(xmlnode)
{
if (xmlnode.xml)
  return xmlnode.xml;
else if (window.XMLSerializer)
  {
  var xmlSerializer = new XMLSerializer();
  return xmlSerializer.serializeToString(xmlnode);
  }
else
  alert('XML Node2Str is not supported with this browser.');
}

function getPropValue(prop)
{
    if (prop.childNodes.length==0)
      return "";
      
    return prop.childNodes[0].nodeValue;
}