//Check to see if a radio button has been selected out of a group
function check(theField)
{
	for(x = 0; x < theField.length; x++)
	{
		if (theField[x].checked == true)
		{
			return true;
		}
	}
	
	return false;
}

//Highlight the invalid fields
function highlightInvalid(theField)
{
	theField.parentNode.id = 'invalid';
}

//Un-Highlight valid fields
function unhighlightInvalid(theField)
{
	theField.parentNode.id = '';
}

//Validate form
function validate(formName)
{
	var theForm = document.getElementById(formName);
	var invalid = false;
	
	//REGEX for field validation
	var phone = /^(\d){3}(\-){1}(\d){3}(\-){1}(\d){4}$/;
	var email = /\w+@[a-zA-Z0-9_]+?\.[a-zA-Z]{2,6}/;
	var zip = /^[0-9]{5}$/;
	
	for(i = 0; i < theForm.length; i++)
	{
		
		//Validate required fields
		if (theForm[i].className.indexOf('required') != -1)
		{
			
			//Validate fields
			if (theForm[i].value == '')
			{
				invalid = true;
				highlightInvalid(theForm[i]);
			}else
			{
				unhighlightInvalid(theForm[i]);
			}
			
			//Check for vaild email address
			if (theForm[i].className.indexOf('email') != -1)
			{
				if (!email.test(theForm[i].value)) {
					invalid = true;
					highlightInvalid(theForm[i]);
				}else
				{
					unhighlightInvalid(theForm[i]);
				}
			}
			
			//Check for valid phone number
			if (theForm[i].className.indexOf('phone') != -1)
			{
				if (!phone.test(theForm[i].value)) {
					invalid = true;
					highlightInvalid(theForm[i]);
				}else
				{
					unhighlightInvalid(theForm[i]);
				}
			}
			
			//Check for valid zip code
			if (theForm[i].className.indexOf('zip') != -1)
			{
				if (!zip.test(theForm[i].value)) {
					invalid = true;
					highlightInvalid(theForm[i]);
				}else
				{
					unhighlightInvalid(theForm[i]);
				}
			}
			
			//Validate howheard field if Other is selected
			if (theForm[i].className.indexOf('howheard') != -1)
			{
				if (theForm[i].value == 'Other' && theForm.other.value == '') {
					invalid = true;
					highlightInvalid(theForm[i]);
					highlightInvalid(theForm.other);
				}else
				{
					unhighlightInvalid(theForm[i]);
					unhighlightInvalid(theForm.other);
				}
			}
			
		//Validate non-required fields that are filled out
		}else if(theForm[i].value != '')
		{
			//Check for valid zip code
			if (theForm[i].className.indexOf('zip') != -1)
			{
				if (!zip.test(theForm[i].value)) {
					invalid = true;
					highlightInvalid(theForm[i]);
				}else
				{
					unhighlightInvalid(theForm[i]);
				}
			}
			
			//Check for valid phone number
			if (theForm[i].className.indexOf('phone') != -1)
			{
				if (!phone.test(theForm[i].value)) {
					invalid = true;
					highlightInvalid(theForm[i]);
				}else
				{
					unhighlightInvalid(theForm[i]);
				}
			}
			
			//Validate tour info is filled out if taketour is checked
			if (theForm[i].className.indexOf('taketour') != -1 && theForm[i].checked == true)
			{
				var tourday = check(theForm.tourday);
				var tourtime = check(theForm.tourtime);
				var contactmethod = check(theForm.contactmethod);
				
				if (tourday == false || tourtime == false || contactmethod == false)
				{
					invalid = true;
					document.getElementById('tourbox').className = 'invalid';
				}else
				{
					document.getElementById('tourbox').className = 'tourbox';
				}
			}
		}
	}
	
	if (invalid == true)
	{
		alert('This form is invalid. Please check the highlighted fields for accuracy.');
		return;
	}
	
	theForm.submit();
}