// JavaScript Document

// mostra la capa de Loading mentre carrega l'AJAX

function Noticies2(pagina,ordenacio,direccio, cas){
	var crida="formOrdenaNoticies.php?target=lista_eventos"; //url q rep l'ajax
	var i;
	if(typeof pagina != "undefined") crida+="&pagina="+pagina;
	if(typeof ordenacio != "undefined") crida+="&ordenacio="+ordenacio;
	if(typeof direccio != "undefined") crida+="&direccio="+direccio;
	if(typeof cas != "undefined") crida+="&cas="+cas;
	
	cridaAJAX3(crida,1);
}
function Socis2(pagina,ordenacio,direccio){
	var crida="formOrdenaSocis.php?target=lista_eventos"; //url q rep l'ajax
	var i;
	if(typeof pagina != "undefined") crida+="&pagina="+pagina;
	if(typeof ordenacio != "undefined") crida+="&ordenacio="+ordenacio;
	if(typeof direccio != "undefined") crida+="&direccio="+direccio;
	
	cridaAJAX3(crida,1);
}

function mostraDocus(id2, capa, edicio){
	var crida="mostraDocuments.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&edit="+edicio+"&target="+capa;
	cridaAJAX3(crida);
}

function mostraDocusSocis(id2, capa, edicio){
	var crida="mostraDocumentSocis.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&edit="+edicio+"&target="+capa;
	cridaAJAX3(crida);
}

function mostraDocusR(id2, capa, edicio){
	var crida="mostraDocumentsR.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&edit="+edicio+"&target="+capa;
	cridaAJAX3(crida);
}

function mostraDocusJ(id2, capa, edicio){
	var crida="mostraDocumentsJ.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&edit="+edicio+"&target="+capa;
	cridaAJAX3(crida);
}

function eliminaFitxer(id2, caso, nombre)
{
	var crida="esborraFitxer.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&caso="+caso+"&nombre="+nombre;
	cridaAJAX2(crida);
	
}

function eliminaFoto(id2, caso, nombre)
{
	var crida="esborraFoto.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&caso="+caso+"&nombre="+nombre;
	cridaAJAX2(crida);
}

function eliminaButlleti(id2, caso, nombre)
{
	var crida="esborraButlleti.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&caso="+caso+"&nombre="+nombre;
	cridaAJAX2(crida);
	
}

function eliminaDocument(id2, caso, nombre)
{
	var crida="esborraDocument.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&caso="+caso+"&nombre="+nombre;
	cridaAJAX2(crida);
	
}
function eliminaDocumentSoci(id2, caso, nombre)
{
	var crida="esborraDocumentSoci.php?";
	if(typeof id2 != "undefined") crida+="id="+id2+"&caso="+caso+"&nombre="+nombre;
	cridaAJAX2(crida);
	
}

function pintar_capa(id2, cont, total){
	var crida="pintar_capa.php?target=ajax";
	if(typeof id2 != "undefined") crida+="&id2="+id2;
	
	for(i=1;i<=total;i++){
		
		if(document.getElementById('menu_'+i))
			document.getElementById('menu_'+i).className='';	
		
	}
	
	document.getElementById('menu_'+cont).className='sel';
	cridaAJAX3(crida);
	
}


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 d'AJAX");
            return false;
        }
        httpRequest.onreadystatechange = function() { 
			//posicionaCapaLoading();
			//mostraCapaLoading();
			mostraResultat(httpRequest); 
		};
        httpRequest.open('GET', url+"?"+params, true);
        httpRequest.send(null);
}

function mostraResultat(httpRequest) {
	document.location.href="#top";
	if(httpRequest.readyState < 4){//loading
		//alert("estat igual a 1");
		//	document.getElementById("load").innerHTML="<img src=\"imatges/cargando.gif\" id=\"cargando\" name=\"cargando\" width=\"25\"/>";
	}
    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;
			Lightbox.init({descriptions: '.lightboxDesc', showControls: true});
        } else {
            alert('Error amb la petició');
        }
    }

}



function cridaAJAX2(url){
	var httpRequest;		
	var params="";
	
	
	try{
		params=url.split('?')[1];
		url=url.split('?')[0];
	}catch(e){}	
				//alert(params);
				var trozos = params.split ("=");
				var casos =(trozos[2]);
				var c = casos.split ("&");
				var caso= (c[0]);
				//alert(params);
				//alert(caso);
				var nombre =(trozos[3]);
				//alert(nombre);
				
				
				
    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 se ha podido crear una instancia de AJAX");
        return false;
    }

	httpRequest.onreadystatechange = function() { mostraResultat2(httpRequest, caso, nombre); };
					
    //httpRequest.onreadystatechange = function() { mostraResultat(httpRequest); };
    httpRequest.open('GET', url+"?"+params, true);
    httpRequest.send(null);
}


function mostraResultat2(httpRequest, caso, nombre) {
		if(httpRequest.readyState < 4){//loading
			
		}
		num = caso;
		nom=nombre;
        if (httpRequest.readyState == 4) {//la resposta un cop s'ha carregat
            if (httpRequest.status == 200) {
                // Mostrem la taula resultant
                resposta=httpRequest.responseText;

				if(resposta==1){
					document.getElementById("muestra"+num).style.display = "none"; 
					document.getElementById("linkeliminar"+num).style.display = "none";
					
					//alert(document.getElementById("load_imatge1").value);
					document.getElementById("link"+num).style.display = "none";
					//alert(document.getElementById("load_imatge1").value);
				}else alert("Error: "+resposta);
				//alert(resposta);
                //document.getElementById(target).innerHTML=resposta;
            } else {
                alert('Error con la petición');
            }
        }

    }


function cridaAJAX3(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 d'AJAX");
            return false;
        }
        httpRequest.onreadystatechange = function() { 
			//posicionaCapaLoading();
			//mostraCapaLoading();
			mostraResultat3(httpRequest); 
		};
        httpRequest.open('GET', url+"?"+params, true);
        httpRequest.send(null);
}

function mostraResultat3(httpRequest) {
	document.location.href="#top";
	if(httpRequest.readyState < 4){//loading
		//alert("estat igual a 1");
		//	document.getElementById("load").innerHTML="<img src=\"imatges/cargando.gif\" id=\"cargando\" name=\"cargando\" width=\"25\"/>";
	}
    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('Error amb la petició');
        }
    }

}