// Regular Expression to test whether the value is valid
$.tools.validator.fn("[type=time]", "Please supply a valid time", function(input, value) { 
	return /^\d\d:\d\d$/.test(value);
});

$.tools.validator.fn("[data-equals]", "Value not equal with the $1 field", function(input) {
	var name = input.attr("data-equals"),
		 field = this.getInputs().filter("[name=" + name + "]"); 
	return input.val() == field.val() ? true : [name]; 
});

$.tools.validator.fn("[minlength]", function(input, value) {
	var min = input.attr("minlength");
	
	return value.length >= min ? true : {     
		en: "Please provide at least " +min+ " character" + (min > 1 ? "s" : ""),
		fi: "Kent�n minimipituus on " +min+ " merkki�" 
	};
});

$.tools.validator.localizeFn("[type=time]", {
	en: 'Please supply a valid time',
	fi: 'Virheellinen aika'		
});


$("#myform").validator({
	position: 'top left', 
	offset: [4, 0],
	message: '<div><em/></div>' // em element is the arrow
});

$("#myform").bind("onFail", function(e, errors)  {

        if (e.originalEvent !== undefined) {
	// we are only doing stuff when the form is submitted
	if (e.originalEvent.type == 'submit') {

		// loop through Error objects and add the border color
                c = 0;
		$.each(errors, function()  {
                    if (c == 0) {
                      input = this.input;
                    }
                    c = c +1;
		});
                if (c > 0) {
                        var target_offset = $("#myform").offset();
			var target_top = target_offset.top;
			//goto that anchor by setting the body scroll top to anchor top
			$('html, body').animate({scrollTop:target_top}, 1500);
                }
                //input.focus();
	}
        }
});



