var regex=new Object();
regex.fname = /^([a-zA-Z]+)$/;
regex.lname = /^([a-zA-Z]+)$/;
regex.name = /^([a-zA-Z]+)$/;
regex.address = /^([0-9A-Za-z]+)$/;
regex.email = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
regex.url = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
regex.dob = /^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)([0-9]){4}$/;
//alert(reg.fname.test("Testing"));

$.fn.checkForm = function() {
	$bReturn = true;
	$sError = '';
	this.each(function() {
		$(':input', this).each(function() {
			if (
				($(this).attr('required')) && 
				(
					(!this.value) ||
					(
						(this.type == 'checkbox') &&
						(!this.checked)
					)
				)
			)
			{
				$sError = $sError+'"'+$(this).attr('required')+'" field is required.<br/>';
				$(this).parent().parent().addClass('red');
			}
			else if ($(this).attr('syntax'))
			{
				$sSyntax = $(this).attr('syntax');
				if ($sSyntax == 'email')
				{
					if (!regex.email.test(this.value))
					{
						$sError = $sError+'Invalid E-mail Address provided.<br/>';
						$(this).parent().parent().addClass('red');
					}
					else
					{
						$(this).parent().parent().removeClass('red');
					}
				}
			}
			else if ($(this).attr('match'))
			{
				if ($(this).val() != $(this).parents('form').find("input[name='"+$(this).attr('match')+"']").val())
				{
					$sOrg = $(this).parents('form').find("input[name='"+$(this).attr('match')+"']").attr('required');
					$sVer = $(this).attr('required');
					$sError = $sError+'"'+$sOrg+'" and "'+$sVer+'" do not match.<br/>';
					$(this).parents('form').find("input[name='"+$(this).attr('match')+"']").parent().parent().addClass('red');
					$(this).parent().parent().addClass('red');
				}
			}
			else
			{
				$(this).parent().parent().removeClass('red');
			}
		});
		if ($sError)
		{
			if ($('div.status', this).length)
			{
				//$('div.status', this).addClass('red').slideUp('normal', function() { $(this).html('<span class="heading">The following errors occured:</span><br/>'+$sError).slideDown(); });
				$('div.status', this).addClass('red').slideUp('normal', function() { $(this).html('<span class="bigger">Required Fields Missing:</span><br/>Please fill in the required fields, highlighted in red.').slideDown(); });
			}
			else
			{
				alert("The following errors occured:\r\n\r\n"+$sError.replace(/<br\/>/gi, "\r\n"));
			}
			$bReturn = false;
		}
		else
		{
			if ($('div.status', this).length)
			{
				$('div.status', this).removeClass('red').slideUp('normal', function() { $(this).html(''); });
			}
		}
	});
	
	return $bReturn;
}