var AJAX = '/ajax/ajax.php';

function les_noticies( lloc ) {

	if( lloc == 'comentaris' )
	{
		$("#mes_comentat").slideDown();
		$("#mes_llegit").slideUp();
		
		$("#comentaris_tag").removeClass("fons_noticies_tag_inactiu");
		$("#comentaris_tag").addClass("fons_noticies_tag_actiu");
		$("#noticies_tag").removeClass("fons_noticies_tag_actiu");
		$("#noticies_tag").addClass("fons_noticies_tag_inactiu");
	}
	else
	{
		$("#mes_comentat").slideUp();
		$("#mes_llegit").slideDown();
		
		$("#noticies_tag").removeClass("fons_noticies_tag_inactiu");
		$("#noticies_tag").addClass("fons_noticies_tag_actiu");
		$("#comentaris_tag").removeClass("fons_noticies_tag_actiu");
		$("#comentaris_tag").addClass("fons_noticies_tag_inactiu");
	}
}

function votar()
{
	var enq = $("input[@name='enq']:checked").attr("value");
  	if( enq )
	{
		$.getJSON( AJAX, { id_enquesta_opcio: enq, accio: 'votar' },
		function( resposta )
		{
			var vots = 0;
			vots = parseInt( vots );
				
 			$('#enquesta-opcions').fadeOut("slow", function()
 			{
				$(this).empty();

				for(var i = 0; i < resposta.opcions.length; i++)
				{
					tmp = parseInt( resposta.opcions[i].vots );
					vots = tmp + vots;
					afegeixOpcio(resposta.opcions[i].id_element, resposta.opcions[i].titol, resposta.opcions[i].width1, resposta.opcions[i].percentatge, i);
				}

				html = '<div class="llegenda"><img src="img/icones/vots.gif" alt="Vots" /> <span id="enquesta-vots">'+ vots +'</span> vots&nbsp;&nbsp;<a href="enquesta/'+ resposta.uri  +'#comentaris"><img src="img/icones/comentari_vermell.gif" alt="Comentaris" /> '+ resposta.comentaris +'</a></div></div>';
				$('#enquesta-opcions').append( html );
				$('#enquesta-opcions').fadeIn("slow",function(){animateResults();});
			});
		});
	}
	else
	{
		alert("Has de seleccionar una opció!");
		return false;
	}
}


function afegeixOpcio(id, titol, width1, percentatge, i)
{
	var html = '';

	html += '<label for="enq_opt'+id+'">'+titol+'</label><br />\n';
	html += '<div class="enq_barra" style="width: 0%;"></div>\n';
	html += '<div class="enq_percent">'+percentatge+'%</div>\n';
	html += '<br class="p" /><br />\n';

	$('#enquesta-opcions').append( html );
}

function animateResults()
{
  $("#enquesta-opcions div.enq_barra").each(function()
  {
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({width: percentage}, 'slow');
  });
}


function inscriure( accio )
{
	var email = $("#email-titulars").val();
	var nom = $("#nom-titulars").val();

	if( email )
	{
		if( !echeck( email ) )
		{
			alert("El correu electrònic que has posat és incorrecte");
			return false;
		}
		
		if( accio == 'alta' ) accio = 'inscriure_alta';
		if( accio == 'baixa' ) accio = 'inscriure_baixa';
				
		$.getJSON( AJAX, { email: email, nom: nom, accio: accio },
		function( resposta )
		{
			if( resposta.exit == 'true' ) $("#inscriure-formulari").empty();
			
			$("#inscriure-text").html( resposta.text );
		});
	}
	else
	{
		alert("Falta posar el teu correu electrònic");
		return false;
	}
}

function comprovaCamp ( id_camp, color )
{
	if( $("#"+id_camp).val() == "" )
	{
		$("#"+id_camp).addClass("comentari-error");
		$("#"+id_camp).removeClass("comentari-"+color);
	}
	else
	{
		$("#"+id_camp).addClass("comentari-"+color);
		$("#"+id_camp).removeClass("comentari-error");
	}
}

