﻿var __ComboGlobalSuggestionsIFrame = null;
var __ComboGlobalSuggestionsDiv = null;
var __ComboGlobalSuggestionsDivInner = null;

function __ComboDispatcher()
{
	this.ComboID = null;
	this.Items = [];
	this.NeedsOverlay = false;
	if (document.all && !window.opera)
	{
		this.NeedsOverlay = true;
	}
}

__ComboDispatcher.prototype.Invoke = function()
{
	__ComboItemsRender();
};

function __ComboCloseSuggestions()
{
	if (__ComboGlobalSuggestionsDiv.style.display != "none")
	{
		if (__GCD.NeedsOverlay)
		{
			__ComboGlobalSuggestionsIFrame.style.display = "none";
			__ComboGlobalSuggestionsIFrame.style.top = "-1000px";
			__ComboGlobalSuggestionsIFrame.style.left = "-1000px";
		}
		__ComboGlobalSuggestionsDiv.style.display = "none";
		__ComboGlobalSuggestionsDiv.style.top = "-1000px";
		__ComboGlobalSuggestionsDiv.style.left = "-1000px";
		__ComboGlobalSuggestionsDiv.owner.SuggestionBoxPopped = false;
		__ComboGlobalSuggestionsDiv.owner = null;
	}
}

function __ComboRedrawDropDown()
{
	__GCD.CurrentFrame++;
	__GCD.CurrentDrawTop += __GCD.FrameSize;
	if (__GCD.CurrentDrawTop > __GCD.MaxDrawTop)
	{
		__GCD.CurrentDrawTop = __GCD.MaxDrawTop;
	}
	__ComboGlobalSuggestionsDiv.style.top = __GCD.CurrentDrawTop + "px";
	__ComboGlobalSuggestionsDiv.style.clip = "rect(" + ((__GCD.MaxFrames - __GCD.CurrentFrame) * __GCD.FrameSize) + "px auto auto 0px)";
	
	if (__GCD.NeedsOverlay)
	{
		__ComboGlobalSuggestionsIFrame.style.clip = "rect(" + ((__GCD.MaxFrames - __GCD.CurrentFrame) * __GCD.FrameSize) + "px auto auto 0px)";
		__ComboGlobalSuggestionsIFrame.style.top = __GCD.CurrentDrawTop + "px";
	}
	
	if (__GCD.CurrentFrame < __GCD.MaxFrames)
	{
		window.setTimeout("__ComboRedrawDropDown()", __GCD.RedrawTimeout);
	}
}

