var messagestart = "The following error(s) occurred:";

function trim(inputString) 
{
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.

   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user

} // Ends the "trim" function


//Check to validate the email address
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

//Create the error message
function checkMessage(message)
{
	// is there an error message?   		
	if ( message.length > 0 ) 
	{ 
		   message = messagestart + "\n\n" + message;	
		   alert( message ); // display error message 
	       return false; // return false, not ok to process 
    }
	else 
	{
		   return true; // no error message to display, return ok to process 
	} 
}

//Check the mail form for errors
function checkMail()
{
	// create error message string variable, with nothing in it
	var message = ""; 
	var trimmed_string;
	var form = "";      //The form return of true or false

	//Checking for empty first name field
	trimmed_string = trim(document.mailform.firstname.value);
	if (trimmed_string.length == 0) 
	{
	        message = message + "First Name is invalid\n";
			document.mailform.firstname.focus();
	}

	//Checking for empty last name field
	trimmed_string = trim(document.mailform.surname.value);
	if (trimmed_string.length == 0) 
	{
	        message = message + "Surname is invalid\n";
			document.mailform.surname.focus();
	}

	//Check the email address
	if (!isEmailAddr(document.mailform.email.value))
	{
		message = message + "Email Address is invalid\n";		
		document.mailform.email.focus();
	}

	//check for empty subject
	if(document.mailform.subject.value == 0)
	{
		message = message + "Subject is invalid\n";
		document.mailform.subject.focus();
	}


	//Checking for empty comments field
	trimmed_string = trim(document.mailform.comments.value);
	if (trimmed_string.length == 0) 
	{
	        message = message + "Comments is invalid\n";
			document.mailform.comments.focus();
	}


	form = checkMessage(message);
return form;
}



