﻿var LoadDone = false;
var XmlHttpCallback = null;
var XmlHttp = null;
var TimeoutId = null;

function diag(msg)
{
}

function callInProgress(xmlhttp) 
{
    if ( typeof(xmlhttp)=="undefined" ) return false;
    if ( xmlhttp==null ) return false;
	switch ( xmlhttp.readyState ) 
	{
		case 1, 2, 3: return true;  break;
		default:      return false; break;
	}
}  

// from http://www-128.ibm.com/developerworks/web/library/wa-ajaxintro1.html?ca=dgr-lnxw01MasterAJAX
function CreateXmlHttpRequest()
{
    diag("CreateXmlHttpRequest...");
	/* Create a new XMLHttpRequest object to talk to the Web server */
      var xmlHttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try 
    {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e2) 
        {
            xmlHttp = false;
        }
    }
    @end @*/

    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
    {
        xmlHttp = new XMLHttpRequest();
    }

    diag("...CreateXmlHttpRequest");
    return xmlHttp;
}

function load(url,args,callBack) 
{
    diag("load "+url+"?"+args+"...");
    if ( XmlHttp==null ) XmlHttp = CreateXmlHttpRequest();

	LoadDone = false;
    XmlHttpCallback = callBack;

    /*
    * XmlHttp.open("POST", "/query.cgi");
    * XmlHttp.send("name=Bob&email=bob@example.com");
    */

    /* if you are concerned about possible timeouts of your server process, 
    * you can modify the loadXMLDoc() function to save a global time-stamp 
    * of the send() method, and then modify the event handler function to 
    * calculate the elapsed time with each firing of the event. If the time 
    * exceeds an acceptable limit, then invoke the req.abort() method to 
    * cancel the send operation, and alert the user about the failure.
    */

    // Open a connection to the server
    //		XmlHttp.open("GET", url+"?op="+act+"&"+args, true);
    diag("load.open...");
    XmlHttp.open("POST",url+"?"+args,true);

    // Setup a function for the server to run when it's done
    XmlHttp.onreadystatechange = finishLoad;

    // Send the request
    // XmlHttp.send(null);

    XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 

    diag("load.send...");
    XmlHttp.send(args);

    // 10 second timeout
    TimeoutId = window.setTimeout( timeoutLoad, 10000 );
    diag("...load");
}

function timeoutLoad()
{
    if ( callInProgress(XmlHttp) ) 
    { 
        diag("timeoutLoad...");
        XmlHttp.abort(); 
        LoadDone = true;
        XmlHttpCallback( true, null, null );
        diag("...timeoutLoad");
    } 
}

function diagXmlBrief(node,level)
{
  var s = "";
      
  if ( node.tagName==null )
  {
    if ( node.textContent!=null ) s+=node.textContent;
  }
  else 
    s+="<b>"+node.tagName+"</b>";
  if ( level>0 && node.childNodes.length>0 )
  {
    s += "<ul>";
    for ( var i=0; i<node.childNodes.length; i++ ) // for ( var i in node.childNodes )
    {
      s+="<li>"+diagXmlBrief(node.childNodes[i],level-1)+"</li>";
    }
      s+="</ul>";
  }
  return s;
}

function diagXml(node,level)
{
  var s = "<table border='1' style='margin-left:15px;'><tr><td>";
      
  s += "tagName = "+node.tagName+" ";
  s += "nodeName = "+node.nodeName+" ";
  s += "nodeType = "+node.nodeType+" ";
  s += "nodeValue = "+node.nodeValue+" ";
  
  if ( node.tagName==null )
    s+="text = "+node.textContent;
  else
    s+="tag  = "+node.tagName;
  s+="</td></tr><tr><td>";
  if ( level>0 )
  {
    for ( var i=0; i<node.childNodes.length; i++ ) // for ( var i in node.childNodes )
    {
      s+=diagXml(node.childNodes[i],level-1);
    }
  }
  s+="</td></tr></table>";
  return s;
}

function finishLoad() 
{
    /*
    * 0 - Uninitialised
    * 1 - Loading
    * 2 - Loaded
    * 3 - Interactive
    * 4 - Completed
    */
    if (XmlHttp.readyState == 4) 
    {
        diag("finishLoad...");
        window.clearTimeout(TimeoutId);
        LoadDone = true;
        
        diag("finishLoad.status="+XmlHttp.status+"");
        
        if ( XmlHttp.status==200 )
            XmlHttpCallback( false, 200, XmlHttp.responseXML );
        else
            XmlHttpCallback( false, XmlHttp.status, null );
        diag("...finishLoad");
    }
}	

function getChildrenByTagName( element, tagName )
{
    if ( element==null ) return null;
    var result = new Array();
    for ( var i=0; i<element.childNodes.length; i++ )  // for ( var i in element.childNodes ) doesnt work for IE
    {
        var node = element.childNodes[i];
        if ( node.tagName==tagName )
        {
            result.push(node);
        }
    }
    return result;
}

function getFirstChildByTagName( element, tagName )
{
    if ( element==null ) return null;
    for ( var i=0; i<element.childNodes.length; i++ )  // for ( var i in element.childNodes ) doesnt work for IE
    {
        var node = element.childNodes[i];
        if ( node.tagName==tagName )
        {
            return node;
        }
    }
    return null;
}
  
function getFirstChildByNodeType( element, nodeType )
{
    if ( element==null ) return null;
    for ( var i=0; i<element.childNodes.length; i++ )  // for ( var i in element.childNodes ) doesnt work for IE
    {
        var node = element.childNodes[i];
        if ( node.nodeType==nodeType )
        {
            return node;
        }
    }
    return null;
}
    