function __ComboItemsRender()
{
	var comboObj;
	var box;

	eval("comboObj = " + __GCD.ComboID + ";");
	box = __$(comboObj.ID);

	comboObj.SuggestionsCount = Math.ceil(__GCD.Items.length / 3);

	if (comboObj.MaxSuggestionsCount > -1 && comboObj.SuggestionsCount - 1 > comboObj.MaxSuggestionsCount)
	{
		comboObj.SuggestionsCount = comboObj.MaxSuggestionsCount;
	}

	if (comboObj.ComboBoxType != "dropdown")
	{
		comboObj.SelectedIndex = -1;
		comboObj.LastSelectedCell = -1;
	}

	var sw = comboObj.SuggestionsDropDownWidth;
	if (parseInt(comboObj.SuggestionsDropDownWidth) < 0 || comboObj.SuggestionsDropDownWidth == "")
	{
		sw = (parseInt(box.offsetWidth) - 4);
	}

	if (__GCD.NeedsOverlay)
	{
		__ComboGlobalSuggestionsIFrame.style.width = sw + "px";
	}
	__ComboGlobalSuggestionsDiv.style.width = sw + "px";
	
	if (__GCD.Items.length > 0)
	{
		//if (document.defaultView && document.defaultView.getComputedStyle)
		//{
		//	var zzz = document.defaultView.getComputedStyle(__$("__ComboSuggestionTable"), null);
		//	alert(zzz);
		//}

		var multi = 24;
		if (!comboObj.UseDefaultStyle)
		{
			var cell0 = __$(comboObj.ID + "_Cell_0");
			multi = 15; // TODO: Calculate actual height
		}
		var maxHeight = comboObj.SuggestionsCount * multi;
		var shrinkTable = false;

		__ComboGlobalSuggestionsDivInner.style.overflowY = "auto";
		if (comboObj.MaxDropDownHeight != -1)
		{
			if (maxHeight > comboObj.MaxDropDownHeight)
			{
				__ComboGlobalSuggestionsDivInner.style.overflowY = "scroll";
				shrinkTable = true;
			}

		    maxHeight = comboObj.MaxDropDownHeight;
		}

		if (__GCD.NeedsOverlay)
		{
			__ComboGlobalSuggestionsIFrame.style.height = maxHeight + "px";
		}
		__ComboGlobalSuggestionsDiv.style.height = maxHeight + "px";

		var newHtml = comboObj.BuildSuggestions();
		var pos = __findPos(box);

		if (__GCD.NeedsOverlay)
		{
			__ComboGlobalSuggestionsIFrame.style.left = (pos.x - 1) + "px";
			__ComboGlobalSuggestionsIFrame.style.top = (pos.y + parseInt(box.offsetHeight)) + "px";
		}

		__ComboGlobalSuggestionsDiv.style.left = (pos.x - 1) + "px";
		
		if (comboObj.SuggestionsDrowDownShowDelay > 0 && !comboObj.SuggestionBoxPopped)
		{
			comboObj.SuggestionBoxPopped = true;

			__ComboGlobalSuggestionsDiv.style.clip = "rect(" + maxHeight + "px auto " + maxHeight + "px 0px)";
			__ComboGlobalSuggestionsDiv.style.top = (pos.y + parseInt(box.offsetHeight) - maxHeight) + "px";

			if (__GCD.NeedsOverlay)
			{
				__ComboGlobalSuggestionsIFrame.style.clip = "rect(" + maxHeight + "px auto " + maxHeight + "px 0px)";
				__ComboGlobalSuggestionsIFrame.style.top = (pos.y + parseInt(box.offsetHeight) - maxHeight) + "px";
			}

			__GCD.MaxFrames = Math.ceil(maxHeight / 10);
			__GCD.CurrentFrame = 0;
			__GCD.RedrawTimeout = Math.ceil(comboObj.SuggestionsDrowDownShowDelay / __GCD.MaxFrames);
			__GCD.CurrentDrawTop = pos.y + parseInt(box.offsetHeight) - maxHeight;
			__GCD.MaxDrawTop = pos.y + parseInt(box.offsetHeight);
			__GCD.FrameSize = Math.ceil(maxHeight / __GCD.MaxFrames);
			__ComboRedrawDropDown();
		}
		else
		{
			comboObj.SuggestionBoxPopped = true;

			__ComboGlobalSuggestionsDiv.style.clip = "rect(0px auto auto 0px)";
			__ComboGlobalSuggestionsDiv.style.top = (pos.y + parseInt(box.offsetHeight)) + "px";
		}

		__ComboGlobalSuggestionsDivInner.innerHTML = newHtml;
		__ComboGlobalSuggestionsDiv.owner = comboObj;
		
		var suggestionTable = __$("__ComboSuggestionTable");
		suggestionTable.width = sw + "px";
		if (shrinkTable)
		{
			suggestionTable.width = (sw - 17) + "px";
		}
		else
		{
			suggestionTable.width = "100%";
		}

		if (document.all && !window.opera)
		{
			if (comboObj.SuggestionsDropDownFilter != "" && comboObj.SuggestionsDropDownFilter != "None")
			{
				__ComboGlobalSuggestionsDiv.style.filter = "progid:DXImageTransform.Microsoft." + comboObj.SuggestionsDropDownFilter + "(duration=" + comboObj.SuggestionsDropDownFilterDuration + ")";
				__ComboGlobalSuggestionsDiv.filters[0].apply();
				__ComboGlobalSuggestionsDiv.filters[0].play();
			}
		}

		if (__GCD.NeedsOverlay)
		{
			__ComboGlobalSuggestionsIFrame.style.display = "block";
		}
		__ComboGlobalSuggestionsDiv.style.display = "block";
		
		if (comboObj.ComboBoxType != "dropdown")
		{		
			comboObj.ScrollToCell(0);
		}
		else
		{
			comboObj.ScrollToCell(parseInt(comboObj.SelectedIndex));
		}
	}
	else
	{
		if (__GCD.NeedsOverlay)
		{
			__ComboGlobalSuggestionsIFrame.style.display = "none";
		}
		__ComboGlobalSuggestionsDiv.style.display = "none";
	}

	__GCD.ComboID = null;
	__GCD.Items = [];
	comboObj = null;
}

