﻿
// -------
var xmlDoc;
var _soapRequest;
var _soapResponseXML;
var _request;

// -------
function LoadXmlDocument(xmlContents)
{
   var xdoc;

   if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
   {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = false;
      xmlDoc.load(xmlContents);     
      return true;
   }
   else if( document.implementation && document.implementation.createDocument )
   {
	
      return true;
      xdoc = document.implementation.createDocument("", "", null);
      xdoc.load(xmlContents);	
      xdoc.onload = function()
      {
         
         xmlDoc = xdoc;
         
      }

		
      return true;
   }
   else
   {
      return false;
   }
}

// -------
function getXmlHttp()
{   
   //get the XMLHttpRequest object for this browser.   
   _soapRequest = false;
   // branch for native XMLHttpRequest object
   if(window.XMLHttpRequest) 
   {
      try 
      {
         _soapRequest = new XMLHttpRequest();
      } 
      catch(e) 
      {
         _soapRequest = false;
      }      
   } 
   else if(window.ActiveXObject) // branch for IE/Windows ActiveX version
   {
      try 
      {
         _soapRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch(e) 
      {
         try 
         {
            _soapRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } 
         catch(e) 
         {
            _soapRequest = false;
         }
       }
    }
    
}

// -------
function sendSoapPostRequest(url, host, soapAction, soapBody, callBackFunc)
{   
    var header;   
    var headerName;   
    var headerValue;   
    
    var timestamp = new Date();                                       // Get a new date (this will make the url unique)         
    url = url+'?timestamp='+(timestamp*1); 
            
    //the xml sent to the web service     
    var body = '' +    '<?xml version="1.0" encoding="utf-8"?>' +     
         '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '+
         'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' + 
         'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
             '<soap:Body>' +      
             soapBody +   
             ' </soap:Body>' +   
             ' </soap:Envelope>' ; 
    getXmlHttp();      
    //alert (_soapRequest);
    if(_soapRequest)   
    {      //once the state changes, fire the processReqChange event      
        _soapRequest.onreadystatechange = callBackFunc;            
        //send a post      
        //attach the necessary headers      
        _soapRequest.open("POST", url,true);      
        _soapRequest.setRequestHeader("Host", host)      
        _soapRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8")      
        _soapRequest.setRequestHeader("Content-Length", body.length)      
        _soapRequest.setRequestHeader("SOAPAction", soapAction)      
        //alert ('Soap Body: ' + body);
        //alert ('Soap Action: ' + soapAction);
        _soapRequest.send(body)    
    }
} 

// -------
// POST or GET AJAX request
// -------
function initiateAjaxRequest(url,responseF,xmlName,xmlData)
{
	_request=false;
	if(window.XMLHttpRequest)
	{
		try{_request=new XMLHttpRequest();
	} catch(e)
		{ _request=false; }
	}
	else if(window.ActiveXObject)
	{
	  try {  _request=new ActiveXObject("Msxml2.XMLHTTP"); }
	  catch(e) { 
	  	try{ _request=new ActiveXObject("Microsoft.XMLHTTP"); }
	  	catch(e){ _request=false; }
	  }
	}

	if(_request)
	{
	    if( responseF != null )
	    {
    		_request.onreadystatechange=function(){ if(_request.readyState==4) { if(_request.status==200) { responseF(); } else{} } };
        }

		if(xmlData==null) 
		{ 
		  _request.open("GET",url,true);
  		  _request.send("");
        }
        else
        {        
          _request.open("POST",url,true);
          _request.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
          _request.send(xmlName+"="+encodeURIComponent(xmlData));
        }
    }
} 


//new ajaxobject created on 12/10/2009
function AjaxObject(url, node) 
{                                    
    // This is the object constructor   
    var that=this;                                                          // A workaround for some javascript idiosyncrocies   
    this.updating = false;                                                  // Set to true if this object is already working on a request  
    var response = ""; 
    that.callback = function() { }                                          // A post-processing call -- a stub you overwrite.  
    
    this.request = function()
    {
        var obj;
        
        if(window.XMLHttpRequest)                                           // branch for native XMLHttpRequest object        
        {
            obj = new XMLHttpRequest();
        }    
        else
        {  
            obj = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        
          
        if (obj==null) 
        {                                              
            // If we couldn't initialize Ajax...         
            alert("Your browser doesn't support AJAX.");                    // Sorry msg.                                                       
            return null;                                                   // Return false (WARNING - SAME AS ALREADY PROCESSING!)      
        } 

        return obj;
    } 
  
    this.getdata = function(passData) 
    {                                                                       // Initiates the server call.      
        if (this.updating==true) {  return false; }                         // Abort if we're already processing a call.      
        this.updating=true;                                                 // Set the updating flag.      
        var AJAX = this.request();

        if (AJAX != null)
        {         
            AJAX.onreadystatechange = function() 
            {                      
                // When the browser has the request info..            
                if (AJAX.readyState==4 || AJAX.readyState=="complete") 
                {                                                           //   see if the complete flag is set.               
                    response =AJAX.responseText;                            //   It is, so put the new data in the object's layer               
                    //return a particular node;
                    if (node != "")
                        response = ParseElement(AJAX.responseText, node,'');
                    
                    delete AJAX;                                            //   delete the AJAX object since it's done.               
                    this.updating=false;                                         //   Set the updating flag to false so we can do a new request               
                    that.callback();                                        //   Call the post-processing function.            
                 }// End Ajax readystate check.         
            } 
            
            var timestamp = new Date();                                     // Get a new date (this will make the url unique)         
            var uri = urlCall +'?'+passData+'&timestamp=' + timestamp;   
    
                                                                            // End create post-process fucntion block.         
            AJAX.open("GET", uri, true);                                    // Open the url this object was set-up with.         
            AJAX.send(null);                                                // Send the request.         
        }
        
        return true;                                                        // Everything went a-ok.
                                                                            // End Ajax setup aok if/else block                    
    }  
    
    this.postdata = function(url, host, soapAction, soapBody)
    {                                                                       // Initiates the server call.      
        if (this.updating==true) { return false; }                          // Abort if we're already processing a call.      
        this.updating=true;                                                 // Set the updating flag.      
        var AJAX = this.request();                           

        if (AJAX != null) 
        {         
            AJAX.onreadystatechange = function() 
            {                      
                // When the browser has the request info..    
                if (AJAX.readyState==4 || AJAX.readyState=="complete") 
                {     
                    //layerID.innerHTML = AJAX.responseText;                  //   see if the complete flag is set.               
                    response =AJAX.responseText;                              //   It is, so put the new data in the object's layer               
                    if (node != "")
                        response = ParseElement(AJAX.responseText, node,'');
                                            
                    delete AJAX;                                              //   delete the AJAX object since it's done.               
                    this.updating=false;                                      //   Set the updating flag to false so we can do a new request               
                    that.callback();                                          // Call the post-processing function.            
                 }                                                            // End Ajax readystate check.         
            } 
                                                                              // End create post-process fucntion block.    
            var timestamp = new Date();                                       // Get a new date (this will make the url unique)         
            var uri= urlCall+'?timestamp='+(timestamp*1);  

             //the xml sent to the web service    
             var body = '' + '<?xml version="1.0" encoding="utf-8"?>' +     
                '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '+
                'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' + 
                'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
                '<soap:Body>' +      
                soapBody +   
                ' </soap:Body>' +   
                ' </soap:Envelope>' ; 

            AJAX.open("POST", uri, true);      
            AJAX.setRequestHeader("Host", host);     
            AJAX.setRequestHeader("Content-Type", "text/xml; charset=utf-8");      
            AJAX.setRequestHeader("Content-Length", body.length);      
            AJAX.setRequestHeader("SOAPAction", soapAction);      
            AJAX.send(body);
        }
        
        return true;
    }  
    
    this.getresponse = function()
    {
        return response;
    }   

    //var layerID = layer;                                                   // Remember the layer associated with this object.   
    var urlCall = url;                                                       // Remember the url associated with this object.
}                                                                            // End AjaxObject
