function carrito_accio(accio,id_prod,prod_nom,prod_qty, preu_prod) {
	//envia accio i id_prod per afegir, eliminar items del carrito
	//alert ("dintre carrito_accio:" + accio + "idprod:" + id_prod);
   // var content = document.getElementById('bombolla_carrito');      
	var callback2 = {
		success : function(o) {
		 // alert (o.responseText);
			var a_resposta =o.responseText.split('##');
			document.getElementById('capa_content_num_prods_cistella').innerHTML=a_resposta[2];
			alert (a_resposta[1]);
		   // content.innerHTML = o.responseText;
		  //  content.style.visibility = "visible";
			if (accio=="afegir_felicitacio") { document.getElementById('bombolla_envia_felicitacio').style.visibility='hidden'; }
			if (accio=='eliminar') document.location='veurecistella.php';
		},
		failure : function(o) {
			content.innerHTML = o.responseText;
			content.style.visibility = "visible";
			content.innerHTML = "Error de conexión";
		}
	}
	if (accio=="afegir_felicitacio") {
		id_form=document.getElementById('idform').value; 
		
 		//Definit objecte formulari pq sino, no carrega correctament les dades del formulari 
 		  var formObjecte = document.getElementById('form_felicitacio');
      YAHOO.util.Connect.setForm(formObjecte); var conn =	YAHOO.util.Connect.asyncRequest('POST', "carrito_accions.php", callback2);
  }
  else
  {
  	 var postData = "accio=" + accio + "&id_prod=" + id_prod +"&nom_prod=" +prod_nom+"&prod_qty="+prod_qty+"&preu_prod=" +preu_prod;
   	 var conn = YAHOO.util.Connect.asyncRequest("POST", "carrito_accions.php", callback2, postData); } 
}

