//------------------------------------------------------------------------
// application.js
//
// Application-specific JavaScript functions and classes.
// This file is automatically included by javascript_include_tag :defaults
// See http://www.sitepoint.com/article/standards-compliant-world
//------------------------------------------------------------------------

// externalLinks
// Ensures that each link to an external website, i.e. those with the
// attribute rel="external" are opened in a new window.
function externalLinks() {
  // check that DOM1 supported (not on older browsers) 
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") &&
        anchor.getAttribute("rel") == "external")
      anchor.target = "_blank";
  }
}
// Call externalLinks on each page load
window.onload = externalLinks;

// restrictFieldLength
// If the supplied field exceeds a specified length, truncates the field
// and displays an alert.
// Called on modifying a text field.
function restrictFieldLength(text_field, field_name, max_length)
{
  // In Ruby on Rails, a line break is 2 characters, whereas in javascript it is 1 character.
  // Count each line break as 2 characters when determining the length of the field.
  linebreak_count = text_field.value.split(/\n/g).length - 1;
  if ((text_field.value.length + linebreak_count) > max_length) {
    text_field.value = text_field.value.substring(0, max_length - linebreak_count);
    alert("Error: " + field_name + " has a maximum length of " + max_length + " characters.");
    return false;
  }
  else {
    return true; 
  }
}

// confirmAction
// Does the following when a (usually delete) button is pressed:
// - disables the button
// - changes the button text to 'Please wait...'
// - displays a confirmation pop-up
// - if the user confirms the action, submits the button's form
// - if the user does not confirm the action, or there is a problem submitting
//   the form, sets the button text back to its original text and re-enables the button.
// NOTE: The button MUST have an id attribute.
function confirmAction(buttonId, waitMessage, confirmationMessage)
{
  var button = document.getElementById(buttonId);
  if (window.hiddenCommit) {
    window.hiddenCommit.setAttribute('value', button.value);
  }
  else {
    hiddenCommit = button.cloneNode(false);
    hiddenCommit.setAttribute('type', 'hidden');
    button.form.appendChild(hiddenCommit);
  }
  button.setAttribute('originalValue', button.value);
  button.disabled = true;
  button.value = waitMessage;
  confirmValue = confirm(confirmationMessage);
  if (confirmValue == true) {
    button.form.submit();
  }
  else {
    button.value = button.getAttribute('originalValue');
    button.disabled = false;
  }
  return confirmValue;
}	

// disableWith
// Does the following when a button is pressed:
// - disables the button
// - changes the button text to 'Please wait...'
// - submits the button's form
// - if any problem, sets the button text back to its original text.
// NOTE: The button MUST have an id attribute.
function disableWith(buttonId, waitMessage)
{
  var button = document.getElementById(buttonId);
  var hiddenCommit;
  if (window.hiddenCommit) {
    window.hiddenCommit.setAttribute('value', button.value);
  }
  else {
    hiddenCommit = button.cloneNode(false);
    hiddenCommit.setAttribute('type', 'hidden');
    button.form.appendChild(hiddenCommit);
  }
  button.setAttribute('originalValue', button.value);
  button.disabled = true;
  button.value = waitMessage;
  //result = (button.form.onsubmit ? (button.form.onsubmit() ? button.form.submit() : false) : button.form.submit());
  //if (result == false) {
  //  button.value = button.getAttribute('originalValue');
  //  button.disabled = false;
  //}
  button.form.submit();
  return true;
}

// End of file