// Custom class for handling our json responses from the back-end
// The returned result is a hash with the following fields:
// command: indicates how the results should be interpreted.  When null, they're just interpreted as standard
// status: indicates if the functional operation was a success or a failure
// result: the result of the operation
// messages: any other relevant messages (an array)
// Everything is string format unless specfied otherwise
// 
// This takes a few options, represented as a hash
//    onSuccess: if this is a string, then it's interpreted as a location and the result is updated on error
//         If it's a function, then the result is passed to that function
//    onError: if this is a string, then it's interpreted as a location where the error messages are painted
//         If it's a function, then the messages are passed as an array to the this function
var AjaxResponse = Class.create();
AjaxResponse.prototype = {
  
  initialize: function( options ) {
    this.setOptions(options) ;
  },
  
  process: function(transport) {
    var processed = true;

    // console.log(transport.responseText);
    var json = eval('('+transport.responseText+')');
    if ( json.command == 'reload' )
      window.location.reload();
    else if ( json.command == 'redirect' )
      window.location=json.result;
    else if ( json.command == 'requires_login' )
      this.options.login();
    else if ( json.command == 'ignore' ) {
      // Do nothing
    } else if ( this.options ) {
      if ( typeof json == 'xml' ) {
        // # Assume we just got a normal string that hasn't been formatted, so just
        // display it or do whatever
        json = { status: 'success', result: transport.responseText }
      }        
      // evaluate anything that is int the results
      if ( json.status == 'success' && this.options.onSuccess) {
        setTimeout(function() {json.result.evalScripts()}, 10);
        if ( typeof this.options.onSuccess == 'string' )
          $(this.options.onSuccess).innerHTML = json.result;
        else if ( typeof this.options.onSuccess == 'function' ) 
          this.options.onSuccess(json.result);
      } else if ( json.status == 'error' && this.options.onError )
        if ( typeof this.options.onError == 'string' )
          $(this.options.onError).innerHTML = json.messages.join('<br>');
        else if ( typeof this.options.onError == 'function' )
          this.options.onError(json.messages);
      else
        processed = false;
    } else 
      processed = false
    return processed;
  },
  
  // Be default we redirect the user to the login page
  login: function() {
    // console.log("Processing login")
    window.location = '/login'
  },
  
  setOptions: function(options) {
    this.options = {
      login: this.login,          // function to call if the system requires login
      onSuccess: null,             // function or div ID
      onError: null                // function or div ID 
    }
    Object.extend(this.options, options || {});
  }
  
}