function Ajax(){
	// Define XML HTTP Object
	var objXMLHTTP = false;
	try{
		objXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){
		try{
			objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}catch (E){
			objXMLHTTP = false;
		}
	}

	// Define XML HTTP Object for not IE Browsers
	if (!objXMLHTTP && typeof XMLHttpRequest!='undefined') {
		objXMLHTTP = new XMLHttpRequest();
	}
	return objXMLHTTP;
}

var objAjax = "";
var strXML = "";
var finishAjaxCall = function(strResponse){
	// Do Nothing
};
var xslFile = "xsl/resultados_empresa.xsl";

function callAjaxURL(strURL, arrData, blnCallerMode){
	if (objAjax == "")
		objAjax = Ajax();

	// Parse Data
	if (arrData){
		strURL += "?";
		for (i = 0; i < arrData.length; i++){
			strURL += arrData[i][0] +"=" + arrData[i][1] + "&";
		}
	}

	// Send Data
	strURLFinal = (blnCallerMode) ? "callers/caller.php?strURL=" + encodeURIComponent(strURL) : strURL;

	if (blnCallerMode)
		objAjax.open("GET", strURLFinal, true);
	function HttpRequest(){
		if (objAjax.readyState == 4){
			finishAjaxCall(objAjax.responseText);
		}
	};

	if (blnCallerMode){
		objAjax.onreadystatechange = HttpRequest;
		objAjax.send(null);
	}else{
		alert(strURLFinal);
	}
}

function getSearch(strURL, arrData, intPagina){
	strXML = "";

	// Call URL in Ajax
	finishAjaxCall = function(strResponse){
		strXML = strResponse;
		writeXMLStyled(intPagina);
	};
	callAjaxURL(strURL, arrData, true);
}

function writeXMLStyled(intPagina){
	var strTransformedXML = "";

	try{
		var xmlDoc;
		if (isIE){
			// Load XML
			xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
			xmlDoc.async = false;
			xmlDoc.loadXML(strXML);

			// Load XSL
			var xslDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
			xslDoc.async = false;
			xslDoc.load(xslFile);

			// Prepare the xsl document for transformation
			docCache = new ActiveXObject("MSXML2.XSLTemplate");
			docCache.stylesheet = xslDoc;
			docProcessor = docCache.createProcessor();
			docProcessor.input = xmlDoc;

			// Add parameters to the xsl document
			arrTextoBusqueda = strTextoBusqueda.split(" ");
			docProcessor.addParameter("busqueda", strTextoBusqueda, "");
			docProcessor.addParameter("pagina", intPagina, "");
			docProcessor.addParameter("paginado", 10, "");
			docProcessor.addParameter("timestamp", new Date().getTime(), "");
			docProcessor.addParameter("cantidadPalabras", arrTextoBusqueda.length, "");

			// Process the documents
			docProcessor.transform();
			strTransformedXML = docProcessor.output

			objLayerResultados = document.getElementById('divResultados');
			objLayerResultados.innerHTML = strTransformedXML;
		}else{
			// Load XML
			var objParser = new DOMParser();
			xmlDoc = objParser.parseFromString(strXML, "text/xml");

			// Load XSL
			var myXMLHTTPRequest = new XMLHttpRequest(); 
			myXMLHTTPRequest.open("GET", xslFile, false);
			myXMLHTTPRequest.send(null);
			xslStylesheet = myXMLHTTPRequest.responseXML;

			// Prepare the xsl document for transformation
			var xsltProcessor = new XSLTProcessor(); 
			xsltProcessor.importStylesheet(xslStylesheet);

			// Add parameters to the xsl document
			arrTextoBusqueda = strTextoBusqueda.split(" ");
			xsltProcessor.setParameter(null, "busqueda", strTextoBusqueda);
			xsltProcessor.setParameter(null, "pagina", intPagina);
			xsltProcessor.setParameter(null, "paginado", 10);
			xsltProcessor.setParameter(null, "timestamp", new Date().getTime());
			xsltProcessor.setParameter(null, "cantidadPalabras", arrTextoBusqueda.length);

			var strTransformedXML = xsltProcessor.transformToFragment(xmlDoc, document);

			objLayerResultados = document.getElementById('divResultados');
			objLayerResultados.innerHTML = "";
			objLayerResultados.appendChild(strTransformedXML);
		}

		document.getElementById('trBuscando').style.display = "none";
		document.getElementById('trResultados').style.display = (isIE) ? "block" : "table-row";
	}catch(e){
		document.getElementById('divErrorXML').innerHTML = "<br>" + ((isIE) ? e.description : e) + "<br>&nbsp;";
		document.getElementById('trBuscando').style.display = "none";
		document.getElementById('trErrorResultados').style.display = (isIE) ? "block" : "table-row";
	}
}
