/**
 * <p>Title: servlet.js</p>
 * <p>Description: Consistent servlet management.</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Kreber Graphics, Inc.</p>
 * @author Charlie Reading
 */

// Includes
var servlet_js = 1;
var includes = "";
if (includes != "")
{
	alert("servlet.js: you must first include:\n" + includes);
}

_ServletIsProxy = ((self != top) && (typeof top.Servlet != 'undefined'));

var Servlet = (_ServletIsProxy) ? top.Servlet :
{
	isIgnoringActions: function()
	{
		return this._ignoringActions;
	},
	
	ignoreActions: function()
	{
		this._ignoringActions = true;
	},
	
	allowActions: function()
	{
		if (this._ignoringActions)
		{
			this._ignoringActions = false;
			top.document.body.style.cursor = "auto";
		}
	},
	
	doAction: function(servletName, action, parameters)
	{
		var options = this.toOptions(parameters || {}, servletName, action);
		this.doActionWithOptions(options);
	},
	
	doActionWithOptions: function(options)
	{
		// if ignoring, ignore!
		if ((this.isIgnoringActions()) && (! options.force))
		{
			return;
		}
		
		// get the form object
		var oForm = this.toForm(options);

		// load the form
		oForm.action = options.servletName;
		oForm.target = options.target;
		oForm.method = ((options.method != null) ? options.method : oForm.method);
		oForm.encoding = ((options.encoding != null) ? options.encoding : oForm.encoding);
		
		// load the action/subaction
		if (oForm.elements[options.actionName]) { oForm.elements[options.actionName].value = options.action; }
		if (oForm.elements[options.subActionName]) { oForm.elements[options.subActionName].value = options.subAction; }

		// form data
		var formData = $H(options.formData || {});
		formData.each(function(pair)
			{
				var e = oForm.elements[pair.key];
				if (e)
				{
					e.value = pair.value;
				}
				else
				{
					debug("servlet.doActionWithOptions: "+pair.key+" field not found in "+oForm.name+" in "+oForm.ownerDocument.location.pathname);
				}
			});
		
		// show wait cursor
		top.document.body.style.cursor = "wait";	
		
		// ignore actions
		this.ignoreActions();
		
		// submit
		oForm.submit();
	},

	toOptions: function(parameters, servletName, action)
	{
		var options = Object.extend({
			servletName:			servletName,
			action:						action,
			subAction:				"",
			force:						false,
			encoding:					null,
			method:						null,
			actionName:			Servlet.kAction,
			subActionName: 	Servlet.kSubAction,
			document:					document,
			form:							Servlet.kForm,
			target:						Servlet.kTarget,
			formData:					null	// array of {name, value} pairs
			}, parameters || {});
		return options;
	},
	
	toForm: function(options)
	{
		var oForm = ((typeof options.form != "string") 
				? options.form 
				: ((options.form.indexOf('.') >= 0)
						? eval(options.form)
						: options.document.forms[options.form]));
		return oForm;
	},
	
	// privates
	_ignoringActions: false
};


//
// Constants (mutable)
//

Servlet.kForm = "superform";
Servlet.kAction = "servletAction";
Servlet.kSubAction = "servletSubAction";
Servlet.kTarget = "_self";


// Ensure a clear slate if proxy
if (_ServletIsProxy)
{
	Servlet.allowActions();
}
