var subfocus, moron, t, tf, ffx;

moron = false;      // Workaround for a stupid bug in IE (stop it sending spurious
                    // events to the 1st button)
subfocus = false;   // Only submit the form when the "submit" button has the focus

tf = false;         // Timeout for submit button focus

ffx = false;        // Initialized to the form object when the page loads

function toggle_mon(n)
{
  if (moron) {
    t = !(ffx.mon_am.checked && ffx.mon_pm.checked && ffx.mon_ev.checked);
    ffx.mon_am.checked = t;
    ffx.mon_pm.checked = t;
    ffx.mon_ev.checked = t;
    moron = false;
  }
}

function toggle_tue()
{
  t = !(ffx.tue_am.checked && ffx.tue_pm.checked && ffx.tue_ev.checked);
  ffx.tue_am.checked = t;
  ffx.tue_pm.checked = t;
  ffx.tue_ev.checked = t;
}

function toggle_wed()
{
  t = !(ffx.wed_am.checked && ffx.wed_pm.checked && ffx.wed_ev.checked);
  ffx.wed_am.checked = t;
  ffx.wed_pm.checked = t;
  ffx.wed_ev.checked = t;
}

function toggle_thu()
{
  t = !(ffx.thu_am.checked && ffx.thu_pm.checked && ffx.thu_ev.checked);
  ffx.thu_am.checked = t;
  ffx.thu_pm.checked = t;
  ffx.thu_ev.checked = t;
}

function toggle_fri()
{
  t = !(ffx.fri_am.checked && ffx.fri_pm.checked && ffx.fri_ev.checked);
  ffx.fri_am.checked = t;
  ffx.fri_pm.checked = t;
  ffx.fri_ev.checked = t;
}

function toggle_sat()
{
  t = !(ffx.sat_am.checked && ffx.sat_pm.checked && ffx.sat_ev.checked);
  ffx.sat_am.checked = t;
  ffx.sat_pm.checked = t;
  ffx.sat_ev.checked = t;
}

function toggle_sun()
{
  t = !(ffx.sun_am.checked && ffx.sun_pm.checked && ffx.sun_ev.checked);
  ffx.sun_am.checked = t;
  ffx.sun_pm.checked = t;
  ffx.sun_ev.checked = t;
}

function toggle_am()
{
  t = !(ffx.mon_am.checked && ffx.tue_am.checked && ffx.wed_am.checked && ffx.thu_am.checked && ffx.fri_am.checked && ffx.sat_am.checked && ffx.sun_am.checked);
  ffx.mon_am.checked = t;
  ffx.tue_am.checked = t;
  ffx.wed_am.checked = t;
  ffx.thu_am.checked = t;
  ffx.fri_am.checked = t;
  ffx.sat_am.checked = t;
  ffx.sun_am.checked = t;
}

function toggle_pm()
{
  t = !(ffx.mon_pm.checked && ffx.tue_pm.checked && ffx.wed_pm.checked && ffx.thu_pm.checked && ffx.fri_pm.checked && ffx.sat_pm.checked && ffx.sun_pm.checked);
  ffx.mon_pm.checked = t;
  ffx.tue_pm.checked = t;
  ffx.wed_pm.checked = t;
  ffx.thu_pm.checked = t;
  ffx.fri_pm.checked = t;
  ffx.sat_pm.checked = t;
  ffx.sun_pm.checked = t;
}

function toggle_ev()
{
  t = !(ffx.mon_ev.checked && ffx.tue_ev.checked && ffx.wed_ev.checked && ffx.thu_ev.checked && ffx.fri_ev.checked && ffx.sat_ev.checked && ffx.sun_ev.checked);
  ffx.mon_ev.checked = t;
  ffx.tue_ev.checked = t;
  ffx.wed_ev.checked = t;
  ffx.thu_ev.checked = t;
  ffx.fri_ev.checked = t;
  ffx.sat_ev.checked = t;
  ffx.sun_ev.checked = t;
}

function DocSetup()
{
  ffx = document.forms[0];
}

function DocSetup2()
{
  ffx = document.forms[0];
}

