var xmlHttp

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);
		}
	}
}

//LLISTAT PRODUCTES
function llistat_productes(id_categoria,id_familia,id_subfamilia,cerca,oferta,seleccio,limit1,limit2,titol,capa,carret,filtres,camp,ordre,registres,id_marca)
{ 
	if(capa==1){
		xmlHttp_llistat=GetXmlHttpObject()
		if (xmlHttp_llistat==null)
		{
			 alert ("Este navegador no soporta este gestor.")
			 return
		}
	}else if(capa==2){
		xmlHttp_llistat_2=GetXmlHttpObject()
		if (xmlHttp_llistat_2==null)
		{
			 alert ("Este navegador no soporta este gestor.")
			 return
		}
		
	}else{
		xmlHttp_llistat_3=GetXmlHttpObject()
		if (xmlHttp_llistat_3==null)
		{
			 alert ("Este navegador no soporta este gestor.")
			 return
		}
		
	}
	
	var url="llistat_productes.php"
	url=url+"?id_categoria="+id_categoria
	url=url+"&id_familia="+id_familia
	url=url+"&id_subfamilia="+id_subfamilia
	url=url+"&cerca="+cerca
	url=url+"&oferta="+oferta
	url=url+"&seleccio="+seleccio
	url=url+"&limit1="+limit1
	url=url+"&limit2="+limit2
	url=url+"&titol="+titol
	url=url+"&carret="+carret
	url=url+"&filtres="+filtres
	url=url+"&sort="+camp
	url=url+"&ordre="+ordre  
	url=url+"&registres="+registres
	url=url+"&id_marca="+id_marca
	url=url+"&sid="+Math.random()
	
	//document.formulari.id_categoria.value=id_categoria;
	if(capa==1){
	xmlHttp_llistat.onreadystatechange=llistat_cargat;
	xmlHttp_llistat.open("GET",url,true)
	xmlHttp_llistat.send(null)
	}else if(capa==2){
	xmlHttp_llistat_2.onreadystatechange=llistat_cargat_2;	
	xmlHttp_llistat_2.open("GET",url,true)
	xmlHttp_llistat_2.send(null)
	}else{
	xmlHttp_llistat_3.onreadystatechange=llistat_cargat_3;	
	xmlHttp_llistat_3.open("GET",url,true)
	xmlHttp_llistat_3.send(null)	
	}
	
	
}

function llistat_cargat() 
{ 
	if (xmlHttp_llistat.readyState==1)
	{
		document.getElementById('llistat_productes').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('llistat_productes', xmlHttp_llistat.responseText, true);
		//alert(typeof Shadowbox);
		Shadowbox.setup();
		setTimeout('',1000);
	} 
}

function llistat_cargat_2() 
{ 
	if (xmlHttp_llistat_2.readyState==1)
	{
		document.getElementById('llistat_productes_2').innerHTML=''			
	}
	if (xmlHttp_llistat_2.readyState==4 || xmlHttp_llistat_2.readyState=="complete")
	{ 
		//document.getElementById("afegir_familia").style.display="block";
		//document.getElementById('llistat_productes_2').innerHTML=xmlHttp_llistat_2.responseText;
		SetContainerHTML('llistat_productes_2', xmlHttp_llistat_2.responseText, true);
		//alert(typeof Shadowbox);
		Shadowbox.setup();
		setTimeout('',1000);
	} 
}

function llistat_cargat_3() 
{ 
	if (xmlHttp_llistat_3.readyState==1)
	{
		document.getElementById('llistat_productes_3').innerHTML=''			
	}
	if (xmlHttp_llistat_3.readyState==4 || xmlHttp_llistat_3.readyState=="complete")
	{ 
		//document.getElementById("afegir_familia").style.display="block";
		//document.getElementById('llistat_productes_2').innerHTML=xmlHttp_llistat_2.responseText;
		SetContainerHTML('llistat_productes_3', xmlHttp_llistat_3.responseText, true);
		//alert(typeof Shadowbox);
		Shadowbox.setup();
		setTimeout('',1000);
	} 
}

