// JavaScript Document

function contactar(quien, defecto) {
	var reg 	= /^[ ,\.\-\$_:;¿\?\+\*\(\)\[\]\{\}¡!\/]*$/;
	var regm	= /^([a-zA-Z0-9_\.]+)@([a-z0-9_]+)\.([a-z0-9]+)(\.[a-z0-9]+)?$/;
	var regtel	= /^[\d ]{6,11}$/;
	
	var resul	= true;
	
	var nom 	= document.getElementById('nom').value.replace(reg, "");
	var cog1	= document.getElementById('cognom1').value.replace(reg, "");
	var cog2	= document.getElementById('cognom2').value.replace(reg, "");
	var adr		= document.getElementById('adresa').value.replace(reg, "");
	var loc		= document.getElementById('localitat').value.replace(reg, "");
	var tel		= document.getElementById('tel').value;
	var con		= document.getElementById('consulta').value.replace(reg, "");
	var img		= document.getElementById('chrImagen').value.replace(reg, "");
	
	var lblNom	= document.getElementById("lblNom").innerHTML;
	var lblApe1	= document.getElementById("lblApe1").innerHTML;
	var lblApe2	= document.getElementById("lblApe2").innerHTML;
	var lblDir	= document.getElementById("lblDir").innerHTML;
	var lblLoc	= document.getElementById("lblLoc").innerHTML;
	var lblTel	= document.getElementById("lblTel").innerHTML;
	var lblMail	= document.getElementById("lblMail").innerHTML;
	var lblCons	= document.getElementById("lblCons").innerHTML;
	var lblImg	= document.getElementById("lblImage").innerHTML;
	
	var aux = "* " + nom;
	if (nom != '' && nom != lblNom && aux != lblNom) {
		aux = "* " + cog1;
	
		if (cog1 != '' && cog1 != lblApe1 && aux != lblApe1) {
			aux = "* " + cog2
			
			if (cog2 == lblApe2 || aux == lblApe2)
				document.getElementById("cognom2").value = '';
			
			aux = "* " + adr;
			if (adr != '' && adr != lblDir && aux != lblDir) {
				aux = "* " + loc
				
				if (loc != '' && loc != lblLoc && aux != lblLoc) {
					aux = "* " + tel;
					
					if (tel == lblTel || aux == lblTel)
						document.getElementById("tel").value = '';
					else {
						if (tel != '') {
							if (!regtel.test(tel)) {
								resul = false;
							}
						}
					}
					
					if (regm.test(document.getElementById("email").value) && resul == true) {
						aux = "* " + con;
						
						if (con != '' && con != lblCons && aux != lblCons) {
							if (nom.length >= 2 && cog1.length >= 2 && adr.length >= 3 && loc.length >= 2 && con.length >= 10) {
								resul = true;
							}
						}
					}
					else
						resul = false;
				}
				else {
					resul = false;
				}
			}
			else
				resul = false;
		}	
		else
			resul = false;
	}
	else
		resul = false;
	
	if (resul == true)
		if (img == '' || img == lblImg)
			resul = false;
	
	if (resul == true) {
		document.forms[0].action = 'morella/ayuntamiento/contactar/' + quien;
		document.getElementById("consultar").value = 'si';
		document.forms[0].submit();
	}
	else
	{
		document.getElementById("error").style.color = "#f00";
		document.getElementById("error").innerHTML = document.getElementById("errorMsg").value;
		document.getElementById("error").style.display = 'block';
	}
	return false;
}

function penjalateua(id) {
	window.open("http://www.morella.net/morella/participa/fotos_ampliadas/" + id, "PENJA_LA_TEUA", "alwaysRaised=yes,menubar=yes,resizable=no,scrollbars=no,status=no,toolbar=no,width=550,height=560");
}

function galerias(tipo, id) {
	window.open("http://www.morella.net/morella/participa/galerias_ampliadas/" + tipo + "/" + id, "GALERIES_DE_FOTOS", "alwaysRaised=yes,menubar=yes,resizable=no,scrollbars=no,status=no,toolbar=no,width=550,height=560");
}

