function CanviaEstil (objecte, operacio)
{
  if (operacio=="on"){
    objecte.style.background='#333';
    objecte.style.color='#fff';
    objecte.style.cursor='pointer';
  }
  if (operacio=="off") {
    objecte.style.background='#ccc';
    objecte.style.color='#000';
  }
}

function afegir_cistella (text_alert)
{
  MostraAlert(text_alert);
}

function carrito_accio(accio,id_prod,prod_nom,prod_qty, preu_prod, text_missatge) {
      
	var callback_carrito = {
		success : function(o) {
		  //alert (o.responseText);
			var a_resposta =o.responseText.split('##');
			//var s_resposta =o.responseText;
			TancaMostraLoading();
			if (a_resposta[0]=="OK")
			{
				if (accio=="eliminar") location.reload();
				if (accio=="actualitzar")
				{
					MostraAlert(a_resposta[1]);
					window.location.reload();
				}
			}
			else
			{
				document.getElementById('capa_cistella').innerHTML=o.responseText;
				MostraAlert(text_missatge);
			}
		   // 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) {
			alert ("Error de conexión");
		}
	}

	MostraCapaLoading();
	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", callback_carrito, postData); 
}

function MostraCapaLoading()
{
  YAHOO.namespace("capa.loading");
  if (!YAHOO.capa.loading.wait) {

    // Initialize the temporary Panel to display while waiting for external content to load
    YAHOO.capa.loading.wait = 
            new YAHOO.widget.Panel("wait",  
                                            { width: "100px", 
                                              fixedcenter: true, 
                                              close: false, 
                                              draggable: false, 
                                              zindex:4,
                                              modal: true,
                                              visible: false
                                            } 
                                        );

    //YAHOO.capa.loading.wait.setHeader("{titol_carregantse}");
    //YAHOO.capa.loading.wait.setBody("<img src=\"imatges/loading.gif\" align=\"absmiddle\"/> {text_carregantse}");
    YAHOO.capa.loading.wait.render(document.body);
  }
  document.getElementById("wait").style.display="block";
  YAHOO.capa.loading.wait.show();
}

function TancaMostraLoading()
{
  YAHOO.capa.loading.wait.hide();  
}
  
function MostraAlert (missatge)
{
  YAHOO.namespace("capa.loading");
  if (!YAHOO.capa.loading.alert) {

    // Initialize the temporary Panel to display while waiting for external content to load
    YAHOO.capa.loading.alert = 
            new YAHOO.widget.Panel("alert",  
                                            { width: "275px",
                                              height: "60px",
                                              fixedcenter: true, 
                                              close: false, 
                                              draggable: false, 
                                              zindex:4,
                                              modal: true,
                                              visible: false
                                            } 
                                        );

    YAHOO.capa.loading.alert.render(document.body);
  }
  document.getElementById("alert").style.display="block";
  document.getElementById("alert_text").innerHTML=missatge;
  YAHOO.capa.loading.alert.show();
 
}

