$(document).ready(function(){
	$("div#errorMsg").hide().css({color: "#cc0000", margin: "0 0 10px 0"});
	$("div#alertMsg").hide().css({color: "#cc0000", margin: "0 0 10px 0"});
	$("div#confirmMsg").hide().css({color: "#cc0000", margin: "0 0 10px 0"});
	$("div#matchMsg").hide().css({color: "#cc0000", margin: "0 0 10px 0"});
});

emailValue= $("input#Email").val();
confirmValue= $("input#ConfirmEmail").val();

function validate_required(field,errortxt)
{
	with (field)
	{
		if (value==null||value=="")
		{
			$("div#errorsContainer").fadeIn(500);
			$("div#errorMsg").fadeIn(500).text(errortxt);
			return false;
		}
		else
		{
			$("div#errorMsg").hide();
			return true;
		}
	}
}

function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2)
		{
			$("div#errorsContainer").fadeIn(500);
			$("div#alertMsg").fadeIn(500).text(alerttxt);
			return false;
		}
		else
		{
			$("div#alertMsg").hide(500);
			return true;
		}
	}
}

function confirm_email(field,confirmtxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2)
		{
			$("div#errorsContainer").fadeIn(500);
			$("div#confirmMsg").fadeIn(500).text(confirmtxt);
			return false;
		}
		else
		{
			$("div#confirmMsg").hide(500);
			return true;
		}
	}
}

function match_emails(field,matchtxt)
{
	with (field)
	{
		emailValue = $("input#Email").val();
		confirmValue = $("input#ConfirmEmail").val();
		
		if ( confirmValue != emailValue )
		{
			$("div#errorsContainer").fadeIn(500);
			$("div#matchMsg").fadeIn(500).text(matchtxt);
			return false;
		}
		else
		{
			$("div#matchMsg").slideUp(500);
			return true;
		}
	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_required(ChooseProgram,"Please choose a program")==false)
		{
			ChooseProgram.focus();
			return false;
		}
		if (validate_required(FirstName,"Please enter your first name")==false)
		{
			FirstName.focus();
			return false;
		}
		if (validate_required(LastName,"Please enter your last name")==false)
		{
			LastName.focus();
			return false;
		}
		if (validate_email(Email,"Please enter a valid email address")==false)
		{
			Email.focus();
			return false;
		}
		if (confirm_email(ConfirmEmail,"Please confirm your email address")==false)
		{
			ConfirmEmail.focus();
			return false;
		}
		if (match_emails(ConfirmEmail,"the two emails do not match")==false)
		{
			ConfirmEmail.focus();
			return false;
		}
		
	}
}