var regex=new Object();
regex.integer = /^([0-9]+)$/;
regex.number = /^([0-9]+)$/;
regex.name = /^([a-zA-Z]+) ([a-zA-Z]+)$/;
regex.fname = /^([a-zA-Z]+)$/;
regex.lname = /^([a-zA-Z]+)$/;
regex.address = /^([0-9A-Za-z]+)$/;
regex.phone = /(\d){3}\-(\d){3}\-(\d){4}/;
//regex.email = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
regex.email = /^([\w]+)(.[\w]+)*@([\w]+)\.(com|net|org|gov|info|tv|mobi|biz|us|me|ca|tel|co\.uk|eu|asia|jobs|name|cc|md|de|cn|bz|fm|am|in|la|es|li|nu|se|hn|nl|dk|fr|it|at|ch|be|org\.uk|me\.uk|com\.cn|net\.cn|org\.cn|br\.com|uk\.com|uk\.net|eu\.com|jpn\.com|us\.com|cn\.com|de\.com|hu\.com|no\.com|qc\.com|ru\.com|sa\.com|se\.com|se\.net|uy\.com|za\.com|sc|vc|com\.tw|org\.tw)$/;
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}$/;
regex.date = /^([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('error');
			}
			else if (($(this).attr('syntax')) && (this.value))
			{
				$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('error');
					}
					else
					{
						$(this).parent().parent().removeClass('error');
					}
				}
				else if ($sSyntax == 'name')
				{
					if (!regex.name.test(this.value))
					{
						$sError = $sError+'Invalid name provided must be firstname lastname.<br/>';
						$(this).parent().parent().addClass('error');
					}
					else
					{
						$(this).parent().parent().removeClass('error');
					}
				}
				else if ($sSyntax == 'address')
				{
					if (!regex.address.test(this.value))
					{
						$sError = $sError+'Invalid address provided.<br/>';
						$(this).parent().parent().addClass('error');
					}
					else
					{
						$(this).parent().parent().removeClass('error');
					}
				}
				else if ($sSyntax == 'number')
				{
					if (!regex.number.test(this.value))
					{
						$sError = $sError+'Invalid number provided.<br/>';
						$(this).parent().parent().addClass('error');
					}
					else
					{
						$(this).parent().parent().removeClass('error');
					}
				}
				else if ($sSyntax == 'phone')
				{
					if (!regex.phone.test(this.value))
					{
						$sError = $sError+'Invalid phone number provided.<br/>';
						$(this).parent().parent().addClass('error');
					}
					else
					{
						$(this).parent().parent().removeClass('error');
					}
				}
			}
			else if ($(this).attr('match'))
			{
				//alert($(this).attr('match'));
				//input[@name='"+sItem+"']
				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('error');
					$(this).parent().parent().addClass('error');
				}
			}
			else
			{
				$(this).parent().parent().removeClass('error');
			}
		});
		if ($sError)
		{
			if ($('div.status', this).length)
			{
				$('div.status', this).addClass('error').slideUp('normal', function() { $(this).html('<b>The following errors occured:</b><br/>'+$sError).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('error').slideUp('normal', function() { $(this).html(''); });
			}
		}
	});
	
	return $bReturn;
}