//initial registration
function validateForm(form)
{
	errorStr = "";
	error = false;
	
	if(form.firstname.value == ""){
		errorStr += "Firstname\n";
		error = true;
	}
	if(form.lastname.value == ""){
		errorStr += "Lastname\n";
		error = true;
	}
	if(! isValidEmailAddress(form.email.value)){
		errorStr += "Email\n";
		error = true;
	}
	if(form.address.value == ""){
		errorStr += "Address\n";
		error = true;
	}
	if(form.postcode.value == ""){
		errorStr += "Postcode\n";
		error = true;
	}
	if(! validateDOB(form.dob.value)){
		errorStr += "Date of Birth\n";
		error = true;
	}
	if(form.nationality.value == ""){
		errorStr += "Nationality\n";
		error = true;
	}
	if(form.hphone.value == ""){
		errorStr += "Home Phone\n";
		error = true;
	}
	if(form.cv.value == ""){
		errorStr += "CV\n";
		error = true;
	}
	
	if(error == true){
		completedStr = "The following fields are empty or incorrect.\n\n" + errorStr;
		alert(completedStr);
		return false;
	}else{
		return true;
	}
}

function isValidEmailAddress(emailAddress)
{
   /* Check for empty address or invalid characters */
   if (emailAddress == "" || hasInvalidChar(emailAddress))
   {
      return false;
   }

   /* check for presence of the @ character */
   var atPos = emailAddress.indexOf("@", 1)
   if (atPos == -1)
   {
      return false;
   }
   
   /* Check that there are no more @ characters */
   if (emailAddress.indexOf("@", atPos + 1) > -1)
   {
      return false;
   }

   /* Check for the presence of a dot somewhere after @ */
   var dotPos = emailAddress.indexOf(".", atPos + 1);
   if (dotPos == -1)
   {
      return false;
   }

   /* Check for presence of two or more characters after last dot */
   var lastDotPos = emailAddress.lastIndexOf(".");
   if (lastDotPos + 3 >  emailAddress.length)
   {
      return false;
   }
   return true;
}

function hasInvalidChar(emailAddress)
{
   var invalidChars = "/;:,"; // this list is not complete

   for (var k = 0; k < invalidChars.length; k++)
   {
      var ch = invalidChars.charAt(k);
      if (emailAddress.indexOf(ch) > -1)
      {
         return true;
      }
   }
   return false;
}

function validateDOB(dob)
{
	var dobRegxp = /^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)([0-9]){4}$/;
	return dobRegxp.test(dob);
}
