$(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"});

	$("div#brochureForm").hide();
	toggleBrochure();

});

function toggleBrochure()
{
	$("a#brochureCheckbox").toggle(
		function(){
			$(this).addClass("checked");
			$("div#brochureForm").slideDown(500);
			$('div#brochureForm input').removeAttr("disabled");
		},
		function(){
			$(this).removeClass("checked");
			$('div#brochureForm input').attr("disabled", true); 
			$("div#brochureForm").slideUp(500);
		}
	);
}

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();
			window.location.hash = "errorsContainer";
			return false;
		}
		if (validate_required(FirstName,"Please enter your first name")==false)
		{
			FirstName.focus();
			window.location.hash = "errorsContainer";
			return false;
		}
		if (validate_required(LastName,"Please enter your last name")==false)
		{
			LastName.focus();
			window.location.hash = "errorsContainer";
			return false;
		}
		if (validate_email(Email,"Please enter a valid email address")==false)
		{
			Email.focus();
			window.location.hash = "errorsContainer";
			return false;
		}
		if (confirm_email(ConfirmEmail,"Please confirm your email address")==false)
		{
			ConfirmEmail.focus();
			window.location.hash = "errorsContainer";
			return false;
		}
		if (match_emails(ConfirmEmail,"the two emails do not match")==false)
		{
			ConfirmEmail.focus();
			window.location.hash = "errorsContainer";
			return false;
		}
		
		if ($("a#brochureCheckbox").hasClass("checked")){
		
			if (validate_required(Address,"Please enter your address")==false)
			{
				Address.focus();
				window.location.hash = "errorsContainer";
				return false;
			}
			if (validate_required(City,"Please enter your city")==false)
			{
				City.focus();
				window.location.hash = "errorsContainer";
				return false;
			}
			if (validate_required(State,"Please enter your state")==false)
			{
				State.focus();
				window.location.hash = "errorsContainer";
				return false;
			}
			if (validate_required(Zip,"Please enter your zip")==false)
			{
				Zip.focus();
				window.location.hash = "errorsContainer";
				return false;
			}
		
		}
				
	}
}