/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer

        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}

function getCommentsHandler (req, callback,site_id)
{	
	return function () 
	{
		    	// If the request's status is " complete"
    	if (req.readyState == 4) 
    	{
            // Check that a successful server response was received
      		if (req.status == 200) 
      		{
      			callback (req.responseText,site_id);
				// doTooltip(event, 'Esto es un tip' );
      		} 
      		else 
      		{
				alert (req.status);
      		}
    	}
  	}
}

function getConfirmationHandler (req, textoError)
{
	return function () 
	{
    	// If the request's status is "complete"
    	if (req.readyState == 4) 
    	{
            // Check that a successful server response was received
      		if (req.status == 200) 
      		{
				alert (req.responseText);
      		} 
      		else 
      		{
				alert (textoError+" ("+req.status+")");
      		}
    	}
  	}
}

function getReadyStateHandler(req, responseXmlHandler,div_id) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
	document.getElementById(div_id).innerHTML=req.responseText;	
        //responseXmlHandler(req.responseText,div_id);

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}


function sustituye (data, div_id)
{
	document.getElementById(div_id).innerHTML=data;	
}

function replacedivcontents (div_id, url)
{
	 var req = newXMLHttpRequest();
	 var handlerFunction = getReadyStateHandler(req, sustituye,div_id);
	 req.onreadystatechange = handlerFunction;
	 req.open("GET",url,true);
	 req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	 req.send("");	 
}

function hacerPost (url, parametros, textoError)
{
	 var req = newXMLHttpRequest();
	 var handlerFunction = 	getConfirmationHandler (req, textoError);
	 req.onreadystatechange = handlerFunction;
	 req.open("POST",url,true);
	 req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	 req.send(parametros); 
}

function descargarComentarios (site_id, callback)
{
alert("descargarComentarios");
	 var req = newXMLHttpRequest();
	 var handlerFunction = 	getCommentsHandler (req, callback, site_id);
	 req.onreadystatechange = handlerFunction;
	 req.open("POST","adm_datosnews_get.php",true);
	 req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	 req.send("site_id="+site_id); 
}

function descargarInformación (site_id, callback)
{
alert("descargarInformación");
	 var req = newXMLHttpRequest();
	 var handlerFunction = 	getCommentsHandler (req, callback, site_id);
	 req.onreadystatechange = handlerFunction;
	 req.open("POST","adm_datosnews_get.php",true);
	 req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	 req.send("site_id="+site_id); 
}
