/*______________
|       ______  |   U I Z E     J A V A S C R I P T     A P I
|     /      /  |   -----------------------------------------
|    /    O /   |    MODULE : Uize.Comm.Ajax Class (version 1.0.0)
|   /    / /    |    AUTHOR : Jan Borgersen, Chris van Rensburg (original code donated by Zazzle, Inc.), Ben Ilegbodu
|  /    / /  /| |    ONLINE : http://www.tomkidding.com/uize/uize-js-api
| /____/ /__/_| | COPYRIGHT : (c)2004-2006 UIZE
|          /___ |   LICENSE : Distributed under the terms of the GNU General Public License
|_______________|             http://www.gnu.org/licenses/gpl.txt
*/

/*
	DESCRIPTION
		Subclasses Uize.Comm to provide a communications layer to the server via XMLHttpRequest.

	REQUIRES
		- Uize.Comm.js (base class)

	TO DO
		- better handling of error
*/

/*ScruncherSettings Mappings="=c" LineCompacting="TRUE"*/

(function () {

	/*** Object Constructor ***/
		var
			_superclass = Uize.Comm,
			_class = _superclass.Ajax = _superclass.subclass (),
			_classPrototype = _class.prototype
		;

	/*** Public Instance Methods ***/
		_classPrototype.performRequest = function (_requestInfo,_callback) {
			var
				_this = this,
				_returnType = _requestInfo.returnType,
				_returnTypeIsObject = _returnType == 'object',
				_requestUrl = _requestInfo.url +
					(_requestInfo.url.indexOf ('?') > 0 ? '&' : '?') + 'comm_mode=ajax&output=js' +
					(_requestInfo.cache == 'never' ? ('&rnd=' + _class.getCacheDefeatStr ()) : ''),
				_requestData = _requestInfo.data || '',
				_requestMethod = _requestInfo.requestMethod,
				_requestMethodIsPost = _requestMethod == 'POST'
			;
			if (!_this._xmlHttpRequest)
				_this._xmlHttpRequest = window.XMLHttpRequest
					? new XMLHttpRequest ()
					: new ActiveXObject ('Microsoft.XMLHTTP')
				;
			_this._xmlHttpRequest.onreadystatechange = _class._handlers [_this.objectName] = function () {
				if (_this._xmlHttpRequest.readyState == 4) {
					_this._xmlHttpRequest.onreadystatechange = _class._doNothing;
					if (_this._xmlHttpRequest.status == 200) {
						if (_returnTypeIsObject || _returnType == 'xml')
							_requestInfo.responseXml = _this._xmlHttpRequest.responseXML
						;
						if (_returnTypeIsObject || _returnType == 'text')
							_requestInfo.responseText = _this._xmlHttpRequest.responseText
						;
						if (_returnTypeIsObject || _returnType == 'json')
							_requestInfo.responseJson = eval ('blah=' + (_this._xmlHttpRequest.responseText || 'null'))
						;

						_this._xmlHttpRequest.abort ();
						_callback ();
					} else {
						alert (
							'There was a problem retrieving the data:\n' +
							_this._xmlHttpRequest.statusText
						);
						_this._xmlHttpRequest.abort ();
					}
				}
			};
			if (_requestMethodIsPost && !_requestData) {
				var _queryPos = _requestUrl.indexOf ('?');
				_requestData = _requestUrl.substr (_queryPos + 1);
				_requestUrl = _requestUrl.substring (0,_queryPos);
			}
			_this._xmlHttpRequest.open (_requestMethod,_requestUrl,true);
			if (_requestMethodIsPost) {
				_this._xmlHttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
				_this._xmlHttpRequest.setRequestHeader('Content-length', _requestData.length);
			}
			_this._xmlHttpRequest.send (_requestData);
		};

	/*** Public Static Methods ***/
		_class._handlers = {};
}) ();

Uize.Comm.Ajax._doNothing = function () {};

Uize.Comm.Ajax._makeHandlerCaller = function (_handlerId) {
	return Uize.Comm.Ajax._handlers [_handlerId] ();
};


