$(function()
{
	


	// append a asterisk to the required fields labels
	$('body label.required').each(function()
	{
		if ( $('img', $(this)).length == 0 )
		{
			$(this).append('<img src="/img/forms/required.gif" alt="Required Field" style="margin:0 0 5px 5px;" />');
		}
	});
	




	
	// go through each form assigning a handler to save buttons
	// making sure that all labels 
	var errorAlert = '<p class="message adminmessage error">Please fill in all required fields (marked with a <img src="/img/forms/required.gif" style="background:#fff;padding:4px;border-radius:4px;-moz-border-radius:4px;" />)</p>';

	$('form').submit(function()
	{
		
		$('.error').remove();
		
		var ret = true;
		var f = $(this).parent();
		var errorText = '<p><strong>Please fix the following errors:</strong><br/>';

		$('label[class="required"]', $(this)).each(function()
		{
			var lf = $(this).attr('for');
			var e = $('*[name="' + lf + '"]', f);
			var v = e.val();
			if ( v == '' )
			{
				e.addClass('errorInput').effect("pulsate", { times: 3 }, 150);
				$('#errorAlert').remove();
				$('p.adminmessage').after(errorAlert);
				$('#errorAlert').show();
				errorText += '<span style="width:30%;float:left;">' + lf.replace(/_/g, ' ').replace('cust', '').replace('del', 'Delivery') + '</span>';

				
				ret = false;
			}
			else
			{
				$('#errorAlert').hide();
				e.removeClass('errorInput');
				// ret = true;
			}
		});

		
		if ( ret === false || !ret )
		{
			$('#errorAlert').remove();
		
			errorText + '</p>';
			$(this).before(errorAlert);
			window.location.href = window.location.href.split('#')[0] += '#content';
		}
		
		// if (! ret ) return false;
		// return $(this).onsubmit();
		// wtf is that for ?
		
		return ret;
		
		
		
		
	});
	

	
	
	
});
