document.write('<link href="' + XPS_INCLUDE_PATH_PUBLIC + '../wfs/css/wfs.css" rel="stylesheet" type="text/css" />');

function WFS_XHR(module, action, data, parameters) {
	parameters = 
		Object.extend(
			{
				callback: null,
				icon: null,
				icon_mode: 'icon'
			}, 
			parameters || {}
		)
	;
	if (parameters['icon']) {
		parameters['icon'] = XPS_elem(parameters['icon'], true);
		if (parameters['icon']) {
			// container mode for icon
			// insert icon in container
			if (parameters['icon'].tagName != 'IMG') {
				parameters['icon_mode'] = 'container';
				parameters['icon'].update(new Element('img', { src: XPS_INCLUDE_PATH_PUBLIC + 'inc/ico/' + XPS_LAYOUT_THEME + '/xhr_pending.gif' }));
			}
		}
	}
	
	var ajax_parameters = 
		{
			wfs_xhr: true,
			wfs_xhr_module: module,
			wfs_xhr_action: action
		}
	;
	
	// merge data with ajax parameters
	for (var i in data) { ajax_parameters['wfs_xhr_param_' + i] = data[i]; }
	
	// xhr
	new Ajax.Request(
		window.location.toString().substring(0, window.location.toString().indexOf('?')) + '?',
		{ 
			parameters: ajax_parameters,
			onSuccess: xhr_callback
		}
	);
	
	function xhr_callback(transport, reply) {
		// icon
		if (parameters['icon']) {
			if (parameters['icon_mode'] == 'icon') {
				
			}
			else {
				parameters['icon'].update();
			}
		}
		
		if (!reply) reply = transport.responseJSON;
		
		if (reply == 'false') reply = false;
		if (reply == 'true') reply = true;
		
		if (parameters['callback']) {
			return(parameters['callback'](reply));
		}
		return(reply);
	}
}

function WFS_XHR_deprecated(module, action, data, callback) {
	// stand-alone XHR operation
	// instantiate new XHRClass object
	var oXHR = new XHRClass();
	if (!oXHR) return(false);
	
	var params = '';
	// build parameter array
	for (var i in data) {
		if (i in Array.prototype) continue;
		params = params + '&wfs_xhr_param_' + i + '=' + escape(data[i]);
	}
	var query = '?&wfs_xhr=1&wfs_xhr_module=' + module + '&wfs_xhr_action=' + action + params;
	
	// send XHR
	oXML = oXHR.send(query, true, { callback: xhr_callback });
	
	function xhr_callback(oXML) {
		if (!oXML) return(callback(false));
		if (!oXML.hasChildNodes) return(callback(oXML));
		
		// convert XML to array
		data = oXHR.reply2MultiDimensionalArray(oXML);
		
		if (!data) return(callback(false));
		return(callback(data));
	}
	
	return(null);
}
