/*

This file contains javascript wrappers for ajax calls.

The prototype ajax calls are not difficult to use but hiding it
behind our own ajax wrapper will hopefully allow a few things:

1 - the ability to easily change to a different ajax library
    if we want
 
2 - The ability to automate common tasks
    
3 - the ability to put some infrastructure around calls such
    as updating undo controls

*/



function Ajax_send(pURL,params,callback){

  if(typeof SaveSubsystem == 'object'){ 
    (new SaveSubsystem()).setIconSaving(); 
  }
  new Ajax.Request(pURL,
      {
        method     : "post",
        parameters : params,
        onSuccess  : function(resp) {

                       if (resp.responseText==""){
                          // Pops up when it shouldn't. Commenting out.
                          // alert("igajax.js: Ajax_send() server did not return any data.");
                          return; 
                       }
                       var returned_data = eval("("+resp.responseText+")");
                       returned_data.request = resp.request;
                       Ajax_HandleUndoData(returned_data);
                       //Set the state of the save icon, but if the icon 
                       //doesn't exist, no biggie
                       try{ (new SaveSubsystem()).setIconSaved(); }
                       catch (ex){}
                       callback(returned_data);
                     }
      });
}

function Ajax_HandleUndoData(data){
  //if there's undo data AND an undo control
  if (typeof(data.undo_count) != undefined && $('undo-link') && $('undo-disabled-link')){
     if (data.undo_count>0){
        $('undo-link').setAttribute('title', "Undo "+data.undo_message);
        $('undo-link').show(); 
        $('undo-disabled-link').hide(); 
     }
  }
}

