var htmlHelper =
{
	/** 
	*	Check all checkboxes with specified name
	*
	*	@param		name		string				checkboxes name
	*	@param		ob			element				'select all' checkbox object
	*
	*	@return		void
	*/
	checkall: function(name, ob)
	{
		var checkboxes = document.getElementsByName(name);

		if (!checkboxes)
			return;
		
		if (checkboxes.length>0)
		{
			for(i=0; i<checkboxes.length; i++)
				checkboxes[i].checked = ob.checked;
		}
		else
		{
			checkboxes.checked = ob.checked;
		}
	},

	/** 
	*	Move cursor to the start of the textarea
	*
	*	@param		ob			element			textarea element
	*
	*	@return		void
	*/
	moveCaretToStart: function(ob)
	{
		if (ob.createTextRange)
		{
			var r = ob.createTextRange();
			r.collapse(true);
			r.select();
		}
	},

	/** 
	*	Move cursor to the end of the textarea
	*
	*	@param		ob			element			textarea element
	*
	*	@return		void
	*/
	moveCaretToEnd: function (ob)
	{
		if (ob.createTextRange)
		{
			var r = ob.createTextRange();
			r.collapse(false);
			r.select();
		}
	},

	/** 
	*	Delete all options of the dropdown select element.
	*
	*	@param		element		DOM element				dropdown element
	*
	*	@return		bool	success
	*/
	removeOptions: function (element)
	{
		element.options.length=0;
		return true;
	},

	/** 
	*	Add new options to the specified dropdown list.
	*
	*	@param		element		SELECT element			dropdown or multiselect element
	*	@param		options		hash<value:title>		options
	*
	*	@return		bool		success
	*/
	addOptions: function (element, options)
	{
		$H(options).each(function(pair)
		{
			var option = document.createElement("OPTION");
			element.options.add(option);
			option.innerHTML = pair.value;
			option.value = pair.key;
		});

		return true;
	},

	/** 
	*	Replace options in the specified dropdown list.
	*
	*	@param		element		SELECT element			dropdown or multiselect element
	*	@param		options		hash<value:title>		options
	*
	*	@return		bool		success
	*/
	replaceOptions: function(element, options)
	{
		htmlHelper.removeOptions(element);
		return htmlHelper.addOptions(element, options);
	},

	/** 
	*	Select options of multi-select element.
	*
	*	@param		element		SELECT element					element
	*	@param		value		list < string | number > 		values of the option to be selected
	*
	*	@return		bool		success
	*/
	selectOptions: function (multiselect, values)
	{
		$A(multiselect.options).each(function(option)
		{
			if (values.indexOf(option.value)!=-1)
				option.selected = true;
		});
		return true;
	},

	/** 
	*	Return current selected option
	*
	*	@param		element		SELECT element		multiselect element
	*
	*	@return		list < OPTION element >
	*/
	currentOptions: function (multiselect)
	{
		var selectedOptions = $A();
		$A(multiselect.options).each(function(option)
		{
			if (option.selected)
				selectedOptions[selectedOptions.length] = option;
		});

		return selectedOptions;
	},

	/** 
	*	Select option of dropdown select.
	*
	*	@param		dropdown	SELECT element			dropdown element
	*	@param		value		string | number			value(s) of the option to be selected
	*
	*	@return		bool		success
	*/
	selectOption: function (dropdown, value)
	{
		$A(dropdown.options).each(function(option)
		{
			if (option.value==value)
			{
				option.selected = true;
				return true;
			}
		});
		return false;
	},

	/** 
	*	Return current selected option
	*
	*	@param		dropdown	SELECT element		dropdown element
	*
	*	@return		OPTION element
	*/
	currentOption: function (dropdown)
	{
		return dropdown.options[dropdown.selectedIndex];
	},
		
	/** 
	*	Open popup window.
	*
	*	@param		url			string		URL of the document to display
	*	@param		name		string		name of the window, this name is used as the value for the TARGET attribute on a form or an a element:
	*											_blank		the url is loaded into a new, unnamed window. 
	*											_media		the url is loaded into the HTML content area of the Media Bar. Available in IE 6 or later. 
	*											_parent		the url is loaded into the current frame's parent. If the frame has no parent, this value acts as the value _self. 
	*											_search		available in IE5 and later. The url is opened in the browser's search pane. 
	*											_self		the current document is replaced with the specified url . 
	*											_top		url replaces any framesets that may be loaded. If there are no framesets defined, this value acts as the value _self. 
	*	@param		width		int			popup window width
	*	@param		height		int			popup window height
	*	@param		scrollbars	string		show/hide scrollbars on popup window [yes|no]
	*	@param		resizable	string		resizeable popup window [yes|no]
	*
	*	@return		window object
	*/
	popupWindow: function (url, name, width, height, scrollbars, resizable)
	{
		var conf = htmlHelper._initPopupConf(width, height);
		conf['scrollbars'] = scrollbars;
		conf['resizable'] = resizable;
		var features='';
		conf.each(function(pair)
		{
			features += pair.key+'='+pair.value+',';
		});
		return window.open(url, name, features);
	},

	/** 
	*	Open popup window and place into it an image tag (<img>).
	*
	*	@param		src			string			image URL
	*	@param		width		int				popup window width
	*	@param		height		int				popup window height
	*	@param		alt			string			image alt
	*
	*	@return		window object
	*/
	popupImage: function (src, width, height, alt)
	{
		var wnd = htmlHelper.popupWindow('', '', width, height);

		wnd.document.write("<html><head>\n");
		wnd.document.write("<title>"+alt+"</title></head>\n");
		wnd.document.write("<body topmargin=\"0\" leftmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" onKeyPress=\"if(window.event.keyCode == 27) window.close();\">\n");
		wnd.document.write("<img src=\""+src+"\" border=\"0\" alt=\""+alt+"\">");
		wnd.document.write("</body>");
		wnd.document.write("</html>");
		wnd.document.close();

		return wnd;
	},

	//** Implementation

	_initPopupConf: function (width, height)
	{
		var res=$H();

		var screenHeight	= browserHelper.getClientHeight();
		var screenWidth		= browserHelper.getClientWidth();

		res['top']			= (height < screenHeight)	? Math.floor((screenHeight - height)/2)	: 0;
		res['left']			= (width < screenWidth)		? Math.floor((screenWidth - width)/2)	: 0;
		res['width']		= Math.min(width, screenWidth);
		res['height']		= Math.min(height, screenHeight);
		res['scrollbars']	= (res['width'] > screenWidth || res['height'] > screenHeight) ? 'yes' : 'no';
		res['resizable']	= 'yes';

		return res;
	}
}