//
// Funciones para el manejo de Ajax (c) 2007 by zephrax
// Soporte para varios exploradores, método GET y POST
//
// Ejemplo:
// ¯¯¯¯¯¯¯
//   <div id="id_contenedor>Aquí se cargará la página.</div>
//
//   <a onclick="AbreURL('ajax.php','id_contenedor','param1=valor1','POST');">
//   Click aquí para cargar la web mediante Ajax.</a>
//
//   <a onclick="AbreURL('ajax.php?param1=valor1','id_contenedor','','GET');">
//   Click aquí para cargar la web mediante Ajax.</a>
//

function nuevoAjax() {
 if (window.XMLHttpRequest)
  oAjax = new XMLHttpRequest();
 else if (window.ActiveXObject)
  oAjax = new ActiveXObject("Microsoft.XMLHTTP");
 if (oAjax) return(oAjax);
}

function AbreURL(url,cont,valores,metodo) {
 if (url == '') return;
 cont_id = document.getElementById(cont);
 var oAjax = nuevoAjax();
 oAjax.onreadystatechange = function ajaxStateChange() {
  if(oAjax.readyState == 4) {
   InsertarEnContenedor(cont, oAjax.responseText, true); 
  } else {
   cont_id.innerHTML = '<div align="center">Cargando...</div>';
  }
 }
 if (metodo.toUpperCase() == "POST") {
  oAjax.open("POST", url, true);
  oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  oAjax.send(valores);
 } else if (metodo.toUpperCase() == "GET") {
  oAjax.open("GET", url, true);
  oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  oAjax.send(null);
 }
}

function InsertarEnContenedor(id,html,ProcesarScripts) {
 ContenedorPrincipal = document.getElementById(id);
 ContenedorPrincipal.innerHTML = html;
 if (ProcesarScripts!=false) {
  var elementos = ContenedorPrincipal.getElementsByTagName('script');
  for (i=0;i<elementos.length;i++) {
   var etiquetaScript=document.createElement("script");
   document.getElementsByTagName("head")[0].appendChild(etiquetaScript);
   etiquetaScript.text=elementos[0].innerHTML;
  }
 }
}