//-----------------------------------------------------------------------------
//FORM: email-listFilter
//-----------------------------------------------------------------------------

/**
 * submitFilter
 *    Run code to filter email list by year
 *
 * @return void
 */

function submitFilter()
{
   document.listFilter.display = "email_list";
   document.listFilter.submit();
}

//-----------------------------------------------------------------------------
//FORM: file_manager - fileManager
//-----------------------------------------------------------------------------
/**
 * initialize
 *    initialize the form
 *
 * @return void
 * @form:  file_manager - fileManager
 */

function initialize()
{
   toggleItems();
}


function initializeEntryForm()
{
   document.getElementById("errorEmail").style.display = "none";
   document.getElementById("error").style.display = "none";
}

/**
 * submitFilter
 *    Run code to filter email list by year
 *
 * @return void
 * @form:  file_manager - fileManager
 */

function executeAction()
{   
   for(i = 0; i < 4; i++)
      if(document.fileManager.file_options[i].checked)
         document.fileManager.op.value = document.fileManager.file_options[i].value;
   document.fileManager.submit();
}
   
/**
 * toggleItems
 *    Change html element status based on what action will be taken.
 *    if file_options = Export data to CSV"
 *       -hide existing file entry
 *       -show new file entry
 *       -change action button to "Export data"
 *    if file_options = Import data from CSV"
 *       -show existing file entry
 *       -hide new file entry
 *       -change action button to "Import data"
 *    if file_options = Create email list text file"
 *       -hide existing file entry
 *       -show new file entry
 *       -change action button to "Build list"
 *
 * @return void
 * @form:  file_manager - fileManager
 */

function toggleItems()
{
   if (document.fileManager.file_options[0].checked)
   {
      document.getElementById("new_file").style.display = "inline";
      document.getElementById("existing_file").style.display = "none";
      document.fileManager.doAction.value = "Export data";
   }
   else if (document.fileManager.file_options[1].checked)
   {
      document.getElementById("new_file").style.display = "none";
      document.getElementById("existing_file").style.display = "inline";
      document.fileManager.doAction.value = "Import data";
   }
   else if (document.fileManager.file_options[2].checked)
   {
      document.getElementById("new_file").style.display = "inline";
      document.getElementById("existing_file").style.display = "none";
      document.fileManager.doAction.value = "Build list";
   }
   else if (document.fileManager.file_options[3].checked)
   {
      document.getElementById("new_file").style.display = "none";
      document.getElementById("existing_file").style.display = "none";
      document.fileManager.doAction.value = "Build Page";
   }
}
   
//-----------------------------------------------------------------------------
//FORM: results - fileMgrAck
//-----------------------------------------------------------------------------

/**
 * gotoFileManager
 *    submit the data - sending the user back to the file_manager operation
 *
 * @return void
 */

function gotoFileManager()
{
   document.fileMgrAck.submit();
}

   
//-----------------------------------------------------------------------------
//FORM: new_entry - entry
//-----------------------------------------------------------------------------

/**
 * checkEntry
 *    submit the data - sending the user back to the file_manager operation
 *
 * @return void
 */

function checkEntry()
{
   var invalid = false;
   
   invalid = isInvalid("first_name");
   invalid = isInvalid("last_name");      
   invalid = isInvalid("year_final") ;
   invalid = isInvalid("email");
   
   if (!invalid)
      document.entry.submit();

}
function isInvalid(id)
{
   if(document.getElementById(id).value == "")
   {
      document.getElementById(id).style.backgroundColor = "red";
      document.getElementById("error").style.display = "block";
      return true;
   }
   if (id == "email")
   {
      if (!isValidEmailAddress(id))
      {
         document.getElementById(id).style.backgroundColor = "red";
         document.getElementById("errorEmail").style.display = "inline";
         document.getElementById("error").style.display = "none";
         return true
      }
   }
   document.getElementById(id).style.backgroundColor = "white";
   document.getElementById("errorEmail").style.display = "none";
   document.getElementById("error").style.display = "none";
   return false;
}


   
//-----------------------------------------------------------------------------
//FORM: contact_form - contact
//-----------------------------------------------------------------------------

/**
 * checkHeaders
 *    submit the data - sending the user back to the file_manager operation
 *
 * @return void
 */

function checkHeaders()
{
   var invalid = false;
   
//   invalid = document.getElementById(id).value;
   invalid = isInvalid("subject");
   invalid = isInvalid("message");
   invalid = isInvalid("from");      
   
//   if (!invalid)
      document.contact.submit();

}
function isInvalid(id)
{
   if(document.getElementById(id).value == "" ||
     (id == "message" && 
      document.getElementById(id).value == "enter message text..."))
   {
      document.getElementById(id).style.backgroundColor = "red";
      document.getElementById("error").style.display = "block";
      return true;
   }
   if (id == "from")
   {
      if (!isValidEmailAddress(id))
      {
         document.getElementById(id).style.backgroundColor = "red";
         document.getElementById("errorEmail").style.display = "inline";
         document.getElementById("error").style.display = "none";
         return true
      }
   }
   document.getElementById(id).style.backgroundColor = "white";
   document.getElementById("errorEmail").style.display = "none";
   document.getElementById("error").style.display = "none";
   return false;
}      


function isValidEmailAddress(id) 
{
   value = document.getElementById(id).value;
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))
      return true
   return false
}
   

