/**
** Version: 1.0.3
** Coded by: Przemyslaw Sidz
**/

var WebService = (function () {
	
	// private variables
	var $_a_References = [];
	var $_a_Methods = [];
	
	// function to determine which object is being used
	var $_f_GetObject = function ($po_obj) {
		
		var $i;
		
		for ($i = 0; $i < $_a_References.length; ++$i) {
			if (($_a_References [$i].$o_self == $po_obj)) {
				return $_a_References [$i].$o_data;
			}
		}
		
		return null;
	};
	
	// callback handler function called at interval rate
	var $_f_onReadyStateChangeHandler = function () {
		
		var $i, $o_object;
		
		for ($i = 0; $i < $_a_References.length; ++$i) {
			$o_object = $_a_References [$i].$o_data;
			
			if (($o_object.$f_callback != undefined) &&
					($o_object.$o_xmlHttp.readyState == 4)) {
				if ((typeof $o_object.$f_callback == 'function')) {
					$o_object.$f_callback (
							($o_object.$o_xmlHttp.responseText != null) ?
							($o_object.$o_xmlHttp.responseText) : (''));
				}
				$o_object.$f_callback = undefined;
			}
		}
	};
	
	// helper function to create XMLHttpRequest object
	var $_f_GetXmlHttpRequest = function () {
		
		var $o_xmlHttp = null;
		
		if ((window.XMLHttpRequest != undefined)) {
			$o_xmlHttp = new XMLHttpRequest();
		} else if ((window.ActiveXObject != undefined)) {
			$o_xmlHttp = new ActiveXObject ('Microsoft.XMLHTTP');
		}
		
		return $o_xmlHttp;		
	};
	
	// helper function to determine if request is available
	var $_f_isRequestReady = function ($po_webService) {
		
		var $b_ready = (($po_webService.$f_callback == undefined) &&
									 (($po_webService.$o_xmlHttp.readyState == 4) ||
									 ($po_webService.$o_xmlHttp.readyState == 0)));
		
		return $b_ready;
	};
	
	/******************************************************************/
	
	// class constructor
	var $__construct = function () {
		
		$_a_References [$_a_References.length] = ({
			$o_self				:		this,
			$o_data				:		{
				$o_xmlHttp	:		$_f_GetXmlHttpRequest (),
				$f_callback	:		undefined
			}
		});
	};
	
	// createRequest method
	$__construct.prototype.createRequest = function (
		$ps_url, $pf_callback, $ps_get_params, $ps_post_params) {
		
		var $o_object = $_f_GetObject (this);
		var $i;
		
		if (($_f_isRequestReady ($o_object))) {
			
			// if get parameters not supplied , set to null;
			if (($ps_get_params == undefined)) {
				$ps_get_params = null;
			}
			
			// automatic encoding of get parameters
			if (($ps_get_params != null) &&
					($ps_get_params.constructor == Array)) {
				
				for ($i = 0; $i < $ps_get_params.length; ++$i) {
					$ps_get_params [$i] =
										encodeURIComponent ($ps_get_params [$i][0]) + '=' +
										encodeURIComponent ($ps_get_params [$i][1]);
				}
				
				$ps_get_params = $ps_get_params.join ('&');
			}
			
			// if post parameters not supplied , set to null;
			if (($ps_post_params == undefined)) {
				$ps_post_params = null;
			}
			
			// automatic encoding of post parameters
			if (($ps_post_params != null) &&
					($ps_post_params.constructor == Array)) {
				
				for ($i = 0; $i < $ps_post_params.length; ++$i) {
					$ps_post_params [$i] =
										encodeURIComponent ($ps_post_params [$i][0]) + '=' +
										encodeURIComponent ($ps_post_params [$i][1]);
				}
				
				$ps_post_params = $ps_post_params.join ('&');
			}
			
			// append get parameter string to url if provided
			if ((typeof $ps_get_params == 'string')) {
				$ps_url += '?' + $ps_get_params;
			}
			
			// open a connection
			$o_object.$o_xmlHttp.open ('POST', $ps_url, true);
			
			// add necessary request headers
			$o_object.$o_xmlHttp.setRequestHeader ("content-type",
														"application/x-www-form-urlencoded");
			$o_object.$o_xmlHttp.setRequestHeader ("content-length",
														$ps_post_params ? $ps_post_params.length : 0);
			
			// check if params is a string
			if (typeof $ps_post_params != 'string') {
				
				// if not, make it a (zero-length) string
				$ps_post_params = '';
			}
														
			// send the params
			$o_object.$o_xmlHttp.send ($ps_post_params);
			
			// set the callback function
			$o_object.$f_callback = $pf_callback;
		}
	}
	
	// call callback handler at an interval rate
	setInterval ($_f_onReadyStateChangeHandler, 50);
	
	// return constructor
	return $__construct;
})();
