/*ENQUESTA -----------------------------------------------------------------------------------------------------*/

var vot = "";
function RefreshVot(votacio){
	vot = votacio.value;
}

function Votar(id_enquesta,content){
	
	var user_code = document.getElementById("user_code_"+id_enquesta).value;
	
	if(vot == "") alert("Has de triar una resposta!");
	else if(user_code == "")alert("Has de copiar el codi de validació!");
	else llamarasincrono("votacio.php?id="+id_enquesta+"&resposta="+vot+"&user_code_"+id_enquesta+"="+user_code,content);
}

function displayCaptcha(e){

	document.getElementById(e).style.display = 'block';
}
/*--------------------------------------------------------------------------------------------------------------*/


/*FOTOS ------------------------------------------------------------------------------------------------------- */
function ampliar (src){
   return popImage(src);
} 

/*IMATGES*/
w=284;
h=250;
function canviaFoto(src){
	document.getElementById('foto_gran_link').href=src;
	document.getElementById('foto_gran').src = "lib/thumb.php?path=../"+src+"&width="+w+"&height="+h+"&resize=1";
}

/*-----------------------------------------------------------------------------------------------------------------*/

/* @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("#"))
		{//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;
	}
	
	
	//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";
		//ico.style.visibility = "visible";
	
	}
	else 
	{
		e.style.border = "1px solid #5e7885";
		//ico.style.visibility = "hidden";
	}
}

/**
 * Calendari de programes 
*/
var cont = null;
function showEvent(titol_programa,titol, contingut, e, left)
{
		
	//inicialitzem el contenidor
	cont = document.getElementById('window_event');
	contingut = document.getElementById(contingut).value;
	//omplim el div
	cont.innerHTML = "<p>"+titol_programa+"</p><div><h3>"+titol+"</h3><hr /><br /><p>"+contingut+"</p></div>";
	
	//calculem la posicio del div per les dimensions de la finestra!
	var position = positionMouse(e);

	//apliquem els estils per el div!
	
	if(navigator.appName == "Microsoft Internet Explorer") cont.style.width="250px";
	
	cont.style.display 	= 'block';
	
	if(left == 1){
		width = cont.style.width.split('px');
		cont.style.left = ( position.x - (width[0]))+"px";
	}
	else cont.style.left = (position.x)+"px";
	
	cont.style.top		= (position.y+12)+"px";
	
	left = 0;
}

function hiddenEvent(){
	cont.style.display 	= 'none';
}

function positionMouse(event){
	return {
	     		x:(window.event)?(window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft):(event.clientX + (window.scrollX || document.body.scrollLeft || 0)),
                y:(window.event)?(window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop):(event.clientY + (window.scrollY || document.body.scrollTop || 0))
           };
}







