/*
 * GET the specified URL.  Upon completion, pass the result
 * to the provided callback function. The programmer using
 * this function has option of passing us an blob of data
 * "opaque" which we will pass right through to the callback.
 * The programmer using this function can use that opaque to
 * maintain state/etc.
 */
function loadXML(url, cbFunc, opaque) {
  if (document.implementation && document.implementation.createDocument) {
    var xmldoc = document.implementation.createDocument("", "", null);
    xmldoc.onload = function(  ) { cbFunc(xmldoc, url, opaque); }
    xmldoc.load(url);
  } else if (window.ActiveXObject) {
    var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
    xmldoc.onreadystatechange = function() {
        if (xmldoc.readyState == 4) cbFunc(xmldoc, url, opaque); }
    xmldoc.load(url);
  }
}

// Escape is not quite good enough, add extra characters here.
function escapeForXML(str)
{
   var retString = escape(str);
   return retString.replace(/\+/g, '%2b');
}


/*
 * Retrieve url
 * First arg is url to POST to.
 * Second arg is XML obj, the text of which, we'll POST.
 * 3rd arg is name of URL arg we'll post. (eg: p .. then we'll do "p=")
 * 4th arg is callback function.
 */

// here the xml we're posting is a blob of xml
function postXMLUrlTxt(url, xml, argName, cbFunc) {
  if ((null == url) || (0 == url.length)) {
    return;
  }

  try {

    var client = new R9HTTPXml();
    if (null == client) {
      errMsg("Unable to create R9HTTPXml");
      return;
    } else if (client.inprogress) {
      errMsg("Retrieval already in progress");
      return;
    }

    var postBuf = argName + "=" + escapeForXML(xml);
    client.init(url, postBuf, null);

    client.onLoad = function(c) {
      cbFunc(c);
    }

    client.onError = function(s,t,c) {
      errMsg(s + ": " + t + "\n" + url);
    }

    client.asyncGET(null);
  }

  catch(e) {
    errMsg("Error: " + e.message);
  }
}

// here the xml we're posting is a doc
function postXMLUrl(url, xmldoc, argName, cbFunc) {
  return postXMLUrlTxt(url, xmldoc.xml, argName, cbFunc);
}


// be sure to have xmlhttp.js :-)
// giza
// 07-Jan-2006

// eg usage

//function myCbFunc(c) {
//  errMsg("in myCbFunc [" + c + "]");
//}


//<body onload="myProcessor('http://heatwiki.com/test/blob.xml')"

/*
 * Retrieve url
 * First arg is url to retrieve.
 * Second arg is callback function to invoke when done.
 */
function getXMLUrl(url, cbFunc) {
  if ((null == url) || (0 == url.length)) {
    return;
  }
  try {

    var client = new R9HTTPXml();
    if (null == client) {
      errMsg("Unable to create R9HTTPXml");
      return;
    } else if (client.inprogress) {
      errMsg("Retrieval already in progress");
      return;
    }

    client.init(url, null, null);

    client.onLoad = function(c) {
      cbFunc(c);
    }

    client.onError = function(s,t,c) {
      errMsg(s + ": " + t + "\n" + url);
    }

    client.asyncGET(null);
  }

  catch(e) {
    errMsg("Error: " + e.message);
  }
}


//
// Unpack an xml node.
//
function getTextContent(node) {
  if (typeof node.textContent != 'undefined') {
    return node.textContent;
  } else if (node.nodeType == 3 || node.nodeType == 4) {
    return node.nodeValue;
  } else {
    var textContent = '';
    for (var i = 0; i < node.childNodes.length; i++) {
      textContent += getTextContent(node.childNodes[i]);
    }
    return textContent;
  }
}

/*
 * Get's value of first child with this name.
 */
function getSingleChildObjValue(parent, tagName) {
  if (null == parent || tagName == null) {
    return null;
  }

  var childObj = parent.getElementsByTagName(tagName);
  if (null == childObj || 0 == childObj.length) {
    return null;
  }


  return getTextContent(childObj[0]);
}


/*
 * The XMLDocument implementation for mozilla doesn't include the .xml method.
 * Rather than hacking code all over the place, fix it once here.  This will
 * serialize the DOM tree to an XML String. 
 *  Usage: var sXML = oNode.xml
 */
if (window.DOMParser && window.XMLSerializer &&
    window.Node && Node.prototype && Node.prototype.__defineGetter__) {

	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});

	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}
