<!--
/* 
Author: Kurtis Best
Created: February 22, 2006
Description: Runs the Ajax Engine to be used.
Version: 0.2.2

Updated: May 09, 2006
	- Previous version: 0.2.1
 	- Supports all browsers
	- Added functions: 
		CleanUp() - publicly kills the http request object
		Version() - retrieves the current version
Update: May 15, 2006
	- Added functionality to display errors and handle errors
	- Fixed responseFunction such that it allows for external function calls
Update: May 16, 2006
	- Fixed DisplayErrors parameters
*/

//class container for processing ajax
//constructor
function AjaxEngine() {
	
	//private variables
	var xmlhttp = false;
	var err = new Array();
	var errcount = -1;
	var dstId;
	var _version = "1.2.2";
	var _displayStatus = true;
	var _statusText = "Loading..";
	var _resFunction = false;
	var _callCount = 0;
	var _displayErrors = true;

	//private constants
	//const test = 1;
	
	//public variables
	AjaxEngine.prototype.responseText = "";
	AjaxEngine.prototype.Status = 0;
	AjaxEngine.prototype.readyState = 0;
	
	//public functions
	AjaxEngine.prototype.Post = Post;
	AjaxEngine.prototype.PostId = PostId;
	AjaxEngine.prototype.Get = Get;
	AjaxEngine.prototype.GetId = GetId;
	AjaxEngine.prototype.Version = Version;
	AjaxEngine.prototype.responseFunction = false ;
	AjaxEngine.prototype.getErrors = getErrors;
	AjaxEngine.prototype.clearErrors = clearErrors;
	AjaxEngine.prototype.errorCount = getErrorCount;
	AjaxEngine.prototype.setDisplayStatus = setDisplayStatus;
	AjaxEngine.prototype.getDisplayStatus = getDisplayStatus;
	AjaxEngine.prototype.setStatusText = setStatusText;
	AjaxEngine.prototype.getStatusText = getStatusText;
	AjaxEngine.prototype.setResponseFunction = setResponseFunction;
	AjaxEngine.prototype.setDestinationId = setDestinationId;
	AjaxEngine.prototype.writeToHTML = _writeToHTML;
	AjaxEngine.prototype.setDisplayErrors = setDisplayErrors;
	AjaxEngine.prototype.getDisplayErrors = getDisplayErrors;

	
	//process POST request
	function Post (url, sndData) {		
		try {
			_configureXMLHTTP();
			_sndXMLHTTP(url, 'POST', sndData);
		}
		catch(e) {
			_setError( e.message );
		}
	}
	
	//process POST request and renders the result to an id
	function PostId (url, sndData, destId) {		
		dstId = destId;
		Post(url,sndData);
	}
		
	//process GET request
	function Get (url) {	
		try {
			_configureXMLHTTP();
			_sndXMLHTTP(url, 'GET', null);
		}
		catch(e) {
			_setError( e.message );
		}
	}
	
	//process GET request and renders the result to an id	
	function GetId (url, destId) {	
		dstId = destId;
		Get(url);
	}
	
	//destroys the Ajax object
	function CleanUp() {
		xmlhttp = null;	
	}
	
	//version
	function Version() {
		return _version;
	}
	
	//sets whether status will be displayed
	function setDisplayStatus( value ) { _displayStatus = value; }
	
	//gets the current status
	function getDisplayStatus() { return _displayStatus; }
	
	//sets the text to be displayed
	function setStatusText( value ) { _statusText = value; }
	
	//gets the text to be diplayed
	function getStatusText() { return _statusText; }
	
	//sets the response function
	function setResponseFunction( value ) { _resFunction = value; }
	
	//sets the response function
	function setDestinationId( strValue ) { dstId = strValue; }
	
	//sets the Display Errors
	function setDisplayErrors( value ) { _displayErrors = value; }
	
	//gets the display errors
	function getDisplayErrors() { return _displayErrors; }
		
	//creates and configures xmlhttp
	function _configureXMLHTTP() {		
			xmlhttp = false;	
		
			try {
				if (window.XMLHttpRequest) { // Mozilla, Safari, ...
					xmlhttp = new XMLHttpRequest();
				} else if (window.ActiveXObject) { // IE
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}//end else if
			} 
			catch (e) { 
				xmlhttp = false;
			}
					
			//check for an error and if found exit
			if (!xmlhttp) {
				_setError( 'Cannot create an XMLHTTP instance' );
				return false;
			}
			else {
				//check if default function has been requested
				//if( !_resFunction  )
					xmlhttp.onreadystatechange = _processResponse;
				//else {
				//	xmlhttp.onreadystatechange = _resFunction;
				//}
			}			
			
			return true;
		}
	
	//Send information
	function _sndXMLHTTP(url, sndType, sndData) {		
		//continue to send url
		try {
			//ensures no caching is done
			if( url.indexOf("?") < 0 )
				url += "?";
			url += "&timer=" + new Date().getTime();
		
			xmlhttp.open(sndType, url, true);
			if( sndType == "POST" )
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.send(sndData);
			//display event status
			_writeEventStatus();
		}
		catch (e) {	_setError( e.message );	}
	}
	
	//writes content
	function _processResponse() {
		var iserror = false;
		var errstring;
		AjaxEngine.prototype.readyState = xmlhttp.readyState;
		//AjaxEngine.prototype.Status = xmlhttp.status;
		if ( xmlhttp.readyState == 4 ) { //message has been sent and returned
			if (xmlhttp.status == 200) { //stautus is not an error status
				//process response
				AjaxEngine.prototype.responseText = xmlhttp.responseText;
				if( !_resFunction ) { //check to ensure that client doesn't want to do his own processing
					if( dstId != null && dstId != "" ) {	
						document.getElementById(dstId).innerHTML = xmlhttp.responseText;
						dstId = null;
					}
				}
				else { 
						//eval( _resFunction() ); Was hiddin to allow for error functioning as well
				}//end else
						
			}
			else {
				//errors were detected
				errstring = "Error processing information ("+ xmlhttp.status +")"; //. <br/>Contact your administrator.";
				if( _displayErrors ) {
					AjaxEngine.prototype.responseText = errstring;
					if( !_resFunction )
						_writeToHTML( AjaxEngine.prototype.responseText );
				}
				else
					AjaxEngine.prototype.responseText = "";
					
				_setError(errstring);
			}
			if( _resFunction != false ) {
				eval( _resFunction() );
				//alert(_resFunction);
			}
			
			//kill xmlhttp only when status is 4
			CleanUp();		
			//alert("Inside engine: \n"+ AjaxEngine.prototype.responseFunction);
			//run response function 			
			//if( AjaxEngine.prototype.responseFunction  )
			//	AjaxEngine.prototype.responseFunction();
		}
		else {//still communication...not ready
			_writeEventStatus();
		}
		return false;
	}
	
	//displays status
	function _writeEventStatus() {
		try{
			if( _displayStatus ) { //check whether we want status
				if(_statusText != null && _statusText != "")
					_writeToHTML( _statusText );
				else 
					_writeToHTML( "Loading..." );
			}
		} catch(e){}
	}
	
	//writes to destination id
	function _writeToHTML( value ) {
		if(dstId != null && dstId !="" ) {
			document.getElementById(dstId).innerHTML = value;
		}
	}
	
	//set error
	function _setError(strErr) {
		if( errcount < 0 ) errcount = 0;
		err[errcount] = strErr;
		errcount++;
	}
	
	//get all errors
	function getErrors () {
		var strErrs="";
		var i;
		if(errcount > -1 )
		for(i=0;i<errcount;i++) {
			strErrs += err[i];
		}
		return strErrs;
	}
	
	//clear errors
	function clearErrors () {
		err = new Array();
		errcount = -1;
	}
	
	//gets the number of errors
	function getErrorCount () {
		if(errcount > -1)
			return errcount;
		else
			return 0;
	}

}
-->
