// Javascript for Forms on the JHOC Website

//====================THIS IS ALL CODE (NOTHING TO UPDATE)====================

function resetWarning()
{
	var result = confirm("You are about to clear all the fields. Press OK to continue.");
	setTimeout("checkForOtherStatus()",50);
	return result;
}

function noDigits(e)
{
//Code from W3Schools
	var keynum
	var keychar
	var numcheck
	
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

keychar = String.fromCharCode(keynum);
numcheck = /\d/;

return !numcheck.test(keychar);
}

function focusNextField(num,thisField,nextField)
{
	if (document.getElementById(thisField).value.length == num)
		document.getElementById(nextField).focus();
}

function checkForOtherStatus()
{
	var status = document.getElementById("status");
	var otherStatus = document.getElementById("otherStatus");

	if (status.options[status.selectedIndex].text.search(/other/i) != -1)
		{
		otherStatus.disabled = false;
		otherStatus.focus();
		otherStatus.select();
		}
	else
		otherStatus.disabled = true;	
}

//Validation

function reqField(field,fullName)
{
//Code from W3Schools
	var tempName = fullName;
	if (fullName.search(/phone/i) != -1)
		tempName = "Phone #";
	var alerttxt = "\n" + tempName + " was not completed";
	
	var fieldValue = field.value;
	if (field.type == "select-one")
		fieldValue = field.options[field.selectedIndex].text;
	else
		fieldValue = field.value;

	if (fieldValue == null || fieldValue == "" || fieldValue == "Select..." || fieldValue == "None available" || fieldValue == "Other...")
		return alerttxt;
	else if (fullName.search(/phone/i) != -1)
		return validatePhone(field,fullName)
	else if (fullName.search(/mail/i) != -1)
		return validateEmail(field,fullName);
	else
		return "";
}

function validatePhone(field,fullName)
{
var alerttxt = "\nPhone # was not completed";
var nonDigitText = "\nPhone # can only contain digits";

with (field)
	{
	value += "";
	
	if (fullName.search(/4/) != -1)
		{
		if (value.length < 4)
			return alerttxt;
		else if (value.length == 4 && value.search(/\D/) != -1)
			return nonDigitText;
		}
	else
		{
		if (value.length < 3)
			return alerttxt;
		else if (value.length == 3 && value.search(/\D/) != -1)
			return nonDigitText;
		}
	}
	return "";
}

function validateEmail(field,fullName)
{
//Code from W3Schools
var alerttxt = "\n" + fullName + " is invalid";

with (field)
	{
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	
	if (apos<1||dotpos-apos<2) 
		return alerttxt;
	else
		return "";
	}
}

function validateTripSignUp(thisForm)
{
var alerttxt = "";
var numErrors = 0;
var numPhoneErrors = 0;
var phoneText = "";
var firstBadField;
	
with (thisForm)
	{
	alerttxt += reqField(trip,"Trip Selection")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = trip;
				numErrors++;
			}
	alerttxt += reqField(fname,"First Name")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = fname;
				numErrors++;
			}
	alerttxt += reqField(lname,"Last Name")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = lname;
				numErrors++;
			}
	if (document.getElementById("status").options[document.getElementById("status").selectedIndex].text == "Other")
	{
	alerttxt += reqField(otherStatus,"Status")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = otherStatus;
				numErrors++;
			}
	}
	else
	{
	alerttxt += reqField(status,"Status")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = status;
				numErrors++;
			}
	}
	alerttxt += reqField(email,"E-mail")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = email;
				numErrors++;
			}
	phoneText = reqField(phoneArea,"phoneArea")
		if (numPhoneErrors == 0 && phoneText != "")
			{
			alerttxt += phoneText;
			numPhoneErrors++;
			if (numErrors == 0)
				{
				firstBadField = phoneArea;
				numErrors++;
				}
			}
	phoneText = reqField(phone3,"phone3")
		if (numPhoneErrors == 0 && phoneText != "")
			{
			alerttxt += phoneText;
			numPhoneErrors++;
			if (numErrors == 0)
				{
				firstBadField = phone3;
				numErrors++;
				}
			}
	phoneText = reqField(phone4,"phone4")
		if (numPhoneErrors == 0 && phoneText != "")
			{
			alerttxt += phoneText;
			numPhoneErrors++;
			if (numErrors == 0)
				{
				firstBadField = phone4;
				numErrors++;
				}
			}
	}
	
	if (numErrors == 0)
		return true;
	else
		{
			alert("You must correct the following problem(s) before you can submit:\n" + alerttxt);
			firstBadField.focus();
			if (firstBadField.type == "text")
				firstBadField.select();
			return false;
		}
}

function validateApplication(thisForm)
{
var alerttxt = "";
var numErrors = 0;
var numPhoneErrors = 0;
var phoneText = "";
var firstBadField;
	
with (thisForm)
	{
	alerttxt += reqField(fname,"First Name")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = fname;
				numErrors++;
			}
	alerttxt += reqField(lname,"Last Name")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = lname;
				numErrors++;
			}
	alerttxt += reqField(status,"Status")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = status;
				numErrors++;
			}
	alerttxt += reqField(email,"E-mail")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = email;
				numErrors++;
			}
	phoneText = reqField(phoneArea,"phoneArea")
		if (numPhoneErrors == 0 && phoneText != "")
			{
			alerttxt += phoneText;
			numPhoneErrors++;
			if (numErrors == 0)
				{
				firstBadField = phoneArea;
				numErrors++;
				}
			}
	phoneText = reqField(phone3,"phone3")
		if (numPhoneErrors == 0 && phoneText != "")
			{
			alerttxt += phoneText;
			numPhoneErrors++;
			if (numErrors == 0)
				{
				firstBadField = phone3;
				numErrors++;
				}
			}
	phoneText = reqField(phone4,"phone4")
		if (numPhoneErrors == 0 && phoneText != "")
			{
			alerttxt += phoneText;
			numPhoneErrors++;
			if (numErrors == 0)
				{
				firstBadField = phone4;
				numErrors++;
				}
			}
	alerttxt += reqField(tripsQ,"Question 1")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = tripsQ;
				numErrors++;
			}
	alerttxt += reqField(whyQ,"Question 2")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = whyQ;
				numErrors++;
			}
	alerttxt += reqField(expQ,"Question 3")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = expQ;
				numErrors++;
			}
	alerttxt += reqField(timeQ,"Question 4")
		if (numErrors == 0 && alerttxt != "")
			{
				firstBadField = timeQ;
				numErrors++;
			}
	}
	
	if (numErrors == 0)
		return confirm("You are about to submit. Press OK to continue.");
	else
		{
			alert("You must correct the following problem(s) before you can submit:\n" + alerttxt);
			firstBadField.focus();
			if (firstBadField.type == "text")
				firstBadField.select();
			return false;
		}
}