function ActualitzaPreus (id_prod, preu, preu_total_prod_actual) {
	//alert (preu_total_prod_actual);
	var id_camp = "quantitat_"+id_prod;
	var id_preu = "suma_prod_"+id_prod;
	var qty = document.getElementById(id_camp).value;
	
	if (isPositiveInteger(qty) && qty>0) {
		//var preu_total_actual=document.getElementById(id_preu).innerHTML.replace(",", ".");

		//alert ("Preu total del producte agafat de la capa:"+document.getElementById(id_preu).innerHTML);	
		var preu_total_actual=document.getElementById(id_preu).innerHTML.replace(".", "");
	    preu_total_actual=parseFloat(preu_total_actual.replace(",","."));
		//alert ("PREU TOTAL DEL PRODUCTE pasat a float: "+preu_total_actual);
		//alert ("suma total del producte: "+preu_total_actual);
		//alert ("preu producte: "+parseFloat(preu));
		var nou_preu=parseFloat(preu) * qty;
		//alert ("nou preu: "+nou_preu);
		var a_nou_preu=nou_preu.toString().split(".");
		var enter = addSeparatorsNF (a_nou_preu[0], '.', ',', '.');
		if (a_nou_preu.length>1)
		{
			//alert (a_nou_preu[1]);
			if (a_nou_preu[1].length>1) var decimals=a_nou_preu[1].charAt(0) + a_nou_preu[1].charAt(1); //decimals=a_nou_preu[1]; 
			else var decimals=a_nou_preu[1]+"0";
			var s_nou_preu=enter+","+decimals;
		}
		else var s_nou_preu=enter+",00";
		document.getElementById(id_preu).innerHTML=s_nou_preu+" &euro;";
		
		//alert ("SUMA TOTAL GUARDADA A LA PAGINA: "+document.getElementById('hidden_preu_total').value);
		var suma_total_actual=parseFloat(document.getElementById('hidden_preu_total').value);
		//alert ("suma total abans canvis: "+suma_total_actual+"   preu total que restem: "+ preu_total_actual);
		suma_total_actual = suma_total_actual - preu_total_actual;
		//alert ("SUMA DESPRES RESTA "+suma_total_actual);
		//alert(suma_total_actual+" = "+suma_total_actual+" + "+nou_preu);
		suma_total_actual = suma_total_actual + nou_preu;
		
		//alert ("SUMA TOTAL NOVA "+suma_total_actual+" Hem sumat: "+nou_preu);
		document.getElementById('hidden_preu_total').value=suma_total_actual;
		var a_suma_total_actual=suma_total_actual.toString().split(".");
		var enter = addSeparatorsNF (a_suma_total_actual[0], '.', ',', '.');
		
		if (a_suma_total_actual.length>1)
		{
			if (a_suma_total_actual[1].length>1) var decimals=a_suma_total_actual[1].charAt(0)+a_suma_total_actual[1].charAt(1);
			else var decimals=a_suma_total_actual[1]+"0";
					
			var s_nou_preu=enter+","+decimals;
		}
		else var s_nou_preu=enter+",00";
		document.getElementById('num_preu_total').innerHTML=s_nou_preu;
		//actualitzar carrito
		carrito_accio('actualitzar', id_prod, "", qty, preu);
	}
	else
	{
		document.getElementById(id_camp).value="";
//		document.getElementById(id_camp).style.bgcolor="#ff0000";
		alert ("Cantidad incorrecta");
		document.getElementById(id_camp).focus();		
	}
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function ComprovaCamp(text_alert_pass, camp)
{
	var error=true;
	switch (camp)
	{
		case 1:
			if (document.getElementById("adresa").value=="")
			{
				document.getElementById("adresa").style.background="#FDBDC2";
				document.getElementById("adresa").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("adresa").style.background="#EFF7F8";
				document.getElementById("adresa").style.color="#1A2225";
				error=false;
			}
			break;
		case 2:
			if (document.getElementById("poblacio").value=="")
			{
				document.getElementById("poblacio").style.background="#FDBDC2";
				document.getElementById("poblacio").style.color="#FF0000";				
				error=true;				
			}
			else
			{
				document.getElementById("poblacio").style.background="#EFF7F8";
				document.getElementById("poblacio").style.color="#1A2225";
				error=false;
			}
			break;
		case 3:
			if (!isZIPCode(document.getElementById("codipostal").value))
			{
				document.getElementById("codipostal").style.background="#FDBDC2";
				document.getElementById("codipostal").style.color="#FF0000";				
				error=true;			
			}
			else
			{
				document.getElementById("codipostal").style.background="#ffffff";
				document.getElementById("codipostal").style.color="#666D72";				
				error=false;		
			}
			break;
		case 4:
			if (document.getElementById("provincia").value=="")
			{
				document.getElementById("provincia").style.background="#FDBDC2";
				document.getElementById("provincia").style.color="#FF0000";
				error=true;				
			}
			else
			{
				document.getElementById("provincia").style.background="#EFF7F8";
				document.getElementById("provincia").style.color="#1A2225";
				error=false;
			}
			break;
		case 5:
			if (!isPhoneNumber(document.getElementById("telefon").value))
			{
				document.getElementById("telefon").style.background="#FDBDC2";
				document.getElementById("telefon").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("telefon").style.background="#ffffff";
				document.getElementById("telefon").style.color="#666D72";
				error=false;
			}
			break;	
		case 6:
			if (!isPhoneNumber(document.getElementById("movil").value))
			{
				document.getElementById("movil").style.background="#FDBDC2";
				document.getElementById("movil").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("movil").style.background="#ffffff";
				document.getElementById("movil").style.color="#666D72";
				error=false;
			}
			break;					
		case 7:
			document.getElementById("camp_mail").value=stripWhitespace(document.getElementById("camp_mail").value);
			if (!isEmail(document.getElementById("camp_mail").value))
			{
				document.getElementById("camp_mail").style.background="#FDBDC2";
				document.getElementById("camp_mail").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("camp_mail").style.background="#EFF7F8";
				document.getElementById("camp_mail").style.color="#1A2225";
				error=false;
			}
			break;
		case 8:
			if (isWhitespace(document.getElementById("nom").value) || document.getElementById("nom").value=="" )
			{
				document.getElementById("nom").style.background="#FDBDC2";
				document.getElementById("nom").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("nom").style.background="#EFF7F8";
				document.getElementById("nom").style.color="#1A2225";
				error=false;
			}
			break;	
		case 9:
			if (isWhitespace(document.getElementById("cognom1").value) || document.getElementById("cognom1").value=="" )
			{
				document.getElementById("cognom1").style.background="#FDBDC2";
				document.getElementById("cognom1").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("cognom1").style.background="#EFF7F8";
				document.getElementById("cognom1").style.color="#1A2225";
				error=false;
			}
			break;	
		case 10:
			if (isWhitespace(document.getElementById("cognom2").value) || document.getElementById("cognom2").value=="" )
			{
				document.getElementById("cognom2").style.background="#FDBDC2";
				document.getElementById("cognom2").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("cognom2").style.background="#EFF7F8";
				document.getElementById("cognom2").style.color="#1A2225";
				error=false;
			}
			break;				
		case 11:
			if (isWhitespace(document.getElementById("contrasenya1").value) || document.getElementById("contrasenya1").value=="" || !isAlphanumeric(document.getElementById("contrasenya1").value) || document.getElementById("contrasenya1").value.length < 6 || document.getElementById("contrasenya1").value.length > 12 )
			{
				document.getElementById("contrasenya1").style.background="#FDBDC2";
				document.getElementById("contrasenya1").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("contrasenya1").style.background="#EFF7F8";
				document.getElementById("contrasenya1").style.color="#1A2225";
				error=false;
			}
			break;				
		case 12:
if (isWhitespace(document.getElementById("contrasenya2").value) || document.getElementById("contrasenya2").value=="" || !isAlphanumeric(document.getElementById("contrasenya2").value) )
			{
				document.getElementById("contrasenya2").style.background="#FDBDC2";
				document.getElementById("contrasenya2").style.color="#FF0000";
				error=true;
			}
			else
			{
				if (document.getElementById("contrasenya2").value==document.getElementById("contrasenya1").value)
				{
					document.getElementById("contrasenya2").style.background="#EFF7F8";
					document.getElementById("contrasenya2").style.color="#1A2225";
					error=false;
				}
				else
				{
					document.getElementById("contrasenya1").style.background="#FDBDC2";
					document.getElementById("contrasenya1").value="";
					document.getElementById("contrasenya2").style.background="#FDBDC2";
					document.getElementById("contrasenya2").value="";
					alert (text_alert_pass);
				}
			}
			break;							
	}
	return error;
}

function AlertForm ()
{
	var error=false;
	
		for (j=1; j<8; j++)
			if (ComprovaCamp(0, j)) {error=true;}
	
	if (error) alert ("ERROR AL FORMULARI");
	else
	{
		document.confirm_final.action='confirmcistellasubmit.php';
		document.confirm_final.submit();
	}
}

function ComprovaFormAdresa ()
{
	var error=false;
	
		for (j=1; j<8; j++)
			if (ComprovaCamp(0, j)) {error=true;}
	
	if (error) return false ;
	else return true;
}

function ComprovaTotsCamps ()
{
	var error=false;
	
		for (j=1; j<13; j++)
			if (ComprovaCamp(0, j)) {error=true;}
	
	if (error) return false ;
	else return true;
}

function TractaCapa (id_noti, icona)
{
	var id_capa="noti_" + id_noti;
	var noticia=document.getElementById(id_capa).style;
	var titol=document.getElementById("titol_noti_" + id_noti).style;
	if (noticia.display=="none")
	{ 
		noticia.display='block';
		icona.src="images/icona_menys.gif";
		titol.color='#B2D63B';
	}
	else
	{ 
		noticia.display='none';
		icona.src="images/icona_mes.gif";
		titol.color='#7490AB';
	}
}

function TractaCapaMenu (index, objecte, idc_submenu)
{
	//alert (index);
	if (index!='opcio_0') {
		var capa = document.getElementById(index).style;
		
		if (capa.display=='none')
		{
			capa.display='block';
			objecte.style.background='url(images/marker_2.gif) no-repeat left top';
			objecte.style.color='#B2D63B';
			objecte.style.fontWeight='bold';
		}
		else
		{
			capa.display='none';
			objecte.style.background='url(images/marker.gif) no-repeat left top';
			objecte.style.color='#7490AB';
			objecte.style.fontWeight='normal';		
	
		}
	  objecte.style.backgroundPosition="5px 6px";
	}
	if (idc_submenu!=0)
	{
		var capa_subcat = document.getElementById('cat_' + idc_submenu);
		capa_subcat.style.color='#B2D63B';
	}
}

function logout(){
	//document.getElementById("logout").style.visibility="visible";
	//var content=document.getElementById('logout');
	var callback = {
            success : function(o) {
               // content.innerHTML = o.responseText;
               // content.style.visibility = "visible";  
                document.location.reload();            
            },
            failure : function(o) {
               // content.innerHTML = o.responseText;
               // content.style.visibility = "visible";
               // content.innerHTML = "Error de conexión";
			   alert ("ERROR DE CONNEXION");
            }
        }
        var conn = YAHOO.util.Connect.asyncRequest("POST", "logout.php", callback);	
}