// 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)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var address = document.forms[form_id].elements[email].value;
   if(reg.test(emailField) == false) {
      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 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


