//*********************************************************************
//
// MFM JavaScript
//
// Copyright (c) 2006, MFM Communication Software, Inc.
//
//*********************************************************************
//
// ajaxloader.js
// $Id: ajaxloader.js,v 1.29 2009/07/14 13:31:28 lbettag Exp $
//
// AJAX routines
//
//*********************************************************************
//
// Tell jsjam.pl to not change these identifier and function names
//
// jsjam-keep:AjaxLoader
// jsjam-keep:AjaxLoader.prototype.load
// jsjam-keep:AjaxLoader.prototype.onData
// jsjam-keep:AjaxLoader.prototype.getXMLHTTPRequest
// jsjam-keep:onData
// jsjam-keep:result
// jsjam-keep:response
// jsjam-keep:xmlHttp
//
//*********************************************************************

/*
* taken from Mike Chamber's "Encapsulating Ajax XMLHTTPRequest Calls within JavaScript classes"
* http://weblogs.macromedia.com/mesh/archives/2006/01/encapsulating_a.html
*/

function AjaxLoader(dataURL)
{
  this.dataURL = dataURL;
}

/**
*	Tells the class to load its data and render the results.
*/
AjaxLoader.prototype.load = function()
{
  // Add a timout
  //this.timer = window.settimeout(somefunction,30);
  
  //get a new XMLHTTPRequest and store it in an instance var.
  this.request = this.getXMLHTTPRequest();

  //set the var so we can scope the callback
  var _this = this;

  //var req = _this.request;
  //var dummyFunc = function(){_this.handleTimeout(req)};
  //someTimeout = setTimeout(dummyFunc, 4000);

  //callback will be an anonymous function that calls back into our class
  //this allows the call back in which we handle the response (onData())
  // to have the correct scope.
  this.request.onreadystatechange = function(){_this.onData()};

  // Initialize the request
  this.request.open("GET", this.dataURL, true);

  // Set the requested-with header:
  //this.request.setRequestHeader("X-Requested-With","XMLHttpRequest");

  // Finish the reuqest
  this.request.send(null);
};

AjaxLoader.prototype.handleTimeout = function (foo)
{
  if(! foo.status == "200")
  {
    foo.abort();
    alert(foo.status);
  }
};

//callback for when the data is loaded from the server
AjaxLoader.prototype.onData = function()
{
  if(this.request.readyState == 4)
  {
    if(this.request.status == "200")
    {
      // here's where we put the eval statment
      response  = this.request.responseXML.documentElement;
      var method = "";
      
      // Build in some error handling. When our debugger
      // is on, it breaks Ajax because it sends an
      // invalid response
      try
      {
        method = response.getElementsByTagName('method')[0].firstChild.data;
        //alert('method ' + method)
        // ...processing statements go here...

        var result   = response.getElementsByTagName('result');

        eval(method + '(\'\', result)');
      }
      catch(err)
      {
        if (method.length == 0)
        {
          alert('Couldn\'t get method.  Invalid XML?');
          // TO DO: add error handling to have calling
          // object change messages
          //throw new Error(35,"null object, call your vendor");
        }
        else
        {
          if (err.description)
          {
            alert("err.description " + err.description);
          }
          else
          {
            alert("Error: responseText " + this.request.responseText + "\nmethod.length " + method.length);
            //alert("err.message " + err.message);
            alert("Error: err.message " + err.message + "\nrequest.status " + this.request.status + "\nrequest.statusText " + this.request.statusText);
            if (method.length)
            {
              alert("method:\n" + method);
            }
            //window.onError({status:this.request.status, 
              //statusText:this.request.statusText});
          }
        }
      }
    }
    else
    {	
      //check if an error callback handler has been defined
      if(this.onError != undefined)
      {
        //pass an object to the callback handler with info
        //about the error
        this.onError({status:this.request.status, 
          statusText:this.request.statusText});
      }
    }
		
    //clean up
    delete this.request;
  }
};
     
//returns an XMLHTTPRequest instance (based on browser)
AjaxLoader.prototype.getXMLHTTPRequest = function()
{
  var xmlHttp;
  try
  {
    xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
  }
    catch(e)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch(e2)
    {
    }
  }
	
  if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
  {
    xmlHttp = new XMLHttpRequest();
  }
	
  if (xmlHttp.overrideMimeType)
  {
    // set the mime type
    // find out what's allowable here
    // can we return images?
    xmlHttp.overrideMimeType('text/xml');
  }
  
  return xmlHttp;
};

