/* JAVAMAIL RELATED FUNCTIONS ------ */

/**
 * Validates the mail form fields.
 * 
 * @param {Object} form the form containing the senderName, senderMail, senderComments fields.
 * @param {String} msgMandatoryName error message displayed when the sender name is missing.
 * @param {String} msgMandatoryMail error message displayed when the sender mail address is missing.
 * @param {String} msgMandatoryCmt error message displayed when the sender comment is missing.
 * @param {String} invalidMailAddress the message to display if the form contains an invalid sender mail addres.
 */
function ctrlJavaMailForm(form, msgMandatoryName, msgMandatoryMail, msgMandatoryCmt, invalidMailAddress) 
{
   var n = form.senderName.value;
   if (n == '') {
      alert (msgMandatoryName);
      form.senderName.focus();
      return false;
   }
   
   var a = form.senderMail.value;
   if (a == '') {
      alert (msgMandatoryMail);
      form.senderMail.focus();
      return false;
   }
   
   var c = form.senderComments.value;
   if (c == '') {
      alert (msgMandatoryCmt);
      form.senderComments.focus();
      return false;
   }
   
   if (!validateEMailAddress(form.senderMail, invalidMailAddress)) {
     return false;
   }
   
   return true;
}

/* MAILTO RELATED FUNCTIONS -------- */
/**
 * Sends a mail by mailto.
 * 
 * The form must obligatorily have the following fields:
 * senderName, senderMail, senderComments.
 * 
 * Using sase call into a form action attribute
 * action='javascript:sendEMail(
 * getRadioValue(document.emailFeedBackForm.mailto), 
 * "Feedback", 
 * document.emailFeedBackForm, 
 * "<fmt:message key="general.mail.invalid.address" />")' 
 * 
 * @param {String} mailto the destination adress.
 * @param {String} subject the subject of the mail.
 * @param {Object} form the form caontaining the senderName, senderMail, senderComments fields.
 * @param {String} invalidMailMsg the message to display if the form contains invalid values.
 */
function sendEMail(mailto, subject, form, invalidMailMsg)
{
   if (validateEMailAddress(form.senderMail, invalidMailMsg)){
      var body = "Sender Name:%0A" + form.senderName.value + "%0A%0A";
      body += "Sender Email:%0A" + form.senderMail.value + " %0A%0A";
      body += "Comments:%0A" + adaptEMailContent(form.senderComments.value) + "%0A%0A";
   
      window.location = "mailto:" + mailto + "?subject=" + subject + "&body="+body;
	}
}

/**
 * Validates a mail adress (syntax).
 * 
 * @param {Object} field the form field containing the email adress to be validated.
 * @param {String} invalidMailMsg the message to display if the email adress is not valid.
 */
function validateEMailAddress(field, invalidMailMsg)
{
	var mailAddressOK = false;
	// The following expression must be all on one line
   var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
   if (goodEmail){
      mailAddressOK = true;
   } else {
      mailAddressOK = false;
      alert(invalidMailMsg);
      field.focus();
      field.select();
   }
	return mailAddressOK;
}

/**
 * Adapts an email content to be sended by mailto by replacing
 * its \n by %0A (the \n equivalent of mailto).
 * 
 * @param {String} mailContent the mail content string to be adapted.
 */
function adaptEMailContent(mailContent)
{
	return mailContent.replace('\n',"%0A");
}

/**
 * Retrieves the selected radio value from a given field. 
 * 
 * @param {Object} field the field of radio buttons.
 */
function getRadioValue(field)
{
   for (var i=0; i < field.length; i++)
   {
      if (field[i].checked)
      {
         return field[i].value;
      }
   }
}
