// <![CDATA[
/*
* Copyright: 2005 - this.year() SI Works Internet Solutions
* If you have come across this page, its cause you are snooping through code, and are generally a developer as such
* If you would like to use some of the code on this page, please simply email support@si-works.net and ask permission
* it would be much appreciated, as we have worked very hard on these fucntions and scripts
* 
* Note: We are not checking for DOM complience in any of these 
* functions, as all of the functions are called from another script
* where we will check for DOM (document.getElementById && document.createTextNode)
* this is generally called in the validation page per each form created.
*
* General class that performs and does all ajax calls
* @page ajax.class.js
* @version 1.4
* @author Greg Shiers, Jarratt Ingram, Roland Lovelock (SI Works Internet)
* @copyright: SI Works Internet Solutions 2005 - this.year()
*/
/* 
* Set the validation namespace "validation" and we can then define all other 
* functions within this namespace.
*/
function ajax() {
   //---------------------
   // Private Declarations
   //---------------------
   var _request = null;
   var _this = null;
   //--------------------
   // Public Declarations
   //--------------------
   this.GetResponseXML = function() {
      return (_request) ? _request.responseXML : null;
   }
   this.GetResponseText = function() {
      return (_request) ? _request.responseText : null;
   }
   this.GetRequestObject = function() {
      return _request;
   }
   this.InitializeRequest = function( method , uri ) {
      _InitializeRequest();
      _this = this;
      switch (arguments.length) {
         case 2:
            _request.open( method , uri );
            break;               
         case 3:
            _request.open( method , uri , arguments[2]);
            break;
      }
      if ( arguments.length >= 4 ) {
      	 _request.open ( method , uri , arguments[2] , arguments[3] );
      }
      this.SetRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
   }
   this.SetRequestHeader = function( field, value ) {
      if (_request) _request.setRequestHeader( field , value );
   }   
   this.Commit = function( data )  {
      if (_request) _request.send( data );
   } 
   this.Close = function( ) {
      if (_request) _request.abort();
   }   
   //---------------------------
   // Public Event Declarations.
   //---------------------------
   this.OnUninitialize = function() { };
   this.OnLoading = function() { };
   this.OnLoaded = function() { };
   this.OnInteractive = function() { };
   this.OnSuccess = function() { };
   this.OnFailure = function() { };
   //---------------------------
   // Private Event Declarations
   //---------------------------
   function _OnUninitialize() { _this.OnUninitialize(); };
   function _OnLoading() { _this.OnLoading(); };
   function _OnLoaded() { _this.OnLoaded(); };
   function _OnInteractive() { _this.OnInteractive(); };
   function _OnSuccess() { _this.OnSuccess(); };
   function _OnFailure() { _this.OnFailure(); };
   //------------------
   // Private Functions
   //------------------
	function _InitializeRequest() {
		_request = _GetRequest();
		_request.onreadystatechange = _StateHandler;
	}       
	function _StateHandler( ) {
		switch ( _request.readyState ) {
			case 0:
				window.setTimeout("void(0)", 100);
				_OnUninitialize( );
			break;               
			case 1:
				window.setTimeout("void(0)", 100);
				_OnLoading( );
			break;                
			case 2:
				window.setTimeout("void(0)", 100);
				_OnLoaded( );
			break; 
			case 3:
				window.setTimeout("void(0)", 100);
					_OnInteractive( );
			break;                
			case 4:
				if (_request.status == 200)
					_OnSuccess( );
				else {
					_OnFailure( );			
				}
			return;
			break;
		}
	}
	function _GetRequest( ) {
	var obj;
		try {
			obj = new XMLHttpRequest();
		}
		catch (error) {
			try {
				obj = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (error) {
				alert("Your browser does not support AJAX!");
				return null;
			}
		}
		return obj;
	}
}
// ]]>
