var crlf = "\r\n";
var Required = "Required Field";
var ErrorMsg = "The following errors must be corrected before submitting this form:" + crlf + crlf;
var ConfirmSubmitMsg = "Are you sure you want to SUBMIT the form?";

function SetFieldError(poField, sTitle)
{
	if (navigator.userAgent.indexOf("MSIE") != -1)
	{
		with (poField)
		{
			if (type != "check")
			{
				style.backgroundColor = "#FFCCCC";
				title = sTitle;
			}
		}
	}
}

function ClearCheck2OnCheck1(poCheck1, poCheck2)
{
	if (poCheck1.checked)
		poCheck2.checked = false;	
}

function register_ValidateForm(oForm)	{
	var RequiredList = "txtBankContactName txtAddressLine1 txtCity txtState txtZip txtBankEmail txtParticipantCount";
	//RequiredList = "";
	var pos;
	var oField;
	var i, r;
	var bErrors = false;

	msg = ErrorMsg;

	for (i = 0; i < oForm.elements.length; i++)
	{
		oField = oForm.elements[i]
		//alert(" oField.type = " + oField.type);
		if ((oField.type == "text") || (oField.type == "password"))
		{
			pos = RequiredList.indexOf(oField.name);
			if (pos >= 0)
			{
				if (oField.value == "")
				{
					SetFieldError(oField, "Required Field");
					bErrors = true;
				}
			}
		}
		else if (oField.type == "select-one")
		{
			pos = RequiredList.indexOf(oField.name);
			if (pos >= 0)
			{
				if (oField.value == "")
				{
					SetFieldError(oField, "Required Field");
					bErrors = true;
				}
			}
		}
		else if (oField.type == "radio")
		{
			pos = RequiredList.indexOf(oField.name);
			if (pos >= 0)
			{//REQUIRED RADIO
				if (RadioCount == 0)
				{//ADD FIRST RADIO
					//alert("add radio " + oField.name);
					RadioName[0] = oField.name;
					RadioCount++;
					if (oField.checked)
						RadioVal[0] = true;
				}
				else
				{//SEARCH FOR RADIO IN ARRAY
					for (r = 0; r < RadioCount; r++)
					{
						if (oField.name == RadioName[r])
						{
							if (oField.checked)
								RadioVal[r] = true;
							//FOUND IT, EXIT LOOP
							break;
						}
					}//LOOP FOR r

					if (r == RadioCount)
					{//NOT FOUND, ADD
						RadioName[RadioCount] = oField.name;
						if (oField.checked)
							RadioVal[RadioCount] = true;
						RadioCount++;
					}
				}//END if (RadioCount == 0)
			}//END if (pos >= 0)
		}//END if (oField.type == "radio")
	}//END LOOP ELEMENTS

	if (bErrors)
		msg =  ErrorMsg + " - Missing required field(s)\r\n";

	//VERIFY ZIP:
	oField = oForm.txtZip;
	if (!verifyZip(oField.value))
	{
		SetFieldError(oField, "Invalid Zip");
		bErrors = true;
		msg += " - Zip code is not valid (valid format: #####-#### or #####)\r\n";
	}

	//VERIFY EMAIL:
	oField = oForm.txtBankEmail;
	if (!verifyEmail(oField.value))
	{
		SetFieldError(oField, "Invalid E-mail Address");
		bErrors = true;
		msg += " - E-mail address is not valid (valid example: emailid@emailhost.com)\r\n";
	}

	//VERIFY EMAIL1:
	oField = oForm.txtParticipant1Email;
	if (!verifyEmail(oField.value))
	{
		SetFieldError(oField, "Invalid E-mail Address");
		bErrors = true;
		msg += " - E-mail address is not valid (valid example: emailid@emailhost.com)\r\n";
	}
	//VERIFY EMAIL2:
	oField = oForm.txtParticipant2Email;
	if (!verifyEmail(oField.value))
	{
		SetFieldError(oField, "Invalid E-mail Address");
		bErrors = true;
		msg += " - E-mail address is not valid (valid example: emailid@emailhost.com)\r\n";
	}
	//VERIFY EMAIL3:
	oField = oForm.txtParticipant3Email;
	if (!verifyEmail(oField.value))
	{
		SetFieldError(oField, "Invalid E-mail Address");
		bErrors = true;
		msg += " - E-mail address is not valid (valid example: emailid@emailhost.com)\r\n";
	}
	//VERIFY EMAIL4:
	oField = oForm.txtParticipant4Email;
	if (!verifyEmail(oField.value))
	{
		SetFieldError(oField, "Invalid E-mail Address");
		bErrors = true;
		msg += " - E-mail address is not valid (valid example: emailid@emailhost.com)\r\n";
	}
	//VERIFY EMAIL5:
	oField = oForm.txtParticipant5Email;
	if (!verifyEmail(oField.value))
	{
		SetFieldError(oField, "Invalid E-mail Address");
		bErrors = true;
		msg += " - E-mail address is not valid (valid example: emailid@emailhost.com)\r\n";
	}

	if (bErrors)
	{
		alert(msg);
		window.scroll(0,0);
		return false;
	}
	else
		return true;
		//return confirm(ConfirmSubmitMsg);
}


