Event.observe(window, 'load', initTOS);

// {{{ initTOS()
function initTOS() {
  var iagree = $('auth_iagree') || $('post_iagree');

  if(iagree) {
    $(iagree.form).observe('submit', checkTOSAgreement);
  }

  var toslink = $('toslink');
  if(toslink) {
    toslink.observe('click', function(ev) {
      ev.stop();
      window.open(Event.element(ev).href, "policydocuments");
    });
  }
} // }}}

// {{{ checkTOSAgreement(ev)
function checkTOSAgreement(ev) {
  var el = $('auth_iagree') || $('post_iagree');

  if(!el.checked) {
    ev.stop();

    var which;
    if(el.id == 'auth_iagree') {
      which = 'your account can be created.';
    } else {
      which = 'your posting can be accepted.';
    }
    alert('You must read and agree to the Terms of Use before ' + which);

    var tos = $('tos');
    if(tos) {
      var row = tos.up('TR');
      new Effect.Highlight(row, {
        afterFinish: function(row) {
          $(row.element).removeClassName('zstripe')
                        .addClassName('needed')
                        .setAttribute('style', '');
          if(typeof(clearNoStripe) === 'function')
            clearNoStripe(row.element);
        }
      })
    }
  }
} // }}}
