/* Valida una direcció d'email */
function isEmailAddress(theElement) {
   var s = theElement;
   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 CampText(camp){
	var valid = false;
	
	el = document.getElementById(camp);
	if (el.value!='') valid = true;
	
	CambiaColor(el,valid);
	
	return valid;
}

function CampEmail(camp){
	
	el = document.getElementById(camp);
	valid = isEmailAddress(el.value);
	
	CambiaColor(el,valid);
	
	return valid;
	
}

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

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("#"))
		{//xapuza
			
			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) return true;
	else{
	//	alert("Hi ha algun camp incorrecte!");
		return false;
	}
	
}


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

function CambiaColor(obj,valid){
	if(!valid) obj.style.border = "1px solid #e51d13";
	else obj.style.border = "1px solid #000000";
}



function showEvents(dia, mes, any, idioma){
    	
    	var text = "";
    	if(idioma == 'ca') text = "Carregant dades, esperi siusplau";
    	else if(idioma == 'es') text ="Carregando datos, espere porfavor";
    	else text = "Loading...";
    	
    	$(".box2-2").ajaxStart(function(){
   				$(".box2-2").html("<p class='loading'><img src='js/ajax-loader.gif' alt='"+text+"' /></p><p class='loading'>"+text+"</p>")
 			});
    	
    	$.ajax({
                type: "GET",
                async: true,
                url: "llista_concerts.php",
				data: "getEvent=1&d="+dia+"&m="+mes+"&y="+any+"&lang="+idioma,
				success: function(msg){
					$(".box2-2").html(msg)
					//alert(msg);
           }
      	});	
	}