// JavaScript Document

function generaNoticies(pagina){
	var crida="ajax/generaNoticias.php?target=ajax";
	if(typeof pagina != "undefined") crida+="&pagina="+pagina;
	cridaAJAX(crida);
}
function comprovar_user(){
	var user= document.getElementById('user').value;
	var crida="ajax/comprUser.php?target=comprovar_user";
	if(typeof user != "undefined") crida+="&user="+user;
	cridaAJAX(crida);
}
function llenar_bd(){
	var mail= document.getElementById('mail').value;
	var crida="ajax/llenar_bd.php?target=llenar_bd";
	if(typeof mail != "undefined") crida+="&mail="+mail;
	cridaAJAX(crida);
	document.getElementById("formulari").submit();
	

}

// funcio generica d'AJAX
function cridaAJAX(url){
	var httpRequest;
		
			
		var params="";

		try{
			params=url.split('?')[1];
			url=url.split('?')[0];
		}catch(e){}
		try{
			target=params.split('target=')[1].split('&')[0];
		}catch(e){
			target="";	
		}
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!httpRequest) {
            alert("Error, no s'ha pogut crear una instància de AJAX");
            return false;
        }
        httpRequest.onreadystatechange = function() { 
			//posicionaCapaLoading();
			//mostraCapaLoading();
			mostraResultat(httpRequest, target); 
		};
        httpRequest.open('GET', url+"?"+params, true);
        httpRequest.send(null);
}

// mostrem el resultat obtingut per AJAX
function mostraResultat(httpRequest, target) {


	
	if(httpRequest.readyState < 4){//loading
		//Opció per possar carregant a la pàgina
		//	document.getElementById("load").innerHTML="<img src=\"imatges/cargando.gif\" id=\"cargando\" name=\"cargando\" width=\"25\"/>";
		
		//setTimeout(doNext,15000);
	}
    if (httpRequest.readyState == 4) {//la resposta un cop s'ha carregat
		//setTimeout(amagaCapaLoading,1000); 
		
        if (httpRequest.status == 200) {
            // Mostrem la taula resultant
			
            resposta=httpRequest.responseText;
			document.getElementById(target).innerHTML=resposta;
			
			
        } 
		else {
            alert(httpRequest.status);
        }
    }

}