// Add an 'are you sure' message to all links with class name 'deletelink'
// (only if no onclick action has already been defined)

Event.observe(window, 'load', function() {
  $$('.deletelink').each(function (e) {
    Event.observe(e, 'click', function (e) {
      if (!confirm('Weet u het zeker?')) Event.stop(e);
    });
  });
  $$('form').each(function (el) {
    Event.observe(el, 'submit', checkForm);
  });
});

// Set the focus to the first element with class name 'focused'

Event.observe(window, 'load', function() {
  if ($$('.focused').first()) $$('.focused').first().focus();
});

// Enable form validation for all forms with class 'validate'

var validatedForms = {};

Event.observe(window, 'load', function() {
  $$('form.validate').each(function(e) {
    validatedForms[e.id] = new Validation(e);
  });
});

Event.observe(window, 'load', function() {
  if ($('message')) {
    $('message').hide();
    $('message').style.visibility = 'visible';
  }
});

// Date functions

Event.observe(window, 'load', function() {
  $$('.date .w2em').each(function (el) {
    Event.observe(el, 'keydown', function (ev) {
      switch (ev.keyCode) {
        case 109:
          e = Event.element(ev).nextSibling;
          while (e != null && (e.nodeType != 1 && e.nodeName != 'INPUT')) e = e.nextSibling;
          if (e != null) {
            // e.focus();
            e.select();
            Event.stop(ev);
          }
          break;
      }
    });
  });
});

// Custom validation rules

if (typeof(Validation) != 'undefined') {
  Validation.addAllThese([
    ['required', 'Dit veld is verplicht.', {minLength: 1}],
    ['validate-password', 'Wachtwoord moet minimaal 5 tekens bevatten', {minLength: 5 }],
    ['validate-password-confirm', 'Wachtwoorden komen niet overeen.', {equalToField: 'i_pwd1'}]
  ]);
}

displayMessage = function (message) {
  if ($('message')) {
    $('message').update(message);
    new Effect.Appear($('message'), {duration: 0.5});
    setTimeout("new Effect.Fade($('message'), {duration: 1.0});", message.length * 120);
  } else {
    alert(message);
  }
}

Ajax.Responders.register({
  onComplete: function(o) {
    if (o.transport.responseXML) {
      doc = o.transport.responseXML;
      if (doc.firstChild.nodeType == 1 && doc.firstChild.nodeName == 'message' && doc.firstChild.hasChildNodes && (doc.firstChild.firstChild.nodeType == 3 || doc.firstChild.firstChild.nodeType == 4)) {
        displayMessage(doc.firstChild.firstChild.nodeValue);
      }
    }
  }
});

submitForm = function (e) {
  while (e != null && !(e.nodeType == 1 && e.nodeName == 'FORM')) e = e.parentNode;
  if (e != null && (e.nodeType == 1 && e.nodeName == 'FORM')) e.submit();
}

toggleDisplay = function (ela, elb) {
  ela.style.display = (ela.style.display == 'none') ? 'block' : 'none';
  elb.style.display = (ela.style.display == 'none') ? 'block' : 'none';
}

function stringinabbr (a, b) {
  var aa = a[fdTableSort.pos];
  var bb = b[fdTableSort.pos];
  if(aa[0] == bb[0] && aa[1] == bb[1]) { return 0; };
  if(aa[0] != bb[0]) {
  if(aa[0] != "" && bb[0] != "") { return aa[0] - bb[0]; };
  if(aa[0] == "" && bb[0] != "") { return -1; };
  return 1;
  };
  if(aa[1] == bb[1]) return 0;
  if(aa[1] < bb[1]) return -1;
  return 1;
}

function numericabbr (a, b) {
  if (a == b) return 0;
  if (a < b) return -1;
  return 1;
}

function numericinabbrPrepareData (a) {
  return a.getAttribute('abbr');
}

function stringinabbrPrepareData (a) {
  return a.getAttribute('abbr');
}

Event.observe(window, 'load', function() {
  $$('form').each(function (el) {
    Event.observe(el, 'submit', checkForm);
  });
});

// Required form fields stuff

checkForm = function (ev) {
  inputfields = Event.element(ev).getElementsBySelector('input.required');
  allOK = true;
  inputfields.each(function (el) {
    if (el.value == '') {
      el.addClassName('error');
      allOK = false;
    } else {
      el.removeClassName('error');
    }
  });
  if (!allOK) {
    window.alert('Niet alle verplichte velden zijn ingevuld.\nVul a.u.b. de met rood gemarkeerde velden aan.')
    Event.stop(ev);
  }
}
