// wrapper object for xmlhttp
// the object uses GET to fetch the data
// then dynamic composition pattern to plug in
// a data handler and a navigator interpreter.
// usage:
// create a data event handler with a process(text) member
// create a navigator with a destination([object]) member
// create a connection object with a data event
// and a navigator vis
// a_connection = ajax_connection(data_event_handler,navigator)
//
// attach the connection to an event
// onclick = "return a_connection.get([parameter])"
// the get parameter will be passed to the navigator for
// additional processing

// examples - these use the yahoo object style to return closure objects
// having access to private function variables etc
//
// HACK: recreates the xmlHttp object for each call
//
// ***example data_event - replaces the content into the id'd element
function fill_div(dest_id)
{
  var dest = dest_id;
  return {
  	process: function(content){ //TODO: add error handler
  		document.getElementById(dest).innerHTML = content;
  		return true;
  	}
  };
}
// postprocess data handler
// usage postprocess(data_handler,postprocess_fn)
function post_p(handler,proc){
  var dest = handler;
  var post_p = proc;
  return {
  	process: function(content){ //TODO: add error handler
        dest.process(content);
        post_p();
  		return true;
  	}
  };
//  return true;
}
// ***example navigator - trivial it just passes the input string
function string_nav(){
	return {
		destination: function(url){
			return url;
		}
	};
}
// ***example navigator -  uses fixed as the destination
function fixed_nav(fixed){
  var url = fixed;
	return {
		destination: function(){
			return url;
		}
	};
}
////////// xmlhttp factory /////////////////////////////
function xmlHttp_factory()
{
	try
  {
  // Firefox, Opera 8.0+, Safari
  	xmlHttp=new XMLHttpRequest();
  }
	catch (e)
	{
	  // Internet Explorer
	  try
	  {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e)
	  {
	    try
	    {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e)
	    {
	      alert("Your browser does not support AJAX!");
	      return false;
	    }
	  }
	}
	return xmlHttp;
}
////////// connection factory //////////////////////////
function ajax_connection(data_handler,navigator){
	var data = data_handler;
	var navi = navigator;
//	var xmlHttp = xmlHttp_factory();
//-----------------------------------------
	var statechange = function()
		{
	    switch(xmlHttp.readyState)
	    {
	    	case 1:
	    		break;
	    	case 2:
	    	case 3:
	     		data.process(xmlHttp.readyState+ ' - ' + xmlHttp.statusText);
	     		break;
	    	case 4:
					data.process(xmlHttp.responseText);
					break;
		    default:
	     		data.process(xmlHttp.readyState+ ' - ' + xmlHttp.statusText);
	    }
    };
 //----------------------------------------
	return {get: function(x){
		var xmlHttp = xmlHttp_factory();
		xmlHttp.onreadystatechange = function()
		{
	    switch(xmlHttp.readyState)
	    {
	    	case 1:
	    		break;
	    	case 2:
	    	case 3:
	     		data.process(xmlHttp.readyState+ ' - ' + xmlHttp.statusText);
	     		break;
	    	case 4:
				data.process(xmlHttp.responseText);
				break;
		    default:
	     		data.process(xmlHttp.readyState+ ' - ' + xmlHttp.statusText);
	    }
    };

		if (typeof x == 'undefined'){
			xmlHttp.open("GET",navi.destination(),true);
		}
		else{
			xmlHttp.open("GET",navi.destination(x),true);
		}
  	xmlHttp.send(null);
  	return false;
  	}
  };
}
