/**
 * 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;
}

/**
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response to the given handler function.
 * @param object req - The XMLHttpRequest whose state is changing
 * @param function responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, responseXmlHandler, format) {

  // 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
        if (format!=undefined && format=='text')
        {
            responseXmlHandler(req.responseText);
        }
        else if (format!=undefined && format=='xml')
        {
            responseXmlHandler(req.responseXML);
        }
        else
        {
            // Default XML
            responseXmlHandler(req.responseXML);
        }
        
      } else {

        // An HTTP problem has occurred
        debug("HTTP error: "+req.status);
      }
    }
  }
}


/**
 * Define o url para o POST dos dados e
 * define a funcao a ser chamada para tratar os dados
 */
function XMLHTTPSend(params,responseHandler, format) {

	var method 	= 'POST';
	var page	= "/xmlhttp.php";
	var async	= true;
	// Obtain an XMLHttpRequest instance
	var req = newXMLHttpRequest();
	// Set the handler function to receive callback notifications
	
    if (format==undefined)
        format = 'xml';
    
    // from the request object
	req.onreadystatechange = getReadyStateHandler(req, responseHandler, format);
  	
	// Third parameter specifies request is asynchronous.
  	req.open(method, page, async);

  	// Specify that the body of the request contains form data
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
    req.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    req.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
    req.setRequestHeader("Pragma", "no-cache");
    
  	// Send form encoded data

  	req.send(params);

}

function debug(msg)
{
    if (console!=undefined)
    {
        console.debug(msg); // Firebug
    }
    else
    {
        alert(msg);
    }
}