try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

var myimages = new Array();
var oldsrc = null;
function preloadImgs(){
	for (i=0;i<preloadImgs.arguments.length;i++){
		myimages[i] = new Image();
		myimages[i].src = preloadImgs.arguments[i];
	}
}

function changeProdImg(iSrc,iTarget){
	oldsrc = document.getElementById(iSrc).src;
	document.getElementById(iSrc).src = iTarget;
}
function restoreProdImg(iSrc,iTarget){
	document.getElementById(iSrc).src = oldsrc;
}

function toggleId(id){
	if (document.getElementById(id).style.display == 'none'){
		if (document.getElementById(id).tagName == 'TR' && !document.all){
			document.getElementById(id).style.display = "table-row";
		} else if ((navigator.userAgent.indexOf("Opera")!=-1 || navigator.userAgent.indexOf("Opera/")!=-1) && document.getElementById(id).tagName == 'TR') {
			document.getElementById(id).style.display = "table-row";
		} else if (document.getElementById(id).tagName == 'SPAN' && !document.all){
			document.getElementById(id).style.display = "inline";
		} else if (document.getElementById(id).tagName == 'TBODY' && !document.all){
			document.getElementById(id).style.display = "table-row-group";
		} else if ((navigator.userAgent.indexOf("Opera")!=-1 || navigator.userAgent.indexOf("Opera/")!=-1) && document.getElementById(id).tagName == 'IMG') {
			document.getElementById(id).style.display = "inline";
		} else if (document.getElementById(id).tagName == 'SPAN'){
			document.getElementById(id).style.display = "inline";
		} else {
			document.getElementById(id).style.display = 'block';
		}
	} else {
		document.getElementById(id).style.display = 'none';
	}
	return false;
}
function showId(id){
	var element = document.getElementById(id);

	if (element.style.display == 'none'){
		if (element.tagName == 'TBODY') {
			element.style.display = "";
		} else if (element.tagName == 'TABLE') {
			element.style.display = "";
		} else if (element.tagName == 'TR' && !document.all){
			element.style.display = "table-row";
		} else if ((navigator.userAgent.indexOf("Opera")!=-1 || navigator.userAgent.indexOf("Opera/")!=-1) && element.tagName == 'TR') {
			element.style.display = "table-row";
		} else if (element.tagName == 'A' || element.tagName == 'SPAN') {
			element.style.display = "inline";
		} else {
			element.style.display = 'block';
		}
	}
	return false;
}
function hideId(id){
	document.getElementById(id).style.display = 'none';
	return false;
}
function toggleItems(){
	var count=toggleItems.arguments.length;
	for (var i=0; i<count; i++)
	{
		toggleId(toggleItems.arguments[i]);
	}
	return false;
}

/* getElementsByClass & show-hide
-------------------------------------------------- */
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"($|\\s)");

	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
function hideClass(node,searchClass,tag) {
	var myEls = getElementsByClass(node,searchClass,tag);

	for ( i=0;i<myEls.length;i++ ) {
		myEls[i].style.display = 'none';
	}
}
function showClass(node,searchClass,tag) {
	var myEls = getElementsByClass(node,searchClass,tag);

	for ( i=0;i<myEls.length;i++ ) {
		myEls[i].style.display = '';
	}
}

/* Abiinfo
-------------------------------------------------- */
function showabiinfo(id) {
	var el = document.getElementById(id);
	el.src = "gfx/dot_ekool4.gif";
}
function hideabiinfo(id) {
	var el = document.getElementById(id);
	el.src = "gfx/dot_ekool2.gif";
}
function hideabiinfo2() {
	document.getElementById(ttimg).src = "gfx/dot_ekool2.gif";
}

/**/

function toggleHelp(target,innerId,innerIdText1,innerIdText2,component){
	t = document.getElementById(target);
	i = document.getElementById(innerId);
	if (t.style.display == 'none'){
		t.style.display = 'block';
		i.innerHTML = innerIdText2;
		document.cookie = component + "=1";
	} else {
		t.style.display = 'none';
		i.innerHTML = innerIdText1;
		document.cookie = component + "=0";
	}
	return false;
}

function initHelp(target,innerId,innerIdText1,innerIdText2,component,opened){
	toggle = false;
	if (opened == 'true') {
		if (getCookieValue(component) != '0') {
			toggle = true;
		}
	}
	else if (getCookieValue(component) == '1') {
		toggle = true;
	}
	if (toggle) {
		toggleHelp(target,innerId,innerIdText1,innerIdText2,component);
	}
	return false;
}