function __ComboItemsCallback()
{
	if (__GrimGlobalXmlHttp.readyState == 4)
	{
		var response = __GrimGlobalXmlHttp.responseText;
		var responseXml = __GrimGlobalXmlHttp.responseXML;
		var newHtml = "";
		__GCD.Items = [];

		if (response != "")
		{
			var arrTemp = response.split(";");
			var arrLength = arrTemp.length;

			for (var i = 0; i < arrLength; i++)
			{
				__GCD.Items[i] = [true,,];
				__GCD.Items[i][1] = arrTemp[i];
			}
			__GCD.ComboID = __GCD.Items[0][1];
			__GCD.Items = __GCD.Items.splice(1, __GCD.Items.length - 1);

			__GCD.Invoke();
		}

		response = null;
	}
}

/*
 * GrimComboBox class
 */

function GrimComboBox(
	id,
	uniqueId,
	minPrefixLength,
	autoCompleteSource,
	suggestionsUrl,
	suggestionsWebServiceMethod,
	autoPostBack,
	maxSuggestionsCount,
	popSuggestionsOnDoubleClick,
	suggestionsDropDownFilter,
	suggestionsDropDownWidth,
	boldStartingLetters,
	comboBoxType,
	arrowSrc,
	arrowOverSrc,
	arrowClickSrc,
	items,
	multiWordSuggestions,
	multiWordSuggestionsSeparator,
	onClientSelectedIndexChanged,
	maxDropDownHeight,
	useDefaultStyle,
	defaultItemCssClass,
	disabledItemCssClass,
	selectedItemCssClass,
	selectedIndex,
	suggestionsDrowDownShowDelay,
	suggestionsDropDownFilterDuration
	)
{
	this.ID = id;
	this.UniqueID = uniqueId;
	this.MinPrefixLength = minPrefixLength;
	this.AutoCompleteSource = autoCompleteSource;
	this.SuggestionsUrl = suggestionsUrl;
	this.SuggestionsWebServiceMethod = suggestionsWebServiceMethod;
	this.AutoPostBack = autoPostBack;
	this.MaxSuggestionsCount = maxSuggestionsCount;
	this.PopSuggestionsOnDoubleClick = popSuggestionsOnDoubleClick;
	this.SuggestionsDropDownFilter = suggestionsDropDownFilter;
	this.SuggestionsDropDownWidth = suggestionsDropDownWidth;
	this.BoldStartingLetters = boldStartingLetters;
	this.ComboBoxType = comboBoxType;
	this.ArrowSrc = arrowSrc;
	this.ArrowOverSrc = arrowOverSrc;
	this.ArrowClickSrc = arrowClickSrc;
	this.Items = items;
	this.MultiWordSuggestions = multiWordSuggestions;
	this.MultiWordSuggestionsSeparator = multiWordSuggestionsSeparator;
	this.OnClientSelectedIndexChanged = onClientSelectedIndexChanged;
	this.MaxDropDownHeight = maxDropDownHeight;
	this.UseDefaultStyle = useDefaultStyle;
	this.DefaultItemCssClass = defaultItemCssClass;
	this.DisabledItemCssClass = disabledItemCssClass;
	this.SelectedItemCssClass = selectedItemCssClass;
	this.SelectedIndex = selectedIndex;
	this.SuggestionsDrowDownShowDelay = suggestionsDrowDownShowDelay;
	this.SuggestionsDropDownFilterDuration = suggestionsDropDownFilterDuration;
	
	this.TextBox = null;
	this.DropDownArrow = null;
	this.HiddenField = null;
	this.Init();
}

