var http_request = false;

function peticionAjax(url, array_param)
{
	str = "";
	for(i = 0; i < array_param.length; i = i + 2) 
	{
		if (str.length > 0)	
		{
			str += "&";
		}
		str += array_param[i] + "=" + array_param[i+1];
	}

	peticion = creaAjax();

	if (peticion)
	{
		peticion.open("POST", url, false); //true -> cambiado para que sea síncrona
		peticion.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); //iso-8859-1

		peticion.send(str);

		strResultado = peticion.responseText;

		return strResultado;
	}
	else
	{
		alert('There was an error with the request.');
		return false;
	}
}

function creaAjax()
{
	http_request = false;
	
	if (window.XMLHttpRequest)
	{ 
		// Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) 
	{ 
		// IE
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}

	if (!http_request) 
	{
		alert('No se ha podido crear una instancia del objeto XMLHTTP');
		return false;
	}
	
	return http_request;
}

function stringToXML(xmlString)
{
    var xmlString = "<?xml version=\"1.0\"?><root>" + xmlString + "</root>";
    var oDomDoc = (new DOMParser()).parseFromString(xmlString, "text/xml");
    return oDomDoc;
}

function dameNodos(xmlDoc, strNombreNodo)
{
    return xmlDoc.getElementsByTagName(strNombreNodo);
}

function dameValorNodo(xmlDoc, strNombreNodo)
{
    return dameValorNodo(xmlDoc, strNombreNodo, 0);
}

function dameValorNodo(xmlDoc, strNombreNodo, num)
{
    if (esNodoVacio(xmlDoc, strNombreNodo)) return "";

    if ((dameNodos(xmlDoc, strNombreNodo).item(num).firstChild) != null)
    {
        return unescape(dameNodos(xmlDoc, strNombreNodo).item(num).firstChild.data);
    }

    return "";
}

function dameNodoChild(parent, numNodo)
{
    if (parent.childNodes[numNodo] == null) return "";

    if (parent.childNodes[numNodo].firstChild == null) return "";

    return parent.childNodes[numNodo].firstChild.nodeValue;
}

function esNodoVacio(xmlDoc, strNombreNodo)
{
    //true si es vacio o no existe el nodo
    nodos = dameNodos(xmlDoc, strNombreNodo);
    return ((nodos.length == 0) || (nodos.item(0).firstChild == null));
}
