function helloWorld()
{
	alert("hello world");
}

function validate_form(formName)
{
	var valid = true;  //---- false means the validation failed
	var foundRequired=false;	//---- true means the current form field matched a required field
	var errorMessage = "";	//---- stores the error message to present the user
	var focus = "";	//---- stores the field id to set focus to
	var formElements=document.getElementById(formName);	//---- holds all the elements of the calling form

	//alert("This is the " + formName + " form."); //---- debugging: identifies the current form
	
	for (var i=0;i<formElements.length;i++)	//---- this code validates each empty form code
 	{
		errorMessage = errorMessage + requiredFieldCheck(formName,formElements,i,focus);	//---- validates all required fields
		if (formElements.elements[i].name.match("zip") && formElements.elements[i].value!="")	//---- validates all zip code fields
		{
			errorMessage = errorMessage + zipCodeCheck(formName,formElements,i,focus);
		}
		if (formElements.elements[i].name.match("email") && formElements.elements[i].value!="")	//---- validates all email addresses
		{
			errorMessage = errorMessage + emailCheck(formName,formElements,i,focus);
		}
		//---- validates all phone numbers
		if (formElements.elements[i].value!="" && formElements.elements[i].name.match("phone") && !formElements.elements[i].name.match("code"))
		{
			errorMessage = errorMessage + phoneCheck(formName,formElements,i,focus);
		}		
	}
	if (errorMessage !="")
	{
		alert(errorMessage);
		valid = false;
	}
	return valid;
}

function requiredFieldCheck(formName,formElements,i,focus)
{
	var valid = true;  //---- false means the validation failed
	var foundRequired=false;	//---- true means the current form field matched a required field
	var errorMessage = "";	//---- stores the error message to present the user
	var focus = "";	//---- stores the field id to set focus to
	//var formElements=document.getElementById(formName);	//---- holds all the elements of the calling form
	var requiredFields=setRequiredFields(formName);	//---- holds the list of required fields
	//---- the following code checks to see if the current form field is blank
	if (formElements.elements[i].value == "")
	{
		foundRequired=false;
		
		//---- the folloing code loops through the list of form fields and stops looping if a match was found
		for(var j=0;(j<requiredFields.length)&&(foundRequired==false);j++)
		{
			
			//---- the folloing code attempts to find a match between the current form field and the list of required
			//		fields for the form
			if (formElements.elements[i].name == requiredFields[j])
			{
				foundRequired=true;
				valid=false;
				errorMessage = errorMessage + "Please enter your " + formElements.elements[i].title + ".\n";
				
				//---- the following code sets the focus to the first missing element
				if (focus == "")
				{
					focus = formElements.elements[i].name;
					document.forms[formName].elements[focus].focus();
				}
			}
		}
	}
	if (errorMessage !="")
	{
		return errorMessage;
	}
	else
		return "";
}

function setRequiredFields(formName)
{
	if (formName == "registerForm")
	{
		var requiredRegistration = new Array();
		requiredRegistration[0] = "firstName";
		requiredRegistration[1] = "lastName";
		requiredRegistration[2] = "street1";
		requiredRegistration[3] = "city";
		requiredRegistration[4] = "state";
		requiredRegistration[5] = "zip";
		requiredRegistration[6] = "school";
		requiredRegistration[7] = "code_coachingLevel";
		requiredRegistration[8] = "yearsCoaching";
		requiredRegistration[9] = "phonePrimary";
		requiredRegistration[10] = "email";
		requiredRegistration[11] = "password";
		requiredRegistration[12] = "code_phonePrimary";
		return requiredRegistration;
	}
}

function zipCodeCheck(formName,formElements,i,focus)
{
	var numericExpression = /^[0-9]+$/;
	if (document.forms[formName].elements[i].value.length!=5)	//---- verifies the user has entered 5-digits
	{
		if (focus=="")
		{
			focus = formElements.elements[i].name;
			document.forms[formName].elements[focus].focus();
		}
		return "Please enter your 5 digit Zip Code.\n";
	}
	else if(!formElements.elements[i].value.match(numericExpression))	// ---- Checks for invalid characters in the zip code
	{
		if (focus=="")
		{
			focus = formElements.elements[i].name;
			document.forms[formName].elements[focus].focus();
		}
		return "Invalid characters in your Zip Code. Please try again.\n";
	}
	else
		return "";
}

function emailCheck(formName,formElements,i,focus)
{
	// -------- Checks for a valid email address ----------------
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(document.forms[formName].elements[i].value)))
	{
		if (focus=="")
		{
			focus = formElements.elements[i].name;
			document.forms[formName].elements[focus].focus();
		}
		return "Please enter a valid Email Address.\n";
	}
	else
		return "";
}

function phoneCheck(formName,formElements,i,focus)
{
	var stripped = document.forms[formName].elements[i].value;
	if (stripped.search(/\d{0}\(\d{3}\)\d{3}\-\d{4}/)==-1)
	{
		return "Please enter a valid " + document.forms[formName].elements[i].title +".\n";
	}
	//---- Strip out acceptable non-numeric characters	
	var stripped = stripped.replace(/[\(\)\-]/g, '');
	if (isNaN(stripped)==true) 
	{
	   return "Please enter numeric values only in " + document.forms[formName].elements[i].title +".\n";
	}
	else if (!(stripped.length == 10))
	{
		return "Please enter an area code in" + document.forms[formName].elements[i].title + ".\n";
	}
	else
		return "";	
}

//---- Mask function ---------------------

function mask(str,textbox,loc,delim)
{
	if (!textbox.name.match("phone")&&!textbox.name.match("Fax"))
	{
		var locs = loc.split(',');
		for (var i = 0; i <= locs.length; i++)
		{
			for (var k = 0; k <= str.length; k++)
			{
				if (k == locs[i])
				{
					if (str.substring(k, k+1) != delim)
					{
						if (event.keyCode != 8)
						{ //backspace
							str = str.substring(0,k) + delim + str.substring(k,str.length);
						}
					}
				}
			}
		}
	}
	else
	{
		for (var j=0; j<str.length; j++)
		{
			if (event.keyCode != 8)
			{ //backspace
				if (str.length==1)
				{
					str = "(" + str;
				}
				else if (str.length==4 && str.substring(str.length-1,str.length)!=")")
				{
					str = str.substring(0,str.length) + ")" + str.substring(str.length,str.length+1);
				}
				else if (str.length==5 && str.substring(str.length-1,str.length)!=")")
				{
					str = str.substring(0,4) + ")" + str.substring(4,str.length+1);
				}				
				else if (str.length==8 && str.substring(str.length-1,str.length)!="-")
				{
					str = str.substring(0,str.length) + "-" + str.substring(str.length,str.length+1);
				}
				else if (str.length==9 && str.substring(str.length-1,str.length)!="-")
				{
					str = str.substring(0,8) + "-" + str.substring(8,str.length+1);
				}	
			}
			else if (event.keyCode==8 && (str.slice(str.length-1,str.length)=='-' || str.slice(str.length-1,str.length)==')' || str.slice(str.length-1,str.length)=='('))
			{
					str = str.slice(0,-1);
			}
		}
	}
	textbox.value = str
}