function IsAllNumbers(sString)
{
	var i;
	var x;
	for (i=0; i < sString.length; i++)
	{
		x = parseInt(sString.substring(i, i + 1));
		if (isNaN(x))
			return false;
	}
	return true;
}

function verifyAreaCode(sString)
{
	if (sString.length == 0)
		return true;

	if (sString.length != 3)
		return false;
	else
		if (IsAllNumbers(sString))
			return true;
		else
			return false;
}

function verifyPhoneNumber(sString)
{
	if (sString.length == 0)
		return true;

	if (sString.length != 8)
		return false;
	else
	{
		var at = sString.indexOf("-");
		if (at != 3)
			return false;

		var prefix = sString.substring(0, 3)
		var suffix = sString.substring(4, 8)

		if (!IsAllNumbers(prefix))
			return false;

		if (!IsAllNumbers(suffix))
			return false;
	}
	return true;
}

function verifyZip(zip)
{
	if (zip.length == 0)
		return true;

	if ((zip.length != 5) && (zip.length != 10))
		return false;
	else
	{
		var prefix = zip.substring(0, 5);
		if (!IsAllNumbers(prefix))
			return false;

		if (zip.length == 5)
			return true;
		else
		{
			var at = zip.indexOf("-");
			if (at != 5)
				return false;

			var suffix = zip.substring(6, 11);
			if (IsAllNumbers(suffix))
				return true;
			else
				return false;
		}
	}
}

function verifyYear(sString)
{
	if (sString.length == 0)
		return true;

	if (sString.length != 4)
		return false;
	else
	{
		if (!IsAllNumbers(sString))
			return false;
		else
			if ((sString < 1800) || (sString > 2200))
				return false;
			else
				return true;
	}
}

function verifyEmail(email)
{
	if (email.length == 0)
		return true;

	if(email.length > 7)
	{
		var at = email.indexOf("@");
		if((at > 1) && (at==email.lastIndexOf("@")))
		{
			var dot = email.lastIndexOf(".");
			if ((dot>4) && ((dot-at)>2) && ((email.length-dot)>2))
			{
				return true; // valid!
			}
		}
	}
	return false;
}

