
/**
 * Static class with methods associated
 * with GKO.Web.Controls.HTML5 C# controls
 */
Buttons = function()
{
	/**
	 * Submit the form that the button belongs to. The form
	 * can either be found automatically (default) or by passing
	 * the name of the form to submit as the second parameter
	 *
	 * @param jQuery A jQuery instance of the clicked button
	 * @param [string] The name of the form to submit
	 */
	function submit(elm, form)
	{
		Debug.trace('Buttons.submit')

		// search for specific form
		if (typeof(form) != undefined && form != null && form != '')
		{
			var frm = $("form[name='"+form+"']")
			
			if (frm.length == 1)
			{
				Debug.trace( frm )
				frm.submit()
				return
			}
		}
		
		// in case we have not found anything yet, traverse
		submitTraverse(elm)
	}
	
	/**
	 * Traverse the tree upwards from "elm" and submit
	 * the first form found
	 *
	 * @param jQuery A jQuery instance of the clicked button
	 */
	function submitTraverse(elm)
	{
		Debug.trace('Buttons.submitTraverse')
		
		var max = 500
		var cur = 0
		
		elm = $( elm.parent()[0] )
		
		while (true)
		{
			if (++cur == max)
				break
				
			try
			{
				if (elm.parent()[0].nodeName.toLowerCase() == 'form')
				{
					elm.parent()[0].submit()
					return
				}
			}
			
			catch (err)
			{
				break
			}
			
			elm = $( elm.parent()[0] )
		}
		
		throw 'Unable to submit - no form found'
	}

	return {
		"submit" : submit
	}
}()

