﻿// Download an xml file using xmlhttp (cross browser)

function XmlDownload()
{
    var xmlhttp;
    var url;
    var xmlDoc;
    var txt;
    var callBack;
    this.loadXmlDoc = loadXmlDoc;
    this.stateChange = stateChange;
    
    
    //the documentElement   (rowSet Node) will be passed to the callback funtion
    function loadXmlDoc(theURL, theCallBack)
    {
    xmlhttp=null;
    url = theURL;
    //alert ('url is: ' + url);
    callBack = theCallBack;
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    // code for IE
    else if (window.ActiveXObject)
      {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (xmlhttp!=null)
      {
      xmlhttp.onreadystatechange= stateChange;
      xmlhttp.open("GET",url,true);
      //xmlhttp.setRequestHeader('Content-Type','text/xml');
      
      
      xmlhttp.send(null);
      }
    else
      {
      alert("Your browser does not support XMLHTTP.");
      }
    }
    
    function stateChange()
    {
    
    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4)
      {
      // if "OK"
      if (xmlhttp.status==200)
        {   
    
             var text = xmlhttp.responseText; 
             //alert ("the txt is " + text);
            // code for IE
            if (window.ActiveXObject)
              {
              var doc=new ActiveXObject("Microsoft.XMLDOM");
              doc.async="false";
              doc.loadXML(text);
              }
            // code for Mozilla, Firefox, Opera, etc.
            else
              {
              var parser=new DOMParser();
              var doc=parser.parseFromString(text,"text/xml");
              }          
            callBack(doc.documentElement);        
        }
      else
        {
        alert("Problem retrieving XML data from: " + url);
        }
      }
    }
    
    

}


