// JavaScript Document
function isEmail(emailString) {

if (emailString =="") {return false} //empty string

// next section *********************************************** 

// these characters are not valid in an email address address
var invalidChars= " /:;," 

// take each character in turn in the string invaldChars
for (i=0; i<invalidChars.length; i++) {
   var badChar = invalidChars.charAt(i);

   // for each character look for it in the string emailString
   // indexOf() will be the postion in the string if found
   // (remember, if not found, badChar = -1) 
   // return false if invalid char found (ie > -1)
   if(emailString.indexOf(badChar,0) > -1) {return false}
} // close the for loop

// next section *********************************************** 

// looks for "@" from string postion 1
// there must be at least one character before the "@"
// thus start search at 1 rather than 0

var atPos = emailString.indexOf("@",1);
if (atPos == -1) {return false} // "@" not found if atPos=-1

// look for a second '@' after atPos (from atPos+1)
if (emailString.indexOf("@",atPos+1) > -1) {return false}
// next section *********************************************** 

// looks for the last period (.) in the string emailString
var periodPos = emailString.lastIndexOf(".");
if (periodPos == -1) {return false} // "." not found if periodPos=-1
// next section *********************************************** 

// looks for at least two Characters following the postion of "."
// periodPos holds the position of the last '.'
//add 3 to periodPos and see if it is greater than the total string length

if (periodPos+3 > emailString.length) {return false}

// next section *********************************************** 

// return true if the above tests fail
return true
} // close the function


function validateInput(thisForm) {

if (thisForm.name.value == "") {
   alert("Please enter a value for the \"Your name\" field.");
   thisForm.name.focus();  //gives that field focus
   return (false);
}
if (thisForm.surname.value == "") {
   alert("Please enter a value for the \"Your surname\" field.");
   thisForm.surname.focus();  //gives that field focus
   return (false);
}
if (!isEmail(thisForm.email.value)) { // ! means NOT
   alert("Please enter a valid email address.");
   thisForm.email.focus();  //gives that field focus
   thisForm.email.select();  //selects the field
   return (false);
}
return true;
}
