/*
 * validation(form)
 *
 * This function validates whether or not the contact form meets
 * the required input standards
 *
 */
function validation(form)
{
  // Initialize error message, isError
  var errorMessage = "";
  var isError = false;

  // Remove all HTML tags from user input
  format(form.title);
  format(form.fname);
  format(form.lname);	
  format(form.cname);
  format(form.email);
  format(form.dphone);
  format(form.ephone);
  format(form.comments);

  // Validate that user has entered first name
  if (form.fname.value.length < 1) {
    errorMessage += "Please enter your first name \n";
    isError = true;
  }

  // Validate that user has entered last name
  if (form.lname.value.length < 1) {
    errorMessage += "Please enter your last name \n";
    isError = true;
  }

  // Validate that user has entered email
  if (form.email.value.length < 1) {
    errorMessage += "Please enter your email address \n";
    isError = true;
  }
  // Validate that entered email is in valid email format
  else if (!isValidEmail(form.email.value)) {
    errorMessage += "Email must be valid format 'example@test.com' \n";
    isError = true;
  }

  // Validate that user has selected area of interest
  if (form.area.selectedIndex == 0) {
    errorMessage += "Please choose area of interest \n";
    isError = true;	  
  }

  form.comments.value = trim(form.comments.value);
  // Validate that user has entered comments
  if (form.comments.value < 1) {
    errorMessage += "Please enter your question/comments \n";
    isError = true;
  }

  // If there is an error display error message and return that 
  // page is invalid, otherwise return that page is valid
  if (isError) {
    alert(errorMessage);
    return false;
  }
  else
    return true;
  }

/* removeTags(theWord)
 *
 * This function checks whether email is in correct 'x@y.z' format
 *
 */
function isValidEmail(email)
{
  return ((email.indexOf(".") > 2) && (email.indexOf("@") > 0));
}

/*
 * removeTags(theWord)
 *
 * This function removes all HTML tags from a given string (Word)
 *
 */
function removeTags(theWord) {
  /* 
   * Sets begTag to beginning of tag, the first '<'
   * Sets endTag to end of tag, the first '>'
   * Sets preTag to all text before the tag
   * Sets length to the length of theWord
   */
  begTag = theWord.indexOf("<");
  endTag = theWord.indexOf(">");
  preTag = theWord.substring(0, begTag);
  len = theWord.length;

  // If there is no ending bracket, set the end to the front bracket
  if(endTag == -1)
    endTag = begTag;

  // Sets postTag to all text after the tag, sets theWord to all text
  // minus the tags and then checks for another tag
  postTag = theWord.substring((endTag+1), len);
  theWord = preTag + postTag;
  tagCheck = theWord.indexOf("<");

  // If another tag exists recursively remove tags
  if(tagCheck != -1)
    theWord = removeTags(theWord);
	
  // Return the new word sans tag
  return theWord;
}

/*
 * noHTML(theForm)
 *
 * This function accepts an input form as input, and
 * recursively removes any HTML tags if they exits.
 *
 * Currently this function returns a boolean. I haven't set up this
 * this property but could easily extend it to return whether 
 * or not the form contained any html tags
 *
 */
function noHTML(theForm) {
  theForm.value = removeTags(theForm.value);
  return true;
}

/*
 * trim(theWord)
 *
 * This function takes a string as input and outputs the string minus
 * any leading or trailing white space
 *
 */
function trim(theWord) {
  theWord = theWord.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
  return theWord;
}

/*
 * format(theForm)
 *
 * This functio ntake a form as input, removes HTML from it, and trims
 * away white space by use of sub-functions noHTML and trim
 *
 * Sub-functions must be called noHTML first, trim second 
 *
 */
function format(theForm) {
  noHTML(theForm);
  trim(theForm.value);	
}

/*
 * subjectSelect(ddl)
 *
 * This function takes a drop down list as input and depending on 
 * the selected item creates a drop down list in a table row below
 *
 * This function is tailored specifically for this application
 *
 */
function subjectSelect(ddl) {

  // trow is the row where the new ddl will go
  var trow = document.getElementById("subject");
  // index is the selected index
  var index = ddl.selectedIndex;

  // If we so desired we could get the selected index's text value
  //var selValue = ddl.options[index].value;
	
  // Control flow: set new ddl depending on user's selection
  if (index == 0)
  {
    trow.innerHTML = "";
  }
  if (index == 1)
  {
    trow.innerHTML = "<select name='subjectList'>" +
	             "<option>Cards & Card Programs</option>" +
	             "<option>Checking & Savings</option>" +
	             "<option>Home Buying</option>" +
	             "<option>International Services</option>" +
	             "<option>Loans & Lines of Credit</option>" +
	             "<option>Online Banking</option>" +
	             "<option>Retirement Planning</option>" +
	             "<option>Other</option>" +
		     "</select>";
  }
  if (index == 2)
  {
    trow.innerHTML = "<select name='subjectList'>" +
	             "<option>Cards & Card Programs</option>" +
	             "<option>Cash Management</option>" +
				 "<option>Checking & Savings</option>" +
	             "<option>Commercial Real Estate</option>" +
	             "<option>International Services</option>" +
	             "<option>Loans & Lines of Credit</option>" +
	             "<option>Online Banking</option>" +
	             "<option>Retirement Planning</option>" +
	             "<option>Other</option>" +
		     "</select>";
  }
  if (index == 3 || index == 4)
  {
    trow.innerHTML = "<select name='subjectList' id='subjectList'>" +
	             "<option>General Information</option>" +
		     "</select>";
  }
  if (index == 5)
  {
    trow.innerHTML = "<select name='subjectList'>" +
	             "<option>Community Involvement</option>" +
	             "<option>Employee Opportunities</option>" +
	             "<option>Media Inquiries</option>" +
	             "<option>Other</option>" +
		     "</select>";
  }
}
