/*  Juice Library ajax.juice.js, version 0.1.1.20071231
 *  Copyright (c) 2007, 2008 Stephen Whiteley (http://jui.ce.it)
 *
 *  See core.juice.js for full license.
 *
/*--------------------------------------------------------------------------*/

(function() {

	if ( typeof Juice == 'undefined' ) {
		throw new Error( 'ajax.juice.js requires the core.juice.js component.' );
	}

	Juice.Ajax = function() {

		var xhttpo, timer, myself = this;

		this.setup = function() {

			try {
				xhttpo = new ActiveXObject( 'Msxml2.XMLHTTP' );
			} catch ( e ) {
				try {
					xhttpo = new ActiveXObject( 'Microsoft.XMLHTTP' );
				} catch ( e ) {
					xhttpo = false;
				}
			}

			if ( ( !xhttpo ) && ( typeof XMLHttpRequest != 'undefined' ) ) {
				try {
					xhttpo = new XMLHttpRequest();
				} catch ( e ) {
					xhttpo = false;
					myself.error( 'XHTTP object could not be created' );
				}
			}

		};

		this.request = function( url, method, cache, timeout ) {

			if ( !xhttpo ) { return false; }

			var params;

			if ( !url ) { myself.error( 'No request url supplied' ); }
			if ( !method ) { method = 'GET'; }

			method	= method.toUpperCase();
			timeout	= typeof timeout == 'number' ? timeout : false;

			if ( method == 'POST' ) {

				var position = url.indexOf( '?' );

				if ( position !== -1 ) {
					this.page	= url.substring( 0, position );
					this.url	= cache ? this.page : myself.nocache( this.page );
					this.params	= url.substring( position + 1, url.length );
				} else {
					this.url	= allowCache ? url : myself.nocache( url );
				}

				this.method			= 'POST';
				this.contentType	= 'application/x-www-form-urlencoded;';

			} else {

				this.method			= 'GET';
				this.url			= cache ? url : myself.nocache( url );
				this.params			= false;
				this.contentType	= 'text/plain';

			}

			try {

				xhttpo.open( this.method, this.url, true );
				xhttpo.setRequestHeader( 'Content-Type', this.contentType );
				if ( this.contentLength ) xhttpo.setRequestHeader( 'Content-Length', this.contentLength );
				xhttpo.onreadystatechange = myself.response;
				xhttpo.send( this.params );

				if ( timeout ) timer = setTimeout( this.abort, ( timeout * 1000 ) );

			} catch ( e ) {
				myself.error( e );
			}

		};

		this.abort = function() {

			xhttpo.abort();
			myself.error( 'Request aborted' );

		};

		this.response = function() {

			if ( xhttpo.readyState != 4 ) { return; }

			if ( timer ) {
				clearTimeout( timer );
			}

			try {
				if ( xhttpo.status == '200' ) {
					if ( xhttpo.responseText ) {
						return myself.ready( xhttpo.responseText );
					} else {
						myself.error( 'No response text defined.' );
					}
				} else {
					myself.error( 'Request URL responded with a ' +  xhttpo.status + ' status' );
				}
			} catch ( e ) {
				myself.error( e );
			}

		};

		this.evaluate = function( res ) {

			try {
				return eval( 'res = ' + res );
			} catch( e ) {
				myself.error( 'Response could not be evaluated' );
			}

			return false;

		};

		this.error = function( err ) {

			Juice.Debug( new Error( 'Juice.Ajax: ' + err ) );

		};

		this.nocache = function( url ) {

			var preventCache = 'nocache=' + new Date().getTime();

			if ( url.indexOf( '?' ) !== -1 ) {
				url += '&' + preventCache;
			} else {
				url += '?' + preventCache;
			}

			return url;

		};

		this.encode = function( values ) {

			return Juice.Format.encode( values );

		};

		this.setup();

	}

})();