function comprovaMail ( id_camp, color )
{
	if ( $("#"+id_camp).val() == "" || !echeck( $("#"+id_camp).val() ) )
	{
		$("#"+id_camp).addClass("comentari-error");
		$("#"+id_camp).removeClass("comentari-"+color);
	}
	else
	{
		$("#"+id_camp).addClass("comentari-"+color);
		$("#"+id_camp).removeClass("comentari-error");
	}
}


function enviar_comentari()
{
	var error = false;
	var error_text = '';
	var id_article;
	var nom;
	var tmp;
	var email;
	var comentari;
	var poblacio = "";
	
	var id_article = $("#id_article").val();
	var lloc = $("#lloc").val();
	var color = $("#color").val();
	
	if( $("#email").val() ) return false;
	
	if( $("#nom").val() )
	{
		$("#nom").removeClass("comentari-error");
		$("#nom").addClass("comentari-"+color);
		var nom = $("#nom").val();
	}
	else
	{
		$("#nom").addClass("comentari-error");
		$("#nom").removeClass("comentari-"+color);
		error_text += "<li>Has de posar el teu nom</li>\n";
		error = true;
	}
	
	if( $("#telefon").val() )
	{
		var telefon = $("#telefon").val();
		
		if( telefon.length == 9 )
		{
			$("#telefon").removeClass("comentari-error");
			$("#telefon").addClass("comentari-"+color);
		}
		else
		{
			$("#telefon").addClass("comentari-error");
			$("#telefon").removeClass("comentari-"+color);
			error_text += "<li>Has de posar un DNI correcte</li>\n";
			error = true;
		}
	}
	else
	{
		$("#telefon").addClass("comentari-error");
		$("#telefon").removeClass("comentari-"+color);
		error_text += "<li>Has de posar el teu telèfon</li>\n";
		error = true;
	}
	
	if( $("#dni").val() )
	{
		var dni = $("#dni").val();
		
		if( dnicheck( dni ) )
		{
			$("#dni").removeClass("comentari-error");
			$("#dni").addClass("comentari-"+color);
		}
		else
		{
			$("#dni").addClass("comentari-error");
			$("#dni").removeClass("comentari-"+color);
			error_text += "<li>Has de posar un DNI correcte</li>\n";
			error = true;
		}
	}
	else
	{
		$("#dni").addClass("comentari-error");
		$("#dni").removeClass("comentari-"+color);
		error_text += "<li>Has de posar el teu DNI</li>\n";
		error = true;
	}
		
	if( $("#poblacio").val() )
		var poblacio = $("#poblacio").val();
			
	
	if( $("#ae").val() )
	{
		var ae = $("#ae").val();

		if( echeck( ae ) )
		{
			$("#ae").removeClass("comentari-error");
			$("#ae").addClass("comentari-"+color);
		}
		else
		{
			$("#ae").addClass("comentari-error");
			$("#ae").removeClass("comentari-"+color);
			error_text += "<li>El correu electrònic que has posat és erroni</li>\n";
			error = true;
		}
	}
	else
	{
		$("#ae").addClass("comentari-error");
		$("#ae").removeClass("comentari-"+color);
		error_text += "<li>Has de posar el correu electrònic</li>\n";
		error = true;
	}
		
	if( $("#comentari").val() )
	{
		var comentari = $("#comentari").val();
		$("#comentari").removeClass("comentari-error");
		$("#comentari").addClass("comentari-"+color);
	}
	else
	{
		$("#comentari").addClass("comentari-error");
		$("#comentari").removeClass("comentari-"+color);
		error_text += "<li>Has d'escriure un comentari</li>\n";
		error = true;
	}

	if( $("#notifica")[0].checked )
		var notifica = "si";
	else
		var notifica = "no";

 	if( !error )
	{
		$("#comentari-formulari").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { id_article: id_article, lloc: lloc, nom: nom, dni: dni, telefon: telefon, poblacio: poblacio, email: ae, comentari: comentari, notifica: notifica, accio: 'enviar_comentari' },
		function( resposta )
		{
			$("#comentari-error").css("display", "")
			$("#comentari-formulari").fadeOut("slow");
			$("#comentari-enviat").fadeIn("slow");
		});
	}
	else
	{
		$("#comentari-error").fadeIn("slow");
		$("#comentari-error").html( "<ul>" + error_text + "</ul>");
	}
}

