// JScript File


var SetValue = function()
{
	return {
		Options:
		{
			AppendToEnd		:new Object(),
			Replace			:new Object()
		}
};}();//Options


Object.extend(Element,
{
	__onReadyElements: new Array(),
	
	onready:	function()
	{
		var a = arguments;
		
		if(!a || a.length == 0)
		{
			var i=0;
			var j=0;
			var k=0;
			var thisObj = this;
			var I = setInterval(function()
			{
				var fn = thisObj.__onReadyElements[i];
				if(fn)
				{
					var obj;
					if(obj = $(fn.element))
					{
						fn.method();
						thisObj.__onReadyElements[i] = null;
						k++;
					}//if.obj
				}
				else if(k==thisObj.__onReadyElements.length || j>50000)
					clearInterval(I);
				i = (i > thisObj.__onReadyElements.length) ? 0 : (i+1);
				j++;
			},10);
		}/*******#
		*
		*
		*	Public overload ([0] :String, [1] :Function) :Void
		*
		*#*/
		else if(a.length == 2 && typeof a[0] == "string" && typeof a[1] == "function")
		{
			if(a[0]) this.__onReadyElements.push({element: a[0], method:a[1]});
		}/*******#
		*
		*
		*	Public overload ([0] :Form Field Object, [1] :Function) :Void
		*
		*#*/
		else if(a.length == 2 && typeof a[0] == "object" && typeof a[1] == "function")
		{
			this.__onReadyElements.push({element: $(a[0]), method:a[1]});
		}/*******#
		*
		*
		*	Public overload ([0] :Form Field Object, [1] :Function [, [2] :Form Field Object, [3] :Function]...) :Void
		*
		*#*/
		else if(a.length > 2 && typeof a[0] == "object" && typeof a[1] == "function")
		{
			if(a.length%2)
				throw "Error Element.onready(Overload 3) - Object, Method arguments are not coupled.";
			
			
		}
		else
		{
			throw "Error, no overload for Element.onready matches arguments provided";
		}
		//overloads
		
	},//onready()
	
	setValue: function(field, value)
	{		
		var v1 = value != null ? value.toString().toUpperCase() : "";
		var obj;
		if(obj = $(field))
		{
			if(obj.tagName)
			{
				switch(obj.tagName.toUpperCase())
				{
					case "INPUT":
						if(obj.type)
						{
							var t = obj.type.toUpperCase();
							if(t == "TEXT" 
								|| t == "PASSWORD"
								|| t == "CHECKBOX"
								|| t == "HIDDEN"
								|| t == "RADIO"
								|| t == "SUBMIT"
								|| t == "BUTTON"
							) 
							{
								obj.value = value.toString();
								return true;
							}
						}//if.obj.type
						break;
					case "TEXTAREA":
						if(	Prototype.Browser.IE
						||	Prototype.Browser.Opera)
							obj.innerText = value.toString().replace("\r\n","\n").replace("\n\r","\n").replace("\r","\n");
						else
							obj.innerHTML = value.toString().replace("\n","\n\n");
						break;
					case "DIV":
						obj.innerHTML = value.toString();
						return true;
						break;
					case "SELECT":
						var flag = false;
						var i=0;
						var selectIndex = typeof value == "number" ? value : -1;
						var val;
						
						if(selectIndex > -1 && (val = obj.options[selectIndex]))
						{
							val.selected = true;
						}
						
						for(var v in obj.options)
						{
							if(typeof obj.options[v] == "function") continue;
							var ele;
							if(ele = $(obj.options[i]))
							{
								if(selectIndex > -1 && ele != val)
								{
									ele.selected = false;
								}		
								else if(ele.value.toUpperCase() == v1 && !flag)
								{
									flag = true;
									ele.selected = true;
								}
								else
									ele.selected = false;
							}//if.ele
							i++;
						}
						if(flag) return true;
						break;
					case "TABLE": // this is for asp:RadioButtonList && asp:CheckBoxList Compat..
						var kids;
						if(kids = obj.getElementsByTagName("INPUT"))
						{
							var i=0;
							for(var v in kids)
							{
								if(typeof kids[v] == "function") continue;
								var kid = kids[i];
								if(	kid
									&& kid.type 
									&& (kid.type.toUpperCase() == "CHECKBOX" || kid.type.toUpperCase() == "RADIO")
									&& kid.name
									&& kid.name == obj.id
								)
								{	
									kid.checked = kid.value.toUpperCase() == v1 ? true : false;
								}//if.kids
								i++;
							}//for.kids
						}//if.kids
						break;
				}//switch.obj
			}//if.obj.tagName
		}//if.obj
		
		return false;
	},//setValue
	
	
	
	IsFormField :function()
	{
		var a = arguments;
		var F = Prototype.BrowserFeatures;
		
		
		/*******#
		*
		*
		*	Public overload ([0] :String) :Boolean
		*
		*#*/
		if(a.length == 1 && typeof a[0] == "string")
		{
			return Element.IsFormField($(a[0]));			
		}/*******#
		*
		*
		*	Public overload ([0] :Form Field Object) :Boolean
		*
		*#*/
		else if(a.length == 1 && typeof a[0] == "object")
		{
			var element = a[0];
			return (
				!element 
				|| !element.tagName 
				|| element.nodeType == 3 
				|| element == window
				)
				? false
				: true;
		}//overloads
	}//IsFormField
});



