
	// Slash2 (www.slash2.nl)
	// Ajax Handling Class
	// (C) 2009, Slash2, Groningen, NL
	
	/*
	--------->  ENVIRONMENTAL SETTINGS  <---------
	*/
	
	/*
	--------->  INITIALIZATION  <---------
	*/

	function AjaxHandler(target_app) {
		
		this.target_app				= target_app;
		this.ajax_result			= '';
		this.ajax_result_type		= '';
		this.cleanUp();
		
	} // end function AjaxHandler
	
	// Clean AjaxHandler set-up
	AjaxHandler.prototype.cleanUp = function() {
	
		this.target_method			= 'GET';
		this.target_parser			= '';
		this.target_vars			= '';
		this.target_callback		= '';
		this.target_callback_eval	= '';
		this.target_eval_line		= '';
		this.target_var_prefix		= '';
	
		if(this.target_app == 'shop') {
			this.target_url = 'webshop_ajax.php';	
			this.target_var_prefix = 'shop_';	
		}
	
	} // end function set Parser
		
	/*
	--------->  REQUEST DEFINITION  <---------
	*/

	// Set Slash2 AJAX parser
	AjaxHandler.prototype.setParser = function(parser) {
	
		this.target_parser = parser;
	
	} // end function set Parser

	// Set HTTP request method
	AjaxHandler.prototype.setMethod = function(method) {
	
		if(method == 'post') {
			this.target_method = 'POST';
		} else {
			this.target_method = 'GET';
		}
	
	} // end function setMethod

	// Set Slash2 Ajax Callback
	AjaxHandler.prototype.setCallback = function(callback) {
	
		this.target_callback = callback;
	
	} // end function setCallback

	// Set Callback evaluation javascript commandline
	AjaxHandler.prototype.setCallbackEval = function(callback) {
	
		this.target_callback_eval = callback;
	
	} // end function setCallbackEval

	// Set Variable
	AjaxHandler.prototype.setVar = function(varname, value) {

		if(this.target_vars == '') {
			sign = '';
		} else {
			sign = '&';	
		}

		this.target_vars += sign + urlencode(this.target_var_prefix + varname) + '=' + urlencode(value);

	} // end function setVar
		
		
	/*
	--------->  REQUEST SUBMISSION  <---------
	*/

	// Submit request to AJAX
	AjaxHandler.prototype.sendRequest = function() {
	
		http_request = false;
		
		// Define request for Firefox, Safari, ...
		if (window.XMLHttpRequest) {
			
			http_request = new XMLHttpRequest();
			
			if (http_request.overrideMimeType) {

				//http_request.overrideMimeType('text/xml');
				//http_request.overrideMimeType('text/html');
				
			}

		// Define request for IE
		} else if (window.ActiveXObject) {
			
			try {
				
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {

				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}

			}
			
		}
		
		if (!http_request) {
			alert('Slash2\nAn error occured during the AJAX request (errorno. 1).\nThe HTTP request object could not be created.\nPlease try again');
			return false;
		}
		
		ajax_callback		= this.target_callback;
		ajax_callback_eval	= this.target_callback_eval;
		target_rand			= rand(1,1048576) + microtime(true);
		this.target_url		= ajax_rel_path + this.target_url + '?' + '_ajax_parser=' + this.target_parser + '&_ajax_randomizer=' + target_rand + '&' + this.target_vars;

		if(typeof(slash2_ajax_debug) != "undefined") {
			if(slash2_ajax_debug == true) {
				alert('Target URL: \n'+this.target_url);	
			}
		}

		http_request.onreadystatechange = this.catchResult;
		http_request.open(this.target_method, this.target_url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", this.target_vars.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(null);
		
		this.cleanUp();
	
	} // end function sendRequest
		
	/*
	--------->  RESULT HANDLING  <---------
	*/

	// Result handler
	AjaxHandler.prototype.catchResult = function() {
	
		// If readyState = ready
		if (http_request.readyState == 4) {
			 
			if(typeof(slash2_ajax_debug) != "undefined") {
				if(slash2_ajax_debug == true) {
					 alert('HTTP Status: \n' + http_request.status);
					 alert('HTTP Response: \n' + http_request.responseText);
				}
			}
			 
			// If HTTP status = 200 and result != error
			if (http_request.status == 200 && http_request.responseText != 'error') {
				
				// Save Ajax result
				ajax_result			= http_request.responseText;
				
			//	alert(http_request.getResponseHeader("Content-Type"));
				
				// Detect XML/text result type
				if(ajax_result.substring(0,5) == '<?xml') {
				
					ajax_result_type		= 'xml';
					ajax_result				= http_request.responseXML;
				
				} else {
				
					ajax_result_type		= 'text';	
				
				}
				
				// Callback for Javascript command line
				if(ajax_callback_eval != "" && ajax_callback_eval != undefined) {
					
					// eval(ajax_callback_eval);
					setTimeout(ajax_callback_eval,50);
					
				}
				
				// Callback for Slash2 Ajax callback functions
				if(ajax_callback != "" && ajax_callback != undefined) {
				
					// eval(ajax_callback + '(ajax_result, ajax_result_type)');
					setTimeout(ajax_callback + '(ajax_result, ajax_result_type)',50);
				
				}		 
				
			 } else {
				
				// Error notification
				alert('Slash2\nAn error occured during the AJAX request (errorno. 2).\nPlease try again');
				
			 }
			 
		}
	
	} // end function catchResult

