﻿function initCheckBox(elm)
{
	if (window._checkBoxFunctions == null) {
		window._checkBoxFunctions = 
		{	
			_onChangeHandler: [],
			_label: [],
					
			setVisible: function(val)
			{		
				this.style.visibility = val ? "visible" : "hidden";
				if (elm.getAttribute("hasText") == "1")
					this._label.style.visibility = this.style.visibility;
			},

			setDisplay: function(val)
			{
				this.style.display = val ? "" : "none"; this.setVisible(val);
				if (elm.getAttribute("hasText") == "1")
					this._label.style.display = this.style.display;
			},

			getEnabled: function() 		{ return !this.disabled; },
			setEnabled: function(val)	{ this.disabled = !val; },
			getInitValue: function()	{ return this._initValue; },
			setInitValue: function(val)	{ this._initValue = val; },
			getValue: function()		{ return this.checked; },
			setValue: function(val)		{ this.checked = castBool(val); },
			clear: function()			{ this.setValue(false); },
			reset: function()			{ this.setValue(this.getInitValue()); },
			
			onclick: function()
			{
				if (this._onChangeHandler)
					window.postAction("try {" + this.id + "._onChangeHandler(); } catch(e) {displayErr('CheckBox_OnChange', e);}");
			},

			setOnChangeHandler: function(handler) 
			{
				this._onChangeHandler = handler;
			},
			
			getTextValue: function() {return this.value;},
			setTextValue: function(val) {this.value = val;}
		};
	}

	// save a copy of any pre-initialized handlers
	var customOnClickHandler = elm.onclick;
	var initOnChangeHandler = elm._onChangeHandler;

	// add new interfaces 
	elm = extendElement(elm);
	elm = Object.extend(elm, window._checkBoxFunctions);
	
	// initialize
	elm._initValue = elm.checked;
	elm._label = document.getElementById(elm.id + "_Lbl");	

	// auto event wire up
	elm._onChangeHandler = (initOnChangeHandler ? initOnChangeHandler : getEventHandler(elm.id, "OnChange"));

	// allow override of onclick handler
	if (customOnClickHandler)
		elm.onclick = customOnClickHandler;
	
	return elm;
}