function busquedaAgenda() {
	if (document.getElementById("fIni").value == "")
		document.getElementById("resetearFIni").value = '1';
	
	if (document.getElementById("fFin").value == "")
		document.getElementById("resetearFFin").value = '1';
	
	x	= document.getElementById("tipos");
	prioritari	= x.options[x.selectedIndex].value;
	
	if (prioritari == -1)
		document.getElementById("resetearTipo").value = '1';
	
	if (document.getElementById("txtAgenda").value == "")
		document.getElementById("resetearTxt").value = '1';
	
	document.forms[0].action='morella/participa/agenda_de_morella/';
	document.getElementById("pagina").value = 1;
	document.getElementById("resetearPagina").value = '1';
	document.forms[0].submit();
	return false
}

function busquedaAgendaInicio() {
	ini = document.getElementById('fIni').value;
	fin	= document.getElementById('fFin').value;
	
	if ((ini != '' || fin != '') && (ini != 'dd/mm/aaaa' || fin != 'dd/mm/aaaa')) {
		document.forms[0].action='morella/participa/agenda_de_morella/';
		document.forms[0].submit();
	}
	return false;
}

function votar() {
	var seleccionado = 0;
	
	for (i=0;i<document.forms[0].encuesta_morella.length;i++) {
		if(document.forms[0].encuesta_morella[i].checked)
			seleccionado = 1;
	}
	
	if (seleccionado == 1) {
		document.forms[0].action = "morella/participa/encuestas_resultados";
		document.forms[0].submit();
	}
	return false;
}



/***********************************************
* Fading Scroller- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
var maxsteps=30; // number of steps to take to change from start color to endcolor
var startcolor= new Array(255,255,255); // start color (red, green, blue)
var endcolor=new Array(255,0,0); // end color (red, green, blue)
var delay = 7000; //set delay between message change (in miliseconds)

begintag=''; //set opening tag, such as font declarations
closetag='';

var index=0;
var fadecounter;
var delaycounter;


//function to change content
function mostrarUltimaHora() {
	if (fcontent.length > 0) {
		if (index >= fcontent.length) {
			index=0;
		}
	  
		document.getElementById("avisos").style.color="rgb("+startcolor[0]+", "+startcolor[1]+", "+startcolor[2]+")";
		document.getElementById("avisos").innerHTML=begintag+fcontent[index]+closetag;

		if (fcontent.length > 1) {
			document.getElementById("enlacesAvisos").innerHTML = index+1 + "-" + fcontent.length;
			document.getElementById("avisosCantidad").style.display = "block";
		}
		else {
			document.getElementById("avisosCantidad").style.display = "none";
		}

		colorfade(1);
		index++;
	}
}

function colorfade(step) {
	var stepdelay=40; // time in miliseconds of a single step
  //alert(step + "--" + maxsteps);
	if(step<=maxsteps) {	
		document.getElementById("avisos").style.color=getstepcolor(step);
		
		step++;
		fadecounter=setTimeout("colorfade("+step+")",stepdelay);
	}else{
		clearTimeout(fadecounter);
		document.getElementById("avisos").style.color="rgb("+endcolor[0]+", "+endcolor[1]+", "+endcolor[2]+")";
		delaycounter = setTimeout("mostrarUltimaHora()", delay);
	}   
}

function getstepcolor(step) {
	var diff
	var newcolor=new Array(3);
  
	for(var i=0;i<3;i++) {
		diff = (startcolor[i]-endcolor[i]);
		
		if(diff > 0) {
			newcolor[i] = startcolor[i]-(Math.round((diff/maxsteps))*step);
		} else {
			newcolor[i] = startcolor[i]+(Math.round((Math.abs(diff)/maxsteps))*step);
		}
	}
	
	return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
}

function incrementar() {
	if (index > fcontent.length)
		index++;
	clearTimeout(delaycounter);
	clearTimeout(fadecounter);
	mostrarUltimaHora();
}

function decrementar() {
	if (index == 1)
		index--;
	else if (index > 0)
		index = index - 2;
	
	clearTimeout(delaycounter);
	clearTimeout(fadecounter);
	mostrarUltimaHora();
}
