/*** Coded and projected by 'Mauro Ferrara' @ MITdesign <mauro@mitdesign.it> ***/

function error_box(item, content)
{
	$('#'+item).parent().append('<div class="error_box">'+content+'</div>');
	$('#'+item).addClass('error_highlight');
	$('#'+item).fadeTo(100, 1, function(){
   		$('#'+item).focus();
  	});
}
function error_check(form)
{
	$('#'+form+' .error_box').each(function(index_el, item_el) { $(item_el).remove(); } );
	
	var focus = 0;
	var error = 0;

	$('#'+form+' fieldset').each(
		function(index_fset, item_fset)
		{	
			$('input[title],select[title],textarea[title]', $(this)).each(
				function(index_el, item_el)
				{
					var type = $(this).attr('title').substring(0, 5);
					if ( 
							((type == "issel") && ( (!$(this).val()) || ($(this).val() == 0) || ($(this).val() == '-') )) || 
							((type == "empty") && !$(this).val()) || 
							((type == "isnum") && isNaN($(this).val())) ||
							((type == "isnty") && (isNaN($(this).val()) || !$(this).val())) || 
							((type == "isimg") && ($(this).val()) && ($(this).val().toLowerCase().lastIndexOf(".jpg") == -1)) || 
							((type == "isvid") && ($(this).val()) && ($(this).val().toLowerCase().lastIndexOf(".wmv") == -1) && ($(this).val().toLowerCase().lastIndexOf(".avi") == -1) && ($(this).val().toLowerCase().lastIndexOf(".mpg") == -1) && ($(this).val().toLowerCase().lastIndexOf("mpeg") == -1))
					)
					{
						var desc;
						if ($(this).attr('title').substring(6, 100))
							desc = $(this).attr('title').substring(6, 100);
						else
						{
							if (type == "empty") desc = '* Campo obbligatorio.';
							else if (type == "issel") desc = '* Campo obbligatorio.';
							else if (type == "isnum") desc = '* Campo numerico (decimali separati da . ex. 10.53).';
							else if (type == "isnty") desc = '* Campo obbligatorio e numerico (decimali separati da . ex. 10.53).';	
							else if (type == "isimg") desc = '* Inserire immmagini solo di formato .jpg (jpeg).';
							else if (type == "isvid") desc = '* Inserire video solo di tipo .wmv / .avi / .mpg / .mpeg.';
						}
						$(item_fset).append('<div class="error_box">'+desc+'</div>');

						$(item_el).addClass('error_highlight');
						if (focus == 0) 
						{ 
							$(this).animate({}, 200, function() { $(item_el).focus(); } );
							focus = 1;
						}
						error = 1;
					}
					else
						$(this).removeClass('error_highlight');
				}
			);
		}
	);
	return error;
}
