/*
ajax.js >> Llibreria per realitzar crides Ajax a Asp's

Mòde d'ús:

Per fer GET:
q="denom_proj=" + document.dades.denom_proj.value;
q=q + "&data_in=" + document.dades.data_in.value;
q=q + "&data_fi=" + document.dades.data_fi.value;
execAjax("detall_projecte_ajax.asp",q,"");

Per fer POST:
q="denom_proj=" + document.dades.denom_proj.value;
q=q + "&data_in=" + document.dades.data_in.value;
q=q + "&data_fi=" + document.dades.data_fi.value;
execAjax("detall_projecte_ajax.asp","",q);

Es possible fer POST i a l'hora passar vía GET també algunes dades: execAjax("detall_projecte_ajax.asp","opcio=1&tip=S",q);

Notes:
- Ha d'existir una funció repostaAjax(request) que rebrà lo que retorna el asp cridat vía Ajax ja amb el unescape fet.
- Pot existir un div amb el id="espera" que contingui una imatge i texte d'espera. Es mostrarà centrat al fer les crides ajax.
- Pot existir una imatge amb el id="espera_in". Es mostrarà "inline" en el lloc on sigui al fer les crides ajax.
*/
function objetus(file) {
	xmlhttp=false;
	this.AjaxFailedAlert="El seu navegador no soporta les funcionalitats d'aquest lloc web i podría experimentar un mal funcionament. Si us plau, habiliti el javascript en el seu navegador per veure aquest lloc correctament.\n";
   this.requestFile=file;
   this.encodeURIString=true;
   this.execute=false;
   if (window.XMLHttpRequest) { 
		this.xmlhttp=new XMLHttpRequest();
      if (this.xmlhttp.overrideMimeType) {
			this.xmlhttp.overrideMimeType("text/xml");
      }
   } 
   else if (window.ActiveXObject) { // IE
		try {
			this.xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e) {
			try {
				this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {
				this.xmlhttp=null;
         }
      }
      if (!this.xmlhttp && typeof XMLHttpRequest!="undefined") {
			this.xmlhttp=new XMLHttpRequest();
         if (!this.xmlhttp) {
				this.failed=true; 
         }
      } 
   }
   return this.xmlhttp;
}
function execAjax(_pagina, valorget, valorpost) {
    if (typeof respostaAjax == "function") {
        execAjaxFunct(_pagina, valorget, valorpost, respostaAjax);
    }
    else {
        alert("Ha d'existir una funció respostaAjax(response) per tractar la resposta retornada");
    }
  
}

function execAjaxFunct(_pagina,valorget,valorpost,functRespostaAjax) {  
   ajax=objetus(_pagina);
   if (valorpost!="") {
		ajax.open("POST", _pagina+"?"+valorget+"&temps="+new Date().getTime(),true);
   }
	else {
		ajax.open("GET", _pagina+"?"+valorget+"&temps="+new Date().getTime(),true);
   }
   ajax.onreadystatechange=function() {
		if (ajax.readyState==1) {
			if (document.getElementById("espera")) {
				document.getElementById("espera").style.display="block";
				document.getElementById("espera").style.top=(document.body.clientHeight/2) - (document.getElementById("espera").offsetHeight/2) + document.body.scrollTop;
				document.getElementById("espera").style.left=(document.body.clientWidth/2) - (document.getElementById("tEspera").offsetWidth/2) + document.body.scrollLeft;
			}
			if (document.getElementById("espera_in")) {
				document.getElementById("espera_in").style.display="inline";
			}
		}
      if (ajax.readyState==4) {
			if (ajax.status==200) {
			    //if (typeof functRespostaAjax == "function") {
			        functRespostaAjax(unescape(ajax.responseText));
					
				//}
				//else {
					//alert("Ha d'existir una funció respostaAjax(response) per tractar la resposta retornada");
				//}
			}
         else if (ajax.status==404) {
				alert("La URL que ha fet servir per la crida AJAX no existeix.");
         }
         else {
				//alert("Error en l'execució de l'URL: "+ajax.status+"\n\n"+unescape(ajax.responseText));
         }
			if (document.getElementById("espera")) {
				document.getElementById("espera").style.display="none";
			}
			if (document.getElementById("espera_in")) {
				document.getElementById("espera_in").style.display="none";
			}
      }
   }
   if (valorpost!="") {
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send(valorpost);
   }
	else {
		ajax.send(null);
   }
}