function dnicheck( dni )
{
	numero = dni.substr(0,dni.length-1);
	let = dni.substr(dni.length-1,1);
	numero = numero % 23;
	letra='TRWAGMYFPDXBNJZSQVHLCKET';
	letra=letra.substring(numero,numero+1);
	if ( letra!=let )
	{
		letra='trwagmyfpdxbnjzsqvhlcket';
		letra=letra.substring(numero,numero+1);
		if ( letra!=let )
			return false;
		else
			return true;
	}
	else
		return true;
}


function genera_comentaris( pagina, id_article, lloc )
{
 	if( pagina && id_article )
	{
		$("#comentaris-cnt").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { id_article: id_article, pagina: pagina, lloc: lloc, accio: 'genera_comentaris' },
		function( resposta )
		{
			$("#comentaris-cnt").html( resposta );
			window.location.hash = "comentaris";
		});
	}
	else
	{
		alert("Error inesperat!");
	}
	
}
function veure_comentari( id_comentari, lloc )
{
 	if( id_comentari )
	{
		$.getJSON( AJAX, { id_comentari: id_comentari, lloc: lloc, accio: 'veure_comentari' },
		function( resposta )
		{
			$("#comentari-cnt-" + resposta.id_comentari ).html( resposta.text );
		});
	}
	else
	{
		alert("Error inesperat!");
	}
}
function limitacio( numero, id_article, lloc )
{
 	if( numero && id_article && lloc )
	{
		$("#comentaris-cnt").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { numero: numero, accio: 'limitacio' },
		function()
		{
			genera_comentaris( "1", id_article, lloc );
		});
	}
	else
	{
		alert("Error inesperat!");
	}
}


function echeck(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
		return false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;

	if (str.indexOf(at,(lat+1))!=-1)
		return false;

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	if (str.indexOf(dot,(lat+2))==-1)
		return false;

	if (str.indexOf(" ")!=-1)
		return false;

	return true;
}


function veure_guia_comercial( id_guia_comercial )
{
 	if( id_guia_comercial )
	{
		$.getJSON( AJAX, { id_guia_comercial: id_guia_comercial, accio: 'veure_guia_comercial' },
		function( resposta )
		{
			$("#veure_guia_comercial-" + id_guia_comercial ).css( "display", "none" );
			$("#amaga_guia_comercial-" + id_guia_comercial ).css( "display", "block" );
						
			if( GBrowserIsCompatible() && resposta.lat != 0 && resposta.lon != 0 )
			{
				$("#mapat_" + id_guia_comercial ).css( "display", "block" );
				mapat = new GMap2(document.getElementById( "mapat_" + id_guia_comercial ));
				var geocoder = new GClientGeocoder();
				
				mapat.addControl (new GSmallZoomControl());
				mapat.addControl (new GMapTypeControl());
				
				mapat.enableDoubleClickZoom();
				mapat.enableScrollWheelZoom();
			
				var punt = new GLatLng( resposta.lat , resposta.lon );
				
				mapat.setCenter( punt , 15 );
				marker = new GMarker( punt, {draggable: true}  );
				mapat.addOverlay(marker);
			}
		});
	}
	else
	{
		alert("Error inesperat!");
	}
}

function amaga_guia_comercial( id_guia_comercial )
{
	$("#veure_guia_comercial-" + id_guia_comercial ).css( "display", "block" );
	$("#amaga_guia_comercial-" + id_guia_comercial ).css( "display", "none" );
	$("#mapat_" + id_guia_comercial ).css( "display", "none" );
}
