/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.process = function(sURL, sMethod, sVars, fnDone, elemento)
  {
    //alert("yendo a buscar")
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars+"&"+Math.random(), true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp, elemento);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { 
      alert("exception " + z)
      return false; 
    }
    return true;
  };
  return this;
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
//        if (result.childNodes.length > 1 && result.childNodes[1].nodeValue != null) {
        if (result.childNodes.length > 1) {
            var total = "";
            for (var i = 0; i < result.childNodes.length; i++) {
              if (result.childNodes[i].nodeValue != null) 
                total += result.childNodes[i].nodeValue;
            }
            return total;
        } else if (result.childNodes.length > 0 && result.firstChild.nodeValue != null) {
//        } else if (result.childNodes.length > 0) {
            return result.firstChild.nodeValue;    		
        } else {
            return "";
        }
    } else {
        return "n/a";
    }
}

var fnWhenDone = function (oXML, elemento) { 
  if (oXML.status == '404') {
    document.getElementById(elemento).innerHTML = "En este momento no hay noticias para mostrar.";
  } else {
    // Transform
    document.getElementById(elemento).innerHTML = oXML.responseText;
  }
}

function ImportXML(XMLPath) 
{ 
  if (document.implementation && document.implementation.createDocument) 
  { 
    //Mozilla 
    xmlDOC = document.implementation.createDocument("", "doc", null); 
    xmlDOC.async = false; 
    xmlDOC.load(XMLPath); 
  }
  else if (window.ActiveXObject) 
  { 
    //IE 
    xmlDOC = new ActiveXObject("Microsoft.XMLDOM"); 
    xmlDOC.async = false; 
    xmlDOC.load(XMLPath); 

  } 
  else 
  { 
    alert('Su navegador no soporta XML. Actualice a Internet Explorer 6.'); 
    return; 
  } 
  return xmlDOC; 
} 

/**
 * This XML.Transformer class encapsulates an XSL stylesheet.
 * If the stylesheet parameter is a URL, we load it.
 * Otherwise, we assume it is an appropriate DOM Document.
 */
  if (document.implementation && document.implementation.createDocument) 
  { 

XML.Transformer = function(stylesheet) {
    // Load the stylesheet if necessary.
    if (typeof stylesheet == "string") stylesheet = XML.load(stylesheet);
    this.stylesheet = stylesheet;
    // In Mozilla-based browsers, create an XSLTProcessor object and
    // tell it about the stylesheet.
    if (typeof XSLTProcessor != "undefined") {
        this.processor = new XSLTProcessor();
        this.processor.importStylesheet(this.stylesheet);
    }
};
/**
 * This is the transform() method of the XML.Transformer class.
 * It transforms the specified xml node using the encapsulated stylesheet.
 * The results of the transformation are assumed to be HTML and are used to
 * replace the content of the specified element.
 */
XML.Transformer.prototype.transform = function(node, element) {
    // If element is specified by id, look it up.
    if (typeof element == "string") element = document.getElementById(element);
    if (this.processor) {
        // If we've created an XSLTProcessor (i.e., we're in Mozilla) use it.
        // Transform the node into a DOM DocumentFragment.
        var fragment = this.processor.transformToFragment(node, document);
        // Erase the existing content of element.
        element.innerHTML = "";
        // And insert the transformed nodes.
        element.appendChild(fragment);
    }
//    else if ("transformNode" in node) {
        //element.innerHTML = node.transformNode(this.stylesheet);
//    }
    else {
        // Otherwise, we're out of luck.
        throw "XSLT is not supported in this browser";
    }
};
/**
 * This is an XSLT utility function that is useful when a stylesheet is
 * used only once.
 */
XML.transform = function(xmldoc, stylesheet, element) {
    var transformer = new XML.Transformer(stylesheet);
    transformer.transform(xmldoc, element);
}
}
