$(document).ready(function() {
	
	$(".rating_star img").hover(function() {
		var box = $(this).parent().attr("id");
		var idstar = $(this).attr("id").substring(4);
		rating('on',''+idstar+'',''+box+'');
	}, function() {
		var box = $(this).parent().attr("id");
		var idstar = $(this).attr("id").substring(4);
		rating('off',''+idstar+'',''+box+'');
	});
	
	$("#rating_commento").keyup(function() {
		
		var messaggio = $(this).val();
		var lunghezza = messaggio.length;
		var rimanenti = 150 - lunghezza;
		
		if (rimanenti<0) {
			rimanenti = 0;
			$(this).val(messaggio.substring(0,150));
		}
		
		$("#caratteri_rimanenti").html(rimanenti);
		
		
	});
	
	$(".rating_star img").click(function(e) {
		e.preventDefault();
		var box = $(this).parent().attr("id");
		var idstar = $(this).attr("id").substring(4);
		rating_click(''+idstar+'',''+box+'');		
		$("#hidden_"+box).val(idstar);
	});
	
    $("#rating_vote").click(function(a) { 
		a.preventDefault();
		$("#rating_pop_contents .step1").show();
		$("#rating_pop_contents .step2").hide();
		
		$.blockUI({
			message: $("#rating_pop"),
			css : {
				top: "100px",
				cursor: "default",
				border: "none",
				backgroundColor: "transparent"
			}, 
		    overlayCSS:  { 
		        backgroundColor: "#000", 
		        opacity:         0.9
		    },		
		    focusInput: false
		});     
		
		$(".blockUI").css("z-index","99999999");
		
		if($.browser.safari) {
			$(".flashObject").hide();
		}							  
    });	


	$("#rating_pop .button").click(function(e) {
		e.preventDefault();
		var lingua = $("#hidden_rating_lang").val();

		if (checkFields()) {
			$.ajax({
			  type: "POST",
			  url: "jsp/"+lingua+"/rating/index.jsp",
			  data: "email="+$("#rating_email").val()+"&nickname="+$("#rating_nickname").val()+"&mese="+$("#rating_mese").val()+"&anno="+$("#rating_anno").val()+"&commento="+$("#rating_commento").val()+"&idstore="+$("#hidden_idstore").val()+"&servizio="+$("#hidden_servizio_star").val()+"&allestimento="+$("#hidden_allestimento_star").val(),
			  success: function(msg){
				$("#rating_pop_contents .step1").fadeOut("fast", function() {
					$("#rating_pop_contents .step2 .size").html(msg);
					$("#rating_pop_contents .step2").fadeIn("fast");
				});
			  }
			});
		}
		
	});
	
});

function checkFields() {

	if ($("#hidden_servizio_star").val()=="") {
		alert($("#hidden_servizio_star").attr("rel"));
		return false;
	}

	if ($("#hidden_allestimento_star").val()=="") {
		alert($("#hidden_allestimento_star").attr("rel"));
		return false;
	}

	if ( ($("#rating_email").val()=="") || ($("#rating_email").val()=="EMAIL")) {
		alert($("#rating_email").attr("rel"));
		$("#rating_email").val("").focus();
		return false;
	} else {
		if (!indirizzoEmailValido($("#rating_email").val())) {
			alert($("#rating_email").attr("rel"));			
			$("#rating_email").focus();
			return false;
		}
	}
	
	if ( ($("#rating_nickname").val()=="") || ($("#rating_nickname").val()=="NICKNAME")) {
		alert($("#rating_nickname").attr("rel"));
		$("#rating_nickname").val("").focus();
		return false;
	}
	
	if ($("#rating_mese").val()=="") {
		alert($("#rating_mese").attr("rel"));
		$("#rating_mese").focus();
		return false;
	}

	if ($("#rating_anno").val()=="") {
		alert($("#rating_anno").attr("rel"));
		$("#rating_anno").focus();
		return false;
	}
	
	return true;
}

function rating(metodo,selected,box) {

	var i = 1;
		
	if (metodo=="on") {
		$("#"+box+" img").each(function() {
			if (i<=selected) $(this).addClass("over");
			i++;
		});
	} else {
		$("#"+box+" img").removeClass("over");
	}
}

function rating_click(selected,box) {

	var i = 1;
		
	$("#"+box+" img").removeClass("over");
	$("#"+box+" img").removeClass("sel");

	$("#"+box+" img").each(function() {
		if (i<=selected) $(this).addClass("sel");
		i++;
	});
}

function indirizzoEmailValido(indirizzo) {
	if (window.RegExp) { 
		var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		var regnv = new RegExp(nonvalido);
		var regv = new RegExp(valido);
			
			if (!regnv.test(indirizzo) && regv.test(indirizzo)) return true;

		return false;

	} else {

		if(indirizzo.indexOf("@") >= 0) return true;

		return false;

	}
}