function getCookieValue(cookieName) {
	var exp = new RegExp (cookieName + "=([^;]+)");
	if (exp.test(document.cookie + ";")) {
		exp.exec(document.cookie + ";");
		return RegExp.$1;
	}
	else {
		return '';
	}
}


/* Tooltip
-------------------------------------------------- */
document.write('<div id="tooltip" style="display: none;"><div id="tooltip1"><div id="tooltip2"><div id="tooltip3"><div id="tooltip-label"></div><div id="tooltip-content"></div></div></div></div></div>');
document.write('<div id="pophelp" style="display: none;"><div id="pophelp1"><div id="pophelp2"><div id="pophelp3"><div id="pophelp-label"></div><div id="pophelp-content"></div></div></div></div></div>');

var ie=document.all;
var dom=document.getElementById;
var ns4=document.layers;

function hideElement(elmID,overDiv) {
	if(ie) {
		for( i = 0; i < document.all.tags( elmID ).length; i++ ) {
			obj = document.all.tags( elmID )[i];
			if(!obj || !obj.offsetParent) {
				continue;
			}
			// Find the element's offsetTop and offsetLeft relative to the BODY tag.
			objLeft = obj.offsetLeft;
			objTop = obj.offsetTop;
			objParent = obj.offsetParent;
			while( objParent.tagName.toUpperCase() != "BODY" ) {
				objLeft += objParent.offsetLeft;
				objTop += objParent.offsetTop;
				objParent = objParent.offsetParent;
				if (objParent.tagName == "HTML") break;
			}
			objHeight = obj.offsetHeight;
			objWidth = obj.offsetWidth;
			if (( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
			else if ((overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
			else if ( overDiv.offsetTop >= ( objTop + objHeight ));
			else if ( overDiv.offsetLeft >= ( objLeft + objWidth ));
			else {
				obj.style.visibility = "hidden";
			}
		}
	}
}

function showElement(elmID) {
	if(ie) {
		for( i = 0; i < document.all.tags( elmID ).length; i++ ) {
			obj = document.all.tags( elmID )[i];
			if( !obj || !obj.offsetParent) {
				continue;
			}
			obj.style.visibility = "";
		}
	}
}

var ttimg = '';

function showTooltip(ref,close,txt,img,headerlabel){
	var tooltip = document.getElementById("tooltip");
	var leftpos = 0;
	var toppos = 0;
	aTag = ref;
	do {
		aTag = aTag.offsetParent;
		if (aTag.tagName == "HTML") break;
		leftpos += aTag.offsetLeft;
		toppos += aTag.offsetTop;
	}
	while(aTag.tagName!="BODY");
	leftpos = leftpos + ref.offsetLeft + ref.offsetWidth;
	toppos = toppos + ref.offsetTop;

	tooltip.style.top = toppos+'px';
	if (close == 'noclose') {
		if (headerlabel){
			document.getElementById("tooltip-label").innerHTML = headerlabel;
		} else {
			document.getElementById("tooltip-label").innerHTML = 'Abiinfo';
		}
	} else {
		if(img != ''){
			ttimg = img;
			if (headerlabel){
				document.getElementById("tooltip-label").innerHTML = '<img onclick="hideTooltip(); hideabiinfo(\''+img+'\');" src="gfx/ico_logout2.gif" alt="Sulge" />'+headerlabel;
			} else {
				document.getElementById("tooltip-label").innerHTML = '<img onclick="hideTooltip(); hideabiinfo(\''+img+'\');" src="gfx/ico_logout2.gif" alt="Sulge" />'+'Abiinfo';
			}

			addEvent(document, "mousedown", hideTooltip);
			addEvent(document, "mousedown", hideabiinfo2);
		} else {
			if (headerlabel){
				document.getElementById("tooltip-label").innerHTML = '<img onclick="hideTooltip();" src="gfx/ico_logout2.gif" alt="Sulge" />'+headerlabel;
			} else {
				document.getElementById("tooltip-label").innerHTML = '<img onclick="hideTooltip();" src="gfx/ico_logout2.gif" alt="Sulge" />'+'Abiinfo';
			}
			addEvent(document, "mousedown", hideTooltip);
		}
	}
	document.getElementById("tooltip-content").innerHTML = txt;
	tooltip.style.display = 'block';

	if (leftpos + tooltip.offsetWidth + ref.offsetWidth > document.body.clientWidth) {
		tooltip.style.left = (document.body.clientWidth - tooltip.offsetWidth) + 'px';
	} else {
		tooltip.style.left = leftpos + 4 + 'px';
	}

	hideElement('SELECT',tooltip);
	hideElement('APPLET',tooltip);
}

function hideTooltip(){
	var tooltip = document.getElementById("tooltip");

	tooltip.style.display = 'none';
	showElement('SELECT',tooltip);
	showElement('APPLET',tooltip);
}

function showPophelp(ref,close,txt,img,headerlabel){
	var pophelp = document.getElementById("pophelp");
	var leftpos = 0;
	var toppos = 0;
	aTag = ref;
	do {
		aTag = aTag.offsetParent;
		if (aTag.tagName == "HTML") break;
		leftpos += aTag.offsetLeft;
		toppos += aTag.offsetTop;
	}
	while(aTag.tagName!="BODY");
	leftpos = leftpos + ref.offsetLeft + ref.offsetWidth;
	toppos = toppos + ref.offsetTop;

	pophelp.style.top = toppos+'px';
	if (close == 'noclose') {
		if (headerlabel){
			document.getElementById("pophelp-label").innerHTML = headerlabel;
		} else {
			document.getElementById("pophelp-label").innerHTML = 'Abiinfo';
		}
	} else {
		if(img != ''){
			ttimg = img;
			if (headerlabel){
				document.getElementById("pophelp-label").innerHTML = '<img onclick="hidePophelp(); hideabiinfo(\''+img+'\');" src="gfx/ico_logout2.gif" alt="Sulge" />'+headerlabel;
			} else {
				document.getElementById("pophelp-label").innerHTML = '<img onclick="hidePophelp(); hideabiinfo(\''+img+'\');" src="gfx/ico_logout2.gif" alt="Sulge" />'+'Abiinfo';
			}
		} else {
			if (headerlabel){
				document.getElementById("pophelp-label").innerHTML = '<img onclick="hidePophelp();" src="gfx/ico_logout2.gif" alt="Sulge" />'+headerlabel;
			} else {
				document.getElementById("pophelp-label").innerHTML = '<img onclick="hidePophelp();" src="gfx/ico_logout2.gif" alt="Sulge" />'+'Abiinfo';
			}
		}
	}
	document.getElementById("pophelp-content").innerHTML = txt;
	pophelp.style.display = 'block';

	if (leftpos + pophelp.offsetWidth + ref.offsetWidth > document.body.clientWidth) {
		pophelp.style.left = (document.body.clientWidth - pophelp.offsetWidth) + 'px';
	} else {
		pophelp.style.left = leftpos + 4 + 'px';
	}

	hideElement('SELECT',pophelp);
	hideElement('APPLET',pophelp);
}

function hidePophelp(){
	var pophelp = document.getElementById("pophelp");

	pophelp.style.display = 'none';
	showElement('SELECT',pophelp);
	showElement('APPLET',pophelp);
}


function addEvent(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
}

function removeEvent(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
}



// ----------------------------------------------------------------------------
// AddClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function AddClassName(objElement, strClass, blnMayAlreadyExist)
   {
   if ( objElement.className )
      {
      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');
      // if the new class name may already exist in list
      if ( blnMayAlreadyExist )
         {
         // get uppercase class for comparison purposes
         var strClassUpper = strClass.toUpperCase();
         // find all instances and remove them
         for ( var i = 0; i < arrList.length; i++ )
            {
            // if class found
            if ( arrList[i].toUpperCase() == strClassUpper )
               {
               // remove array item
               arrList.splice(i, 1);
               // decrement loop counter as we have adjusted the array's contents
               i--;
               }
            }
         }
      // add the new class to end of list
      arrList[arrList.length] = strClass;
      // add the new class to beginning of list
      //arrList.splice(0, 0, strClass);
      // assign modified class name attribute
      objElement.className = arrList.join(' ');
      }
   // if there was no class
   else
      {
      // assign modified class name attribute
      objElement.className = strClass;
      }
   }
//
// AddClassName
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// RemoveClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
function RemoveClassName(objElement, strClass)
   {
   // if there is a class
   if ( objElement.className )
      {
      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');
      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();
      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )
         {
         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper )
            {
            // remove array item
            arrList.splice(i, 1);
            // decrement loop counter as we have adjusted the array's contents
            i--;
            }
         }
      // assign modified class name attribute
      objElement.className = arrList.join(' ');
      }
   // if there was no class
   // there is nothing to remove
   }
//
// RemoveClassName
// ----------------------------------------------------------------------------


/* Confirm submit
-------------------------------------------------- */
function confirmSubmit(text) {
	var agree = confirm(text);

	if (agree) return true;
	else return false;
}

function getSlaUrl() {
	var slaFrame = getSlaFrame();
	
	if (slaFrame == null) {
		return null;
	}
	
	var url = null;
	
	try {
		url = slaFrame.contentDocument.location;
	} catch(err) {}
	
	if (url != null) {
		return url;
	}
		
	try {
		url = slaFrame.contentWindow.location;
	} catch(err) {}
	
	return url;
}

function getSlaFrame() {
	var slaFrame = null;
	
	try {
		slaFrame = document.getElementById("billframe");
	} catch (err) {}
	
	if (slaFrame != null) {
		return slaFrame;
	}
	
	try {
		slaFrame = document.frames("billframe");
	} catch (err) {}
	
	return slaFrame;
}

function getSlaHeight() {
	var slaFrame = getSlaFrame();
	
	if (slaFrame == null) {
		return null;
	}
	
	var h = null;
	
	try {
		h = slaFrame.document.body.scrollHeight;
	} catch (err) {}
	
	if (h != null) {
		return h;
	}
	
	try {
		h = slaFrame.document.height;
	} catch (err) {}
	
	if (h != null) {
		return h;
	}
	
	try {
		h = slaFrame.contentDocument.height;
	} catch (err) {}
		
	return h;
}

/* Print Preview Popup
-------------------------------------------------- */
function printPreview() {
	var css='';
	var vanaSlaUrl = null;
	var slaFrame = document.getElementById("billframe");
	if (slaFrame) {
		vanaSlaUrl = getSlaUrl();
	}
	var html=document.documentElement.innerHTML;
	if (vanaSlaUrl) {
		html=html.replace(/sla\/[^\o042]*/i, vanaSlaUrl.toString().replace("eteenindus.php", "print.php") + '" ');
	}
	html=html.replace('_styles_screen.css','_styles_print.css');
	for (var i=0; i<arguments.length; i++)
	{
		css+='<link rel="stylesheet" type="text/css" href="gfx/'+arguments[i]+'" media="all" />\n';
	}
	if (css) html=html.replace(/<\/head>/i,css+'<\/head>\n');
	html=html.replace(/src=\"js\/_scripts.js\"/i, "");
	html=html.replace(/src=\"js\/tmv11.js\"/i, "");
	html=html.replace(/initHelp\([^\)]*\)\;/i, "");
	html=html.replace(/<\!\-\-stat\:fun\-\->/i, "function getTMqs() {} function nxtImg() {} function initHelp() {}");
	if(navigator.appName == 'Microsoft Internet Explorer'){
		html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'+
		'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="et" lang="et">'+
		html+
		'</html>';
	   w=window.open('','printPreview','resizable=yes,scrollbars=yes,toolbar=yes');
       w.document.write(html);
    }else{
	   w=window.open('','printPreview','resizable=yes,scrollbars=yes,toolbar=yes,location=yes');
	   w.document.open();
	   w.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'+
			'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="et" lang="et"></html>');
	   w.document.documentElement.innerHTML=html;
	}
	w.document.close();
	w.focus();
	setTimeout("w.print();", 1000);
}

/* Resize IFrame
-------------------------------------------------- */
function iFrameHeight() {
	var billframe = document.getElementById('billframe');

  try {
	  if (billframe) {
			  h = billframe.contentWindow.document.body.scrollHeight;
			  billframe.style.height = h + 'px';
	  }	 
  } catch(err) {}
}

if (document.all && !window.opera) {
	window.attachEvent('onload', iFrameHeight);
} else {
	window.addEventListener('load', iFrameHeight, false);
}

/* Popups
-------------------------------------------------- */
function popup(url,title,size) {
	if (size == 'small') {
		winwidth = '700px';
		winheight = '500px';
	}
	else if (size == 'large') {
		winwidth = '800px';
		winheight = '600px';
	}

	MM_openBrWindow(url, title, "width=700,height=500,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");

	function MM_openBrWindow(theURL,winName,features) {
		var wnd = window.open(theURL,winName,features);
		wnd.focus();
	}
}

/* Mark checkboxes
-------------------------------------------------- */
function markall(id,checked) {
	var f = document.forms[id];
	for (i = 0; i < f.elements.length; i++) {
		if (!f.elements[ i ].disabled){
		  f.elements[ i ].checked = checked;
		  }
	}
}

/* Toggle ID
-------------------------------------------------- */
function toggle(id){
	var im = id + "-toggle";
	var element = document.getElementById(id);

	if (element.style.display == 'none') {
		if (element.tagName == 'TBODY') {
			element.style.display = "";
		} else if (document.getElementById(id).tagName == 'TR' && !document.all){
			document.getElementById(id).style.display = "table-row";
		} else if ((navigator.userAgent.indexOf("Opera")!=-1 || navigator.userAgent.indexOf("Opera/")!=-1) && document.getElementById(id).tagName == 'TR') {
			element.style.display = "table-row";
		} else if (element.tagName == 'TD') {
			element.style.display = "table-cell";
		} else {
			element.style.display = 'block';
		}
		if (document.images[im]) {
			document.images[im].src = "gfx/ico_minus.gif";
			document.images[im].alt = "[-]";
			document.images[im].title = "Sulge";
		}
	} else {
		element.style.display = 'none';
		if (document.images[im]) {
			document.images[im].src = "gfx/ico_plus.gif";
			document.images[im].alt = "[+]";
			document.images[im].title = "Ava";
		}
	}
	return false;
}

/* =IE overflow fix
-------------------------------------------------- */
function fixIEoverflow() {
	// only apply to IE
	if (!/*@cc_on!@*/0) return;

	// find every element to test
	var all = document.getElementsByTagName('div'), i = all.length;

	// fast reverse loop
	while (i--) {
		// if the scrollWidth (the real width) is greater than
		// the visible width, then apply style changes
		if (all[i].className.match(/scroll/) && all[i].scrollWidth > all[i].offsetWidth) {
			all[i].style['paddingBottom'] = '16px';
			all[i].style['overflowY'] = 'hidden';
		}
	}
};

if (document.all && !window.opera) {
	window.attachEvent('onload', fixIEoverflow);
}

/* Input with autocomplition
-------------------------------------------------- */
var suggesionPreviousValueMap = new Array();
var suggestionCursorMap = new Array();
var suggestionListMap = new Array();
var inputWithAutocompletionCount = 0;

function onSuggestionKeyDown(curControl, ev) {
	var suggestionDiv = document.getElementById(curControl.id + "_suggestions");
	var keyCode = (window.event) ? window.event.keyCode : ev.keyCode;
	
	switch ( keyCode ) {
		// Ignore certain characters.
		case 16, 17, 18, 20:
			ev.cancelBubble = true;
			if ( window.event ) { // IE
				ev.returnValue = false;
			} else { // Firefox
				ev.preventDefault();
			}
			break;
		
		// Handle ESCAPE key is pressed.
		case 27:
			hideSelectionList(curControl, suggestionDiv, true);
			ev.cancelBubble = true; 
			if ( window.event ) { // IE
				ev.returnValue = false;
			} else { // Firefox
				ev.preventDefault();
			}
			break;
		
		// Handle ENTER key is pressed.
		case 13:
			if ( suggestionDiv.childNodes.length > 0 ) {
				
				handleSelectSuggestItem(curControl, suggestionDiv);
				ev.cancelBubble = true;
				if ( window.event ) { // IE
					ev.returnValue = false;
				} else { // Firefox
					ev.preventDefault();
				}
				
			}
			break;
	}
}

function onSuggestionKeyUp(curControl, ev) {
	var minChars = 0;
	
	var keyCode = (window.event) ? window.event.keyCode : ev.keyCode;
	var suggestionDiv = document.getElementById(curControl.id + "_suggestions");
	
	switch ( keyCode ) {
		case 13:
			// Ignore ENTER, do nothing.
			break;
			
		case 27:
			// Ignore ESCAPE, do nothing.
			break;
		
		// Handle UP ARROW is pressed.
		case 38:
			var childNodeCount = suggestionDiv.childNodes.length;
			if ( childNodeCount > 0 && suggestionCursorMap[curControl.id] ) {
				// Decrease selected node.
				suggestionCursorMap[curControl.id] = suggestionCursorMap[curControl.id] - 1;
				var selectedNode = suggestionDiv.childNodes[suggestionCursorMap[curControl.id]];
				highlightSelectedNode(suggestionDiv, selectedNode);
				
				var scrollStep = suggestionDiv.scrollHeight/childNodeCount;
				if ( suggestionDiv.scrollTop > scrollStep * suggestionCursorMap[curControl.id] ) {
					suggestionDiv.scrollTop = suggestionDiv.scrollTop - scrollStep;
				}
			}
			break;
		
		// Handle DOWN ARROW is pressed.
		case 40:
			var childNodeCount = suggestionDiv.childNodes.length;
			if ( childNodeCount > 0 && suggestionCursorMap[curControl.id] < childNodeCount - 1 ) { 
				// Increase selected node.
				suggestionCursorMap[curControl.id] = suggestionCursorMap[curControl.id] + 1;
				var selectedNode = suggestionDiv.childNodes[suggestionCursorMap[curControl.id]];
				highlightSelectedNode(suggestionDiv, selectedNode);
				
				var scrollStep = suggestionDiv.scrollHeight/childNodeCount;
				if ( suggestionDiv.scrollTop < scrollStep * (suggestionCursorMap[curControl.id] - (10 -1)) ) {
					suggestionDiv.scrollTop = suggestionDiv.scrollTop + scrollStep;
				}
				
			} else if ( childNodeCount > 0 && suggestionCursorMap[curControl.id] == childNodeCount - 1 ) {
				// Last position.
				break;
			} else {
				// Render check to only autocomplete after minimum number of characters are entered.
				if ( curControl == null || curControl.value.length < minChars ) {
					hideSelectionList(curControl, suggestionDiv, true);
					return;
				}
				
				suggesionPreviousValueMap[curControl.id] = curControl.value;
				
				/*var suggestionRequest = {
					inputId: curControl.id,
					type: "SUGGESION_REQUEST_TYPE_TELNR",
					begining: curControl.value,
					parameters: eval(normalizedId + "_parameters"),
					limit: 30
				};
				dwrService.loadSuggestions(suggestionRequest, loadSuggestionsCallback);*/
				
				loadSuggestionsCallback(curControl, suggestionListMap[curControl.id]);
			}
			break;
		
		default:
			break;
			
			// Render check to only autocomplete after minimum number of characters are entered.
			if ( curControl == null || curControl.value.length < minChars ) {
				hideSelectionList(curControl, suggestionDiv, true);
				return;
			}
			
			if ( curControl.value == suggesionPreviousValueMap[curControl.id] ) {
				return;
			}
			
			/*eval(normalizedId + "_previousValue = '" + curControl.value + "'");*/
			suggesionPreviousValueMap[curControl.id] = curControl.value;
			
			/*var suggestionRequest = {
				inputId: curControl.id,
				type: "SUGGESION_REQUEST_TYPE_TELNR",
				begining: curControl.value,
				parameters: eval(normalizedId + "_parameters"),
				limit: 30
			};
			dwrService.loadSuggestions(suggestionRequest, loadSuggestionsCallback);*/
			
			loadSuggestionsCallback(curControl, suggestionListMap[curControl.id]);
	}
}

/*
function loadSuggestionsCallback(suggestionResponse) {
	var curControl = document.getElementById(suggestionResponse.inputId);
	populateSuggestionDiv(curControl, suggestionResponse.suggestionArray);
}
*/

function loadSuggestionsCallback(curControl, suggestionArray) {
	populateSuggestionDiv(curControl, suggestionArray);
}

function populateSuggestionDiv(curControl, suggestionArray) {
	var suggestionDiv = document.getElementById(curControl.id + "_suggestions");
	
	// If container not found (shouldn't happen), then simply return.
	if ( suggestionDiv == null ) {
		return;
	}
	
	suggestionDiv.innerHTML = ""; 
	
	// If no values return, hide suggestion list.
	if ( suggestionArray == null || suggestionArray == undefined || suggestionArray.length < 1 ) { 
		suggestionDiv.style.display = "none";
		return;
	}
	
	// Show suggestion list.
	suggestionDiv.style.display = "block";
	
	suggestionDiv.style.top = (curControl.offsetTop + curControl.offsetHeight) + "px";
	suggestionDiv.style.left = curControl.offsetLeft + "px";
	
	// Iterate through all values:
	// 1. Create DIV element.
	// 2. Set attributes and text node value.
	// 3. Append new element to the container.
	var itemIdPrefix = curControl.id + "_suggestion_";
	var itemIdPrefixLength = itemIdPrefix.length;
	for ( var i = 0; i < suggestionArray.length; i++ ) {
		// Get current value.
		var curValue= suggestionArray[i];	 
		
		// If value is not blank.
		if ( curValue != null && curValue.length > 0 ) {
			// Create DIV element.
			var newItem = document.createElement("div");
					 
			// Append current value as a text node.
			newItem.appendChild(document.createTextNode(curValue));
			
			// Set attributes.
			newItem.setAttribute("class", "autoCompleteItem");
			newItem.id = itemIdPrefix + i;
			
			if ( document.all ) {
				newItem.onclick = function() {
						handleClickOnSuggestItem(this.id.substr(itemIdPrefixLength), curControl.id);
				};
			} else { // Firefox and other DOM compatible browsers.
				newItem.setAttribute("onclick", "handleClickOnSuggestItem(" + i + ", '" + curControl.id + "')");
			}
			
			// Finally append new element to container.
			suggestionDiv.appendChild(newItem);
		}
	}
	
	// Set first item as the selected node.
	suggestionCursorMap[curControl.id] = 0;
	
	// Get first node.
	var selectedNode = suggestionDiv.childNodes[0]; 
	
	// If first node is equal to the first node, hide the selection list.
	/*if ( selectedNode.childNodes[0].nodeValue == curControl.value ) {
		hideSelectionList(curControl, suggestionDiv, true);
	} else {*/ // Highlight the first node.
		highlightSelectedNode(suggestionDiv, selectedNode);
	/*}*/
}

function highlightSelectedNode(suggestionDiv, selectedNode) {
	if ( suggestionDiv == null || selectedNode == null ) { 
		return;
	}
	
	// Iterate through all items searching for a node that matches the node selected.
	for ( var i = 0; i < suggestionDiv.childNodes.length; i++ ) { 
		var curNode = suggestionDiv.childNodes[i];
		curNode.className = (curNode == selectedNode) ? "autoCompleteItemSelected" : curNode.className = "autoCompleteItem";
	}
}

function handleSelectSuggestItem(curControl, suggestionDiv) {
	// Cursor is a global variable that is incremented/decremented when the UP ARROW or DOWN ARROW key is pressed.
	var selectedNode = suggestionDiv.childNodes[suggestionCursorMap[curControl.id]];
	
	// Get selected value.
	var selectedValue = selectedNode.childNodes[0].nodeValue;
	
	// Set the value of the INPUT control.
	curControl.value = selectedValue;
	
	// Finally hide the selection list.
	hideSelectionList(curControl, suggestionDiv, true);
}

function handleClickOnSuggestItem(itemNumber, controlId) {
	var curControl = document.getElementById(controlId);
	var suggestionDiv = document.getElementById(controlId + "_suggestions");
	
	// Get selected node.
	var selectedNode = suggestionDiv.childNodes[itemNumber];
	
	// Get selected value.
	var selectedValue = selectedNode.childNodes[0].nodeValue;
	
	// Set the value of the INPUT control.
	curControl.value = selectedValue;
	
	// Finally hide the selection list.
	hideSelectionList(curControl, suggestionDiv, true);
}

function hideSelectionList(curControl, suggestionDiv, isFocusOnControl) {
	if ( suggestionDiv == null || suggestionDiv == undefined ) { 
		return;
	}
	
	suggestionDiv.innerHTML = "";
	suggestionDiv.style.display = "none";
	
	if ( isFocusOnControl ) {
		curControl.focus();
	}
}

function toggleSelectionList(controlId) {
	var curControl = document.getElementById(controlId);
	var suggestionDiv = document.getElementById(controlId + "_suggestions");
	
	if ( suggestionDiv.childNodes.length > 0 ) {
		hideSelectionList(curControl, suggestionDiv, true)
	} else {
		populateSuggestionDiv(curControl, suggestionListMap[controlId]);
		curControl.focus();
	}
}

function initializeSuggestionElement(controlId, suggestionList) {
	suggesionPreviousValueMap[controlId] = "";
	suggestionCursorMap[controlId] = -1;
	suggestionListMap[controlId] = suggestionList;
	
	return ++inputWithAutocompletionCount;
}