GrimComboBox.prototype.Init = function()
{
	if (typeof __GrimGlobalXmlHttp == "undefined")
	{
		__GrimGlobalXmlHttp = __GrimCoreGetXmlHttp();
	}
	
	if (typeof __GCD == "undefined")
	{
		__GCD = new __ComboDispatcher();
	}

	__ComboGlobalSuggestionsDiv = __$("__ComboSuggestionDiv");
	__ComboGlobalSuggestionsDivInner = __$("__ComboSuggestionDivInner");
	__ComboGlobalSuggestionsDiv.setAttribute("tabindex", "-1");
	__ComboGlobalSuggestionsDiv.onselectstart = function() { return false; };

	if (__GCD.NeedsOverlay)
	{
		if (__ComboGlobalSuggestionsIFrame == null)
		{
			var newIFrame = document.createElement("IFRAME");
			newIFrame.src = "javascript:''";
			newIFrame.id = "__ComboSuggestionIFrame";
			newIFrame.frameBorder = 0;
			newIFrame.tabIndex = -1;
			newIFrame.style.position = "absolute";
			newIFrame.style.display = "none";
			newIFrame.style.overflow = "hidden";
			newIFrame.style.height = "100px";
			newIFrame.style.zIndex = 200;
			newIFrame.style.top = "-1000px";
			newIFrame.style.left = "-1000px";
			newIFrame.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";

			__ComboGlobalSuggestionsDiv.parentNode.insertBefore(newIFrame, __ComboGlobalSuggestionsDiv);

			__ComboGlobalSuggestionsIFrame = __$("__ComboSuggestionIFrame");
		}
	}

	__GrimGlobalXmlHttp.abort();

	this.TextBox = __$(this.ID);
	this.TextBox.setAttribute("autocomplete", "off");

	this.DropDownArrow = __$(this.ID + "_Arrow");
	this.HiddenField = __$(this.ID + "_H");
	var _self = this;

	if (this.ComboBoxType != "autocomplete" && window.addEventListener)
	{
		this.DropDownArrow.align = "top";
	}
	
	if (this.ComboBoxType == "dropdown" && this.SelectedIndex != -1)
	{
		this.SelectedText = this.Items[this.SelectedIndex][1];
		this.SelectedValue = this.Items[this.SelectedIndex][2];
	}

	__attach(this.TextBox, "keyup", function(e) { _self.GetSuggestions(e) }, false);
	__attach(this.TextBox, "keydown", function(e) { return _self.OnKeyDown(e) }, false);
	if (this.ComboBoxType == "dropdown")
	{
		__attach(this.TextBox, "mousedown", function(e) { _self.DropDown(e) }, true);
	}
	else if (this.PopSuggestionsOnDoubleClick)
	{
		if (this.ComboBoxType == "autocomplete")
		{
			__attach(this.TextBox, "dblclick", function(e) { _self.GetSuggestions(e) }, true);
		}
		else
		{
			__attach(this.TextBox, "dblclick", function(e) { _self.DropDown(e) }, true);
		}
	}

	if (this.AutoPostBack)
	{
		__attach(this.TextBox, "change", function(e) { _self.SelectMarkedText(e) }, false);
	}

	__attach(document, "mousedown", function(e) { _self.DocumentClick(e) }, false);

	this.SuggestionsCount = 0;
	this.LastSelectedCell = this.SelectedIndex;
	this.SuggestionBoxPopped = false;
};

