/* ===================================================================================================
 * booleanControl
 * ===================================================================================================
 */

/// cell: html-div-Element
/// descr: <columnDescriptor>
booleanControl_setup = function(cell, txt)
{
	var descr = cell.description;
	
	cell.enabled = false;
	cell.setAttribute("contenteditable", "true");
	cell.setAttribute("class", cell.getAttribute("class") + " boolean");
	addControlEvent(cell, "focus", function(event) { booleanControl_onFocus(cell, event); });
	addControlEvent(cell, "blur", function(event) { booleanControl_onBlur(cell, event); });
	addControlEvent(cell, "paste", function(event) { booleanControl_onPaste(cell, event); });
	addControlEvent(cell, "beforeinput", function(event) { booleanControl_onBeforeInput(cell, event); });
	addControlEvent(cell, "keydown", function(event) { booleanControl_onKeyDown(cell, event); });
	addControlEvent(cell, "contextmenu", function(event) { control_onContextMenu(cell, event); });
	addControlEvent(cell, "click", function(event) { booleanControl_onClick(cell, event); });
	cell.enabled = descr.enabled && parseInt(descr.enabled);
	booleanControl_setText(cell, txt);
};

booleanControl_setText = function(cell, txt)
{
	cell.value = parseIntEx(txt);

	if (cell.value)
	{
		cell.innerHTML = '<img src="icon24_checked.png">';
	}
	else
	{
		cell.innerHTML = "";
	}
};

booleanControl_onFocus = function(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
		gridControl_cellNotify(cell, "onSetFocus");
};

booleanControl_onBlur = function(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
		gridControl_cellNotify(cell, "onKillFocus");
};

booleanControl_onBeforeInput = function(cell, event)
{
	event.preventDefault();
	switch(event.inputType)
	{
		case "insertParagraph":
			editControl_selectNext(cell);
			break;

		case "insertLineBreak":
			editControl_selectPrev(cell);
			break;
	}
};

booleanControl_onPaste = function(cell, event)
{
	event.preventDefault();
};

booleanControl_onKeyDown = function(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
	{
		switch (event.key)
		{
			case " ":
				event.preventDefault();
				booleanControl_onClick(cell, null);
				break;
			case "ArrowLeft":
				event.preventDefault();
				editControl_selectPrev(cell);
				break;
			case "Enter":
			case "ArrowRight":
				event.preventDefault();
				editControl_selectNext(cell);
				break;
		}
	}
};

booleanControl_onClick = function(cell, event)
{
	if (cell.enabled)
	{
		//alert("Click");
		gridControl_cellNotify(cell, "onValidate", cell.value ? "0" : "1");
	}
};
