// Crea una instancia XMLHttpRequest
function createXmlHttpRequestObject(){
  var xmlHttp;
  try {
    xmlHttp = new XMLHttpRequest(); // Intenta crear el objeto
  }
  catch(e)
  {
	// Para IE6 o versiones anteriores
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");

    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
      try { 
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }

  if (!xmlHttp)
    alert("Error creando Objeto XMLHttpRequest.");
  else return xmlHttp;
}

//MUESTRA EL CALENDARIO
function getCalendario(mes, anio){
 var xmlHttp = createXmlHttpRequestObject(); // Referencia al Objeto XMLHttpRequest
  if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)) {
	if(mes!="" && anio!=""){
  		query = "preparacalendario.php?nuevo_mes="+mes+"&nuevo_year="+anio;
	}else query = "preparacalendario.php";
	
    xmlHttp.open("GET", query, true);	
 	xmlHttp.onreadystatechange = function (){
		 if(xmlHttp.readyState == 0) { document.getElementById("calendarioContent").innerHTML = "Sending Request..."; }   
		 if(xmlHttp.readyState == 1) { document.getElementById("calendarioContent").innerHTML = "Cargando..."; }   
		 if(xmlHttp.readyState == 2) { document.getElementById("calendarioContent").innerHTML = "Cargando..."; }   
		 if(xmlHttp.readyState == 3) { document.getElementById("calendarioContent").innerHTML = "Cargando..."; }  			
		
		  if (xmlHttp.readyState == 4){ 
			if (xmlHttp.status == 200){ 
			  response = xmlHttp.responseText; // read the response
			  //server error?
			  if (response.indexOf("ERRNO") >= 0 /*|| response.indexOf("error") >= 0*/ || response.length == 0){
				alert(response.length == 0 ? "Server serror." : response); // Muestra un mensja de error
				return;
			  }
				if(response!=""){
					var PlaceHolder = document.getElementById("calendarioContent");
					PlaceHolder.innerHTML = response;					
				}
			}
		  } 
	}	
    xmlHttp.send(null);	
  } 
}

//FUNCIONES PARA EL PAGINADO DE NOTICIAS
function getResNoticia(query, page, origen){
 var xmlHttp = createXmlHttpRequestObject(); // Referencia al Objeto XMLHttpRequest
  if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)) {
	
	if(origen==1) { 
		query = "busquedas.php?action=next&query="+query+"&page="+page;	
	}else query = "busquedas.php?action=next&query="+query+"&page="+page+"&origen=2";	

	
    xmlHttp.open("GET", query, true);	
 	xmlHttp.onreadystatechange = function (){	

		  if (xmlHttp.readyState == 4){ 
			if (xmlHttp.status == 200){ 
			  response = xmlHttp.responseText; // read the response
			  //server error?
			  if (response.indexOf("ERRNO") >= 0 || response.length == 0){
				alert(response.length == 0 ? "Server serror." : response); // Muestra un mensja de error
				return;
			  }
		  				
				if(response!=""){
					var PlaceHolder = document.getElementById("ResultadosBusqueda");
					PlaceHolder.innerHTML = response;
				}
			}
		  } 
	}	
    xmlHttp.send(null);
  }				
}


//FUNCIONES PARA LA GESTION DE COMENTARIOS
function getComentarioNoticias(noticia, page, origen){
 var xmlHttp = createXmlHttpRequestObject(); // Referencia al Objeto XMLHttpRequest
  if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)) {
	
	if(origen==1) { 
		query = "comentarios.php?action=nextCom&noticia="+noticia+"&page="+page;	
	}else query = "comentarios.php?action=nextCom&noticia="+noticia+"&page="+page+"&origen=2";	

    xmlHttp.open("GET", query, true);	
 	xmlHttp.onreadystatechange = function (){	

		  if (xmlHttp.readyState == 4){ 
			if (xmlHttp.status == 200){ 
			  response = xmlHttp.responseText; // read the response
			  //server error?
			  if (response.indexOf("ERRNO") >= 0 || response.length == 0){
				alert(response.length == 0 ? "Server serror." : response); // Muestra un mensja de error
				return;
			  }
		  				
				if(response!=""){
					var PlaceHolder = document.getElementById("listaComentarios");
					PlaceHolder.innerHTML = response;
				}
			}
		  } 
	}	
    xmlHttp.send(null);
  }		
}