var includeRegistry = [];

function newXMLHttpRequest() {
  var req;
  // branch for native XMLHttpRequest object
  if ( window.XMLHttpRequest ) {
    try {
      req = new XMLHttpRequest();
    } catch( e ) {
      req = false;
    }
    // branch for IE/Windows ActiveX version
  } else if( window.ActiveXObject ) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch( e ) {
      try {
	req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch( e ) {
	req = false;
      }
    }
  }
  return req;
}

function is_ws(nod) {
  return !(/[^\t\n\r ]/.test(nod.data));
}

function isFileProtocol( url ) {
  var str = "" + url;
  //return str.match(/^file/) != ;
  return str.substr(0, 7) == "file://";
}

function isHTTPProtocol( url ) {
  var str = "" + url;
  return (str.substr(0, 7) == "http://") || (str.substr(0, 8) == "https://");
}


function findWhiteSpace(node, nodeNo) {
  for (i=0; i<node.childNodes.length; i++) {
    if (node.childNodes[i].nodeType == 3 && is_ws(node.childNodes[i])) {
      nodesToDelete[nodesToDelete.length] = node.childNodes[i];
    }
    if (node.childNodes[i].hasChildNodes()) {
      findWhiteSpace(node.childNodes[i], i);
    }
  }
  node = node.parentNode;
  i = nodeNo;
}

function stripWhiteSpace(node) {
  nodesToDelete = Array();
  findWhiteSpace(node, 0);
  for(i=nodesToDelete.length-1;i>=0;i--) {
    nodeRef = nodesToDelete[i];
    nodeRef.parentNode.removeChild(nodeRef);
  }
}

function include( url, forceExport ) {

  if ( ! forceExport )
    forceExport = false;

  // Check if already included
  for (var i = 0; reg = includeRegistry[i]; i++) {
    if (reg.toLowerCase() == url.toLowerCase()) {
      return; // already included
    }
  }

  // Record the inclusion
  includeRegistry[ includeRegistry.length] = url;

  // Load script
  var xhReq = newXMLHttpRequest();

  
  if ( isFileProtocol( this.location ) && ! isFileProtocol( url ) ) {
    // See http://www.mozilla.org/projects/security/components/signed-scripts.html#privs
    netscape.security.PrivilegeManager.enablePrivilege( "UniversalBrowserRead" );
  }

  //alert("ishttp? " + url + " " + isHTTPProtocol( this.location ));
  //alert("isfile? " + url + " " + isFileProtocol( this.location ));
  if ( !isFileProtocol( url ) && !isHTTPProtocol( url ) ) {
    //alert("rel" + url);
    //url = "http://evangelista.tv/javascript/" + url;
    url = "javascript/" + url;
  }

  //alert(url);
  xhReq.open( "GET", url, false);
  xhReq.send(null);
  var script = xhReq.responseText;


  if ( forceExport ) {

    var functionDefRegex = new RegExp ( "^[ \t]*function[ \t]+([^(]*)[(]" , "mg" ); // m = multiline
    
    var functionLines = script.match( functionDefRegex );
    
    var exportStatemets = "\n";
    for ( var i=0; i<functionLines.length; ++i) {
      functionLines[i] = functionLines[i].replace( functionDefRegex, "$1" );
      exportStatemets += "this." + functionLines[i] + " = " + functionLines[i] + ";\n"; 
    }
    
    var variableDefRegex = new RegExp ( "^var ([^=]*)", "mg" ); // m = multiline
    var variableLines = script.match( variableDefRegex );
    
    if ( variableLines != null ) {
      //alert( "--- " + variableLines );
      for ( var i=0; i<variableLines.length; ++i) {
	variableLines[i] = variableLines[i].replace( variableDefRegex, "$1" );
	
	// Multiple Variable declaration can be on the same line. Split comma separated list of variable names
	var variableNames = variableLines[i].split( "," );
	for ( var j=0; j<variableNames.length; ++j ) {
	  exportStatemets += "this." + variableNames[j] + " = " + variableNames[j] + ";\n"; 
	}
      }
    }
    
    //alert( exportStatemets );
    eval( script + exportStatemets);
  } else {
    eval( script );
  }


}


include( "javascript/com/karaszewski/ajaxlib.js" );


/**
 * http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r.html
 */
function loadXML( url ) {
  //alert( this.location );
  var xhReq = newXMLHttpRequest();
  xhReq.open( "GET", url, false );
  //xhReq.overrideMimeType('text/xml');
  xhReq.send(null);
  
  var xml = xhReq.responseXML;

  //
  // The following is for local testing with IE.
  //
  //if ( xIE4Up && url.substr(0, 7) == "file://" ) {
  //alert( location.href );
  var fileProtocolStr = "file://";
  if ( xIE4Up && ( location.href.substr( 0, fileProtocolStr.length ) == fileProtocolStr ) ) {
    var doc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
    doc.async = false;
    //doc.load( "file:///C:/var/tmp/test.xml" );
    doc.load( url );
    //alert( "node? " + doc.childNodes[0].getAttribute("src") );
    xml = doc;
  }

  

  //xhReq.responseXML.loadXML( xhReq.responseText );

  //alert( xhReq.responseText );
  //alert( xhReq.responseXML );

  stripWhiteSpace( xml );
  return xml;
}