GrimComboBox.prototype.SelectMarkedText = function(index)
{
	if (this.LastSelectedCell > -1)
	{
		var textBox = __$(this.ID);
		var selectedCell = __$(this.ID + "_Cell_" + this.LastSelectedCell);
		var newValue = selectedCell.innerHTML.stripTags();

		__ComboCloseSuggestions();

		if (this.MultiWordSuggestions)
		{
			var arrTemp = textBox.value.split(this.MultiWordSuggestionsSeparator);
			var arrLength = arrTemp.length - 1;
			var output = "";
			for (var i = 0; i < arrLength; i++)
			{
				output += arrTemp[i] + this.MultiWordSuggestionsSeparator;
			}
			output += newValue;
			textBox.value = output;
		}
		else
		{
			var re = /<IMG src.*&nbsp;/gi;
			newValue = newValue.replace(re, "");
			textBox.value = newValue;
			__executeClientEvent("OnClientSelectedIndexChanged", this.OnClientSelectedIndexChanged, newValue);
		}

		if (this.ComboBoxType == "dropdown")
		{
			this.SelectedIndex = parseInt(index);
			this.HiddenField.value = index;
			
			if (this.SelectedIndex != -1)
			{
				this.SelectedText = this.Items[this.SelectedIndex][1];
				this.SelectedValue = this.Items[this.SelectedIndex][2];
			}
		}

		textBox.focus();
		textBox.value = textBox.value;

		textBox = null;
		selectedCell = null;
	}
	
	if (this.AutoPostBack)
	{
		setTimeout("__doPostBack('" + this.UniqueID + "','')", 0);
	}
};

GrimComboBox.prototype.GetNextItem = function(delta)
{
	var temp = parseInt(this.LastSelectedCell);
	
	if (this.ComboBoxType == "autocomplete")
	{
		temp += delta;
		if (temp + 1 > this.SuggestionsCount) temp = 0;
		if (temp < 0) temp = this.SuggestionsCount - 1;
		
		this.LastSelectedCell = temp;
	}
	else
	{
		var itemsLength = this.Items.length - 1;
		for (var i = 0; i < itemsLength; i++)
		{
			temp += delta;
			if (temp + 1 > this.SuggestionsCount) temp = 0;
			if (temp < 0) temp = this.SuggestionsCount - 1;

			if (this.Items[temp][0])
			{
				return temp;
			}
		}
	}
	
	return parseInt(this.LastSelectedCell);
};

GrimComboBox.prototype.OnKeyDown = function(e)
{
	if (!e) e = window.event;
	
	if (e.keyCode == 13 && this.SuggestionBoxPopped && this.LastSelectedCell != -1)
	{
		if (this.LastSelectedCell > -1)
		{
			this.SelectMarkedText(this.LastSelectedCell);
		}

		e.returnValue = false;
		if (e.preventDefault) e.preventDefault();
	}
	
	if ((e.keyCode == 38 || e.keyCode == 40) && this.SuggestionsCount > 0)
	{
		if (this.LastSelectedCell > -1)
		{
			var oldCell = __$(this.ID + "_Cell_" + this.LastSelectedCell);
			this.UnMarkCell(oldCell, false);
			oldCell = null;
		}

		if (e.keyCode == 40)
		{
			this.LastSelectedCell = this.GetNextItem(1);
		}
		
		if (e.keyCode == 38)
		{
			this.LastSelectedCell = this.GetNextItem(-1);
		}
		
		var newCell = __$(this.ID + "_Cell_" + this.LastSelectedCell);
		this.MarkCell(newCell);
		this.ScrollToCell(newCell);
		newCell = null;

		return;
	}

	if (e.keyCode == 9)
	{
		__ComboCloseSuggestions();
	}

	if (e.keyCode == 27)
	{
		__ComboCloseSuggestions();
		return false;
	}
};

GrimComboBox.prototype.OnBlur = function(e)
{
	if (this.SuggestionBoxPopped && __ComboGlobalSuggestionsDiv.owner == this)
	{
		__ComboCloseSuggestions();
	}
};

