
img1 = new Image();
img1.src = "/img/p_ac_e.png";

img2 = new Image();
img2.src = "/img/p_ac_d.png";

img3 = new Image();
img3.src = "/img/p_ac_m.png";

imga1 = new Image();
imga1.src = "/img/p_noac_e.png";

imga2 = new Image();
imga2.src = "/img/p_noac_d.png";

imga3 = new Image();
imga3.src = "/img/p_noac_m.png";



/*menu desplegable artesenal*/

var menu = 0;
var num_menu = 0;
var sobre_menu = 0;

function fer_login(){
	f=document.login;
	if(f.usuari.value.length<3)alert("Introdueix el teu nom d'usuari");
	else if(f.contrasenya.value.length<3)alert("Introdueix la teva contrasenya");
	else {
		f.submit();
	}
}

function fer_login2(){
	f=document.login2;
	if(f.usuari.value.length<3)alert("Introdueix el teu nom d'usuari");
	else if(f.contrasenya.value.length<3)alert("Introdueix la teva contrasenya");
	else {
		f.submit();
	}
}

function mhor_over(num,color){
	desactiva_tots_els_menus();
	
	document.getElementById("aE"+num).src="/img/p_ac_e.png";
	document.getElementById("aD"+num).src="/img/p_ac_d.png";
	document.getElementById("aC"+num).style.background = "url(/img/p_ac_m.png)";
	document.getElementById("aC"+num).className = "menu_horitzontal_menu "+color;
	//if(num<5)
		ShowContent("menu_ocult_"+num);
	//else
	//if(num=="5"||num=="6") document.getElementById("lC"+num).className = "menu_horitzontal_menu "+color;
	
}

function desactiva_tots_els_menus(){
	var t=6;
	var i=1;
	for(i=1;i<=t;i++){
		mhor_out(i);
	}


}

function mostrar_grups(){
	ShowContent("linia2_llarga");
	HideContent("linia2_curta");
}

function amagar_grups(){
	ShowContent("linia2_curta");
	HideContent("linia2_llarga");
}


function mhor_out(num){
	document.getElementById("aE"+num).src="/img/p_noac_e.png";
	document.getElementById("aD"+num).src="/img/p_noac_d.png";
	document.getElementById("aC"+num).style.background = "url(/img/p_noac_m.png)";
	document.getElementById("aC"+num).className = "menu_horitzontal_menu";
	//if(num<5)
		HideContent("menu_ocult_"+num);
	//else
	//if(num=="5"||num=="6") document.getElementById("lC"+num).className = "menu_horitzontal_menu";
}


function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "block";
}

/* fi menu */





function agenda_over_a(num,concerts,agenda_dia,agenda_mes,data){
	var cela = 'a'+num;
	var link = 'al'+num;
	document.getElementById(cela).className='agenda_over';
	document.getElementById(link).className='link_blanc';

	var text = 	"<table border='0' cellpadding='0' cellspacing='0' align='center'>"+
			" <tr>"+
			"  <td width='38' height='37' rowspan='3' valign='top'>"+
			"	<table border='0' cellspacing='0' cellpadding='0' background='/img/fons_agenda_lateral.gif' width='36' height='37'>"+
			"	 <tr><td align='center' height='13' class='text_blanc' valign='top'>"+agenda_mes+"</td></tr>"+
			"	 <tr><td align='center' height='23' class='text_negre' valign='top'><span style='font-size:1.8em'><b>"+agenda_dia+"</b></span></td></tr>"+
			"	</table>"+
			"  </td>"+
			" <td class='text_blanc' height='18' style='padding-left:5px'><b>"+data+"</b></td></tr>"+
			" </tr>"+
			" <tr><td height='1'><img src='/img/trans.gif'></td></tr>"+
			" <tr><td class='text_blanc' height='18' style='padding-left:5px'>"+concerts+" concerts</td></tr>"+
			"</table>";

	document.getElementById('num_concerts').innerHTML=text;
}

