/// <columnDescriptor>
/// { id:        <guid>,
///   number:    <int>,
///   label:     <string>,
///   toolTip:   <string>,
///   inputType: <string>,
///   width:     <integer>,
///   limit:     <integer>,
///   decimals:  <integer>,
///   marked:    0|1,
///   enabled:   0|1,
/// }
 
/// <rowDescriptor>
/// { rowId:    <guid>,
///   rowNo:	<integer>
/// }


/* ===================================================================================================
 * control general
 * ===================================================================================================
 */

/* TODO
        Amount_SH = 2,
        Date = 6,
*/

/// cell: html-div-Element
/// descr: <columnDescriptor>
control_setup = function(cell, descr, txt)
{
	cell.description = descr;
	switch(descr.inputType)
	{
		case "Bookmark":
			bookmarkControl_setup(cell, txt);
			break;
		case "Bool":
			booleanControl_setup(cell, txt);
			break;
		case "Label":
		case "Label_Left":
		case "Label_Center":
		case "Label_Right":
		case "Amount_SH":
			labelControl_setup(cell, txt);
			break;
		case "Text":
		case "Text_Left":
		case "Text_Right":
		case "Text_Center":
		case "Decimal":
		case "Integer":
			editControl_setup(cell, txt);
			break;
		case "TreePicture": 
			treePictureControl_setup(cell, txt);
			break;
		default:
			cell.style.justifyContent = "center";
			break;
	}
	addControlEvent(cell, "dblclick", function(event) { control_onDblClick(cell, event); });
	control_setText(cell, txt);
};

control_setText = function(cell, txt)
{
	var descr = cell.description;
	switch(descr.inputType)
	{
		case "Bookmark":
			bookmarkControl_setText(cell, txt);
			break;
		case "Bool":
			booleanControl_setText(cell, txt);
			break;
		case "Label":
		case "Label_Left":
		case "Label_Center":
		case "Label_Right":
		case "Amount_SH":
			labelControl_setText(cell, txt);
			break;
		case "Text":
		case "Text_Left":
		case "Text_Right":
		case "Text_Center":
		case "Decimal":
		case "Integer":
			editControl_setText(cell, txt);
			break;
		case "TreePicture": 
			treePictureControl_setText(cell, txt);
			break;
		default:
			var typeDescr = descr.inputType.toString() + "(" + descr.limit.toString() + "," + descr.decimals.toString() + ")"; 
			cell.innerText = txt;
			cell.title = cell.innerText + "\r\n" + typeDescr;
			break;
	}
};

control_onDblClick = function(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
	{
		event.preventDefault();
		gridControl_cellNotify(cell, "onDblClick");
	}
};

control_onContextMenu = function(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
	{
		var descr = cell.description;
		if (parseIntEx(descr.properties) && control_getIsSelectionEmpty())
		{
			event.preventDefault();
			gridControl_cellNotify(cell, "onProperties");
			return;
		}

		var grid = gridControl(cell.id.split('$')[0]);
		var col = grid.getSelectedColumn();
		if (col.inputType == "Bookmark")
		{
			event.preventDefault();
			gridControl_handleToolCommand(grid, "menu");
			return;
		}
	}
};

control_getIsSelectionEmpty = function()
{
	try
	{
		var selection = window.getSelection();
		if ((selection.rangeCount == 1) && (selection.toString().length == 0))
		{
			return true;
		}
	}
	catch(e)
	{
	}
	return false;
};

control_replaceSelection = function(cell, newText, maxNewLen)
{
	// alert("control_replaceSelection[" + newText + "]");
	try
	{
		var selection = window.getSelection();
		if (selection.rangeCount == 1) 
		{
			var existingText = cell.innerText; 
			var selectedText = selection.toString();
			var newLen = existingText.length + newText.length - selectedText.length;
			if ((maxNewLen <= 0) || (newLen <= maxNewLen))
			{
				var tn = document.createTextNode("");
				// tn.data = newText;
				tn.nodeValue = newText;
				
				selection.deleteFromDocument();
				selection.getRangeAt(0).insertNode(tn);
				selection.collapseToEnd();
				return true;
			}
		}
	}
	catch(e)
	{
	}
	return false;
};