GrimComboBox.prototype.GetSuggestions = function(e)
{
	if (this.SuggestionsUrl == "") return;

	if (!e) e = window.event;

	if (e.keyCode == 13 || e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 27 || e.keyCode == 9 || e.keyCode == 16)
		return;

	var textBox = __$(this.ID);
	var textBoxValue = textBox.value;
	
	if (this.MultiWordSuggestions)
	{
		var arrTemp = textBoxValue.split(this.MultiWordSuggestionsSeparator);
		textBoxValue = arrTemp[arrTemp.length - 1];
	}
	
	if (textBoxValue.length < this.MinPrefixLength)
	{
		if (this.SuggestionBoxPopped)
		{
			__ComboCloseSuggestions();
		}

		return;
	}

	__GrimGlobalXmlHttp.abort();
	__GrimGlobalXmlHttp.onreadystatechange = __ComboItemsCallback;

	if (this.AutoCompleteSource == "webpage")
	{
		var url = this.SuggestionsUrl +
			"?id=" + this.ID +
			"&s=" + encodeURIComponent(textBoxValue);
		
		if (url.substr(0, 2) == "//")
		{
			url = url.substr(1);
		}

		__GrimGlobalXmlHttp.open("GET", url, true);
		__GrimGlobalXmlHttp.send(null);
	}
	else
	{
		var url = this.SuggestionsUrl + "/" + this.SuggestionsWebServiceMethod;

		if (url.substr(0, 2) == "//")
		{
			url = url.substr(1);
		}
		
		var parameters = "prefix=" + escape(textBoxValue) + "&comboId=" + this.ID;
		
		__GrimGlobalXmlHttp.open("POST", url, true);
		__GrimGlobalXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		__GrimGlobalXmlHttp.setRequestHeader("Content-length", parameters.length);
		__GrimGlobalXmlHttp.setRequestHeader("Connection", "close");
		__GrimGlobalXmlHttp.send(parameters);
	}

	textBox = null;
};

GrimComboBox.prototype.DropDown = function()
{
	if (this.SuggestionBoxPopped && __ComboGlobalSuggestionsDiv.owner == this)
	{
		__ComboCloseSuggestions();
	}
	else
	{
		__GCD.ComboID = this.ID;
		__GCD.Items = this.Items;
		__GCD.Invoke();
	}
};

GrimComboBox.prototype.BuildSuggestions = function()
{
	var box = __$(this.ID);
	var newHtml = '<table id="__ComboSuggestionTable" border="0" cellspacing="0" cellpadding="0" width="100%" unselectable="on" tabindex="-1">';
	var itemsLength = Math.ceil(__GCD.Items.length / 3);

	for (i = 0; i < itemsLength; i++)
	{
		if (this.MaxSuggestionsCount > -1 && this.MaxSuggestionsCount < i)
			break;

		var whAdd = 'height="24"';
		if (__GCD.Items[i * 3 + 2][1] == "w") whAdd = 'width="24"';

		newHtml += '<tr height="24"><td><img src="/images/spacer.gif" width="24" height="1" border="0" /><br /><img src="/images/singers/' + __GCD.Items[i * 3 + 1][1]  + '" align="absmiddle" ' + whAdd + ' /></td><td ';
		newHtml += 'counter="' + i + '" ';
		newHtml += 'id="' + this.ID + '_Cell_' + i + '" ';
		newHtml += 'width="100%" ';
		if (!__GCD.Items[i * 3][0])
		{
			if (this.UseDefaultStyle)
			{
				newHtml += 'style="cursor: arrow; color: #6C7919; background-color: #E8E8E8; border: 1px solid #E8E8E8; padding-left:3px;" ';
			}
			else
			{
				newHtml += 'style="cursor: arrow" class="' + this.DisabledItemCssClass + '" ';
			}
		}
		else
		{
			if (this.UseDefaultStyle)
			{
				if (i == this.SelectedIndex)
				{
					newHtml += 'style="cursor: pointer; border: 1px dotted black; color: white; background-color: #AAAAAA; padding-left:3px;" ';
				}
				else
				{
					newHtml += 'style="cursor: pointer; border: 1px solid #D5D5D5; padding-left:3px;" ';
				}
			}
			else
			{
				if (i == this.SelectedIndex)
				{
					newHtml += 'style="cursor: pointer" class="' + this.SelectedItemCssClass + '" ';
				}
				else
				{
					newHtml += 'style="cursor: pointer" class="' + this.DefaultItemCssClass + '" ';
				}
			}
			newHtml += 'onmouseover="' + this.ID + '.MarkCell(this)" ';
			newHtml += 'onmouseout="' + this.ID + '.UnMarkCell(this, false)" ';
			newHtml += 'onclick="' + this.ID + '.SelectMarkedText(this.getAttribute(\'counter\'))"';
		}

		if (this.ComboBoxType == "autocomplete")
		{
			newHtml += '>' + __GCD.Items[i * 3][1] + '</td></tr>';
		}
		else
		{
			if (__GCD.Items[i * 3][3])
			{
				newHtml += '><img src=\'' + __GCD.Items[i * 3][3] + '\' align=\'absmiddle\' />&nbsp;' + __GCD.Items[i * 3][1] + '</td></tr>';
			}
			else
			{
				newHtml += '>' + __GCD.Items[i * 3][1] + '</td></tr>';
			}
		}

		if (this.ComboBoxType == "autocomplete")
		{
			this.Items[i] = [,,];
			this.Items[i][0] = __GCD.Items[i * 3][0];
			this.Items[i][1] = __GCD.Items[i * 3][1];
			this.Items[i][2] = __GCD.Items[i * 3][2];
		}
	}
	
	newHtml += '</table>';
	
	return newHtml;
};