function agenda_out_a(num){
	var cela = 'a'+num;
	var link = 'al'+num;
	document.getElementById(cela).className='mes_actual';
	document.getElementById(link).className='link_negre';
	document.getElementById('num_concerts').innerHTML="";
}

function canviar_menu(num,color,opcio){

	if(document.getElementById('menu'+num).style.display == 'none'){
		document.getElementById('menu'+num).style.display = 'block';
		document.getElementById('fotomenu'+num).src="/img/menys_"+color+".png";
		guarda_opcio(opcio,'1');
	}	
	else{
		document.getElementById('menu'+num).style.display = 'none';
		document.getElementById('fotomenu'+num).src="/img/mes_"+color+".png";
		guarda_opcio(opcio,'0');
	}
}

var ie4=document.all
var ns6=document.getElementById&&!document.all
var ns4=document.layers


function contains_ns6(a, b)
{
	if (b == null)
		return false;
	//Determines if 1 element in contained in another- by Brainjar.com
	while (b.parentNode)
	if ((b = b.parentNode) == a)
		return true;
	return false;
}
function dynamichide(e)
{
	if (ie4&&!menuobj.contains(e.toElement))
		hidemenu()
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		hidemenu()
}

function hidemenu()
{
	if (window.menuobj)
		menuobj.thestyle.visibility=(ie4||ns6)? "hidden" : "hide"
}


function clearhidemenu()
{
	//if (window.delayhide)
		//clearTimeout(delayhide)
}

function showmenu(e,code,downstate)
{
	if (!document.all&&!document.getElementById&&!document.layers)
		return;

	if(downstate == "complete" || downstate == "completing")
		return;

	//clearhidemenu()

	menuobj=ie4? document.all.popmenu : ns6? document.getElementById("popmenu") : ns4? document.popmenu : ""
	menuobj.thestyle=(ie4||ns6)? menuobj.style : menuobj

	if (ie4||ns6)
		menuobj.innerHTML=code
	else
	{
		//menuobj.document.write('<layer name=gui bgColor=#E6E6E6 onmouseover="clearhidemenu()" onmouseout="hidemenu()">' + code + '</layer>')
		menuobj.document.write('<layer name=gui bgColor=#E6E6E6>' + code + '</layer>')
		menuobj.document.close()
	}

	menuobj.contentwidth=(ie4||ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width
	menuobj.contentheight=(ie4||ns6)? menuobj.offsetHeight : menuobj.document.gui.document.height
	eventX=ie4? event.clientX+5 : ns6? e.clientX+5 : e.x+5
	eventY=ie4? event.clientY : ns6? e.clientY : e.y

	var rightedge=ie4? document.body.clientWidth-eventX : window.innerWidth-eventX
	var bottomedge=ie4? document.body.clientHeight-eventY : window.innerHeight-eventY

	if (rightedge<menuobj.contentwidth)
		menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX-menuobj.contentwidth : ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth
	else
		menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX

	if (bottomedge>=menuobj.contentheight)
		menuobj.thestyle.top=eventY + (ie4? document.body.scrollTop : ns6? window.pageYOffset : 0)
	else if (eventY>menuobj.contentheight)
		menuobj.thestyle.top=eventY + (ie4? document.body.scrollTop-menuobj.contentheight : ns6? window.pageYOffset-menuobj.contentheight : -menuobj.contentheight)
	else
		menuobj.thestyle.top=ie4? document.body.scrollTop : ns6? window.pageYOffset : 0

	menuobj.thestyle.visibility="visible"

	return false
}

function buscador_usuaris(event,camp)
{
	codi = 	"<table border='0' cellpadding='5' cellspacing='0' width='100%' height='100%' bgcolor='#1f1f1f'><tr><td align='right' colspan='3'><a href='javascript:hidemenu();'><img src='/img/creu_tancar.gif' border='0'></a></td></tr>"+
		"<tr><td width='3'><img src='/img/trans.gif'></td><td>"+
		"<table border='0' cellpadding='0' cellspacing='0'><tr><td height='25' valign='top'><span class='textg_fons_rosa'><b>&nbsp; Cercar usuaris a repopblica.com: &nbsp;</b></span><br></td></tr></table>"+		
		"<table border='0' cellpadding='0' cellspacing='0'><tr><td class='text_blanc' height='60' valign='top'>Des del següent formulari pots cercar usuaris registrats a repopblica.com introduint diferents paràmetres. Pots buscar usuaris pel nom d'usuari, per l'email o pel nom. Se't mostrarà una llista amb tots els resultats i la informació pública d'aquests.</td></tr></table>"+		
	 	"<input type='hidden' name='fnb' value='' id='fnb'> <input type='text' size='40' class='form_text' name='nb' id='nb'> <input type='button' value='Buscar' onclick='javascript:buscar_usuari();' class='form_boto'>"+
	 	"<div id='rnb'></div>"+
	 	"</td><td width='3'><img src='/img/trans.gif'></td></tr><tr><td colspan='3' height='4'><img src='/img/trans.gif'></td></tr></table>";
	showmenu(event,codi,'waiting');	
	document.getElementById('fnb').value=camp;

}

function escriu_usuari(c,u){
	document.getElementById(c).value=u;
}

function isValidEmail(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(address) == false) {
		return false;
	}
	return true;
}




