var _ajaxBrowser = "IE"

/**************************************** 
 * 
 * This method get the xmlHTTPObject
 * This function should works with all browsers that support HTTP Request 
 * 
 ****************************************/
 function GetHTTPObject()
 {
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}		//IE 1
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}  //IE 2
   try { 
   _ajaxBrowser = "FIREFOX";
	return new XMLHttpRequest(); 
	} catch(e) {}						//Mozilla's, Opera 8.
   var errMsg = "Some error message or other type of degradation.\n";
   alert(errMsg);
   return null;
 }   



/*****************************************
 *
 * This method handles a select call
 * @param method: Can be either GET or POST
 * @param target: the ws id
 * @param where: the where clause: FIELD|COMPARE|VALUE|AND_OR
 *    FIELD: The field to do the compare
 *    COMPARE: E    (=)
 *             L    (<)
 *             LE   (<=)
 *             G    (>)
 *             GE   (>=)
 *             LK   (LIKE)
 *             I    (IS)
 *             IN   (IS NOT)
 *    VALUE:   The value to compare the field to
 *    AND-OR:  Stablish the comparition between wheres
 *             A    (AND)
 *             O    (OR)           
 * @param type: Posible values 'text' 'xml' 
 * 
 ****************************************/
 function DoCall(method,target,where,type){
   
   var MAXIMUM_WAITING_TIME = (20*1000);
   var oXml = GetHTTPObject();
   if (oXml == null){
      return null;   
   }
   if (method.toUpperCase() != 'POST' && method.toUpperCase() != 'GET'){
      document.write("<!-- AJAX Error: DoCall: method parameter invalid -->\n");
      return null;
   }
   if ((target+"").toLowerCase() == "undefined" || target+"" == "" ){
      document.write("<!-- AJAX Error: DoCall: target parameter invalid -->\n"); 
      return null;
   }
   if (type.toLowerCase() != 'text' && type.toLowerCase() != 'xml'){
      type = 'text';
   }
   if (oXml.overrideMimeType && type.toLowerCase() == 'xml'){
    oXml.overrideMimeType('text/xml');
   }
   if (where != '')
   {
    where = '&w=' + where;
   }
   
   /*try
   {
	if (oXml.setRequestHeader && method.toUpperCase() == 'POST'){
		oXml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
   }catch(e){}
   */
   
   if (method.toUpperCase() == 'GET')
   {
	  oXml.open(method, target + where, false); 
      oXml.send(null);
   }else if(method.toUpperCase() == 'POST'){
   	  oXml.open(method, target, false); 
   	  oXml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
      oXml.send(where);
   }

   if (type == 'text'){
      return oXml.responseText;
   }else{
      return oXml.responseXML;
   }
 }
 
 
 
 /*****************************************
 *
 * This method handles an asynchroneous call
 * @param method: Can be either GET or POST
 * @param target: the ws id
 * @param where: the where clause: FIELD|COMPARE|VALUE|AND_OR
 *    FIELD: The field to do the compare
 *    COMPARE: E    (=)
 *             L    (<)
 *             LE   (<=)
 *             G    (>)
 *             GE   (>=)
 *             LK   (LIKE)
 *             I    (IS)
 *             IN   (IS NOT)
 *    VALUE:   The value to compare the field to
 *    AND-OR:  Stablish the comparition between wheres
 *             A    (AND)
 *             O    (OR)           
 * @param type: Posible values 'text' 'xml' 
 * 
 ****************************************/
 function DoCallAsync(method,target,where,type,async,fn){
   
   var MAXIMUM_WAITING_TIME = (20*1000);
   var oXml = GetHTTPObject();
   if (oXml == null){
      return null;   
   }
   if (method.toUpperCase() != 'POST' && method.toUpperCase() != 'GET'){
      document.write("<!-- AJAX Error: DoCall: method parameter invalid -->\n");
      return null;
   }
   
   if ((target+"").toLowerCase() == "undefined" || target+"" == "" ){
      document.write("<!-- AJAX Error: DoCall: target parameter invalid -->\n"); 
      return null;
   }
   if (type.toLowerCase() != 'text' && type.toLowerCase() != 'xml'){
      type = 'text';
   }
   if (oXml.overrideMimeType && type.toLowerCase() == 'xml'){
    oXml.overrideMimeType('text/xml');
   }
   
   if (async)
   {
   	oXml.onreadystatechange = function()
   	{
   		if (oXml.readyState == 4) 
   		{
        	if (oXml.status == 200) 
        	{
        		var o = null;
        			if (type == 'text')
        		   	{
				    	o = oXml.responseText;
					}else{
						o = oXml.responseXML;
					}
            	eval(fn(o));
        	}
    	}
   	}
   }
   
   if (method.toUpperCase() == 'POST'){
        oXml.open(method, target, async); 
   	  	oXml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');        
   		oXml.send(where);
   }else{
   		oXml.open(method, target + where, async); 
		oXml.send(null);
   }

	if (!async)
	{
		if (type == 'text'){
			return oXml.responseText;
		}else{
			return oXml.responseXML;
		}
	}
 }

 /***********************************************************
 *
 * This method do the transformation of an xml field applying the xsl file
 * @param xmlDoc: The xml document to transform
 * @param xslDoc: The xsl document to use to apply the trasformation
 * @param ctrId: The id of the control where we are going to display the result 
 * 
 ***********************************************************/
  function DoTransformation(xmlDoc,xslDoc,ctrId)
  {
    var element = document.getElementById(ctrId);
  	if(document.implementation && document.implementation.createDocument){
  		// Mozilla
  		var xsltProcessor = new XSLTProcessor();
  		xsltProcessor.importStylesheet(xslDoc);
  		//transform
  		var result = xsltProcessor.transformToFragment(xmlDoc, document);
  		element.innerHTML = "";
  		element.appendChild(result);
  		
  	}else if(window.ActiveXObject){
  		// IE	
  		// Load XML
  		xml = new ActiveXObject("MSXML2.DOMDocument");
  		xml.async = false;
  		xml.loadXML(xmlDoc.xml);
  		// Load XSL
  		xsl = new ActiveXObject("MSXML2.DOMDocument");
  		xsl.async = false;
  		xsl.loadXML(xslDoc.xml);
  		// Transform
  		element.innerHTML = xml.transformNode(xsl);
  	}else{
  	 return "";
  	}
  }