function GetXmlHttpObject()
{
	var xmlhttp=false;
    try
    {
                // Creacion del objeto AJAX para navegadores no IE
   		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
    	try
      	{
    		// Creacion del objet AJAX para IE
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 		}
		catch(E)
 		{
  			if (!xmlhttp && typeof XMLHttpRequest!="undefined") xmlhttp=new XMLHttpRequest();
       	}
 	}
  	return xmlhttp; 
}

function SetContainerHTML(id,html,processScripts){
	mydiv = document.getElementById(id);
	mydiv.innerHTML = html;
	if(processScripts!=false){
		var elementos = mydiv.getElementsByTagName('script');
		for(i=0;i<elementos.length;i++) {
			var elemento = elementos[ i ];
			nuevoScript = document.createElement('script');
			nuevoScript.text = elemento.innerHTML;
			nuevoScript.type = 'text/javascript';
			if(elemento.src!=null && elemento.src.length>0)
			nuevoScript.src = elemento.src;
			elemento.parentNode.replaceChild(nuevoScript,elemento);
		}
	}
}

function reiniciar_scroll(capa,altura,amplada){
	document.getElementById(capa).style.height=altura+"px";	
	document.getElementById(capa).style.width=amplada+"px";
	//document.getElementById(capa).style.backgroundColor="#CCCCCC";
	$('#'+capa).jScrollPane({showArrows:true,maintainPosition:false});
	//$('#'+capa).jScrollPane({maintainPosition:false});
	//$('#'+capa)[0].scrollTo($('#'+capa).data('jScrollPaneMaxScroll'));



}

function fi_enviat(errors){

     document.getElementById("errors").innerHTML=errors;
	 document.getElementById("errors").style.display = 'block';
   	
	 document.getElementById('formulari').style.display = 'none';
	  
}