function getfileextension(filename) {  
	if( filename.length == 0 ) return ""; 
	var dot = filename.lastIndexOf(".");  
	if( dot == -1 ) return "";  
	var extension = filename.substr(dot,filename.length);  
	return extension; 
}

function checkfileType(inputId,allowedExt) {
	var ext = getfileextension(inputId);  
	if( ext == allowedExt )   return true;  
	else   return false; 
} 


function submitenter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13){
	   myfield.form.submit();
	   return false;
	}
	else{
	   return true;
	}
}


function afegir_concert(){
	f=document.form_agenda;
	HideContent("af_festival");
	ShowContent("af_concert");
	if(f.nom_festival.value.length>0) f.grup_principal.value=f.nom_festival.value;
	if(f.grups.value.length>0) f.altres_grups.value=f.grups.value;
	f.nom_festival.value="";
	f.grups.value="";	
	document.getElementById('nom_data').innerHTML="Data del concert:";
}
function afegir_festival(){
	f=document.form_agenda;
	HideContent("af_concert");
	ShowContent("af_festival");
	if(f.grup_principal.value.length>0) f.nom_festival.value=f.grup_principal.value;
	if(f.altres_grups.value.length>0) f.grups.value=f.altres_grups.value;	
	f.grup_principal.value="";
	f.altres_grups.value="";		
	document.getElementById('nom_data').innerHTML="Data d'inici:";
	if(f.dia_fi.selectedIndex=="0") f.dia_fi.selectedIndex=f.dia.selectedIndex;
	if(f.mes_fi.selectedIndex=="0") f.mes_fi.selectedIndex=f.mes.selectedIndex;
	if(f.any_fi.selectedIndex=="0") f.any_fi.selectedIndex=f.any.selectedIndex;
}


var http_request = false;

function guarda_opcio(opcio,valor) {

	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	    http_request = new XMLHttpRequest();
	    if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	    }
	} else if (window.ActiveXObject) { // IE
	    try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
		try {
		    http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	    }
	}

	if (!http_request) {
	    return false;
	}
	var d = new Date() 
	http_request.open('GET','/repopblica.php?a=perfil_usuari&b=opcions&opcio='+opcio+'&valor='+valor+'&time='+d.getTime(), true);
	http_request.send(null);

}


function afegir_comentari(){
	f=document.form_afegir_comentari;
	if(f.comentari.value.length<5) alert("Omple el camp de comentaris correctament");
	else f.submit();
}

function borrar_comentari(seccio,id_comentari){
	if(confirm("Segur que vols borrar aquest comentari?")){
		location.href="/repopblica.php?a=comentaris&b=borrar&seccio="+seccio+"&id="+id_comentari;
	}
}

