//Validate Form

function regEmailIs_valid(text){
	var myRegExp = /\S+\@\S+\.\S+/i;
	return(myRegExp.test(text));
}
function Trim(str) {
	return RTrim(LTrim(str)); //  Remove trailing and leading blanks from our string
}
function RTrim(str) {
	// Remove trailing blanks from our string
	// IN: str - the string we want to RTrim
	// RETVAL: An RTrimmed string!
	var whitespace = new String(" \,\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		// We have a string with trailing blank(s)...
		var i = s.length - 1;       // Get length of string
		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}
	return s;
}
function LTrim(str)
	//	Remove leading blanks from our string
	{
	var whitespace = new String(" \,\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
	// We have a string with leading blank(s)...
		var j=0, i = s.length;
		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}
function capitalizeWords(thisField) {	
	string = thisField.value;
var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = string.toLowerCase();
	stringLen = tmpStr.length;
if (stringLen > 0) {
  for (i = 0; i < stringLen; i++) {
    if (i == 0)	{
      tmpChar = tmpStr.substring(0,1).toUpperCase();
      postString = tmpStr.substring(1,stringLen);
      tmpStr = tmpChar + postString;
    }
    else {
      tmpChar = tmpStr.substring(i,i+1);
      if (tmpChar == " " && i < (stringLen-1)) {
      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
      preString = tmpStr.substring(0,i+1);
      postString = tmpStr.substring(i+2,stringLen);
      tmpStr = preString + tmpChar + postString;
      }
    }
  }
}
thisField.value = tmpStr;
}
function purgePhone(str) { // Remove common phone extras
    var cleaned_string = ""
	var bad_chars = " -().,;:_\"/|\\'abcdefghijklmnopqrstuvwxyz"; // Characters to be removed
    for (var counter = 0; counter < str.length; counter++) { 
        current_char = str.charAt(counter)  
       if (bad_chars.indexOf(current_char) == -1) { 
           cleaned_string += current_char
       }
    }	
    return cleaned_string   // Return the numbers only
}

function formatPhone(thisField) {
	inValue = thisField.value;
	var vOutput = Trim(inValue);
	vOutput = vOutput.toLowerCase();
	vOutput = purgePhone(vOutput);
	if (vOutput.length == 10) {
		vOutput = ("(" + vOutput.substr(0,3) + ") " + vOutput.substr(3,3) + "-" + vOutput.substr(6,4));
		thisField.value = vOutput;
	} else {		
		thisField.value = "";			
	}				
}

function validate() {
			thisForm = eval(document.comments);
			var vPhone = thisForm.Phone.value;
			
			var validateOK = true;
			var validateMsg = "   Please correct the following to continue:\n\n";
			validateMsg += "--------------------------------------------------------------------------------\n\n";
			
			if(document.getElementById("FName").value == "") {
					validateOK = false;
					validateMsg += "    - You have not provided your first name\n";
					document.getElementById("FName").focus();
					alert(validateMsg);
					return false;
				}	
			if(document.getElementById("LName").value == "") {
					validateOK = false;
					validateMsg += "    - You have not provided your last name\n";
					document.getElementById("LName").focus();
					alert(validateMsg);
					return false;
				}						
			
			if(document.getElementById("Email").value == "") {
					validateOK = false;
					validateMsg += "    - You have not provided your email address\n";
					document.getElementById("Email").focus();
					alert(validateMsg);
					return false;
			} else {
				if (regEmailIs_valid(document.getElementById("Email").value) == false) {
					validateOK = false;
					validateMsg += "    - Your email address appears invalid\n";
					document.getElementById("Email").focus();
					alert(validateMsg);
					return false;
				}
			}
			if (vPhone.length != 14) {				
				alert("Your phone number is required as 10 digits");
				document.getElementById("Phone").value = "";
				document.getElementById("Phone").focus();
				return false;
			}
			if(document.getElementById("Comment").value == "") {
					validateOK = false;
					validateMsg += "    - Please enter your comments\n";
					document.getElementById("Comment").focus();
					alert(validateMsg);
					return false;
				}		
			validateMsg += "\n--------------------------------------------------------------------------------\n\n";			
			if (!validateOK) {
					alert(validateMsg);
				} else {
					thisForm.Name.value = thisForm.FName.value + " " + thisForm.LName.value;
					this.document.comments.submit();
					alert("Thank you - Your comments have been submitted!");			
				}
		}