//LLISTAT NOTICIES
function llistat_noticies(limit1,limit2,registres)
{ 
	xmlHttp_llistat=GetXmlHttpObject()
	if (xmlHttp_llistat==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
		
	var url="llistat_noticies_index.php"
	url=url+"?limit1="+limit1
	url=url+"&limit2="+limit2
	url=url+"&registres="+registres
	url=url+"&sid="+Math.random()
	
	//document.formulari.id_categoria.value=id_categoria;
	
	xmlHttp_llistat.onreadystatechange=llistat_cargat;
	xmlHttp_llistat.open("GET",url,true)
	xmlHttp_llistat.send(null)
		
}

function llistat_cargat() 
{ 
	if (xmlHttp_llistat.readyState==1)
	{
		document.getElementById('div_inici_noticies').innerHTML='<div style="width:100%;height:32px; min-height:32px; text-align=center;" align="center"><img src="images/loading.gif" vspace="5" align="absmiddle"></div>'		
	}
	if (xmlHttp_llistat.readyState==4 || xmlHttp_llistat.readyState=="complete")
	{ 
		//document.getElementById("afegir_familia").style.display="block";
		//document.getElementById('llistat_productes').innerHTML=xmlHttp_llistat.responseText;
		SetContainerHTML('div_inici_noticies', xmlHttp_llistat.responseText, true);
		//alert(typeof Shadowbox);
		Shadowbox.setup();
		setTimeout('',5000);
		reiniciar_scroll("div_inici_noticies","190","450")
		setTimeout('',5000);
		
		
	} 
}


//FAMILIES PRODUCTES
function llistat_families(id_familia,limit1,limit2,registres)
{ 
	xmlHttp_families=GetXmlHttpObject()
	if (xmlHttp_families==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
		
	var url="llistat_families.php"
	url=url+"?id_familia="+id_familia
	url=url+"&limit1="+limit1
	url=url+"&limit2="+limit2
	url=url+"&registres="+registres
	url=url+"&sid="+Math.random()
	
	//document.formulari.id_categoria.value=id_categoria;
	
	xmlHttp_families.onreadystatechange=families_cargat;
	xmlHttp_families.open("GET",url,true)
	xmlHttp_families.send(null)
		
}



function families_cargat() 
{ 
	if (xmlHttp_families.readyState==1)
	{
		document.getElementById('div_llistat_families').innerHTML='<div style="width:100%;height:32px; min-height:32px; text-align=center;" align="center"><br><br><img src="images/loading.gif" vspace="5" align="absmiddle"></div>'		
	}
	if (xmlHttp_families.readyState==4 || xmlHttp_families.readyState=="complete")
	{ 
		//document.getElementById("afegir_familia").style.display="block";
		//document.getElementById('llistat_productes').innerHTML=xmlHttp_llistat.responseText;
		/*reinitialiseScrollPane = function()
				{
				document.getElementById("div_llistat_families").style.height="350px";	
				document.getElementById("div_llistat_families").style.width="570px";
				$('#div_llistat_families').jScrollPane({showArrows:true});	
				}
		*/
		
		
		
		//$('#div_llistat_families').load(SetContainerHTML('div_llistat_families', xmlHttp_families.responseText, true), '', reinitialiseScrollPane);
		
		SetContainerHTML('div_llistat_families', xmlHttp_families.responseText, true);
		
		//alert(typeof Shadowbox);
		
		Shadowbox.setup();
		setTimeout(reiniciar_scroll("div_llistat_families","350","565"),1000);
	} 
}



//LLISTAT PRODUCTES
function llistat_productes(id_subfamilia,limit1,limit2,registres)
{ 
	xmlHttp_productes=GetXmlHttpObject()
	if (xmlHttp_productes==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
		
	var url="llistat_productes.php"
	url=url+"?id_subfamilia="+id_subfamilia
	url=url+"&limit1="+limit1
	url=url+"&limit2="+limit2
	url=url+"&registres="+registres
	url=url+"&sid="+Math.random()
	
	//document.formulari.id_categoria.value=id_categoria;
	
	xmlHttp_productes.onreadystatechange=productes_cargat;
	xmlHttp_productes.open("GET",url,true)
	xmlHttp_productes.send(null)
		
}

function productes_cargat() 
{ 
	if (xmlHttp_productes.readyState==1)
	{
		document.getElementById('div_llistat_families').innerHTML='<div style="width:100%;height:32px; min-height:32px; text-align=center;" align="center"><br><br><img src="images/loading.gif" vspace="5" align="absmiddle"></div>'		
	}
	if (xmlHttp_productes.readyState==4 || xmlHttp_productes.readyState=="complete")
	{ 
		SetContainerHTML('div_llistat_families', xmlHttp_productes.responseText, true);
		Shadowbox.setup();
		setTimeout(reiniciar_scroll("div_llistat_families","350","565"),1000);
	} 
}



//CERCADOR AVANÇAT
//FAMILIES
function select_families(id_categoria,id_familia)
{ 
	xmlHttp_fami=GetXmlHttpObject()
	if (xmlHttp_fami==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="select_familias.php"
	url=url+"?id_categoria="+id_categoria
	url=url+"&id_familia="+id_familia
	url=url+"&sid="+Math.random()
	//document.formulari_avansat.id_cat.value=id_categoria;
	xmlHttp_fami.onreadystatechange=families_cargades; 
	xmlHttp_fami.open("GET",url,true)
	xmlHttp_fami.send(null)
}

function families_cargades() 
{ 
	if (xmlHttp_fami.readyState==1)
	{
		document.getElementById("familias").innerHTML='<div style="width:100px;height:16px; max-height:16px; text-align=center;" align="center"><img src="images/loading.gif" vspace="0" align="absmiddle" width="16" height="16"></div>'		
	}
	if (xmlHttp_fami.readyState==4 || xmlHttp_fami.readyState=="complete")
	{ 
		
		document.getElementById("familias").innerHTML=xmlHttp_fami.responseText;
		//SetContainerHTML('familias', xmlHttp_fami.responseText, true);
	} 
}

//SUBFAMÍLIES
function select_subfamilies(id_familia,id_subfamilia)
{ 
	xmlHttp_subfami=GetXmlHttpObject()
	if (xmlHttp_subfami==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="select_subfamilies.php"
	url=url+"?id_subfamilia="+id_subfamilia
	url=url+"&id_familia="+id_familia
	url=url+"&sid="+Math.random()
	document.formulari_avansat.id_fam.value=id_familia;
	xmlHttp_subfami.onreadystatechange=subfamilies_cargades; 
	xmlHttp_subfami.open("GET",url,true)
	xmlHttp_subfami.send(null)
}

function subfamilies_cargades() 
{ 
	if (xmlHttp_subfami.readyState==1)
	{
		document.getElementById("subfamilies").innerHTML='<div style="width:100px;height:16px; max-height:16px; text-align=center;" align="center"><img src="images/loading.gif" vspace="0" align="absmiddle" width="16" height="16"></div>'		
	}
	if (xmlHttp_subfami.readyState==4 || xmlHttp_subfami.readyState=="complete")
	{ 
		
		document.getElementById("subfamilies").innerHTML=xmlHttp_subfami.responseText;
		//SetContainerHTML('familias', xmlHttp_fami.responseText, true);
	} 
}




//LLISTAT PRODUCTES CERCA
function llistat_productes_cerca(cerca,id_familia,id_subfamilia,limit1,limit2,registres)
{ 
	xmlHttp_productes_cerca=GetXmlHttpObject()
	if (xmlHttp_productes_cerca==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
		
	var url="llistat_productes.php"
	url=url+"?cerca="+cerca
	url=url+"&id_familia="+id_familia
	url=url+"&id_subfamilia="+id_subfamilia
	url=url+"&limit1="+limit1
	url=url+"&limit2="+limit2
	url=url+"&registres="+registres
	url=url+"&sid="+Math.random()
	
	//document.formulari.id_categoria.value=id_categoria;
	
	xmlHttp_productes_cerca.onreadystatechange=productes_cerca_cargat;
	xmlHttp_productes_cerca.open("GET",url,true)
	xmlHttp_productes_cerca.send(null)
		
}

function productes_cerca_cargat() 
{ 
	if (xmlHttp_productes_cerca.readyState==1)
	{
		document.getElementById('div_llistat_families').innerHTML='<div style="width:100%;height:32px; min-height:32px; text-align=center;" align="center"><br><br><img src="images/loading.gif" vspace="5" align="absmiddle"></div>'		
	}
	if (xmlHttp_productes_cerca.readyState==4 || xmlHttp_productes_cerca.readyState=="complete")
	{ 
		
		SetContainerHTML('div_llistat_families', xmlHttp_productes_cerca.responseText, true);
		Shadowbox.setup();
		setTimeout(reiniciar_scroll("div_llistat_families","270","565"),1000);
	} 
}



//REGISTRE USUARI
function fi_registre(errors){

     document.getElementById("errors").innerHTML=errors;
	 document.getElementById("errors").style.display = 'block';
   	 document.getElementById('formulari').style.display = 'none';
	 document.getElementById('formulari_registrat').style.display = 'none';
	document.getElementById('formulari').innerHTML="";
	setTimeout(reiniciar_scroll("formulari","1000","800"),1000);
		 
}


// FI EMAIL RECORDATORI PASS
function fi_recordatori(errors){
	 window.document.getElementById("div_pass").style.display = 'none';
     window.document.getElementById("errors_recordatori").innerHTML=errors;
	 window.document.getElementById("errors_recordatori").style.display = 'block';
   	
	 //document.getElementById('formulari').style.display = 'none';
	

}

//LOGIN

function fi_login(errors,mostrar){
	 if(mostrar==0){
		document.getElementById('area_login').style.display = 'none'; 
		document.getElementById('area_modif').style.display = 'block';
		document.getElementById("errors_box").style.display = 'none';
		document.getElementById("menu_esq").style.display = 'none';
		document.getElementById("titol_seccions").style.display = 'none';
	 }else{
     document.getElementById("errors_login").innerHTML=errors;
	 document.getElementById("errors_box").style.display = 'block';
   	
	 //document.getElementById('formulari').style.display = 'none';
	 }

}


//loginar
function loginar()
{ 
	xmlHttp_login=GetXmlHttpObject()
	if (xmlHttp_login==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}

	var url="loginar.php"
	url=url+"?login_esq="+document.form_log.login.value;
	url=url+"&password="+document.form_log.pass.value;
	url=url+"&loginar=1";
	url=url+"&sid="+Math.random()
	
	xmlHttp_login.onreadystatechange=login_carregat; 
	xmlHttp_login.open("GET",url,true)
	xmlHttp_login.send(null)
}

function login_carregat() 
{ 
	if (xmlHttp_login.readyState==1)
	{
		document.getElementById("errors_loginar").style.display = 'none';	
	}
	if (xmlHttp_login.readyState==4 || xmlHttp_login.readyState=="complete")
	{ 
	//alert(xmlHttp_login.responseText); //si hi ha -1 es que no existeix
		if (xmlHttp_login.responseText==-1)
		{
			document.getElementById("errors_loginar").style.display = 'block';
		}
		if (xmlHttp_login.responseText==1)
		{
			document.location = 'mostrar_carret.php';
		}
		if (xmlHttp_login.responseText==2)
		{
			document.location = 'botiga.php';
		}
		if (xmlHttp_login.responseText==3)
		{
			document.location = 'botiga.php';
		}
		//document.getElementById("errors_loginar").innerHTML=xmlHttp_login.responseText;
		//SetContainerHTML('familias', xmlHttp_fami.responseText, true);
	} 
}


//MODIFICAR USUARI
function fi_modif_registre(errors){

     document.getElementById("errors").innerHTML=errors;
	 document.getElementById("errors").style.display = 'block';
   	
	 document.getElementById('formulari').style.display = 'none';
	 document.getElementById('formulari').innerHTML="";
	setTimeout(reiniciar_scroll("formulari","1000","800"),1000);
}



//CISTELLA COMPRA
function mostrar_carret()
{ 
	xmlHttp_carret=GetXmlHttpObject()
	if (xmlHttp_carret==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="llistat_carret.php"
	url=url+"?sid="+Math.random()
	
	xmlHttp_carret.onreadystatechange=carret_carregat; 
	xmlHttp_carret.open("GET",url,true)
	xmlHttp_carret.send(null)
}

function carret_carregat() 
{ 
	if (xmlHttp_carret.readyState==1)
	{
		document.getElementById("carret").innerHTML='<div style="width:100%;height:32px; min-height:32px; text-align=center;" align="center"><br><br><img src="images/loading.gif" vspace="5" align="absmiddle"></div>'	
	}
	if (xmlHttp_carret.readyState==4 || xmlHttp_carret.readyState=="complete")
	{ 
		
		//document.getElementById("carret").innerHTML=xmlHttp_carret.responseText;
		SetContainerHTML('carret', xmlHttp_carret.responseText, true);
		setTimeout(reiniciar_scroll("carret","270","565"),1000);
	} 
}

function sessio_carret(id_carret,qtt,pas)
{ 
	xmlHttp_sessio=GetXmlHttpObject()
	if (xmlHttp_sessio==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="afegir.php"
	url=url+"?id_carret="+id_carret
	url=url+"&qtt="+qtt
	url=url+"&pas="+pas
	url=url+"&sid="+Math.random()
	
	xmlHttp_sessio.onreadystatechange=sessio_carregada; 
	xmlHttp_sessio.open("GET",url,true)
	xmlHttp_sessio.send(null)
}

function sessio_carregada() 
{ 
	if (xmlHttp_sessio.readyState==1)
	{
		document.getElementById("carret").innerHTML='<div style="width:100%;height:32px; min-height:32px; text-align=center;" align="center"><br><br><img src="images/loading.gif" vspace="5" align="absmiddle"></div>';
		document.getElementById("errors_stock").style.display="none";
	}
	if (xmlHttp_sessio.readyState==4 || xmlHttp_sessio.readyState=="complete")
	{ 
		
		document.getElementById("carret").style.display="block";
		
		if(xmlHttp_sessio.responseText!=''){
			document.getElementById("errors_stock").innerHTML=xmlHttp_sessio.responseText;
			document.getElementById("errors_stock").style.display="block";
		}
		mostrar_carret('');
		//document.getElementById("carret").innerHTML=xmlHttp_carret.responseText;
		//SetContainerHTML('carret', xmlHttp_sessio.responseText, true);
	} 
}