function buscar(){
	document.formconcert.submit();
}

function myGetCookie()
{
	var name="fac_sessio";
	if (document.cookie)
	{
		var cookies=document.cookie.split(";");
		for (var i=0; i<cookies.length; i++)
		{
			var varName=(cookies[i].split("=")[0]);
			var varValue=(cookies[i].split("=")[1]);

			while (varName.charAt(0)==" ")
				varName=varName.substr(1,varName.length);

				// the escape() function will url encode the value				
			if (varName==name)
				return escape(varValue);
		}
	}
	return " ";
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function cl_login(){
	f=document.login;
	if(f.usuari.value.length==0)f.usuari.className="login_correu_off";
}

function bl_login(){
	f=document.login;
	f.usuari.className="login_correu_on";
}

function cl_pass(){
	f=document.login;
	if(f.contrasenya.value.length==0)f.contrasenya.className="login_pass_off";
}

function bl_pass(){
	f=document.login;
	f.contrasenya.className="login_pass_on";
}


$(document).ready(function(){

	$(".artistes li").click(function(){
		if ($(this).find("a").length) {
			window.location=$(this).find("a").attr("href");}
		return false;
	});

	$(".concerts_destacats li").click(function(){
		if ($(this).find("a").length) {
			window.location=$(this).find("a").attr("href");}
		return false;
	});

	$(".actualitat li").click(function(){
		if ($(this).find("a").length) {
			window.location=$(this).find("a").attr("href");}
		return false;
	});


 
});



function canvi_altre_poblacio(e,opt){
	f=document.registre;
	if(opt=="1" && e.keyCode!="9") f.id_poblacio.selectedIndex=0;
	if(opt=="2" && e.keyCode!="9") f.altre_poblacio.value="";
	
}

function canvi_nom_poblacio_agenda(e,opt){
	f=document.form_agenda;
	if(opt=="1" && e.keyCode!="9") { 
		if(f.id_poblacio.value>0) makeRequest_agenda("999999999999999");
		f.id_poblacio.selectedIndex=0; 
	}
	if(opt=="2" && e.keyCode!="9") { f.nom_poblacio.value="";  }
}

function verificar_formulari_registre(){
	f=document.registre;

	var exp=/^([0-9A-Za-z\-_ç]+$)/;
	
	
	if(f.usuari.value.length<2) alert("Introdueix el nom d'usuari");
	else if (f.usuari.value.search(exp)==-1) alert("El nom d'usuari té caràcters no vàlids");
	else if (!isValidEmail(f.email1.value)) alert("El primer e-mail no és correcte");
	else if (f.email1.value != f.email2.value) alert("L'email introduit no coincideix amb la confirmació");
	else if (f.password1.value.length < 4) alert("Falta introduir la contrasenya. Minim 4 caracters.");
	else if (f.password1.value != f.password2.value) alert("Les contrasenyes no coincideixen");
	else if (f.password1.value.search(exp)==-1) alert("La contrasenya té caràcters no vàlids");
	else if (f.nom.value.length=="0") alert("Falta el nom");
	else if (f.d_dia.value.length=="0" || f.d_mes.value.length=="0" || f.d_any.value.length=="0") alert("Falta la data de naixement");
	else if (f.id_poblacio.value=="0" && f.altre_poblacio.value.length==0) alert("Selecciona la població o introdueix-la si no apareix a la llista");
	else if (f.repeteix_codi.value.length != 5) alert("Introdueix el codi visual");
	else if (getCheckedValue(f.accepto)!="1") alert("Has d'acceptar les condicions d'ús");
	else {
		f.submit();
	}
}

function verificar_formulari_crear_grup(){
	f=document.registre;
	if(f.titol.value.length<5) alert("Introdueix el títol del debat");
	else if(f.contingut.value.length=="0") alert("Introdueix el missatge");
	else f.submit();
}

function verificar_formulari_registre_conjunt(){
	f=document.registre;
	var exp=/^([0-9A-Za-z\-_ç]+$)/;

	var error_grup="0";
	if(f.nom_grup.value.length>15 && f.nom_grup.value.search(/ /)=="-1")error_grup="1";
	

	if(f.usuari.value.length<2) alert("Introdueix el nom d'usuari");
	else if (f.usuari.value.search(exp)==-1) alert("El nom d'usuari té caràcters no vàlids");
	else if (!isValidEmail(f.email1.value)) alert("El primer e-mail no és correcte");
	else if (f.email1.value != f.email2.value) alert("L'email introduit no coincideix amb la confirmació");
	else if (f.password1.value.length < 4) alert("Falta introduir la contrasenya. Minim 4 caracters.");
	else if (f.password1.value != f.password2.value) alert("Les contrasenyes no coincideixen");
	else if (f.password1.value.search(exp)==-1) alert("La contrasenya té caràcters no vàlids");
	else if (f.nom.value.length=="0") alert("Falta el nom");
	else if (f.d_dia.value.length=="0" || f.d_mes.value.length=="0" || f.d_any.value.length=="0") alert("Falta la data de naixement");
	else if (f.id_poblacio.value=="0" && f.altre_poblacio.value.length==0) alert("Selecciona la població o introdueix-la si no apareix a la llista");
	else if (f.nom_grup.value.length < 1) alert("Falta introduir el nom del grup/DJ");

	else if (error_grup=="1") alert("El nom del grup és molt llarg i no està separat per espais");
	else if (f.genere1.value.length<1) alert("Selecciona un estil musical");
	else if (f.usuari_grup.value.length < 4) alert("Falta introduir la direcció a repopblica");
	else if (document.getElementById('verif_user_grup').innerHTML.search("ok_no")!="-1") alert("La direcció indicada no està disponible");
	else if (f.repeteix_codi.value.length != 5) alert("Introdueix el codi visual");
	else if (getCheckedValue(f.accepto)!="1") alert("Has d'acceptar les condicions d'ús");
	else {
		f.submit();
	}
}

function recuperar(){
	f=document.formulari;
	if (!isValidEmail(f.email.value)) alert("L'e-mail indicat no és correcte");
	else {
		f.submit();
	}
}

function cercador_comunitat(){
	f=document.form_comunitat;
	if(f.nom.value.length=="0" && f.id_poblacio.value=="0") alert("Introdueix el nom a cercar o selecciona una població");
	else {
		f.submit();
	}
}
/*mapa locals*/

var map;
var geocoder;

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));

		//map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//map.addControl(new GScaleControl());


		map.setCenter(new GLatLng(coordenada1,coordenada2), 16);

		var marker = new GMarker(new GLatLng(coordenada1,coordenada2));
		map.addOverlay(marker);

		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(codi_html);
		});
		marker.openInfoWindowHtml(codi_html);

	}

}

function borrar_forum(id){
	if(confirm("Segur que vols borrar el missatge?")){
		location.href="delete.php?id="+id+"&delete=1";
	}
}


function verificar_formulari_contactar(){
	f=document.formcontactar;
	if(f.nom.value.length<5) alert("Escriu el teu nom");
	else if(f.email.value.length==0) alert("Escriu el teu e-mail");
	else if(!isValidEmail(f.email.value)) alert("L'e-mail introduit no és correcte");
	else if(f.comentaris.value.length==0) alert("Escriu els teus comentaris");
	else f.submit();
}


function mostra_ajuda(ajuda){


	if(document.getElementById(ajuda).style.display == 'none' || document.getElementById(ajuda).style.display == ''){
		document.getElementById(ajuda).style.display = 'block';
		document.getElementById("i"+ajuda).src="/img/fletxa04.png";
	}	
	else{
		document.getElementById(ajuda).style.display = 'none';
		document.getElementById("i"+ajuda).src="/img/fletxa03.png";
	}
}

function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('No pots escriure més de '+limit+' caràcters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('Pots escriure '+ (limit - textlength) +' caràcters més.');
		return true;
	}
}
