/*******************************/

var debugMode = false;

function debug(s) {
  if (debugMode) {
    alert(s)
  }
}

/*******************************/

function NoCheck(strng) {

/* No check needed. */

  debug("In NoCheck, strng='" + strng + "'");

  var error="";

  debug("NoCheck: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckName(strng) {

/* Make sure a Name was entered. */

  debug("In CheckName, strng='" + strng + "'");

  var error="";
  if (strng == "") {
    error = "You didn't enter a Name.\n";
  }

  debug("CheckName: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckContact_Highschool(phone1, phone2, email) {

/* Make sure at least one piece of contact information was entered (i.e. at least one of phone1, phone2 or email must be non-null). */

  debug("In CheckContact_Highschool, phone1='" + phone1 + "', phone2='" + phone2 + "', email='" + email + "'");

  var error="";
  if ((phone1 + phone2 + email) == "") {
    error = "You must enter at least one piece of contact information.\n";
  }

  debug("CheckContact_Highschool: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckContact_RunEvent(phone1, phone2, email) {

/* Make sure at least one piece of contact information was entered (i.e. at least one of phone1, phone2 or email must be non-null). */

  debug("In CheckContact_RunEvent, phone1='" + phone1 + "', phone2='" + phone2 + "', email='" + email + "'");

  var error="";
  if ((phone1 + phone2 + email) == "") {
    error = "You must enter at least one piece of contact information.\n";
  }

  debug("CheckContact_RunEvent: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckContact_ContactUs(phone1, phone2, phone3, email) {

/* Make sure at least one piece of contact information was entered (i.e. at least one of phone1, phone2, phone3, or email must be non-null). */

  debug("In CheckContact_ContactUs, phone1='" + phone1 + "', phone2='" + phone2 + "', email='" + email + "'");

  var error="";
  if ((phone1 + phone2 + phone3 + email) == "") {
    error = "You must enter at least one piece of contact information.\n";
  }

  debug("CheckContact_ContactUs: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckEmailPresent(strng) {

/* Make sure an E-mail Address was entered. */

  debug("In CheckEmailPresent, strng='" + strng + "'");

  var error="";
  if (strng == "") {
    error = "You didn't enter an email address.\n";
  }

  debug("CheckEmailPresent: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckEmailFormat(strng) {

/* Make sure an E-mail Address is valid (it is assumed that the email address is present).  We want it to follow this format: some characters, then an "at" symbol (@), then some more characters, then a dot (.), then two or three more characters, and that’s it. */

  debug("In CheckEmailFormat, strng='" + strng + "'");

  var error="";

  /* The carat (^) means the start of the string and the dollar sign ($) means the end of the string. By surrounding the rest of the regular expression with these characters, we ensure that we evaluate the entire string, rather than just a portion. Each .+ means "at least one character" — the dot (.) means "any character"” and the plus sign (+), you recall, means "one or more." The .{2,3} portion means "two or three characters." And when we want to match a literal dot, we need to escape the character with a backslash. That’s why we have \. there: */

/*    var emailFilter=/^.+@.+\..{2,3,4,6}$/;*/
  var emailFilter=/^.+@.+\..{2,4}$/;
  if (!(emailFilter.test(strng))) { 
    error = "The email address is not valid.\n";
  }

  /* Again, we want to check to make sure that no forbidden characters have slipped in. For email addresses, we’re forbidding the following: 
( ) < > [ ] , ; : \ / "
  */

  else {  //test email for illegal characters
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if (strng.match(illegalChars)) {
      error = "The email address contains illegal characters.\n";
    }
  }

  debug("CheckEmailFormat: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckEmail(strng) {

/* Make sure an E-mail Address was entered and that it is valid.  */
/*    **** NOTE: THIS FUNCTION PERFORMS BOTH VERIFICATIONS. ****  */
/* We want it to follow this format: some characters, then an "at" symbol (@), then some more characters, then a dot (.), then two or three more characters, and that’s it. */

  debug("In CheckEmail, strng='" + strng + "'");

  var error="";
  if (strng == "") {
    error = "You didn't enter an Email address.\n";
  }

  /* The carat (^) means the start of the string and the dollar sign ($) means the end of the string. By surrounding the rest of the regular expression with these characters, we ensure that we evaluate the entire string, rather than just a portion. Each .+ means "at least one character" — the dot (.) means "any character"” and the plus sign (+), you recall, means "one or more." The .{2,3} portion means "two or three characters." And when we want to match a literal dot, we need to escape the character with a backslash. That’s why we have \. there: */

  else {
/*    var emailFilter=/^.+@.+\..{2,3,4,6}$/;*/
    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng))) { 
      error = "The email address is not valid.\n";
    }

    /* Again, we want to check to make sure that no forbidden characters have slipped in. For email addresses, we’re forbidding the following: 
( ) < > [ ] , ; : \ / "
  */

    else {  //test email for illegal characters
      var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
      if (strng.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
      }
    }
  }

  debug("CheckEmail: returning '" + error + "'");

  return error;
}

/*******************************/

function get_radio_value(theRadioSet) {

/* This routine gets the value of the radio button set, theRadioSet. Returns the value of the button that is checked, or the empty string if no button is checked. */

  var i;
  var val = "";

  for (i=0; i < theRadioSet.length; i++) {
    if (theRadioSet[i].checked) {
      val = theRadioSet[i].value;
    }
  }
  return val;
}

/*******************************/

function CheckPlan(theRadioSet) {

/* Nothing to check because the "plan" radiobutton set has "No" preset as a default.  But we'll do it anyway. */

  debug("In CheckPlan");

  var error="";
  var val = get_radio_value(theRadioSet);

  if (val == "") {
    error = "Please choose either Yes or No.\n";
  }

  debug("CheckPlan: returning '" + error + "'");

  return error;

}

/*******************************/

function get_radio_value_ORIG (theForm) {

/* This routine gets the value of the radio button set, "subscription" (theForm.subscription.value doesn't work in Internet Explorer or Nescape (they both return "undefined" as value).  Returns the value of the button that is checked, or the empty string if no button is checked. */

  var i;
  var val = "";

  for (i=0; i < theForm.subscription.length; i++) {
    if (theForm.subscription[i].checked) {
      val = theForm.subscription[i].value;
    }
  }
  return val;
}

function CheckSubscription(theForm) {

/* Make sure user chose Subscribe or UnSubscribe (radio button). */

  debug("In CheckSubscription");

  var error="";
  var val = get_radio_value(theForm);

  if (val == "") {
    error = "Please choose either Subscribe or Unsubscribe.\n";
  }

  debug("CheckSubscription: returning '" + error + "'");

  return error;
}

/*******************************/

function CheckForm(theForm) {
  var ok = true;
  var why = "";
  debug("In CheckForm, form='" + theForm.name + "'");

  switch(theForm.name)
  {
    case highschoolForm:

      why += CheckName(theForm.personName.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.organisation.value);
      debug("CheckForm: why='" + why + "'");

      why += CheckContact_Highschool(theForm.homephone.value, theForm.cellphone.value, theForm.mailfrom.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.homephone.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.cellphone.value);
      debug("CheckForm: why='" + why + "'");

      if (theForm.mailfrom.value !== "") {
        why += CheckEmailFormat(theForm.mailfrom.value);
        debug("CheckForm: why='" + why + "'");
      }

      why += CheckPlan(theForm.plan);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.challenge.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.comment.value);
      debug("CheckForm: why='" + why + "'");

      break;

    case runeventForm:
      why += CheckName(theForm.personName.value);
      debug("CheckForm: why='" + why + "'");

      why += CheckContact_RunEvent(theForm.homephone.value, theForm.cellphone.value, theForm.mailfrom.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.homephone.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.cellphone.value);
      debug("CheckForm: why='" + why + "'");

      if (theForm.mailfrom.value !== "") {
        why += CheckEmailFormat(theForm.mailfrom.value);
        debug("CheckForm: why='" + why + "'");
      }

      why += NoCheck(theForm.comment.value);
      debug("CheckForm: why='" + why + "'");

      break;

    case contactForm:
      why += CheckName(theForm.personName.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.organisation.value);
      debug("CheckForm: why='" + why + "'");

      why += CheckContact_ContactUs(theForm.homephone.value, theForm.workphone.value, theForm.cellphone.value, theForm.mailfrom.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.homephone.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.workphone.value);
      debug("CheckForm: why='" + why + "'");

      why += NoCheck(theForm.cellphone.value);
      debug("CheckForm: why='" + why + "'");

      if (theForm.mailfrom.value !== "") {
        why += CheckEmailFormat(theForm.mailfrom.value);
        debug("CheckForm: why='" + why + "'");
      }

      break;

    default:
  }

  if (why != "") {
    ok = false;
    alert("The following error(s) occurred:\n\n"+why);
  }
  debug("Leaving CheckForm, ok=" + ok);
  return ok;
}

/*******************************/


highschoolForm = "highschoolForm";
runeventForm = "runeventForm";
contactForm = "contactForm";

var submitted = 0;

function FormValidation(form) {

  debug("In FormValidation");

  var ok = true;

    var emailFilter="^.+@.+\..{2,3}$";
/*    if (!(emailFilter.test(form.mailfrom.value))) { */

/*    if (!(/^.+@.+\..{2,3}$/.test(form.mailfrom.value))) { */

  if (submitted) {
    alert("Form already submitted, please be patient");
    ok = false;
  }
  else {
    if (CheckForm(form)) {
      debug("Form checked, okay");
      form.mySubmit.disabled=true;
      submitted = 1;
      form.submit();
    }
    else {
      debug("Form checked, not okay");
      ok = false;
    }
  }
  document.MM_returnValue = ok;
}

/*******************************/