function TancaAlert()
{
  YAHOO.capa.loading.alert.hide();

}

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;
	
	//var tecla=String.fromCharCode(event.keycode); 
	//if (parseInt(tecla)>0 && parseInt(tecla)<1000 )
	//{
	
	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]; //decimals=a_nou_preu[1].charAt(0).a_nou_preu[1].charAt(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=="" || isWhitespace(document.getElementById("adresa").value))
			{
				document.getElementById("adresa").style.background="#FDBDC2";
				document.getElementById("adresa").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("adresa").style.background="#FFFFFF";
				document.getElementById("adresa").style.color="#333638";
				error=false;
			}
			break;
		case 2:
			if (document.getElementById("poblacio").value=="" || isWhitespace(document.getElementById("poblacio").value))
			{
				document.getElementById("poblacio").style.background="#FDBDC2";
				document.getElementById("poblacio").style.color="#FF0000";				
				error=true;				
			}
			else
			{
				document.getElementById("poblacio").style.background="#FFFFFF";
				document.getElementById("poblacio").style.color="#333638";
				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=="" || isWhitespace(document.getElementById("provincia").value))
			{
				document.getElementById("provincia").style.background="#FDBDC2";
				document.getElementById("provincia").style.color="#FF0000";
				error=true;				
			}
			else
			{
				document.getElementById("provincia").style.background="#FFFFFF";
				document.getElementById("provincia").style.color="#333638";
				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:
			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="#FFFFFF";
				document.getElementById("camp_mail").style.color="#333638";
				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="#FFFFFF";
				document.getElementById("nom").style.color="#333638";
				error=false;
			}
			break;	
		case 9:
			if (isWhitespace(document.getElementById("cognom1").value) )
			{
				document.getElementById("cognom1").style.background="#FDBDC2";
				document.getElementById("cognom1").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("cognom1").style.background="#FFFFFF";
				document.getElementById("cognom1").style.color="#333638";
				error=false;
			}
			break;	
		case 10:
			if (isWhitespace(document.getElementById("cognom2").value) )
			{
				document.getElementById("cognom2").style.background="#FDBDC2";
				document.getElementById("cognom2").style.color="#FF0000";
				error=true;
			}
			else
			{
				document.getElementById("cognom2").style.background="#FFFFFF";
				document.getElementById("cognom2").style.color="#333638";
				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="#FFFFFF";
				document.getElementById("contrasenya1").style.color="#333638";
				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="#FFFFFF";
					document.getElementById("contrasenya2").style.color="#333638";
					error=false;
				}
				else
				{
					document.getElementById("contrasenya1").style.background="#FDBDC2";
					document.getElementById("contrasenya1").value="";
					document.getElementById("contrasenya2").style.background="#FDBDC2";
					document.getElementById("contrasenya2").value="";
					MostraAlert (text_alert_pass);
				}
			}
			break;							
	}
	return error;
}

function AlertForm (text_alert, activat)
{
	var error=false;
	var limit=11;
	
	if (activat=='true') limit=6

	
	for (j=1; j<limit; j++)
	{
		if (j!=9 && j!=10) {if (ComprovaCamp(0, j)) {error=true;}}
	}
	
	if (error) MostraAlert (text_alert);
	else
	{
		document.confirm_final.action='confirmcistellasubmit.php';
		document.confirm_final.submit();
	}
}

function ComprovaFormAdresa ()
{
	var error=false;
	
		for (j=1; j<6; j++)
			if (ComprovaCamp(0, j)) {error=true;}
	
	if (error) return false ;
	else return true;
}

function ComprovaCampCerca (text_alert)
{
    if (document.getElementById('buscador').buscaprod.value=='')
    {
        document.getElementById('buscador').buscaprod.style.background="#FDBDC2";
        alert (text_alert);
        document.getElementById('buscador').buscaprod.focus();
        return false;
    }
    else document.getElementById('buscador').submit();    
}

function ComprovaTotsCamps ()
{
	var error=false;
	
		//for (j=1; j<13; j++)
	if (ComprovaCamp(0, 9)) {error=true;}
	if (ComprovaCamp(0, 10)) {error=true;}
	if (ComprovaCamp(0, 11)) {error=true;}
	if (ComprovaCamp(0, 12)) {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' && document.getElementById(index)) {
		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='#1860a9';
			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 && document.getElementById('cat_' + idc_submenu))
	{
		var capa_subcat = document.getElementById('cat_' + idc_submenu);
		capa_subcat.style.color='#1860a9';
	}
}

function ObreFinestraImpr(id)
{
  var url="catalegdetall_imrpimible.php?idp="+id;
  window.open(url,"intro","resizable=no,scrollbars=yes,menubar=no,width=700,height=545,top=50,left=125,location=no");
}    

function Imprimeix(text) {
  if (window.print)
    window.print();
  else
    alert(text);
}

function addBookmark(title,url) { 
	if (window.sidebar) { 
	 window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) { 
	 window.external.AddFavorite( url, title); 
	} else if( window.opera && window.print ) { 
	 return true; 
	} 
} 

//Per eliminar # dels enllasos que no porten enlloc
//function null(){} 
