/**
 * 
 * @author Istvan Vincze (istvan@vincze.biz)
 * @param {string} formId The ID of the form we're submitting.
 * @param {string} targetFile The name of the file to which the form will be submitted to for processing.
 * @param {string} responseContId The ID of the element inside which the response will be displayed using innerHTML.
 * @param {string} failureMessage The message to include on failure - displayed before the returned data.
 * @param {string} successMessage The message to include on success - displayed before the returned data.
 * 
 * 
 * Usage:
 * 
 * Include this in <head>:
 * 
 * // Initiate the form function
 * YAHOO.util.Event.on(window, 'load', saveData.init, saveData, true);
 * 
 */

//-----------------------------------------------------------------------------------
// Ajax Form sender 
//-----------------------------------------------------------------------------------

var saveData = function() {

	return {

		init : function() {

			var processSubmit = function(e) {

				var responseObj	= document.getElementById(responseContId);
				
				var success = function(o) {
					var theResponse = o.responseText;
					var responseArray = theResponse.split(' | ');
					responseObj.innerHTML = successMessage+responseArray[1];
					if (responseArray[0] == 'OK') {
						responseObj.className = 'responseContainerOK';
						document.getElementById(formId).reset();
						}
					else {
						responseObj.className = 'responseContainerFAIL';
						}
					};

				var failure = function(o) { 
					responseObj.innerHTML = failureMessage+o.responseText;
					responseObj.className = 'responseContainerFAIL';	
					};

				var callback = {
					success : success,
					failure : failure
					};

				var theUrl = targetFile+'?rtype=ajax';
				YAHOO.util.Connect.setForm(formId);
				var connectionObject = YAHOO.util.Connect.asyncRequest('GET', theUrl, callback);
				
				YAHOO.util.Event.stopEvent(e);
				};

			YAHOO.util.Event.on(formId, 'submit', processSubmit);
			}
		}
	}();

YAHOO.util.Event.onAvailable(formId, saveData.init, saveData, true);
