// Global variables
var form;
var messageTestbox;
var nameTextbox;
var emailTextbox;
var submitButton;
// Text for alerts on errors submitting form
var alertConfirmNameEmail = '\
  Name and Email Address\n\n\
  You do not need to enter a name and email address,\n\
  but if you do we can contact you about your feedback.\n\n\
  Press OK to submit your feedback anyway\n\
  or Cancel to add your name and/or email.';
var alertNoFeedback = '\
  Please enter your feedback in the large textbox.';

// Initialise the form adding events to relevant elements
function init_Banner() {
  if ($('ctl00_ContentPlaceHolder_ui_MessageTextbox'))
  {
    // Initialise variables
    messageTestbox = $('ctl00_ContentPlaceHolder_ui_MessageTextbox');
    nameTextbox = $('ctl00_ContentPlaceHolder_ui_NameTextbox');
    emailTextbox = $('ctl00_ContentPlaceHolder_ui_EmailTextbox');
    submitButton = $('ctl00_ContentPlaceHolder_ui_SendFeedbackButton');
    form = window.document.getElementsByTagName('form')[0];
    // Observe
    Event.observe(form, 'submit', checkForm, true);
    Event.observe(messageTestbox, 'change', clearError, true);
    Event.observe(nameTextbox, 'change', clearError, true);
    Event.observe(emailTextbox, 'change', clearError, true);
  }
}

// Check the form on submission
function checkForm(e) {
  submitButton.disable();
  if ( messageTestbox.value == '' ) {
    messageTestbox.style.border = '2px solid red';
    messageTestbox.activate();
    alert(alertNoFeedback);
    Event.stop(e);
  }
  else {
    if ( nameTextbox.value == '' || emailTextbox.value == '' ) {
      nameTextbox.style.border = '1px solid red';
      emailTextbox.style.border = '1px solid red';
      nameTextbox.activate();
      if (!confirm(alertConfirmNameEmail)) {
        Event.stop(e);
      }
    }
  }
  submitButton.enable();
}

// Clear error
function clearError(e) {
  Event.element(e).style.border = '1px solid #640000';
}


// Initialise the form on load
Event.observe(window, 'load', init_Banner, true);
