jQuery.validator.addMethod("requiredIfVisible", function(value, element, params) { 
	
	return ( ( $(element).parent().css('display') == 'none' ) || (value != ''));

}, "Required"); 

jQuery.validator.addMethod("mustAccept", function(value, element, params) { 

	var name = $(element).attr('name');
	
	var radioValue = $("input[@name='"+name+"']:checked").val();
	
	return  (radioValue == 'true');

}, "Consent required"); 

jQuery.validator.addMethod("phone", function(value, element, params) { 

	return  this.optional(element) || /^[\d\s]+$/.test(value);

}, "Use numbers only"); 

jQuery.validator.addMethod(
	"dateITA",
	function(value, element) {
		var check = false;
		var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
		if( re.test(value)){
			var adata = value.split('/');
			var gg = parseInt(adata[0],10);
			var mm = parseInt(adata[1],10);
			var aaaa = parseInt(adata[2],10);
			var xdata = new Date(aaaa,mm-1,gg);
			if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) )
				check = true;
			else
				check = false;
		} else
			check = false;
		return this.optional(element) || check;
	}, 
	"Invalid date"
);
 
showJobAreaOther = function() {

		if ( $("#job_area").attr('value') == "Altro" ) {
			$("#job_area_other").parent().slideDown("normal");
		} else {
			$("#job_area_other").parent().slideUp("normal");
		}
		
	};
	
showRoleOther = function() {

		if ( $("#role").attr('value') == "Altro" ) {
			$("#role_other").parent().slideDown("normal");
		} else {
			$("#role_other").parent().slideUp("normal");
		}
		
	};

showKnownOther = function() {

		if ( $("#known").attr('value') == "Altro" ) {
			$("#known_other").parent().slideDown("normal");
		} else {
			$("#known_other").parent().slideUp("normal");

		}
		
	};
	
handleJobChange = function() {
	
	var job = $("#job").attr('value');
		
	if     ( job == 'LiberoProfessionista' ) {

		$("#job_area, #role").parent().slideDown("normal");
		$("#company, #university, #faculty,  #job_other").parent().slideUp("normal");
		
		showJobAreaOther();
		showRoleOther();

	} 
	else if ( job == 'Dipendente' ) {

		$("#company, #job_area, #role").parent().slideDown("normal");
		$("#university, #faculty, #job_other").parent().slideUp("normal");
		
		showJobAreaOther();
		showRoleOther();
	}
	else if ( job == 'ContrattoProgetto' ) {

		$("#company, #job_area, #role").parent().slideDown("normal");
		$("#university, #faculty, #job_other").parent().slideUp("normal");
		
		showJobAreaOther();
		showRoleOther();
	
	}
	else if ( job == 'Studente' ) {

		$("#university, #faculty").parent().slideDown("normal");
		$("#company, #job_area, #role, #job_other, #job_area_other, #role_other").parent().slideUp("normal");

	}
	else if ( job == 'AttualmenteNonOccupato' ) {

		$("#company, #job_area, #role, #university, #faculty, #job_other, #job_area_other, #role_other").parent().slideUp("normal");

	}
	else if ( job == "Altro" ) {
		
		$("#job_other").parent().slideDown("normal");
		$("#company, #job_area, #role, #university, #faculty, #job_area_other, #role_other").parent().slideUp("normal");
		
	}
				

	
}
showProvince = function() {
	if ( $("#country").attr('value') == "Italy" ) {
		$("#province").parent().slideDown("normal");
	}
	else {
		$("#province").parent().slideUp("normal");
	}
}


$(document).ready(function() {
	
	$("#job_area").bind('change', showJobAreaOther);
	$("#role").bind('change',showRoleOther);
	$("#job").bind('change',handleJobChange);
	$("#known").bind('change',showKnownOther);
	$("#country").bind('change',showProvince);
	
	showJobAreaOther();
	showRoleOther();
	showKnownOther();
	handleJobChange();

	var validator = $("#agileletter").validate({
		rules: {
			name: {
				required: true
			},
			surname: {
				required: true
			},
			country: {
				required:true
			},
				province: {
					requiredIfVisible: true
				},
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true,
				phone: true
			},	
			job: {
				required: true
			},
				job_other: {
					requiredIfVisible: true		
				},
			job_area: {
				requiredIfVisible: true
			},
				job_area_other: {
					requiredIfVisible: true
				},
			role: {
				requiredIfVisible: true
			},
				role_other: {
					requiredIfVisible: true
				},
			known: {
				required: true
			},
				known_other: {
					requiredIfVisible: true					
				},
			privacy_1a: {
				mustAccept: true,
				required: true				
			},
			privacy_1b: {
				mustAccept: true,
				required: true
			}

		},
		messages: {
			name: "Required",
			surname: "Required",
			country: {
				required: "Required"
			},
			province: {
				required: "Required"
			},
			email: {
				required: "Required",
				email: "Invalid Email"
			},
			phone: { 
				required: "Required",
				phone: "Use numbers only"
			},
			job: "Required",
			job_area: "Required",
			role: "Required",
			known: "Required",
			privacy_1a: {
				required: "Required",
				mustAccept: "Consent to point 1A required."
			},
			privacy_1b: {
				required: "Required",
				mustAccept: "Consent to point 1B required."
			}
		},
		errorPlacement: function(error, element) {
				error.appendTo( element.parent() );
		}
	});
	


});