//MENÚ CENTRAL FAMÍLIES
function llistat_families(id_categoria){
	
xmlHttp_llis_fam=GetXmlHttpObject()
	if (xmlHttp_llis_fam==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="llistat_families.php"
	url=url+"?id_categoria="+id_categoria
	url=url+"&sid="+Math.random()
	
	xmlHttp_llis_fam.onreadystatechange=llistat_families_carregat; 
	xmlHttp_llis_fam.open("GET",url,true)
	xmlHttp_llis_fam.send(null)
}

function llistat_families_carregat() 
{ 
	if (xmlHttp_llis_fam.readyState==1)
	{
		document.getElementById("llistat_families").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_llis_fam.readyState==4 || xmlHttp_llis_fam.readyState=="complete")
	{ 
		
		//document.getElementById("carret").innerHTML=xmlHttp_carret.responseText;
		SetContainerHTML('llistat_families', xmlHttp_llis_fam.responseText, true);
	} 
}

//MENÚ CENTRAL SUBFAMÍLIES
function llistat_subfamilies(id_categoria,id_familia){
	
xmlHttp_llis_subfam=GetXmlHttpObject()
	if (xmlHttp_llis_subfam==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="llistat_subfamilies.php"
	url=url+"?id_categoria="+id_categoria
	url=url+"&id_familia="+id_familia
	url=url+"&sid="+Math.random()
	
	xmlHttp_llis_subfam.onreadystatechange=llistat_subfamilies_carregat; 
	xmlHttp_llis_subfam.open("GET",url,true)
	xmlHttp_llis_subfam.send(null)
}

function llistat_subfamilies_carregat() 
{ 
	if (xmlHttp_llis_subfam.readyState==1)
	{
		document.getElementById("llistat_subfamilies").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_llis_subfam.readyState==4 || xmlHttp_llis_subfam.readyState=="complete")
	{ 
		
		//document.getElementById("carret").innerHTML=xmlHttp_carret.responseText;
		SetContainerHTML('llistat_subfamilies', xmlHttp_llis_subfam.responseText, true);
	} 
}


//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"><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);
	} 
}

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"><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);
	} 
}


//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';
}

//MODIFICAR USUARI
function fi_modif_registre(errors){

     document.getElementById("errors").innerHTML=errors;
	 document.getElementById("errors").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';
	 }

}

// FI COMPRA
function fi_compra(errors){
	 document.getElementById("formulari_compra").style.display = 'none';
     document.getElementById("errors_compra").innerHTML=errors;
	 document.getElementById("errors_compra").style.display = 'block';
   	
	 //document.getElementById('formulari').style.display = 'none';
	

}

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

}
// FI EMAIL RECOMANAR
function fi_recomanar(errors){
	 document.getElementById("form_recomanar").style.display = 'none';
     document.getElementById("errors_recordatori").innerHTML=errors;
	 document.getElementById("errors_recordatori").style.display = 'block';
   	
	 //document.getElementById('formulari').style.display = 'none';
	

}


//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='Carregant...'		
	}
	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='Carregant...'		
	}
	if (xmlHttp_subfami.readyState==4 || xmlHttp_subfami.readyState=="complete")
	{ 
		
		document.getElementById("subfamilies").innerHTML=xmlHttp_subfami.responseText;
		//SetContainerHTML('familias', xmlHttp_fami.responseText, true);
	} 
}

//MARQUES
function select_marques(id_categoria,id_marca)
{ 
	xmlHttp_marca=GetXmlHttpObject()
	if (xmlHttp_marca==null)
	{
		 alert ("Este navegador no soporta este gestor.")
		 return
	}
	var url="select_marques.php"
	url=url+"?id_categoria="+id_categoria
	url=url+"&id_marca="+id_marca
	url=url+"&sid="+Math.random()
	document.formulari_avansat.id_cat.value=id_categoria;
	xmlHttp_marca.onreadystatechange=marques_cargades; 
	xmlHttp_marca.open("GET",url,true)
	xmlHttp_marca.send(null)
}

function marques_cargades() 
{ 
	if (xmlHttp_marca.readyState==1)
	{
		document.getElementById("marques").innerHTML='Carregant...'		
	}
	if (xmlHttp_marca.readyState==4 || xmlHttp_marca.readyState=="complete")
	{ 
		
		document.getElementById("marques").innerHTML=xmlHttp_marca.responseText;
		//SetContainerHTML('familias', xmlHttp_fami.responseText, true);
	} 
}


//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 = 'inici.php?id_categoria_menu=1&titol_seccio=Material';
		}
		if (xmlHttp_login.responseText==3)
		{
			document.location = 'professionals.php';
		}
		//document.getElementById("errors_loginar").innerHTML=xmlHttp_login.responseText;
		//SetContainerHTML('familias', xmlHttp_fami.responseText, true);
	} 
}