GrimComboBox.prototype.ScrollToCell = function(cell)
{
	if (typeof cell == "number")
	{
		if (cell == -1) return;
		cell = __$(this.ID + "_Cell_" + cell);
	}

	var a = cell.offsetTop;
	var b = cell.offsetHeight;
	var c = __ComboGlobalSuggestionsDivInner.scrollTop;
	var d = __ComboGlobalSuggestionsDivInner.offsetHeight - 4;
	if (a + b > c + d)
	{
		__ComboGlobalSuggestionsDivInner.scrollTop = a + b - d;
	}
	else
	{
		if (a + b <= c)
		{
			__ComboGlobalSuggestionsDivInner.scrollTop = a;
		}
	}
};

GrimComboBox.prototype.UnMarkCell = function(cell, reset)
{
	if (this.LastSelectedCell == -1) return;

	if (this.Items[this.LastSelectedCell][0])
	{
		if (this.UseDefaultStyle)
		{
			cell.style.color = "black";
			cell.style.backgroundColor = "#D5D5D5";
			cell.style.border = "1px solid #D5D5D5";
		}
		else
		{
			cell.className = this.DefaultItemCssClass;
		}
	}

	if (reset)
	{
		this.LastSelectedCell = -1;
	}
};

GrimComboBox.prototype.MarkCell = function(cell)
{
	if (this.LastSelectedCell != -1)
	{
		var oldCell = __$(this.ID + "_Cell_" + this.LastSelectedCell);
		this.UnMarkCell(oldCell, true);
		oldCell = null;
	}
	
	if (this.UseDefaultStyle)
	{
		cell.style.color = "#DCDCDC";
		cell.style.backgroundColor = "#515151";
		cell.style.border = "1px dotted #7E8F13";
	}
	else
	{
		cell.className = this.SelectedItemCssClass;
	}

	this.LastSelectedCell = cell.getAttribute("counter");
};

GrimComboBox.prototype.DocumentClick = function(e)
{
	if (!e) e = window.event;
	
	var target = e.target || e.srcElement;
	while (target.nodeType !== 9)
	{
		if (target == this.TextBox  || target == __ComboGlobalSuggestionsDiv || target == this.DropDownArrow) return;
		target = target.parentNode;
	}

	if (this.SuggestionBoxPopped && __ComboGlobalSuggestionsDiv.owner == this)
	{
		__ComboCloseSuggestions();
	}
};