function verifyDate(sDate)
{
	var date1 = "";
	var date2 = "";
	var month1 = "";
	var month2 = "";
	var year2 = "";
	var year1 = "";
	var sString1;
	var sString2;
	var slash = "/";

	sNewDate = "";
	if (sDate == "")
		return true;

	sString = sDate;
	var pos = sString.indexOf(slash);
	if (pos >= 0)
	{
		month1 = sString.substring(0,pos);
		sString = sString.substring(pos + 1, sString.length);
		//alert("pos = " + pos + crlf + "month = " + month + crlf + "sString = " + sString);

		pos = sString.indexOf(slash);
		if (pos >= 0)
		{
			date1 = sString.substring(0,pos);
			year1 = sString.substring(pos + 1, sString.length);
			//alert("pos = " + pos + crlf + "day = " + day + crlf + "year = " + year);
		}
	}
	if ((month1 > 0) && (month1 < 13) && (date1 > 0) && (date1 < 32) && (year1 > 1700) && (year1 < 2200))
	{
		var newDateObj = new Date();

		var d, s = "Today's date is: ";
		d = new Date();
		d.setFullYear(year1);
		//SET DATE TO ONE BEFORE SETTING MONTH TO AVOID INVALID DATE ERROR!
		d.setDate(1);
		d.setMonth(month1-1);
		d.setDate(date1);

		//newDateObj.setFullYear(year1);
		//newDateObj.setMonth(month1 - 1);
		//newDateObj.setDate(date1);
		if (date1.length == 1)
			date1 = "0" + date1;
		if (month1.length == 1)
			month1 = "0" + month1;
		sString1 = month1 + "/" + date1 + "/" + year1
		//alert("sString1=" + sString1);

		date2 = (d.getDate()) + "/";
		//alert("date2=" + date2);
		//day2 = getDate();

		month2 = (d.getMonth() + 1) + "/";
		//alert("month2=" + month2);
		//month2 = getMonth();


		s += (d.getMonth() + 1) + "/";
		s += d.getDate() + "/";
		s += d.getYear();
		//alert(s);


		if (date2.length == 2)
			date2 = "0" + date2;
		if (month2.length == 2)
			month2 = "0" + month2;
		sString2 = month2 + date2 + d.getFullYear();
		//alert("sString2=" + sString2);

		if (sString1 == sString2)
		{
			sNewDate = sString1;
			return true;
		}
		else
			return false;

	}
	else
		return false;
}

function GotFocus(poField, poField2)
{
	//alert(poField.name);
	if (navigator.userAgent.indexOf("MSIE") != -1)
	{
		with (poField)
		{
			//style.backgroundColor = 'white';
			style.backgroundColor = poField2.style.backgroundColor;
			title = "";
			//select();  
			// select() only seems to work for textarea fields, which is dangerous in, say, the legal description field,
			// where one false keystroke could wipe out the entire field's contents!
			blnShowError = true;
		}
	}

	window.status = "";
}

function ShowFieldNames(oForm, bWrite) {
	var i;
	var sList = "";
	for (i = 0; i < oForm.elements.length; i++)
	{
		if (!bWrite) {
			if ((oForm.elements[i].type == "text") || (oForm.elements[i].type == "textarea"))
				oForm.elements[i].value = oForm.elements[i].name;
			else if (oForm.elements[i].type == "checkbox")
				oForm.elements[i].checked = true;
		} else {
			//if ((oForm.elements[i].type == "text") || (oForm.elements[i].type == "textarea"))	
				sList = sList +	oForm.elements[i].name + "<br>";
		}
	}

	if (bWrite) 
		document.write(sList);
	else	
		oForm.elements[i-1].scrollIntoView(true)
	
		//window.scroll(0,0);
}

function IsADecimalNumber(sString)
{
	var i;
	var x;
	for (i=0; i < sString.length; i++)
	{
		x = parseInt(sString.substring(i, i + 1));
		if (isNaN(x))
		{
			x = sString.substring(i, i + 1);
			if (x != ".")
				return false;
		}
	}
	return true;
}

function FormatDollar(oField)
{
	if (oField.value.length > 0 && IsADecimalNumber(oField.value))
		oField.value = FormatNumber(oField.value);
}

function FormatNumber(sString)
{
	//Format = "#,###.00"
	var r1;
	var r2;
	var r3;
	var pos;
	var dec;
	var x;
	var r = new Array();
	var i;

	//Force string coversion:
	x = sString + "";

	pos = x.indexOf(".");
	if (pos >= 0)
	{
		dec = x.substring(pos, x.length);
		if (dec.length == 1)
			dec = dec + "00";
		else if (dec.length == 2)
			dec = dec + "0";
		else
			dec = dec.substring(0, 3);
		x = x.substring(0, pos);
	}
	else
		dec = ".00";

	i = Math.ceil(x.length / 3) - 1;
	while (x.length > 0)
	{
		r[i] = x.substring(x.length - 3, x.length);
		x = x.substring(0, x.length - 3);
		i--;
	}
	return (r.join(",") + dec);
}

function ReviseRegistration(oForm)
{
	oForm.confirmed.value = 0;
	oForm.showform.value = 1;
	oForm.submit();
}


function ClearForm(oForm)
{
	if (confirm('Clear ALL form data?'))
	{
		window.location.href = "go.php";
	}
}

function ClearWebinarForm(oForm)
{
	if (confirm('Clear ALL form data?'))
	{
		window.location.href = "sign-up.php";
	}
}

