/****************************************************************************************
* Trim leading and trailing spaces from input 
***************************************************************************************/
function myTrim(theField) {
	var i
	var front_trim
	var rear_trim
	var trimmed_str = ""
	var aString = theField.value
	
	
	if(aString == null){
		trimmed_str = ""
	} else if(aString.length == 0){
		trimmed_str = ""
	}
	else{
	
	for(i=0; i < aString.length; i++){
		if(aString.charAt(i) != " ")
			break
		} 
	front_trim = aString.substring(i, aString.length)
	
	i=front_trim.length
	while (i>0){
		if(front_trim.charAt(i-1) != " ")
			break
			
			i--
		}
	rear_trim = front_trim.substring(0, i)
	trimmed_str = rear_trim 
	}
	theField.value = trimmed_str
}


/************************************************************************************/
function invalid_email(myString){
	//Verify email to ensure it has the correct email convention
	var emailPat = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$"
	var matchArray = myString.value.match(emailPat);
	
	if(matchArray == null)
		return true
	else 
		return false
	}
/**************************************************************************************/	
function is_invalid(myString){
	
	var i
	var num_spaces = 0
	
	// Verify if there is anything entered
	
	if(myString == null){
		return true
	}	
	
	else if(myString.length == 0){
		return true
		}
		
	//Ensure the value entered is not all spaces	 
	for(i=0; i < myString.length; i++){
		
		if(myString.charAt(i) == " ")
			num_spaces++
		} 
		
		if(myString.length == num_spaces)
			return true
	
return false
}

/*************************************************************************************
 * Validates the input. Calls on helper methods to check if there are non space 
 *characters entered and notifies the user to enter the appropriate information
 ************************************************************************************/
 
function validate(theForm) {

	//Validate company/organization
	if (is_invalid(theForm.company.value)) {
		alert("Please enter your company/organization name")	
		myTrim(theForm.company)
		theForm.company.focus()
		return false
	}
	else{
		myTrim(theForm.company)
		}
	//Validate first name
	if (is_invalid(theForm.fname.value)) {
		alert("Please enter your first name")
		myTrim(theForm.fname)
		theForm.fname.focus()
		return false
	}else{
		myTrim(theForm.fname)
		}
	//Validate last name
	if (is_invalid(theForm.lname.value)) {
		alert("Please enter your last name")
		myTrim(theForm.lname)
		theForm.lname.focus()
		return false
	}else{
		myTrim(theForm.lname)
		}
	//Validate title
	if (is_invalid(theForm.title.value)) {
		alert("Please enter your title")
		myTrim(theForm.title)
		theForm.title.focus()
		return false
	}else{
		myTrim(theForm.title)
		}
  	// check address 1
	if (is_invalid(theForm.address1.value)) {
		alert("Please enter your address")
		myTrim(theForm.address1)
		theForm.address1.focus()
		return false
	}else{
		myTrim(theForm.address1)
		}
	// check address 2 (optional)
	if (is_invalid(theForm.address2.value)) {
		theForm.address2.value = "N.A."
	}else{
		myTrim(theForm.address2)
		}
	
	// check city
	if (is_invalid(theForm.city.value)) {
		alert("Please enter your city")
		myTrim(theForm.city)
		theForm.city.focus()
		return false
	}else{
		myTrim(theForm.city)
		}
	// check province
	if (is_invalid(theForm.province.value)) {
		alert("Please enter your province")
		myTrim(theForm.province)
		theForm.province.focus()
		return false
	}else{
		myTrim(theForm.province)
		}
	// check postal code
	if (is_invalid(theForm.pobox.value)) {
		alert("Please enter your postal code")
		myTrim(theForm.pobox)
		theForm.pobox.focus()
		return false
	}else{
		myTrim(theForm.pobox)
		}
	// check phone number
	if (is_invalid(theForm.tel.value)) {
		alert("Please enter your phone number")
		myTrim(theForm.tel)
		theForm.tel.focus()
		return false
	}else{
		myTrim(theForm.tel)
		}
	// check fax number
	if (is_invalid(theForm.fax.value)) {
		alert("Please enter your fax number")
		myTrim(theForm.fax)
		theForm.fax.focus()
		return false
	}else{
		myTrim(theForm.fax)
		}
		   
	// check email address
	if (is_invalid(theForm.email) || invalid_email(theForm.email)) {
		alert("Email address missing or incorrect.  Please re-enter the email address")
		myTrim(theForm.email)
		theForm.email.focus()
		return false;
	}else{
		myTrim(theForm.email)
		}

}// end method

