// JavaScript Document
//handle divisone select event
function validate() {
	var required = true;
	//var privacy = true;
	var captcha = true;
	//hide all message
	$(".msg").hide();
	
	//look for required field
	$("input").each(function(i, selected){  
		//if element has class required
		if($(selected).hasClass('required')) 
			if ($(selected).val()=='') required = false;
	});

	/*
	//look for privacy authorisation
	if(!$("#privacy").attr('checked')) privacy = false;
	//look for captcha code
	*/
	if(jQuery.trim($("#codice").val())!=jQuery.trim($("#captcha").html())) captcha = false;
	
	if(!required) $("#messaggioerror").show();
	else if (!captcha) $("#messaggioerrorcaptcha").show();
	else $("#form").submit();

	return;
}

function getRandomCaptcha()  {
	var captcha = '';
	for(i=0;i<=5;i++) {
		rand_no = Math.random();
		rand_no = rand_no * 9;
		rand_no = Math.ceil(rand_no);
		captcha += rand_no;
	}
	return captcha;
}

jQuery(document).ready(function($) {	
	$('a[rel*=facebox]').facebox();
	
	$("#captcha").html(getRandomCaptcha());
	
	$("#iscrivi").click(function () {
		  validate();
	});
	
	$("#privacy").change(function () {							
	  if($(this).attr("checked"))  {
		  $("#iscrivi").attr("disabled",false);
		  $("#iscrivi").css("background","#DDDDDD");
	  }
	  else  {
		  $("#iscrivi").attr("disabled",true);
		  $("#iscrivi").css("background","#EEEEEE");
	  }
	})

})