if ( typeof formClassSelector == 'undefined' ) {
	var formClassSelector = "form.metis-form";
}

var metis = $.extend({},( metis | {} ) , {
	fn : {}
});

metis.fn = $.extend({},metis.fn,{
	validateForm : function() {
		/* internal function */

		function e_r(b) {
			if(!$(b).hasClass("fieldWithError")){
				$(b).addClass("fieldWithError");
			}
		};

		function sa(b,a){
			if ( b != null && b.length > 0 )
				$(b).html(a).fadeIn();
		};

		function v_e(a){
			return /[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i.test(a);
		}

		var form = this;
		var b=true;
		var m=$(form).find("div.message");

		/* Reset message status */
		m.removeClass("notice").addClass("alert").html("");
		$(form).find(":input[type='submit']").hide();
		$("div.message").hide();
		$(form).find(".fieldWithError").removeClass("fieldWithError");
		$(form).find(":input").each(function(){
			try{
				if ( $(this).val() != null )
					$(this).val($(this).val().replace(/^\s+|\s+$/,""));

				var fieldName = $(this).attr('name');

				if ( $(this).attr("label") && $(this).attr("label") != '' ) {
					fieldName = $(this).attr("label");
				} else {
					$(form).find("label[for='"+ $(this).attr('name') +"']").each(function(){
						fieldName = $(this).html();
					});
				}
	
				if(!isNaN($(this).attr("minlength"))) {
					if($(this).val().length < parseInt($(this).attr("minlength"))){
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Il campo “"+fieldName+"” deve essere lungo almeno "+parseInt($(this).attr("minlength"))+" caratteri");
						}
						b=false;
						$(this).focus();
						return false
					}
				}
				if( $(this).attr("validate") && $(this).attr("validate").search(/\bemail\b/i) >= 0 && $(this).val() != null && $(this).val() != "" ) {
					if(!v_e($(this).val())){
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Devi fornire un'email valida");
						}
						b=false;
						$(this).focus();
						return false
					}
				}
				if ( $(this).attr("validate") && $(this).attr("validate").search(/\beurodate\b/i) >= 0 && $(this).val() != null && $(this).val() != "" ) {
					if ( /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test($(this).val()) == false ) {
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Devi fornire una data nel formato yyyy-mm-dd");
						}
						b=false;
						$(this).focus();
						return false
					}
				}
				if ( $(this).attr("validate") && $(this).attr("validate").search(/\binteger\b/i) >= 0 && $(this).val() != null && $(this).val() != "" ) {
					var v = parseInt($(this).val().replace(/,/gi,"."));
					if ( isNaN(v) ) {
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Devi fornire un valore numerico valido per " + fieldName );
						}
						b=false;
						$(this).focus();
						return false;
					}
				}
				if ( $(this).attr("validate") && $(this).attr("validate").search(/\bfloat\b/i) >= 0 && $(this).val() != null && $(this).val() != "" ) {
					var v = parseFloat($(this).val().replace(/,/gi,"."));
					if ( isNaN(v) ) {
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Devi fornire un valore numerico valido per " + fieldName );
						}
						b=false;
						$(this).focus();
						return false;
					} else {
						$(this).val(v.toFixed(2));
					}
				}
				if ( $(this).attr("validate") && $(this).attr("validate").search(/\bhh:mm\b/i) >= 0 && $(this).val() != null && $(this).val() != "" ) {
					if ( /^[0-9]{2}$/.test($(this).val()) ) {
						$(this).val( $(this).val() + ":00" );
					}
					if ( /^[0-9]{2}:[0-9]{2}$/.test($(this).val()) == false ) {
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Devi fornire un valore nel formato HH:mm" );
						}
						b=false;
						$(this).focus();
						return false;
					}
				}
				if($(this).attr("type")=="password"){
					var f=$(form).find(":input[name='"+$(this).attr("name")+"-confirm']");
					if(f.length>0&&f.val()!=$(this).val()){
						e_r(this);
						e_r(f);
						sa(m,"Le due password non coincidono");
						b=false;
						$(this).focus();
						return false
					}
				}
				if( $(this).attr("required") && ( $(this).attr("required").toString().toLowerCase() =="true" || $(this).attr("required").toLowerCase() =="yes" || $(this).attr("required") == "1" ) ) {
					if ($(this).attr("type")=="checkbox" && $(this).attr("checked")==false) {
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"È necessario selezionare il campo "+fieldName);
						}
						b=false;
						$(this).focus();
						return false
					} else if ( $(this).attr("type") == "radio" ) {
						var radios = $(":input[name='" + $(this).attr("name") + "']");
						if ( radios.length > 0 && $(radios).filter(":checked").length == 0 ) {
							e_r( radios );
							sa(m,"È necessario selezionare un'opzione");
							b=false;
							$(this).focus();
							return false;
						}
					} else if( $(this).val() == null || /^[\t ]*$/.test($(this).val())==true ) {
						e_r(this);
						if ( $(this).attr("message") != undefined ) {
							sa(m,$(this).attr("message"));
						} else {
							sa(m,"Il campo "+fieldName+" non può essere vuoto");
						}
						b=false;
						$(this).focus();
						return false
					}
				}
			}
			catch(d){
				sa(m,d.toString());
				b=false;
				return false
			}
		});
		if ( b == false )
			$(this).find(":input[type='submit']").show();
		return b;
	},
	initForm : function(options) {
		var form = this;
		var defaults = {
			bind:true
		};
		var options = $.extend(defaults, options);
		var msg = $(form).find("div.message");
		if ( msg.length == 0 ) {
			$(form).prepend($("<div/>").addClass("message"));
			msg = $(form).find("div.message");
		}
		if ( msg.length > 0 ) {
			msg.hide();
			if ( msg.html().length > 0 )
				msg.fadeIn("slow");
		}
		if ( options.bind ) {
			$(form).submit(metis.fn.validateForm);
		}
		$(form).find("input[type='submit']").click(function(){
			return $(form).validateForm();
		});
		$(form).find(".submit").click(function(){
			$(form).submit();
		});
		/* return this to make function chainable - marco */
		return this;
	},
	serializeObj : function(options) {
		var form = this;
		var data = {};
		$.each( $(this).serializeArray() , function(i,field) {
			data[field.name] = field.value;
		});
		return data;
	}
});

$.fn.initForm = metis.fn.initForm;
$.fn.validateForm = metis.fn.validateForm;
$.fn.serializeObj = metis.fn.serializeObj;

$(function() {
	$(formClassSelector).initForm();
});