// global ref to form
var accessForm;

// global ref to login button
var loginButton;

// validate access code format and submit form if it passes 
function sendForm(e) {
  // make sure we have the login form
  if (!accessForm) {
    if (document.getElementById) {
      accessForm = document.getElementById('accessForm');
    } else if (document.all) {
      accessForm = document.all['accessForm'];
    } else {
      alert('This web browser doesn\'t support the JavaScript that is necessary to view this website.');
    }
  }
  
  // get the access code
  var accessCode = accessForm.elements.namedItem('accessCode');
  
  // make sure it's 4 chars long
  if (accessCode.value.length <= 3) {
    alert('The Access Code must at least 4 characters long.');
  } else {
    accessForm.submit();
  }
}

// listen to the submit button for click. once clicked, validate the input and submit form
function listenForLogin(e) {
  // make sure we have the login button
  if (!loginButton) {
    if (document.getElementById) {
      loginButton = document.getElementById('loginButton');
    } else if (document.all) {
      loginButton = document.all['loginButton'];
    } else {
      alert('This web browser doesn\'t support the JavaScript that is necessary to view this website.');
    }
  }
  
  // listen for click
  if (loginButton.addEventListener) {
    loginButton.addEventListener('click', sendForm, false);
  } else if (loginButton.attachEvent) {
    loginButton.attachEvent('onclick', sendForm);
  }
}

// listen for form submission
if (window.addEventListener) {
  window.addEventListener('load', listenForLogin, false);
} else if (window.attachEvent) {
  window.attachEvent('onload', listenForLogin);
}