Prototype.AjaxHandler			= Class.create();
Prototype.AjaxHandler.prototype = {
	initialize: function(AjaxDIV) {
		this.divID				= AjaxDIV;
		this.checkAjaxDiv();
	},
	
	registerCallbacks: function() {
	},
	
	dispose: function() {
	},
	
	getParamObject: function( _name, _value ) {
		return { name: _name, value: _value };
	},
	
	setAjaxPage: function(newPage) {
		this.ajaxPage			= newPage;
	},
	
	getAjaxPage: function() {
		return this.ajaxPage;
	},
	
	// Ensure Ajax is created
	checkAjaxDiv: function() {
		// Using the $$(#) method did not seem to work properly for this...
		this.AjaxDIV		= document.getElementById(this.divID);
		if (this.AjaxDIV == null) {
			// document.writeln('<div id="' + this.divID + '" name="' + this.divID+ '"></div>');
			document.writeln('<div id="' + this.divID + '" name="' + this.divID+ '" class="hideLeft" style="display: none;"></div>');
		}
	},
	
	
	// Helper util function that creates an ojbect to hold a Call List member
	getCallListObj: function(callType, keyID, instanceRef) {
		return { type: callType, key: keyID, instance: instanceRef };
	},
	
	// Adds a Call List member to the Call List Array
	addToCallList: function(callType, keyID, instanceRef) {
		if (this.callList == null) {
			this.callList		= new Array();
		}
		
		this.callList.push( this.getCallListObj(callType, keyID, instanceRef) );
	},
	
	// Creates QueryString for Ajax call
	createParamQueryFromArray: function(callType, paramArray) {
		var params		= 'action=' + callType;
		for (var i=0; i < paramArray.length; i++) {
			params		+= ( params.length == 0 ? '?' : '&' ) + paramArray[i].name + '=' + paramArray[i].value;
		}
		return params;
	},
	
	// Creates Ajax response 
	callAjax: function(callType, paramArray, keyID, instanceRef) {
		var params			= this.createParamQueryFromArray(callType, paramArray);
		
		this.checkAjaxDiv();
		//alert('addToCallList\nType: ' + callType + '\n' + keyID + '\n' + instanceRef + '\n' + params);
		this.addToCallList(callType, keyID, instanceRef);
		
//alert('type : ' + callType + '\n' + params);
//document.write('<div style="border: 1px solid blue;">' + this.getAjaxPage() + params + '</div>');
		var results			= new Ajax.Updater(
			this.AjaxDIV,
			this.getAjaxPage(),
//			'/order_review/includes/ajax_review.asp',
			{method: 'get', parameters: params, onFailure: this.ajaxError, onSuccess: this.ajaxSuccess}
		);
	},
	
	// Creates Ajax response, custom callback
	callAjax: function(callType, paramArray, keyID, instanceRef, callbackFunction ) {
		var params			= this.createParamQueryFromArray(callType, paramArray);
		
		this.checkAjaxDiv();
		this.addToCallList(callType, keyID, instanceRef);
		
		var results			= new Ajax.Updater(
			this.AjaxDIV,
			this.getAjaxPage(),
			{method: 'get', parameters: params, onFailure: this.ajaxError, onSuccess: callbackFunction }
		);
	},

	
	// Determines based upon call type (results) which type of response back
	// to calling object is used
	handleCaller: function(type, key, results, instance) {
		try {
			instance.refreshFromJSON(results);
		} catch (ex) { }
	},
	
	// Finds entry in call list to call referencing call(s) to issue updates
	// *** kind of a hack to event handler
	resultsToCallList: function(results) {
		var key				= results.key;
		var callType		= results.type;
		
		for (var i = this.callList.length - 1; i >= 0; i--) {
			if (key == this.callList[i].key && callType == this.callList[i].type) {
				this.handleCaller(callType, key, results, this.callList[i].instance);
				// Removes the element
				this.callList.splice(i,1);
			}
		}
	},
	
	// Handles Ajax errors
	ajaxError: function() {
		alert('There was a problem retrieving results.\n\nPlease retry the given operation again.');
	},

	// Base retrieval function
	retrieveResults: function() {
		var results					= unescape( this.AjaxDIV.innerHTML );
		this.clearResults();
		if (results != null && results.length > 0) {
			try {
				var	json				= eval( '(' + results + ')');
				this.resultsToCallList(json);
			} catch (ex) {
				this.resultsToCallList(results);
			}
		}
	},
	
	// Clears out results from Ajax call
	clearResults: function() {
		this.AjaxDIV.innerHTML		= '';
	}
}

// Sets up Ajax handler class
window.AjaxWrapper			= new Prototype.AjaxHandler('AjaxDIV');

// This sets up an event handler to handle results when action is completed
Ajax.Responders.register({
	onComplete: function(){
		AjaxWrapper.retrieveResults();
	}
});
