function isEmail(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}

$(document).ready(function(){
	
	//clean all the attributes from the font tag
	$('.content-inner').find('font').removeAttr('size');
	
	//text resizer (increase)
	$('.text-up').bind("click", function() {
		
		$('.content-inner').css('font-size', 
								(parseInt($(".content-inner").css('font-size')) + 1) + "px");
		
		//disable a behaviour
		return false;
	});
	
	//text resizer (decrease)
	$('.text-down').click(function(){
		
		$('.content-inner').css('font-size', 
								(parseInt($('.content-inner').css('font-size')) - 1) + "px");
		
		//disable a behaviour
		return false;
	});
	
	//open print window
	$('.toPrint').popupWindow({ 
		height:640, 
		width:960, 
		centerBrowser:1,
		scrollbars:1
	});	
	
	//print window
	$('.toPrintGo').click(function(){
		window.print();
		return false;
	});
	
	$('.print-content').find('a').each(function(){
		$this = $(this);
		if ($this.attr('href') != '#'){
			$this.after(' <span>(' + $this.attr('href') + ')</span>');
		}
		
	});
		
	$("#imageField2").click(function(){
		var url = 'getPoll.php';
		var data = $("#formPoll").serialize();

		if ($("input[name='resposta']").is(":checked")){
			$("#poll_aviso").hide();
			$.ajax({
				type: "POST",
				url: url,
				data: data,
				success: function(msg){
					$('#poll_resposta').html(msg);
				}
			});

			$("#poll_perguntas").hide();
			$("#poll_resposta").show();
		} else {
			$("#poll_aviso").show();
		}
	});

	$("#enviarMensagem").click(function(){
		var valido = true;

		if ($("#nome").val().length == 0 || $("#nome").val() == 'nome' || $("#nome").val() == 'name'){
			valido = false;
			$("#nomeRequired").show();
		} else {
			$("#nomeRequired").hide();
		}

		if (!isEmail($("#email").val())){
			valido = false;
			$("#emailRequired").show();
		} else {
			$("#emailRequired").hide();
		}

		if (valido){
			var url = 'enviaMail.php';
			var data = $("#formEnviar").serialize();

			$.ajax({
				type: "POST",
				url: url,
				data: data,
				success: function(msg){
					$('#resultadoEnvio').html(msg);
				}
			});

		} else {
			return false;
		}
	});

	$("#btnEncomendar").click(function(){
		var valido = true;

		if ($("#nome").val().length == 0){
			valido = false;
			$("#nomeRequired").show();
		} else {
			$("#nomeRequired").hide();
		}

		if ($("#morada").val().length == 0){
			valido = false;
			$("#moradaRequired").show();
		} else {
			$("#moradaRequired").hide();
		}

		if ($("#localidade").val().length == 0){
			valido = false;
			$("#localidadeRequired").show();
		} else {
			$("#localidadeRequired").hide();
		}

		if ($("#cod_postal1").val().length == 0){
			valido = false;
			$("#codPostalRequired").show();
		} else {
			$("#codPostalRequired").hide();
		}

		if (!isEmail($("#email").val())){
			valido = false;
			$("#emailRequired").show();
		} else {
			$("#emailRequired").hide();
		}

		if ($("#contacto").val().length == 0){
			valido = false;
			$("#contactoRequired").show();
		} else {
			$("#contactoRequired").hide();
		}

		if (valido){
			var url = 'enviaEncomenda.php';
			var data = $("#formEncomenda").serialize();

			$.ajax({
				type: "POST",
				url: url,
				data: data,
				success: function(msg){
					$('#divAccao').html(msg);
				}
			});

		} else {
			return false;
		}
	});
});
