function validateEmailForm(theForm) {
  var reason = "";

  reason += validateName(theForm.from_name);
  reason += validateEmail(theForm.from_email);
  reason += validateSubject(theForm.subject);
  reason += validateMessage(theForm.message);
  
  if (reason != "") {
    alert("Correction:\n" + reason);
    return false;
  }
  return true;
}

function validateName(fld) {
    var error = "";

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter your full name.\n";
        fld.style.background = 'yellow';
    } else if ((fld.value.length > 25)) {
        error = "The 'Fullname' is too long. Maximum 25 characters. \n";
        fld.style.background = 'Yellow';
    }else {
        fld.style.background = 'White';
    }
   return error;
}

function validateEmail(fld) {
   var error = "";
   var len = fld.value.length;         // get length of the email string

   if (fld.value == ""){        //if email field is empty - warning displays and return false
       //if (fld.name == "to"){error = "The 'To' field is empty. Please enter the email address.\n"};
       //if (fld.name == "from"){error = "The 'From' field is empty! Please enter the email address.\n"};
       error = "Please enter your email address.\n"
       fld.style.background = 'yellow';
   }else if((fld.value.charAt(0)=='@')||(fld.value.charAt(0)=='.')){           //'@' and '.' must NOT be at the beginning
       //if (fld.name == "to"){error = "Invalid email address in the 'To' field: the @ and/or the '.' must not be at the beginning.\n"};
       //if (fld.name == "from"){error = "Invalid email address in the 'From' field: the @ and/or the '.' must not be at the beginning.\n"};
       error = "Invalid email address: the @ and/or the '.' must not be at the beginning.\n"
       fld.style.background = 'Yellow';
   }else if((fld.value.charAt(len-1)=='@')||(fld.value.charAt(len-1)=='.')){   // @ and '.' must NOT be at the end of string.
       //if (fld.name == "to"){error = "Invalid email address in the 'To' field: the @ and/or '.' must not be at the end.\n"};
       //if (fld.name == "from"){error = "Invalid email address in the 'From' field: the @ and/or '.' must not be at the end.\n"};
       error = "Invalid email address: the @ and/or '.' must not be at the end.\n"
       fld.style.background = 'Yellow';
   }else{
       //count number of @ occurrences and number of dot occurrences
       var count = 0;
       var dotcount = 0;
       for (i=0; i< fld.value.length; i++)
       {
        if(fld.value.charAt(i) == '@')
               count++;
        if(fld.value.charAt(i) == '.')
               dotcount++;
       }
       //if @ or dot occurs not once display warning and return false
       if((count !=1)||(dotcount !=1)){
           if (fld.name == "to"){error = "Invalid email in the 'To' field. The @ and/or '.' must occur once, example: myemail@mysite.com\n"};
           if (fld.name == "from"){error = "Invalid email in the 'From' field. The @ and/or '.' must occur once, example: myemail@mysite.com\n"};
           fld.style.background = 'Yellow';
      }
   }
   return error;
}

function validateSubject(fld) {
    var error = "";

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter a subject.\n";
        fld.style.background = 'yellow';
    } else if ((fld.value.length > 65)) {
        error = "The subject is too long. Maximum 65 characters. \n";
        fld.style.background = 'Yellow';
    }else {
        fld.style.background = 'White';
    }
   return error;
}

function validateMessage(fld) {
    var error = ""

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter a message.\n";
        fld.style.background = 'yellow';
    } else if ((fld.value.length > 600)) {
        error = "The message is too long! Maximum 600 characters. \n";
        fld.style.background = 'Yellow';
    }else {
        fld.style.background = 'White';
    }
   return error;
}

