﻿var gbBoxtmp=null; //onclick 
function cSentence()
{
	this.aSuggestByElement = new Array();
	this.oBox = new oSentenceBox();

}

cSentence.prototype.AddSentence = function(src)
{
    if (!getE('chckUseSuggest').checked) return false;
	var sValue = src.value; if (sValue=='') return false;
	
	this.oBox.HideBox();
	if (src.tagName.toLowerCase() != 'input') return false;
	var nIndex = this.GetIndex(src.id);
	if (nIndex == this.aSuggestByElement.length) {var a = new Array(src.id,''); this.aSuggestByElement[nIndex] = a;}
	
	this.AddWord(sValue.replace(',','.'), nIndex);
}
cSentence.prototype.GetIndex = function(id)
{
	var nIndex = this.aSuggestByElement.length;
	for (var i=0; i<this.aSuggestByElement.length; i++)
		if (this.aSuggestByElement[i][0] == id) {nIndex = i; break;}
	return nIndex;	
}

cSentence.prototype.AddWord = function(sValue, nIndex)
{
	var bFound = false;
	var aValues = this.aSuggestByElement[nIndex][1].split('|'); // hodnoty pro suggest jsou ulozeny jako string s oddelovacem "|"
	if (aValues.length == 0) aValue[0] = src.value;
	else
	{
		for (var i=0; i<aValues.length; i++)
		{
			if (aValues[i] == sValue) {bFound = true; break;}
		}
		if (!bFound) aValues[aValues.length] = sValue;
	}
	this.aSuggestByElement[nIndex][1] = aValues.join('|');
}

cSentence.prototype.GetSentences = function(src)
{
    if (!getE('chckUseSuggest').checked) return false;
    
	var nIndex = this.GetIndex(src.id);
	if (nIndex == this.aSuggestByElement.length) return false;
	this.oBox.ShowBox(src, this.aSuggestByElement[nIndex][1]);
}

// -----
function oSentenceBox()
{
	this.oBox  = null;
	this.srcEl = null;
	this.Items = 0;
}
oSentenceBox.prototype.SetBox = function(sId)
{
	var el = getE('s_'+ sId.split('_')[1]); 
	el.style.display='none';
	return el;
}
oSentenceBox.prototype.ShowBox = function(src, sSplit)
{
	this.srcEl = src;
	this.HideBox();
	this.oBox = this.SetBox(src.id);
	var sValue = src.value; if (sValue == '') return false;
	var n = 0;
	var aValues = sSplit.split('|').sort(SortNum); 
	for (var i=0;i<aValues.length;i++)
	{
		if (aValues[i].search(sValue) == 0) {n++; this.CreateBoxOption(aValues[i]);}
		if (n==5) break;
	}
	if (n>0) this.oBox.style.display='block';
}
oSentenceBox.prototype.HideBox = function()
{
	if (this.oBox)
	{
		this.oBox.style.display='none';
		while (this.oBox.childNodes.length>0)   { this.oBox.removeChild( this.oBox.childNodes[0] );   }
	}
	this.Items = 0;
}

oSentenceBox.prototype.CreateBoxOption = function(sValue)
{
	this.Items++;
	var el = document.createElement("span");
	var oInp = this;
	el.className = 'sugItem';
    el.appendChild(document.createTextNode(sValue));
    el.onmouseover = function() {el.className = 'sugItemIN';}
    el.onmouseout  = function() {el.className = 'sugItem';}
    el.onclick     = function() {gbBoxtmp = oInp; gbBoxtmp.TransSuggest(sValue);}        
	this.oBox.appendChild(el);
}
oSentenceBox.prototype.TransSuggest = function(sValue)
{
    //var src = (window.event)? window.event.srcElement : e.target;
    if (gbBoxtmp.srcEl)
    {
        gbBoxtmp.srcEl.value = sValue;
        gbBoxtmp.srcEl.focus();
        gbBoxtmp.HideBox();
    }
}

function SortNum(a,b)
{
    return a-b;
}