// JavaScript Document
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// MARIO 
var anterior = 0;
var anterior_sender = 0;
function ViewProducts(id, seccio, sender){

	var e = document.getElementById('productes_'+id);
	
	if(!anterior)
	{
		
		//encara no esta asignat
		if(e.style.display != "block" && e.style.display != "")	e.style.display = "block";
		else e.style.display = "none";
		
		sender.style.backgroundImage = "url(img/"+seccio+"_ul_li_a_hover_bkg.png)";
		sender.style.color = "#fff";
	}
	else
	{
		if(e.style.display != "block") //is hidden
		{
			anterior.style.display = "none";
			e.style.display = "block";	
			
			anterior_sender.style.backgroundImage = "";
			anterior_sender.style.color = "#000";
			sender.style.backgroundImage = "url(img/"+seccio+"_ul_li_a_hover_bkg.png)";
			sender.style.color = "#fff";
			
		}
		else
		{
			anterior_sender.style.color = "#000";
			anterior_sender.style.backgroundImage = "";
			
			anterior.style.display = "none";
		 	e.style.display = "none";		 	
		}
	}
	anterior = e;
	anterior_sender = sender;	
}





/*IMATGES*/
function canviaFoto(src){

	var w = document.getElementById('foto_gran').width;
	//var h = document.getElementById('foto_gran').height;

	document.getElementById('foto_gran_link').href=src;
	document.getElementById('foto_gran').src = "lib/thumb.php?path=../"+src+"&pwidth="+w;
}





/* @autor: Mario Martinez
 * FUNCIONS PER VERIFICAR UN FORMULARI --------------------------------------------------------------*/
function CampText(camp){
	var valid = false;
	
	var co = document.getElementById(camp);
	if (co.value.length > 0) valid = true;
	
	CambiaColor(camp,valid);
	
	return valid;
}

function CampEmail(camp){
	
	valid = VerificarEmail(camp);
	CambiaColor(camp,valid);
	
	return valid;
	
}


function VerificarEmail(e) {
   var s = document.getElementById(e).value;
   var filter=/^[A-Za-z][A-Za-z0-9\\.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
   if (s=="") return false;
   if (s.length == 0 ) return false;
   if (filter.test(s)) return true;
   else return false;
}


function CampNumeric(camp,digits){
	
	var num = document.getElementById(camp).value;
	var valid = false;
	
	if(digits == 0){
		if(num.length > 1) valid = soloNumerico(num);
	}
	else if(num.length == digits) valid = soloNumerico(num);
	
	CambiaColor(camp,valid);
	
	return valid;
}

function soloNumerico(valor) {
	return !isNaN(valor);
}

function ValidarFormulari(camps){
	
	//FORMAT ('nom_input=tipus;nom_input2=tipus;...nom_inputX=numeric#NumMax')
	//array de camps
	var tcamps = new Array();
	var camp;
	var aux;
	var numleng = 0;
	var i_ok = 0; //conta el camps correctes. Si son = al total de camps, tot es correcte
	
	tcamps = camps.split(";");
	
	for(i=0; i < tcamps.length; i++)
	{
		numleng = 0;
		aux = tcamps[i].split("=");
		camp = aux[0];
		metode = aux[1];
		
		if(metode.match("#"))
		{
			
			aux = metode.split("#");
			numleng = aux[1];
			metode = aux[0];
		}
		
		if(metode == "text") {
			if(CampText(camp)) i_ok++; 
		}
		else if(metode == "email") {
			if(CampEmail(camp)) i_ok++;
		}
		else if (metode == "numeric") {
			if(CampNumeric(camp, numleng)) i_ok++;
		}
		
	}
	
	if(i_ok == tcamps.length) document.getElementById('form_contacte').submit();
	//else alert("Hi ha algun camp incorrecte!");
	
	
	//return retorn;
}

function CambiaColor(camp,valid){
	
	var e = document.getElementById(camp);
	//var ico = document.getElementById(camp+"_ok");
	if(!valid) 
	{
		e.style.border = "1px solid #e51d13";
		//e.style.backgroundColor = "#ff7777";
		//ico.style.visibility = "visible";
	
	}
	else 
	{
		//e.style.backgroundColor = "#fff";
		e.style.border = "1px solid #4d4638";
		//ico.style.visibility = "hidden";
	}
}


function recomanar(){
	document.getElementById('recomana').style.display = "block";
}

