// Trim off spaces at the beginning and end.
function trim( inputString )
{
	var trimmedString;
	var ch;

	if (typeof inputString != "string")
	{
		return inputString;
	}

	trimmedString = inputString;
	ch = trimmedString.substring(0, 1);

	// Check for spaces at the beginning of the string: \t\n\r
	while ((ch == " ") || (ch == "\n") || (ch == "\n") || (ch == "\r") || (ch == "\t"))
	{
		trimmedString = trimmedString.substring(1, trimmedString.length);
		ch = trimmedString.substring(0, 1);
	}

	// Check for spaces at the end of the string
	ch = trimmedString.substring(trimmedString.length-1, trimmedString.length);
	while (ch == " ")
	{
		trimmedString = trimmedString.substring(0, trimmedString.length-1);
		ch = trimmedString.substring(trimmedString.length-1, trimmedString.length);
	}

	return trimmedString;

} // trim


function validateZIP(zipField)
{
	var valid = "0123456789-";
	var hyphenCount = 0;

	if (zipField.length!=5 && zipField.length!=10)
	{
		alert("Please enter your 5 digit (12345) or 5 digit+4 (12345-6789) zip code.");
		return false;
	}

	for (var i=0; i < zipField.length; i++)
	{
		temp = "" + zipField.substring(i, i+1);
		if (temp == "-") hyphenCount++;
		if (valid.indexOf(temp) == "-1")
		{
			alert("Invalid characters in your zip code. Please try again.");
			return false;
		}
		if ((hyphenCount > 1) || ((zipField.length==10) && ""+zipField.charAt(5)!="-"))
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
			return false;
	   	}
	}
	return true;

}  // validateZIP


function validateEmail(emailField)
{
	// Legal email characters per RFC2821 and RFC2822
  	var allowableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-+@";
  	var firstAt = 0;

    // Check for validity of email format, allowing for one letter domain names
    // e.g. a@b.com; and for @ characters anywhere after the first character.
    if ((emailField.indexOf(".") > 2) && ((firstAt = emailField.indexOf("@")) > 0))
    {
    	// See if there are more than one @
		if (emailField.indexOf("@", firstAt+1) != -1)
		{
			alert("Email address has more than one @.");
			return false;
		}

    	// See if there are contiguous periods
		if (emailField.indexOf("..") != -1)
		{
			alert("Email address has contiguous periods.");
			return false;
		}

  		var ch;
    	var i, j;
		for (i = 0;  i < emailField.length;  i++)
		{
			ch = emailField.charAt(i);
			for (j = 0; j < allowableChars.length; j++)
			{
				if (ch == allowableChars.charAt(j))
						break;
				else
				{
					if (j < (allowableChars.length - 1)) continue;
					alert("Email address contains invalid character(s).");
					return false;
				}
			}
		}

		return true;
	}

	alert( "Please enter a valid email address such as yourname@companyname.com." );
	return false;

}  // validateEmail



function validatePhoneNumber(usPhoneNumber)
{
    if(usPhoneNumber.search(/^(\d\d\d)\-?(\d\d\d)\-?(\d\d\d\d)$/)==-1)    {
		alert( "Please enter a valid phone number. Valid phone numbers should only include numbers such as 123-456-7890." );
    	return false;
    }

    return true;

} // validatePhoneNumber


function validateAssessment()
{
    var thisObject;
    var thisField;


	// employ 10 or more: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("employ-10[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select an answer for all survey questions");
		return false;
	}


	// employ 10 or more: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("employ-10[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select an answer for all survey questions");
		return false;
	}



	// employ 10 or more: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("annu-pay[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select an answer for all survey questions");
		return false;
	}



	// employ 10 or more: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("sui[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select an answer for all survey questions");
		return false;
	}



	// employ 10 or more: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("low-unem[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select an answer for all survey questions");
		return false;
	}



	// employ 10 or more: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("use-advice[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select an answer for all survey questions");
		return false;
	}




	// Disable submit and return true
	thisObject = document.getElementById( "submit-form" );
	thisObject.disabled = true;

	return true;

}  // validateAssessment


function validateContact()
{
    var thisObject;
    var thisField;
	
	// Name - Required
    thisObject = document.getElementById( "name" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your name.");
    	thisObject.focus();
    	return false;
    }
	
	// Company - Required
    thisObject = document.getElementById( "company" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your company.");
    	thisObject.focus();
    	return false;
    }



	//Email - Required
    thisObject = document.getElementById( "email" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your email address.");
    	thisObject.focus();
    	return false;
    }

    if (validateEmail(thisField)==false)
    {
    	thisObject.focus();
    	return false;
    }
	
	//Validate  Phone
    thisObject = document.getElementById( "phone" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			return false;
		}
	}
	


	// Disable submit and return true
	thisObject = document.getElementById( "form-submit" );
	thisObject.disabled = true;

	return true;
	

}  // validateContactUs


function validateWorkshop()
{
    var thisObject;
    var thisField;

	//Agency Name - Required
    thisObject = document.getElementById( "agencyName" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your agency's name.");
    	thisObject.focus();
    	return false;
    }

	//Address - Required
    thisObject = document.getElementById( "address1" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your address.");
    	thisObject.focus();
    	return false;
    }

	//City - Required
    thisObject = document.getElementById( "city" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your city.");
    	thisObject.focus();
    	return false;
    }

	//Zip - Required
    thisObject = document.getElementById( "zip" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your zip.");
    	thisObject.focus();
    	return false;
    }
	
	//Phone - Required
    thisObject = document.getElementById( "telephone" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your telephone number.");
    	thisObject.focus();
    	return false;
    }
	if (validatePhoneNumber(thisField)==false)
	{
		thisObject.focus();
		return false;
	}
	
	//Validate Fax
    thisObject = document.getElementById( "fax" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			return false;
		}
	}
	
	
	//Email - Required
    thisObject = document.getElementById( "email" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your email address.");
    	thisObject.focus();
    	return false;
    }

    if (validateEmail(thisField)==false)
    {
    	thisObject.focus();
    	return false;
    }
	
	// is a member? : At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("member[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select whether you are a member of 501(c) Agencies Trust or not.");
		return false;
	}

	
	
	// Disable submit and return true
	thisObject = document.getElementById( "form-submit" );
	thisObject.disabled = true;

	return true;

}  // validateWorkshop