function checkEmailSyntax(e)
{
  var validEmailSyntax, validChars, midPos, i, t;
  
  validChars = "-_.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  
  // Look for the @ symbol
  midPos = e.indexOf("@");
  
  // Is it missing, or at the start, or <4 chars from the end?
  if ((midPos < 1) || (midPos > (e.length - 5))) return false;
  
  // Is there another one?
  if (e.indexOf("@", midPos+1) != -1) return false;
  
  // Check for other illegal syntaxes
  if (e.indexOf(".@") != -1) return false;
  if (e.indexOf("@.") != -1) return false;
  if (e.indexOf("..") != -1) return false;
  if (e.indexOf(".", e.length - 2) != -1) return false;
  if (e.indexOf(".", midPos+1) == -1) return false;
  
  // Default to success
  validEmailSyntax = true;
  
  // Check for illegal characters (be stricter after the @ symbol)
  
  for (i=0; i < midPos-1; i++) {
    if (validChars.indexOf(e.charAt(i)) == -1) validEmailSyntax = false;
  }
  
  t = 13;
  for (i=e.length-1; i > midPos; i--) {
    if (e.charAt(i) == ".") t = 0;
    if (validChars.indexOf(e.charAt(i),t) == -1) validEmailSyntax = false;
  }
  
  return validEmailSyntax;
}


function CheckEmailBox(tb)
{
  if (checkEmailSyntax(tb.value) == false) {
    alert("The email address you entered is invalid. Please enter a valid address, or leave this field blank.");
    tb.focus();
    tb.select();
  }
}

function CheckNameBox(tb)
{
  var t;
  
  t = tb.value;
  
  if (t.length < 2) {
    alert("Please enter your full name.");
    tb.focus();
    tb.select();
  }
}

function CheckForm()
{
  var haveName, haveEmail, haveAddress, havePhone, msg;
  
  // Don't submit form unless the submit button is pressed
  if (subfocus == false) return false;
  
  if (ffx.email.value.length != 0) {
    if (checkEmailSyntax(ffx.email.value) == false) {
      CheckEmailBox(ffx.email);
      return false;
    }
  }
  
  haveName = (ffx.name.value.length > 1);
  haveEmail = (ffx.email.value.length > 1);
  haveAddress = (ffx.street.value.length > 1) || (ffx.city.value.length > 1) || (ffx.state.value.length > 1);
  havePhone = (ffx.phone.value.length > 5);
  
  if (!haveName) {
    alert("Please enter your name and contact details before submitting this form.");
    ffx.name.focus();
    ffx.name.select();
    return false;
  }
  
  if (!haveAddress && !havePhone && !haveEmail) {
    alert("Please enter your contact details before submitting this form.");
    ffx.street.focus();
    ffx.street.select();
    return false;
  }
  
  if (haveAddress && (!havePhone && !haveEmail)) {
    if (!confirm("You have not provided a phone number\ror a valid email address. Do you still\rwant to submit this form?")) {
      ffx.phone.focus();
      ffx.phone.select();
      return false;
    }
  }
  
  return true;
}

function CheckMiniForm()
{
  var haveName, haveEmail, havePhone, haveMessage;
  
  // Don't submit form unless the submit button is pressed
  if (subfocus == false) return false;
  
  if (ffx.email.value.length != 0) {
    if (checkEmailSyntax(ffx.email.value) == false) {
      CheckEmailBox(ffx.email);
      return false;
    }
  }
  
  haveName = (ffx.name.value.length > 1);
  haveEmail = (ffx.email.value.length > 1);
  havePhone = (ffx.phone.value.length > 5);
  haveMessage = (ffx.message.value.length > 1);
  
  if (!haveName) {
    alert("Please enter your name and contact details before submitting this form.");
    ffx.name.focus();
    ffx.name.select();
    return false;
  }
  
  if (!havePhone && !haveEmail) {
    alert("Please enter your phone number and/or email address before submitting this form.");
    ffx.phone.focus();
    ffx.phone.select();
    return false;
  }
  
  if (!haveMessage) {
    alert("Don't forget to type in your message first!");
    ffx.message.focus();
    ffx.message.select();
    return false;
  }
  
  return true;
}


function WindUp()
{
  if (tf) clearTimeout(tf);
}
