/**
 * This file is part of w@w.
 *
 * W@W web application framework.
 * Copyright (C) 2007 Catholic University of Louvain (Belgium)
 * <blambeau@info.ucl.ac.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

var AjaxModule = Class.create();
AjaxModule.prototype = {
	
	initialize : function() {
	},
	
	/** Invokes a method on the server
	  * @param qPath : the qpath of the action
	  *	@param parmams : the params to pass to the method
	  *	@param async : true for asynchronous mode
	  */
	wrmi : function(qPath, params, async) {
		if (params == null) {
			params = $H();
		}
		params["wajax_protocol"] = "wrmi";
		params["wrmi_action"] = qPath;
		this.request("waw.ajax", "get", params, this.handleWrmiResponse, async); 
	},
	
	/** Invokes a composition on the server
	  * @param qPath : the qpath of the view to compose
	  *	@param async : true for asynchronous mode
	  */
	wrci : function(qPath, async) {
		params = $H();
		params["wajax_protocol"] = "wrci";
		params["wrci_view"] = qPath;
		this.request("waw.ajax", "get", params, this.handleWrciResponse, async); 
	},
	
	/** Do a request on the server
	  * @param url : the base url to do the query
	  * @param method : the method to use ('get'/'post')
	  * @param params : the params to send (an array {key: value})
	  * @param async : the mode to use for the request (asynchronous = true/synchronous = false)
	  */
	request : function(url, method, params, callback, async) {
		var myAjax = new Ajax.Request("waw.ajax/", { method: 'get', 
						                     parameters: params,
						                     onComplete: callback,
						                     asynchronous: async});
	},					                     
	
	/** Handles a response from the server which results of a previous wrmi call 
	  * @param xr : the XMLHttpRequest object
	  */
	handleWrmiResponse : function(xr) {
		alert(xr.responseText);
	},
	
	/** Handles a response from the server which results of a previous wrci call 
	  * @param xr : the XMLHttpRequest object
	  */
	handleWrciResponse : function(xr) {
		alert(xr.responseText);
	},
	
	
	
};

var ajax = new AjaxModule();
