function addLoadEvent(func) {
  if(typeof window.onload != 'function')
    window.onload = func;
  else {
    var oldLoad = window.onload;

    window.onload = function() {
      if(oldLoad) oldLoad();
      func();
    }
  }
}

 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  *                     Button Disabler                         *
  *                       Version 1.0                           *
  *                 Written by Josh Stodola                     *
  *                     January 29, 2008                        *
  *                                                             *
  * * * * * * * * * *CONFIGURATION OPTIONS* * * * * * * * * * * *
  *                                                             *
  *   IsTesting  (Boolean, defaults to false)                   *
  *     When this is set to true, the form will never submit.   *
  *     Use this to confirm that the script is working.         *
  *                                                             *
  *   DisabledButtonValue  (String, defaults to 'Please Wait')  *
  *     This is the value to show on the button once disabled.  *
  *                                                             *
  *   HideNonSubmitButtons  (Boolean, defaults to true)         *
  *     When true, the script will also hide any reset buttons  *
  *     or Javascript buttons it encounters.                    *
  *                                                             *
  *   ShowHourglassCursor  (Boolean, defaults to true)          *
  *     When true, the script will change the cursor to the     *
  *     OS-defined "waiting" symbol to indicate loading.        *
  *                                                             *
  *   StopLoopAtFirstTextbox  (Boolean, defaults to false)      *
  *     When true, the script will stop looping through input   *
  *     elements once it encounters the first textbox. The loop *
  *     begins at the bottom of the form. This can be set to    *
  *     true to make the script more efficient.                 *
  *                                                             *
  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var ButtonDisabler = {
  IsTesting: true,
  DisabledButtonValue: 'Please Wait',
  HideNonSubmitButtons: true,
  ShowHourglassCursor: true,
  StopLoopAtFirstTextbox: false,

  IsCapable: (document.getElementById && document.createElement),
  AddSubmitEvent: function(frm, func) {
    if(typeof frm.onsubmit != 'function')
      frm.onsubmit = func;
    else {
      var oldSub = frm.onsubmit;

      frm.onsubmit = function() {
        if(oldSub) {
          if(oldSub())
            return func();
          else
            return false;
        }
        else
          return func();
      }
    }
  },
  LoadEventHandlers: function() {
    if(ButtonDisabler.IsCapable) {
      for(var i = 0; i < document.forms.length; i++) {
        var frm = document.forms[i];

        ButtonDisabler.AddSubmitEvent(frm, function() {
          ButtonDisabler.DisableForm(frm);
          return !ButtonDisabler.IsTesting;
        });
      }
    }
  },
  DisableForm: function(frm) {
    if(!frm) return;
    var inputs = frm.getElementsByTagName('INPUT');

    for(var j = inputs.length - 1; j >= 0; j--) {
      var elem = inputs[j];

      if(elem.type == 'submit' || elem.type == 'image') {
        var btn = document.createElement('button');

        btn.disabled = true;
        btn.innerHTML = ButtonDisabler.DisabledButtonValue;

        elem.parentNode.insertBefore(btn, elem);
        elem.style.display = 'none';
      }

      if(elem.type == 'reset' || elem.type == 'button') {
        if(ButtonDisabler.HideNonSubmitButtons)
          elem.style.display = 'none';
      }

      if(elem.type == 'text' && ButtonDisabler.StopLoopAtFirstTextbox)
        break;
    }

    if(ButtonDisabler.ShowHourglassCursor)
      document.body.style.cursor = 'wait';

    frm.onsubmit = function() {
      return false;
    }
  }
};

addLoadEvent(ButtonDisabler.LoadEventHandlers);
