﻿function SubmitForm()
{
	var intPrizeCodeNull = 0;
	var intEmailNull = 0;
	var intEmailConfirmNull = 0;

	var intBadEmail = 0;
	var intEmailDontMatch = 0;

	if (document.getElementById("txtPrizeCode").value == "" && document.getElementById("divPrizeCode").style.display != "none")
	{
		intPrizeCodeNull = 1;
	}

	if (document.getElementById("txtEmail").value == "")
	{
		intEmailNull = 1;
	}

	if (document.getElementById("txtEmailConfirm").value == "")
	{
		intEmailConfirmNull = 1;
	}

	if (intEmailNull == 0 && !IsValidEmail(document.getElementById("txtEmail").value) && document.getElementById("txtEmail").value != "")
	{
		intBadEmail = 1;
	}
	
	if (intEmailNull == 0 && document.getElementById("txtEmail").value != document.getElementById("txtEmailConfirm").value)
	{
		intEmailDontMatch = 1;
	}

	if (intPrizeCodeNull == 1 ||
		intEmailNull == 1 ||
		intEmailConfirmNull == 1 ||
		intBadEmail == 1 ||
		intEmailDontMatch == 1)
	{
		var strAlert = "Please correct the following before proceeding:\n\n";

		if (intPrizeCodeNull == 1)
		{
			strAlert += "The Prize Code field cannot be blank.\n";
		}
		if (intEmailNull == 1)
		{
			strAlert += "The e-Mail Address field cannot be blank.\n";
		}
		if (intEmailConfirmNull == 1)
		{
			strAlert += "The Confirm e-Mail Address field cannot be blank.\n";
		}
		if (intBadEmail == 1)
		{
			strAlert += "The e-Mail address you entered, '" + document.getElementById("txtEmail").value + "', is not a valid e-Mail address.\n";
		}
		if (intEmailDontMatch == 1)
		{
			strAlert += "The e-Mail Address field and Confirm e-Mail Address field don't match.\n";
		}

		window.alert(strAlert);
	}
	else
	{
		document.form.submit();
	}
}

function IsValidEmail(strEmail)
{
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp)
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
		return (strEmail.indexOf(".") > 2) && (strEmail.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");
	return (!r1.test(strEmail) && r2.test(strEmail));
}
