var pos; // variable for posting information

/*

*/
function loadXMLPosDoc(url,posData) {
    if (window.XMLHttpRequest) {
        pos = new XMLHttpRequest();
        pos.onreadystatechange = processPosChange;
        pos.open("POST", url, false);
	pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        pos.send(posData);
    } else if (window.ActiveXObject) {
        pos = new ActiveXObject("Microsoft.XMLHTTP");
        if (pos) {
            pos.onreadystatechange = processPosChange;
            pos.open("POST", url, false);
	pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            pos.send(posData);
        }
    }
}

/*

*/
function grabPosXML (xml, tagName) {

	var xmldoc = xml.responseXML;		
	var message_nodes = xmldoc.getElementsByTagName(tagName);
	//alert(message_nodes.length);
	if (message_nodes.length) {
	var str = message_nodes[0].firstChild.data;
	
	//alert(message_nodes[0].firstChild.data);
	
	return str;
	
	//return xml.responseXML.documentElement.getElementsByTagName('status')[0].childNodes[0].nodeValue;
	}
	return "";
}

/*

*/
function processPosChange() {

	//alert(pos.readyState);
	if (pos)
	{
		//alert(pos.status);
		if (pos.readyState == 4) {
			if (pos.status == 200) 
			{
				//alert("Debug: statusText: " +pos.statusText);
				//alert("Debug: responseText: " +pos.responseText);
				if ( grabPosXML(pos, "status") == 'NOTOK' ) 
				{ 
					alert('Ha habido problemas enviando la información. Por favor, compruebe nuevamente en unos minutos.');
				}
			}
		}

	}
}




/** 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;
  //alert("Debug: HTTP_REQUEST");
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, 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);
        }};     
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  this.getXml = function() { return xmlhttp; };
  return this;
}



/*
onreadystatechange
This property sets the method to be called on every state change.  This is usually your event handler for the asynchronous callback.

readyState
This property defines the state of the XmlHttpRequest.  Possible values include:

0      Uninitated
1      Loading
2      Loaded
3      Interactive
4      Complete

When sending the XmlHttpRequest, you will check to see if the readyState is 0 or 4, and in your asynchronous callback handler, you will check to see if the readyState is 4.

responseText
This returns the response from the server as a string.  
If you are only returning one value this is the way to go because it is much easier that trying to walk the XML DOM.

responseXML
This returns the response from the server as an XML document.  
This is the way to go if you need to return multiple values from your AJAX request.  
It does require some knowledge of the XML DOM to use, but is quite powerful.

status
This returns the HTTP status code from the server such as 200 for OK or 404 for not found.

statusText
This returns a string representation of the HTTP status code such as OK for 200 and Not Found for 404

And that's is for the properties available to you via the XmlHttpRequest object.
*/
