/////////////////////////////////////////////////////////////////////////////
//
// Gladiator Components
//
// http://www.gladiatorweb.com
//
// (c) 2006 by Edward H. Trager .  All Rights Reserved
//
// NAME: gladiatorRequest.js
//
// DESCRIPTION: Implements an object which handles XMLHttpRequests.
//
// AUTHOR: Edward H. Trager
//
// LAST UPDATE: 2006.03.16
//
// NOTE: SEE "LICENSE" FILE FOR LICENSING TERMS
//
//////////////////////////////////////////////////////////////////////////////


//
// DEPENDENCY CHECKING:
// 
// Nota bene: The Gladiator SVG wait indicator is actually
//            optional:
//
// if(typeof GLADIATOR_INDICATOR_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorRequest.js" REQUIRES "gladiatorWaitIndicator.js"!');
//

//
// DECLARE OUR EXISTANCE:
//
var GLADIATOR_REQUEST_INCLUDED=true;

//
// gRequest
//
// This class implements an XMLHttpRequest object
//
// URL : The URL to retrieve, i.e. "http:your.domain.com/search.php"
//
// populatePage: A function that takes a (completed) XMLHttpRequest object
//               and processes the responseXML or responseText members
//
// postOrGet: a string, either "post" or "get" -- sets how the data
//            are sent back to the server. POST is preferred and is
//            the default.
//
// waitIndicator: Pass an instance of a gladiator SVG wait indicator
//                if you want to use the wait indicator.
//
function gRequest(URL,populatePage,postOrGet,waitIndicator){
	
	var _url = URL;
	var _request=false;
	
	var _populatePage = populatePage;
	
	var _queryTermsString="";
	
	// Normally we will use POST:
	var _useGet=false;
	if(postOrGet && postOrGet=="get") _useGet=true;
	
	var _self = this;
	
	//
	// Set the wait indicator, if present:
	//
	var _waitIndicator = false;
	if(waitIndicator) _waitIndicator=waitIndicator;
	
	//
	// Global state "constants":
	//
	var REQUEST_IS_UNINITIALIZED = 0;
	var REQUEST_IS_LOADING       = 1;
	var REQUEST_IS_LOADED        = 2;
	var REQUEST_IS_INTERACTIVE   = 3;
	var REQUEST_IS_COMPLETE      = 4;
	var REQUEST_STATUS_IS_OK     = 200;
	
	//
	// load()
	//
	// 
	this.load = function(){
		
		_request=false;
		
		if(window.XMLHttpRequest){
			try{
				_request = new XMLHttpRequest;
			}catch(e){
				_request = false;
			}
		}else if(window.ActiveXObject){
			try{
				_request = new ActiveXObject("Mxml2.XMLHTTP");
			}catch(e){
				try{
					_request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					_request = false;
				}
			}
		}
		if(_request){
			
			_request.onreadystatechange = processRequestChange;
			_request.open( (_useGet?"GET":"POST"),_url,true);
			_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			
			//
			// Show the wait indicator:
			//
			if(_waitIndicator) _waitIndicator.show(true);
			
			//
			// Sending the query string via POST :
			//
			_request.send(_queryTermsString);
			
		}
	}
	
	//
	// processRequestChange()
	//
	var processRequestChange = function(){
		
		// Only process if request show "loaded":
		if(_request.readyState == REQUEST_IS_COMPLETE){
			// Only if "OK":
			if(_request.status==REQUEST_STATUS_IS_OK){
				// Process entry nodes:
				_populatePage(_request);
				
				
			}else{
				alert("Unable to load XML data: "+_request.statusText);
			}
			//
			// Hide the wait indicator:
			//
			if(_waitIndicator) _waitIndicator.show(false);
		}
	}
	
	//
	// setQueryTermsString()
	//
	this.setQueryTermsString = function(queryTermsString){
		
		_queryTermsString = queryTermsString;
		
	}
	
	//
	// abort() : Public wrapper around XMLHttpRequest abort method:
	//
	this.abort = function(){
		
		//
		// NOTE: quirksmode.org has indicated the bug in Mozilla
		// which occurs when abort is used directly without
		// changing the onreadystatechange property to null:
		//
		if( typeof _request.readyState != "undefined" &&
		           _request.readyState != REQUEST_IS_UNINITIALIZED &&
		           _request.readyState != REQUEST_IS_COMPLETE 
		){
			_request.onreadystatechange = null;
			_request.abort();
			//
			// Hide the wait indicator:
			//
			if(_waitIndicator) _waitIndicator.show(false);
		}
		
	}
	
	//
	// getState() : Public function to retrieve the state of the XMLHttpRequest
	//
	this.getState = function(){
	
		return _request.readyState;
	
	}
	
	this.setOnReadyStateChangeToNull = function(){
		
		_request.onreadystatechange = null;
	
	}
	
}


