/**
 * Handle form validation
 */
formsJsonRules = {};

$(document).ready(function() {
	
	var firstForm = null;	
	for(var name in formsJsonRules) {
		value = formsJsonRules[name];

		$('#' + name)
			.formValidation(value)
			.submit(function() {
				var result = $(this).isValid();
				
				clearErrors(this);	
				if(! result.isValid()) {
					displayErrors(this, result, 1);
					return false;
				}
				return true;
			});
		firstForm = firstForm || name;
	}	
	
	if(firstForm) {
		$('#'+firstForm+' input:visible:enable:first').focus();
	}
});

function clearErrors(obj) {
	$('.errorLabel', obj).remove();
	$('.alertBox', obj).remove();
}

function displayErrors(obj, result, mode) {
	if (mode == 1 ) {
		$('#messages').replaceContent(result.getError());
	} else {
		$.each(result.getErrors(), function(field,data){
			$('[name=' + field+ ']', obj).after('<span class="errorLabel">' + data[0] + '</span');
		});
		
	}	
}


