﻿// AJAX Support:

function communicationFailure(request) {
  //alert('An error has occurred while communicating with the server:\n\n' + request.responseText);
  alert('An error has occurred while communicating with the server.  Please try again.');
}

function evaluateRemoteResponse(form, url, indicator) {
  new Ajax.Request(url, {
    asynchronous: true,
    evalScripts: true,
    parameters: Form.serialize(form),
    onFailure: function(request) { Effect.Fade(indicator); communicationFailure(request); },
    onSuccess: function(request) { Effect.Fade(indicator); eval(request.responseText) }
  });
  Effect.Appear(indicator);
  return false;
}

// Behaviors:

function blurMe() {
  this.blur();
}

function autoClear() {
  if (!this.firstFocus) {
    this.value = '';
    this.firstFocus = true;
  }
}

var behaviors = {
  'a' : function(e) { e.onfocus = blurMe; },
  'input.submit' : function(e) { e.onclick = blurMe; },
  'input.autoclear' : function(e) { e.onfocus = autoClear; },
  'textarea.autoclear' : function(e) { e.onfocus = autoClear; }
};
Behaviour.register(behaviors);