/*--------------------------------------------------------------------------------------------------------*/
var AJAX_Enabled=(createHTTPRequestObject()!=null);
var syncXMLRequestCache; // per a les peticions sincrones sempre farem servir una unica instància.
/*--------------------------------------------------------------------------------------------------------*/
//
function httpRequest(url, dadesPOST, callbackName)
{
	// Si l'AJAX no esta engegat donem com a resposta que no hi ha AJAX.
	if (!AJAX_Enabled) {throw new Error('AJAX engine is not running.');}

	//--- Si especifiquem nom de retorn, farem asincron.
	var sync=(callbackName==null);

	//--- Creem l'objecte que ens fara la conexió.
	var xmlRequest = createHTTPRequestObject();

	//--- Ajustem les dades a enviar si s'escau per evitar que sigui un null.
	if (dadesPOST==null) {dadesPOST='';}

	//--- Fem la petició a la pàgina.
	xmlRequest.open("POST", url, !sync);
	xmlRequest.setRequestHeader("Content-Type", "text/html"); // o application/x-www-form-urlencoded ?¿
	xmlRequest.setRequestHeader("Content-Length", dadesPOST.length);
	xmlRequest.send(dadesPOST);
        
	//--- Si som syncrons retornem el resultat.
	switch (sync)
	{
		case true: //Anem amb sincronic.
			if (xmlRequest.status==200) 
				{return xmlRequest.responseText; break;}
			else //Si la petició web ha generat un error, propaguem una excepció.
				{throw new Error(xmlRequest.statusText+' ('+xmlRequest.status+')');break;}
		case false:  //Anem amb asincronic.
			xmlRequest.onreadystatechange = callbackName; return xmlRequest; break;
	}
}
/*--------------------------------------------------------------------------------------------------------*/
function createHTTPRequestObject(isSync)
{
  try
  	{return new XMLHttpRequest();} // Firefox, Opera 8.0+, Safari
  catch (e)
  	{
    	try
      	{return new ActiveXObject("Msxml2.XMLHTTP");} // IE metode 1.
    	catch (e)
      	{
      		try
        		{return new ActiveXObject("Microsoft.XMLHTTP");} // IE metode 2.
      		catch (e)
        		{return null;}
				}
    }
}
/*--------------------------------------------------------------------------------------------------------*/
function URLEncode (str)
{
	if (str==null) {return '';}
	var sortida = new Array()
	for (var bucle=0;bucle<str.length;bucle++)
	{
		sortida[bucle] = str.charCodeAt(bucle).toString(16)
	}
	return '%'+sortida.join('%');
}; 
/*--------------------------------------------------------------------------------------------------------*/