function $V()
{
	var a = arguments;
	if(!a || a.length == 0)
	{
		throw "Error, no overload of $V takes 0 arguemnts";
	}//if.!a
	
	

	/*******#
	*
	*
	*	Public overload ([0] :Form Field Object, [1] :String, [2] :Number | :String) :Void
	*
	*#*/
	if(a.length == 2 && typeof a[0] == "object" && (typeof a[1] == "string" || typeof a[1] == "number"))
	{
		Element.setValue(a[0], a[1]);
	}/*******#
	*
	*
	*	Public overload ([0] :String, [1] :String, [2] :Number | :String) :Void
	*
	*#*/
	else if(a.length == 2 && typeof a[0] == "string" && (typeof a[1] == "string" || typeof a[1]== "number"))
	{
		Element.setValue(a[0], a[1]);
	}/*******#
	*
	*
	*	Public overload ([0] :Form Field Object, [1] :String, [2] :SetValue.Options) :Void
	*
	*#*/
	else if(a.length == 3 && typeof a[0] == "object" && typeof a[1] == "string" && (a[2] == SetValue.Options.AppendToEnd || a[2] == SetValue.Options.Replace))
	{
		if(a[2] == SetValue.Options.AppendToEnd)
		{
			Element.setValue(a[0],$F(a[0])+ '' + a[1]);
		}
		else
		{
			$V(a[0],a[1]);
		}//if.a[2]
	}/*******#
	*
	*
	*	Public overload ([0] :String, [1] :String, [2] :SetValue.Options) :Void
	*
	*#*/
	else if(a.length == 3 && typeof a[0] == "string" && typeof a[1] == "string" && (a[2] == SetValue.Options.AppendToEnd || a[2] == SetValue.Options.Replace))
	{
		if(a[2] == SetValue.Options.AppendToEnd)
		{
			Element.setValue(a[0],$F(a[0])+ '' + a[1]);
		}
		else
		{
			$V(a[0],a[1]);
		}//if.a[2]
	}//overloads
};//$V()




var Maples = Class.create();





Maples.Form = Class.create();
Object.extend(Maples.Form.prototype,{

	_formObj: new Object(),
	ByValue: new Number(),
	
	
	initialize: function(formName)
	{
		this.ByValue = 233232;
		var thisObj = this;
		
		Event.observe(document,'dom:loaded', function(){
		 thisObj.lazyInit(formName);
		});
		
	},//initialize()
	

	
	
	
	
	EnableInput: function()
	{
		var args = arguments;
		
		if(!args)
		{
			alert("Error No overload for Maples.Form.EnableInput takes 0 arguments");
			return;
		}//if.!args
		
		/***#
		 * 
		 * Public overload (args[0]: String)
		 */
		if(args.length == 1 && typeof args[0] == "string")
		{
			Field.enable($(args[0]));
		}
		/***#
		 *
		 * Public overload (args[0]: Form Field Object)
		 */
		else if(args.length == 1 && typeof args[0] == "object")
		{
			Field.enable(args[0]);
		}
		/***#
		 *
		 * Public overload (args[0] :String , args[1] :String *Radio)
		 * 
		 * Examples:
		 *		RadioButtonList:
		 *			<asp:RadioButtonList ID="myList">...</asp:RadioButtonList>
		 *			Form.EnableInput("<%=myList.ClientID%>","<%=myList.Items[0].Value%>");
		 *			Form.EnableInput("<%=myList.ClientID%>","<%=myList.SelectedValue%>");
		 */
		else if(args.length == 2 && typeof args[0] == "string" && typeof args[1] == "string")
		{
			var obj;
			var val = args[1].toUpperCase();
			
			// does the object exist?
			if(obj = $(args[0]))
			{
				alert(typeof obj.getElementsByTagName("input"))
				// cause radio button list usually is nested in table tag,
				// get input fields out of tag
				var ch;
				if(ch = obj.getElementsByTagName("input"))
				{
					var i=0;
					for(var v in ch)
					{
						if(typeof ch[v] == "function") continue;
						var j;
						if((j = $(ch[i])) && j.value && j.value.toUpperCase() == val)
						{
							Field.enable(j);
							return;	
						}//if.j
						i++;
					}//for.v
				}//if.ch
			}//if.obj
		}//elseif
		
	},//EnableInput
	
	
	
	CancelSubmit: function()
	{
		_formObj.onsubmit = function(){return false;}
	},
	
	
	EnableSubmit: function()
	{
		_formObj.onsubmit = null;
	},
	
	
	
	lazyInit: function(formName)
	{
		var obj;
		if(!(obj = $(formName)))
				alert("Error, could not find form");
				
		_formObj = $(formName);
		
	}//lazyInit()
	
	
	
	

});//Maples.Form :Class