/* jquery validate customized rules */

$(function() {

	$.validator.setDefaults({
		errorContainer: "#errorcontainer",
		errorLabelContainer: "#errorcontainer ul",
		wrapper: "li",
		//validClass: "correct",
		//errorClass: "error",
		//errorElement: "label",
		//ignoreTitle: true,
		meta: "validate"
		//submitHandler: function() {
		//alert("OK")
		//}
		//, showErrors: function(errorMap, errorList) {
		//	$("#errorcontainer").fadeIn();
		//	$("#errorsummary").text("Sono presenti "
		//		+ this.numberOfInvalids() 
		//		+ " errori:");
		//this.defaultShowErrors();
		//}
		//,errorPlacement: function(error, element) {
		//	error.appendTo( element.next("label") );
		//}
		, invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				//var message = errors == 1
				//		? 'You missed 1 field. It has been highlighted'
				//		: 'You missed ' + errors + ' fields. They have been highlighted';
				//$("#errorcontainer span").html(message);
				$("#errorcontainer").show();
			} else {
				$("#errorcontainer").hide();
			}
			//$("#errorcontainer").fadeIn();
			//$("#errorsummary").text(validator.numberOfInvalids() + " campi non validi");
		}
		//,success: function(label) {
		//	if (validator.numberOfInvalids()==0) $("#errorcontainer").fadeOut();
		//	//label.addClass("correct").text("Ok!")
		//}
	});

	$.validator.addMethod(
		"date",
		function(value, element) {
  		return value.match(/^(0?[1-9]|[12][0-9]|3[01])[-\/\.](0?[1-9]|1[012])[-/.](19|20)?\d\d$/);
		},
		"Indicare la data nel formato dd/mm/yyyy"
	);
	
	$.validator.addMethod(
	    "price",
	    function(value, element) {
	        return value.match(/^[0-9]+(\,[0-9]{1,2})?$/);
	    },
	    "Indicare il prezzo (utilizzare la virgola come separatore decimale"
	);
	
	$.validator.addMethod(
	    "price-as-percent",
	    function(value, element) {
	        return (parseFloat(value) >= 0) && (parseFloat(value) <= 100);
	    },
	    "Il campo prezzo se espresso in percentuale deve essere compreso tra 0 e 100%"
	);
	
//	$.validator.addMethod(
//	    "editor",
//	    function(value, element) {
//	        alert(element);
//	        return false;
//	    },
//	    "Inserire una descrizione completa del pacchetto"
//	);

	//submit on save
	$(".save").click(function() {
		return $("form").validate().form();
	});

    //submit on "save and new"
    $("[id$=LinkSaveAndNew]").click(function() {
		return $("form").validate().form();
	});
});

