/*
 * Function that uses Prototype to invoke an action of a form. Slurps the values
 * from the form using prototype's 'Form.serialize()' method, and then submits
 * them to the server using prototype's 'Ajax.Updater' which transmits the request
 * and then renders the resposne text into the named container.
 *
 * @param form reference to the form object being submitted
 * @param event the name of the event to be triggered, or null
 * @param container the name of the HTML container to insert the result into
 */
 
var FIELD_SOURCE_PAGE = "_sourcePage";

function invoke(form, event, container) {
	Logger.debug(">> invoke");
    var params = Form.serialize(form);
    if (event != null) params = event + '=&' + params;
    if (Logger.isDebugEnabled()) {
		Logger.debug("form.action = " + form.action);
		Logger.debug("event = " + event);
		Logger.debug("params = " + params.truncate(100));
    }
    new Ajax.Updater(container, form.action, {method:'post', postBody:params});
	Logger.debug("<< invoke");
}

function invokeRequest(form, event, onCompleteCall, asynch, useFormParams, additionalParams) {
	
	if (additionalParams == null) {
		additionalParams = "";	
	}
	
	if (useFormParams == null) {
		useFormParams = false;
	}
		
	if (useFormParams) {
		additionalParams += "&" + serializeForm(form);
	}
	
	invokeRequestUrl(form.action, event, onCompleteCall, asynch, additionalParams);
}

function invokeRequestUrl(actionUrl, event, onCompleteCall, asynch, additionalParams) {
	Logger.debug(">> invokeRequest");
	if (asynch === null) {
		asynch = true;
	}
	
	var params = "";
	
	if (additionalParams != null) {
		params += "&" + additionalParams;
	}
	
	if (event != null) params = event + '=&' + params;
	Logger.debug("event = " + event);
	Logger.debug("params = " + params.truncate(100));
	Logger.debug("asynch = " + asynch);
	var ajaxReq = new Ajax.Request(actionUrl, {asynchronous: asynch, method: 'post', postBody:params, onComplete: onCompleteCall});

	Logger.debug("<< invokeRequest");
}

function invokeAction(action, params, event, container) {

    if (event != null) params = event + '=&' + params;
    new Ajax.Updater(container, action, {method:'post', postBody:params});

}

function invokeWithMessage(form, event, container, message) {
	Element.update(container, message);
	
	invoke(form, event, container);
}

/**
 * foreRequest - Invoke a request in the foreground (non-AJAX)
 * @param {Form} form 
 * @param {String} event (opt)
 * @param {String} parameter string (opt)
 */
function foreRequest(form, event, params) {
 	Logger.debug(">> foreRequest");
 	if (params == null) 
 		params = Form.serialize(form);
 	
 	if (event != null) 
 		params = event + "=&" + params;
 	
 	var u = form.action + "?" + params;
 	
	Logger.debug("url = " + u);
	
	window.location = u;
	
 	Logger.debug("<< foreRequest");
}

/**
 * Serialize the form, skipping buttons and submits.
 * @param theForm The form to serialize
 */
function serializeForm(theForm) {
	var ser = "";
	var inputs = Form.getElements(theForm);
	inputs.forEach(function (input){
		if (input.type != "submit" && input.type != "button") {
			Logger.debug("serializing " + input.name + " (" + input.type + ")");
			ser += Form.Element.serialize(input);
			ser += "&";
		}
	});
	return ser;
}
	

var myGlobalHandlers = {
	onCreate: function(){
		Element.show('systemWorking');
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('systemWorking');
		}
	}
};


Ajax.Responders.register(myGlobalHandlers);
