/********************************************************************

  ECubeAjax : an object for dealing with Ajax-calls
  
  provides methods/variables for handling states which you can set 
  from functions that call the ECubeAjax object or from elsewhere.
  
  The simple method:
  =================
  
  ECubeAjax['displayObject'] : the html element that will hold
                               the data returned from the Ajax
                               call, simply by setting its innerHTML
                               property to the data returned
  ECubeAjax['waitMessage'] : a html string that will be shown inside
                             ECubeAjax['displayObject'] (innerHTML)
                             while the request takes place before
                             it is completed
                             
  The method that allows more control:
  ====================================
  
  Assign a function (which takes Ajax output as input (one string)) to
  a specific ready state using 
  
  ECubeAjax['assignHandlerToState'](readyState, functionObject).
  
  where: 
  - readyState should be one of READY_STATE_UNINITIALIZED, READY_STATE_LOADING,
  READY_STATE_LOADED, READY_STATE_INTERACTIVE, READY_STATE_COMPLETE
  
  - functionObject is a function.
  As a shorthand for assigning a function to all states except READY_STATE_COMPLETE,
  set readyState to READY_STATE_INCOMPLETE
  
  If you're a lazy typist, you can omit the 'READY_STATE_'. it will
  be added automatically
  
  Making the actual call:
  =======================
  

  - set up either displayObject and waitMessage or assign handlers using assignHandlerToState
  
  - make the connection:
  
  ECubeAjax['sendRequest'](url,params,HttpMethod)
  
  where params and HttpMethod are optional, in which case HttpMethod defaults
  to GET.
  
  NOTE: ECubeAjax treats the returned data as a string, not as XML
  
  NOTE: There can only be one ECubeAjax on a page until I figure out why
        I can't get prototyping and such to work                       
********************************************************************/
var ECubeAjax = new Object();

// some things you can / should set
ECubeAjax['displayObject'] = null;
ECubeAjax['waitMessage'] = '';
// these should be functions that accept one string (the returned
// data as input. if a function is defined for the current ready state,
// it will be used to handle the data. if it isn't and a displayObject
// is, the inner html of the displayObject is set, either to a waitMessage
// or to the data
ECubeAjax['handlers'] = new Object;

ECubeAjax['handlers']['READY_STATE_UNINITIALIZED'] = null; 
ECubeAjax['handlers']['READY_STATE_LOADING'] = null;         
ECubeAjax['handlers']['READY_STATE_LOADED'] = null;          
ECubeAjax['handlers']['READY_STATE_INTERACTIVE'] = null;     
ECubeAjax['handlers']['READY_STATE_COMPLETE'] = null;





ECubeAjax['READY_STATE_UNINITIALIZED']=0; 
ECubeAjax['READY_STATE_LOADING']=1;         
ECubeAjax['READY_STATE_LOADED']=2;          
ECubeAjax['READY_STATE_INTERACTIVE']=3;     
ECubeAjax['READY_STATE_COMPLETE']=4;

ECubeAjax['getXMLHTTPRequest'] = function () { 
  var xRequest=null; 
  if (window.XMLHttpRequest) {                        
    xRequest=new XMLHttpRequest();   
  }else if (typeof ActiveXObject != "undefined"){     
    xRequest=new ActiveXObject 
     ("Microsoft.XMLHTTP");   
  } 
  return xRequest; 
};

ECubeAjax['req'] = null;
ECubeAjax['returnedData'] = '';

// this function should be called to initiate the Ajax request
ECubeAjax['sendRequest'] = function(url,params,HttpMethod){ 
  if (!HttpMethod){ 
    HttpMethod="GET"; 
  } 
  ECubeAjax['req']=ECubeAjax['getXMLHTTPRequest'](); 
  if (ECubeAjax['req']){ 

    ECubeAjax['req'].onreadystatechange=ECubeAjax['onReadyState']; 
    ECubeAjax['req'].open(HttpMethod,url,true); 
    ECubeAjax['req'].setRequestHeader 
         ("Content-Type", "application/x-www-form-urlencoded"); 
    ECubeAjax['req'].send(params); 
  } 
}

// assign a function (which takes Ajax output as input (one string)) to
// a specific ready state.
//
// readyState should be one of READY_STATE_UNINITIALIZED, READY_STATE_LOADING,
// READY_STATE_LOADED, READY_STATE_INTERACTIVE, READY_STATE_COMPLETE
// functionObject is a function.
// as a shorthand for assigning a function to all states except READY_STATE_COMPLETE,
// set readyState to READY_STATE_INCOMPLETE
//
// if you're a lazy typist, you can omit the 'READY_STATE_'. it will
// be added  automatically
ECubeAjax['assignHandlerToState'] = function(readyState, functionObject) {
  if(readyState && functionObject) {
    if(! readyState.match(/^READY_STATE_/)) {
      readyState = 'READY_STATE_' + readyState;
    };
    if(readyState == 'READY_STATE_INCOMPLETE') {
      ECubeAjax['handlers']['READY_STATE_UNINITIALIZED'] = functionObject; 
      ECubeAjax['handlers']['READY_STATE_LOADING'] = functionObject;         
      ECubeAjax['handlers']['READY_STATE_LOADED'] = functionObject;          
      ECubeAjax['handlers']['READY_STATE_INTERACTIVE'] = functionObject;
    } else {     
      ECubeAjax['handlers'][readyState] = functionObject;
    };
    return true;
  } else {
    return false;
  }
}
// this is where the actual magic happens (responding to ready state changes, that is)
// don't call this directly.
ECubeAjax['onReadyState'] = function() {
  var ready=ECubeAjax['req'].readyState;                        
  var data=null; 
  if (ready==ECubeAjax['READY_STATE_COMPLETE']){   
    data=ECubeAjax['req'].responseText;   
  }else{ 
    data=ECubeAjax['waitMessage'];
  }
  var handlerFunction = null;
  if(ready == ECubeAjax['READY_STATE_UNINITIALIZED']) {
    handlerFunction = ECubeAjax['handlers']['READY_STATE_UNINITIALIZED'];
  };
  if(ready == ECubeAjax['READY_STATE_LOADING']) {
    handlerFunction = ECubeAjax['handlers']['READY_STATE_LOADING'];
  };
  if(ready == ECubeAjax['READY_STATE_LOADED']) {
    handlerFunction = ECubeAjax['handlers']['READY_STATE_LOADED'];
  };
  if(ready == ECubeAjax['READY_STATE_INTERACTIVE']) {
    handlerFunction = ECubeAjax['handlers']['READY_STATE_INTERACTIVE'];
  };
  if(ready == ECubeAjax['READY_STATE_COMPLETE']) {
    handlerFunction = ECubeAjax['handlers']['READY_STATE_COMPLETE'];
  };
  if(handlerFunction) {
    handlerFunction(data);
  } else {
    if(ECubeAjax['displayObject']) {
        ECubeAjax['displayObject'].innerHTML = data;
    };
  };
};



