// Codi extret de : http://www-128.ibm.com/developerworks/library/j-ajax1/

/*****
*    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.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, requestObject, responseXmlHandler) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      //DebugOut("getReadyStateHandler status: " + req.status,DEBUG);
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(requestObject, req.responseText);

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }// else DebugOut("getReadyStateHandler readystate: " + req.readyState,DEBUG);
  }
}

/**
*	Crea un nou document XML
*	@return {XMLDocument}
*/
function newObjectXML(){
   if(window.ActiveXObject){
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  return xmlDoc;
   }else if(document.implementation && document.implementation.createDocument){
      xmlDoc = document.implementation.createDocument("","",null);
	  return xmlDoc;
   }else{
      alert ('newObjectXML: Su navegador no puede soportar este script');
   }
}

/**
*	Afegeix un node amb un text com a valor
*	@param doc El document XML
*	@param {Node} parentNode Node on s'afegirà el fill
*	@param {String} nodeName Nom del node
*	@param {String} nodeValue Text del node
*
*/
function addChild(doc,parentNode,nodeName,nodeValue) {
	var aux=doc.createElement(nodeName);
	var aux2=doc.createTextNode(nodeValue);
	aux.appendChild(aux2);
	//alert ('>>2 :' + aux2.nodeValue);
	parentNode.appendChild(aux);
}

/**
*	Afegeix un node amb un boolea com a valor. Si nodeValue es TRUE posa Y, si es FALSE posa N. Ex: <nodeName>Y</nodeName>
*	@param doc El document XML
*	@param {Node} parentNode Node on s'afegirà el fill
*	@param {String} nodeName Nom del node
*	@param {String} nodeValue Text del node
*
*/
function addBooleanChild(doc,parentNode,nodeName,nodeValue) {
	var v;
	if (nodeValue) v="Y";
	else v="N";
	addChild(doc,parentNode,nodeName,v);
}

/**
*	Retorna un boolea a partir de l'xpath donat. Y|1: true, N|0:false.
*	DEPEN DE SARISSA!
*
*	@param {Document} doc El document xml
*	@param {String} xpath Xpath al valor
*/
function selectBoolean(doc,xpath) {
	try {	
		var objNodeList = doc.selectNodes(xpath);
		//---DEBUG-------------
		var v
		for(var i=0;i<objNodeList.length;i++) {
			v=objNodeList[i].nodeValue;
			//alert("> "+i+" / "+objNodeList.length+"\n"+xpath+"\n\n"+ objNodeList[i].nodeValue );
		}
		//---DEBUG-------------
		var v=objNodeList[0].nodeValue;
		//alert(">loadValue: "+ v );

		if (v=="Y" || v=="1") return true;
		else if (v=="N" || v=="0") return false;
		else {
			alert("Error selectBoolean()\nS'esperava un valor 0/1 i s'ha trobat:"+v+"\n\nXpath: "+xpath);
		}
	
	} catch (e) {
		if (objNodeList.length==0) return false;// si no te valor no es un error.
		alert("Error selectBoolean()\n" + e.message + "\n\n"+xpath);
		return false;
	}
}

/**
*	Retorna un string a partir de l'xpath donat.
*	DEPEN DE SARISSA!
*
*	@param {Document} doc El document xml
*	@param {String} xpath Xpath al valor
*/
function selectString(doc,xpath) {
	try {	
		var objNodeList = doc.selectNodes(xpath);
		//---DEBUG-------------
		for(var i=0;i<objNodeList.length;i++) {
			var v=objNodeList[i].nodeValue;
//			alert("> "+i+" / "+objNodeList.length+"\n"+xpath+"\n\n"+ objNodeList[i].nodeValue );
		}
		//---DEBUG-------------
		var v=objNodeList[0].nodeValue;
		//alert(">loadValue: "+ v );
		//DebugOut(">> selectString :: "+xpath+" = " +v,DEBUG );
		return v;
	
	} catch (e) {
		if (objNodeList!=null && objNodeList.length==0) return "";// si no te valor no es un error.
		alert("Error selectString()\n" + e.message + "\n\n"+xpath);
		return "";
	}
}

/**
*	Retorna un float a partir de l'xpath donat.
*	DEPEN DE SARISSA!
*
*	@param {Document} doc El document xml
*	@param {String} xpath Xpath al valor
*/
function selectFloat(doc,xpath) {
	try {	
		var objNodeList = doc.selectNodes(xpath);
		//---DEBUG-------------
		for(var i=0;i<objNodeList.length;i++) {
			var v=objNodeList[i].nodeValue;
//			alert("> "+i+" / "+objNodeList.length+"\n"+xpath+"\n\n"+ objNodeList[i].nodeValue );
		}
		//---DEBUG-------------
		var v=objNodeList[0].nodeValue;
		//alert(">loadValue: "+ v );
		return parseFloat(v);
	
	} catch (e) {
		if (objNodeList.length==0) return "";// si no te valor no es un error.
		alert("Error selectFloat()\n" + e.message + "\n\n"+xpath);
		return 0;
	}
}