
var tplb_current;
var tplb_last_y;
var tplb_element_height;
var tplb_body_height;
var tplb_div;
var tplb_div_up_y;
var tplb_div_down_y;
var tplb_interval = "";

function select_Item(text, value) {
	this.text = text;
	this.value =  value;	
}

function tplb_StopTimer() {
	if(tplb_interval != "") 
	{
		window.clearInterval( tplb_interval );
		tplb_interval = "";
	}
}

function tplb_Up_Selection( Y, scrollCount ) {
	if(tplb_div.tplb_last_element == null) return;
	var index = tplb_div.tplb_last_element.rowIndex;
	if( scrollCount == null ) scrollCount = 1;
	while ( Y  < tplb_last_y && index-- > 0 && scrollCount-- > 0 )
	{
		if( tplb_div.tplb_start_index < tplb_div.tplb_last_element.rowIndex )
			tplb_Selection( tplb_div.tplb_last_element );
		else
		{
			if(	index == tplb_div.tplb_start_index ) continue;
			tplb_Selection( tplb_current.rows[ index ] );
		}	
		
		tplb_div.tplb_last_element = tplb_current.rows[ index ];
		
		if (tplb_last_y > tplb_div_up_y)
			tplb_last_y -= tplb_element_height;
		else
		{
			tplb_div.scrollTop -= tplb_element_height;
			if( index == 0 ) tplb_div.scrollTop -= tplb_element_height * 2;
		}
	}
}

function tplb_Down_Selection( Y, scrollCount )
{
	if( tplb_div.tplb_last_element == null ) return;
	
	var index = tplb_div.tplb_last_element.rowIndex;

	if( scrollCount == null ) scrollCount = 1;

	while ( ( Y > tplb_last_y + tplb_element_height || Y > tplb_div_down_y ) && tplb_div.tplb_last_element.rowIndex < tplb_current.rows.length -1 && scrollCount-- > 0 )
	{
		index++;

		if(tplb_div.tplb_start_index > tplb_div.tplb_last_element.rowIndex )
			tplb_Selection( tplb_div.tplb_last_element );
		else
		{
			if(	index == tplb_div.tplb_start_index ) continue;
			tplb_Selection( tplb_current.rows[ index ] );
		}
		
		tplb_div.tplb_last_element = tplb_current.rows[ index ];
		
		if( tplb_last_y < tplb_div_down_y )
			tplb_last_y += tplb_element_height;
		else
		{	
			tplb_div.scrollTop += tplb_element_height;
			if( tplb_div.tplb_last_element.rowIndex == tplb_current.rows.length -1 ) tplb_div.scrollTop += tplb_element_height * 2;
		}
	}
}

function tplb_GetDivY(evt)
{
	return evt.clientY - (evt.offsetY +  evt.srcElement.offsetTop - tplb_div.scrollTop );
}

function tplb_ScrollCountSpeed( difference )
{
	if(difference / tplb_element_height < 1)
		return 1;
	if(difference / tplb_element_height < 3)
		return 2;

	return 10;
}

function tplb_ScrollIntervalSpeed( difference )
{
	if(difference / tplb_element_height < 1)
		return 120;
	if(difference / tplb_element_height < 2)
		return 50;

	return 1;
}

function tplb_ResolveSelection(evt)
{	
	var clientY = evt.clientY;
	if ( tplb_last_y  < clientY )
	{
		if( tplb_div_down_y < clientY )
			tplb_interval = window.setInterval("tplb_Down_Selection(" + clientY + ", " + tplb_ScrollCountSpeed( clientY - tplb_div_down_y ) + ")", tplb_ScrollIntervalSpeed( clientY - tplb_div_down_y ) );
		else
			tplb_Down_Selection( clientY);
	}
	else if ( tplb_last_y > clientY )
	{
		if( tplb_div_up_y > clientY )
			tplb_interval = window.setInterval("tplb_Up_Selection(" + clientY + ", " + tplb_ScrollCountSpeed( tplb_div_up_y - clientY ) + ")", tplb_ScrollIntervalSpeed( tplb_div_up_y - clientY ) );
		else
			tplb_Up_Selection( clientY );
	}
}

function tplb_SelectionProcess()
{	
	tplb_StopTimer();
	tplb_ResolveSelection(event);
}

function tplb_StopSelection()
{
	tplb_StopTimer();
	dom_detachEventForObject(document.body, "mousemove", tplb_SelectionProcess);
	dom_detachEventForObject(document.body, "mouseup", tplb_StopSelection);
	if( typeof(dom_findParent(tplb_current.parentElement, "table").getAttribute("OnSelected")) != 'undefined' );
		eval( dom_findParent(tplb_current.parentElement, "table").getAttribute("OnSelected") );
}

function tplb_StartSelection(evt)
{
	if( tplb_div.tplb_last_element == null )
		return;
	dom_attachEventForObject(document.body, "mouseup", tplb_StopSelection);
	tplb_div_up_y = tplb_GetDivY(evt);
	tplb_div_down_y = tplb_div.clientHeight + tplb_div_up_y;
	tplb_element_height =  dom_findParent(evt.srcElement, "tr").clientHeight;
	tplb_body_height = tplb_div.clientHeight;
	tplb_current = dom_findParent(evt.srcElement, "table");
	tplb_last_y = evt.clientY - evt.offsetY;
	tplb_div.tplb_start_index = tplb_div.tplb_last_element.rowIndex;
	dom_attachEventForObject(document.body, "mousemove", tplb_SelectionProcess);
}

function tplb_StringSubSort( str1, str2 )
{
	var s1=str1.text.trim().toLowerCase(); s2=str2.text.trim().toLowerCase();
	if(s1>s2)return 1;else if(s1<s2)return -1;return 0;
}

function tplb_list_population( dest, values, clear )
{
	if(values==null)return;
	var ar;
	if( clear != false )
	{
		tplb_Clear( dest );		
	}
	if( document.getElementById( dest ).getAttribute("Sorting") == "True" )
		ar = values.sort( tplb_StringSubSort );
	else
		ar = values;
	for(var i = 0; i < ar.length; i++)
		tplb_AddRow( dest, ar[i].text, ar[i].value );
}

function tplb_XML_population( xml, dest, name_id, name_title, clear )
{
	if (dest == null) return;
	if( clear != false )
		tplb_Clear( dest );
	if (xml == null) return;
	if (name_id == null) name_id = "ID";
	if (name_title == null) name_title = "Title";
	var ar;
	for(var i = 0; i < xml.GetCount(); i++)
	{
		var el = xml.GetByIndex(i);
		tplb_AddRow( dest, el.GetNodeValue(name_title), el.GetNodeValue(name_id) );
	}
}

function tplb_UnselectAll( table, evt )
{
//	if ( table.parentElement.parentElement.multiselect == "True" && event.type == "mousemove" /*|| ( table.parentElement.parentElement.multiselect == "True" && event.ctrlKey )*/ )
//		return;
	if( evt.ctrlKey || evt.shiftKey )
	{
		tplb_div.last_control_selection = true;
		return;
	}
	
	if( tplb_div.tplb_last_element == null )return;
	if( tplb_div.tplb_last_element.rowIndex < 0 )return;
	
	if( tplb_div.last_control_selection && typeof(table.rows) != "undefined" && table.rows != null )
	{
		for ( var i = 0; i < table.rows.length; i++ )
			tplb_UnSelect( table.rows[i] );
		tplb_div.last_control_selection = false;
		return;
	}

	if( tplb_div.tplb_start_index > tplb_div.tplb_last_element.rowIndex )
		for ( var i = tplb_div.tplb_last_element.rowIndex; i <= tplb_div.tplb_start_index; i++ )
			tplb_UnSelect( table.rows[i] );
	else if( tplb_div.tplb_start_index < tplb_div.tplb_last_element.rowIndex )
		for ( var i = tplb_div.tplb_start_index; i <= tplb_div.tplb_last_element.rowIndex; i++ )
			tplb_UnSelect( table.rows[i] );
	else
		tplb_UnSelect( tplb_div.tplb_last_element );
}

function tplb_ShiftSelection( table, evt )
{
		if( tplb_div.tplb_last_element == null ) return;
		if( tplb_div.tplb_last_element.rowIndex > evt.srcElement.parentElement.rowIndex )
			for ( var i = evt.srcElement.parentElement.rowIndex; i <= tplb_div.tplb_last_element.rowIndex; i++ )
				tplb_Select( table.rows[i] );
		else
			for ( var i = tplb_div.tplb_last_element.rowIndex; i <= evt.srcElement.parentElement.rowIndex; i++ )
				tplb_Select( table.rows[i] );
		
}

function tplb_MouseSelect(newevent)
{	
	newevent.returnValue = false;
	tplb_div = newevent.srcElement.parentElement.parentElement.parentElement.parentElement;
	if( newevent.srcElement.parentElement.parentElement.rows.length == 0 )
		return false;
	tplb_div.focus();	
	tplb_UnselectAll( newevent.srcElement.parentElement.parentElement, newevent );
	if( newevent.shiftKey )
	{
		tplb_ShiftSelection( newevent.srcElement.parentElement.parentElement, newevent);
	}else if( newevent.ctrlKey )
		tplb_Selection( newevent.srcElement.parentElement );
	else
		tplb_Select( newevent.srcElement.parentElement );	
	tplb_div.tplb_last_element = newevent.srcElement.parentElement;	
	tplb_StartSelection(newevent);
	return false;	
}

function tplb_Select( dest )
{
	var table = dom_findParent(dest, "table");
	if (!table) return;
	if (dom_findParent(table, "table").getAttribute("multiselect") == "false")
		for ( var i = 0; i < table.rows.length; i++ )
			tplb_UnSelect( table.rows[i] );
	if (dest.is_selected == true)
		return;
	dest.style.backgroundColor = table.getAttribute("SelectionColor");
	dest.style.color = table.getAttribute("TextSelectionColor");
	dest.is_selected = true;
}

function tplb_UnSelect( dest )
{
	if (dest.is_selected == false)
		return;
	dest.style.backgroundColor = 'white';
	dest.style.color = dom_findParent(dest, "table").getAttribute("TextColor");
	dest.is_selected = false;
}

function tplb_Selection( dest )
{
	if (dest.is_selected == true )
	{
		tplb_UnSelect( dest );
	}
	else
	{
		tplb_Select( dest );
	}
}

function tplb_GetTableElement( src )
{
	var table = document.getElementById( src );
	if( table == null ) return null;	
	return table.getElementsByTagName("table")[0];
}

function tplb_DeleteSelected(src) 
{
	var table = tplb_GetTableElement(src);
	if (table == null) return;
	for (var i = 0;	i<table.rows.length; i++) 
		if (table.rows[i].is_selected == true)
			table.deleteRow(i--);
	//tplb_GenerateState( src );
}

function tplb_GetSelected(src) 
{
	var table = tplb_GetTableElement(src), res = [];
	if (table == null) return res;
	for (var i=0, ic=table.rows.length; i<ic; ++i)
		if (table.rows[i].is_selected == true)
			res.push(table.rows[i].children[1].outerText);
	return res;
	//tplb_GenerateState( src );
}

function tplb_GetItems(src) {
	var table = tplb_GetTableElement(src), res = [];
	if(!table) return res;
	for (var i=0, ic = table.rows.length; i < ic; ++i)
		res.push(new select_Item(table.rows[i].children[0].outerText, table.rows[i].children[1].outerText));
	return res;
}

function tplb_GetSelectedItems(src)
{
	var table = tplb_GetTableElement(src), res = [];
	if (table == null) return res;
	for (var i = 0, ic=table.rows.length; i < ic; ++i)
		if (table.rows[i].is_selected == true)
			res.push(new select_Item(table.rows[i].children[0].outerText, table.rows[i].children[1].outerText));
	return res;	
}

function tplb_SetSelectedItems(src, items)
{	
	var table = tplb_GetTableElement(src);
	if (table == null || table.rows.length == 0)
		return;
	tplb_div = table.parentElement;
	for ( var i=0, ic=table.rows.length; i<ic; ++i)
	{
		tplb_UnSelect(table.rows[i]);
		if(items!=null)
			for (var j=0,jc=items.length; j<jc; ++j)
				if (table.rows[i].children[1].outerText == items[j])
				{
					tplb_Selection(table.rows[i]);
					tplb_div.tplb_last_element = table.rows[i];
				}	
	}	
}

function tplb_AddToState( dest, name, value )
{
	var state = document.getElementById( dest + "_state" );
	if( state == null ) return;
	
	if(state.value != "")
			state.value += ',';
		state.value += escape( name ) + ( name == value ? "" : (":" + escape( value )) );
}

function tplb_DeleteByValue( src, value )
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return;

	for ( var i = 0; i < table.rows.length; i++ )
	{
		if( table.rows[i].children[1].outerText == value )
			table.deleteRow(i--);
			
	}
	tplb_GenerateState( src );
}

function tplb_DeleteDupes( src, dest)
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return;

	for ( var i = 0; i < table.rows.length; i++ )
	{
		tplb_DeleteByValue( dest, table.rows[i].children[1].outerText );
	}
}

function tplb_GetNameByValue(src, value)
{
	var table = tplb_GetTableElement( src );
	if (table == null) return;
	for ( var i=0,ic=table.rows.length;i<ic; ++i)
		if(table.rows[i].children[1].outerText == value)
			return table.rows[i].children[0].outerText;
}

function tplb_GetValueByName( src, name )
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return;
	for (var i = 0,ic=table.rows.length; i<ic; ++i)
		if(table.rows[i].children[0].outerText == name)
			return table.rows[i].children[1].outerText;
}

function tplb_AddRow( dest, name, value, index, onlyUnique )
{	
	if ( name == "") return;
	if (onlyUnique == true && tplb_GetValueByName(dest, name) == value)
		return;
	var table = tplb_GetTableElement( dest );
	if( table == null ) return;
	var pclass = document.getElementById(dest).className;
	var position = index;
	if( index == null)
		position = -1;
			
	var tr = table.insertRow( position );//document.createElement("TR");
	var tdName = document.createElement("TD");
	var tdValue = document.createElement("TD");
	
	// TO DO: Viacheslav Kopichev (Memory leak problem)	
	//dom_setProperty(tr, "onmousedown", tplb_MouseSelect);
	dom_attachEventForObject(tr, "mousedown", tplb_MouseSelect);
	
	tr.UNSELECTABLE = "on";
	tdName.style.paddingLeft = "2px";
	tdName.innerText = name ;
	tdName.UNSELECTABLE = "on";
	tdName.noWrap = "on";
	tdName.className = pclass;
	if( document.getElementById( dest ).getAttribute("ShowToolTip") == "True" )
		tdName.title = name;
	tdValue.innerText = value;
	tdValue.style.display = "none";
	tr.appendChild( tdName );
	tr.appendChild( tdValue );
	
	//table.firstChild.appendChild( tr );
	tplb_AddToState( dest, name, value );
}

function tplb_GenerateState( src )
{
	var state = document.getElementById( src + "_state" );
	if( state == null ) return;
	
	var table = tplb_GetTableElement( src );
	if( table == null ) return;
	
	var newstate = "";

	for ( var i = 0; i < table.rows.length; i++ )
	{
		if(newstate != "")
			newstate += ',';
		newstate += escape( table.rows[i].children[0].outerText) + ( table.rows[i].children[0].outerText == table.rows[i].children[1].outerText? "" : (":" + escape( table.rows[i].children[1].outerText) ));
	}
	state.value = newstate;
	// for SP	
	if (typeof(tplb_sp_update) != 'undefined') try {eval(tplb_sp_update);} catch(e) {}
}

function tplb_GetSortedIndex( dest, name )
{
	name = name.toLowerCase();
	var table = tplb_GetTableElement( dest );
	if( table == null && dest.tagName == null ) return;
	if( dest.tagName == "TABLE" )
		table = dest;
	//table.offsetParent.last_control_selection = true;
	if(table.rows.length==0 || table.rows[table.rows.length-1].children[0].outerText.toLowerCase() <= name )
		return -1;
	var top = 0, bottom=table.rows.length -1;
	while(top<bottom)
	{
		if(bottom-top==1)
			return (table.rows[top].children[0].outerText.toLowerCase() > name)?top:bottom;
		var tmp = top + Number(((bottom-top)/2).toFixed(0));
		if(table.rows[tmp].children[0].outerText.toLowerCase() > name)
			bottom = tmp;
		else
			top = tmp;			
	}
	return top;
}

function tplb_MoveAllRows( src, dest )
{
	var div = document.getElementById( dest );
	if( div == null ) return;
	
	if( div.empty == "True" )
	{
		tplb_Clear( dest );
		tplb_Clear( src );
		tplb_GenerateState( src );
		tplb_GenerateState( dest );
		return;
	}
	
	var table = tplb_GetTableElement( src );
	
	if( table == null ) return;
	if( table.rows.length < 1 ) return;
	
	var numRows = table.rows.length;
	for ( var i = 0; i < numRows; i++ )
	{
			tplb_AddRow( dest,
				table.rows[i].children[0].outerText,
				table.rows[i].children[1].outerText,
				tplb_GetSortedIndex( dest, table.rows[i].children[0].outerText ) );
	}
	
	tplb_Clear( src );
	tplb_GenerateState( src );
}

function tplb_MoveSelectRows( src, dest ) {
	var table = tplb_GetTableElement( src );
	
	if( table == null ) return;
	if( table.rows.length < 1 ) return;
	var numRows = table.rows.length, isSelected = false;
	for ( var i = 0; i < numRows; i++ ) {
		if ( table.rows[i].is_selected == true ) {
			isSelected = true;
			tplb_AddRow( dest,
				table.rows[i].children[0].outerText,
				table.rows[i].children[1].outerText,
				tplb_GetSortedIndex( dest, table.rows[i].children[0].outerText ) );
		}
	}
	if (isSelected) {
		tplb_DeleteSelected( src );
		tplb_GenerateState( src );
		tplb_ClearSelections(dest);
		if (tplb_div != null)
			tplb_div.tplb_last_element = null;
	}
}

function tplb_IsValueExist( src, value )
{
	var table = tplb_GetTableElement( src );
	if (table == null) return;
	for (var i = 0, ic=table.rows.length; i < ic; ++i)
		if(table.rows[i].children[1].outerText == value)
			return true;
	return false;
}

function tplb_CopySelectRows( src, dest )
{
	var table = tplb_GetTableElement( src );
	var dest_table = tplb_GetTableElement( dest );
	
	if( table == null ) return;
	if( table.rows.length < 1 ) return;
	
	var numRows = table.rows.length;
	for ( var i = 0; i < numRows; i++ )
	{
		if ( table.rows[i].is_selected == true )
		{
			if ( !tplb_IsValueExist( dest, table.rows[i].children[1].outerText ) )
			{
				tplb_AddRow( dest,
					table.rows[i].children[0].outerText,
					table.rows[i].children[1].outerText,
					dest_table.rows.length );
			}
			tplb_UnSelect( table.rows[i] );
		}
	}
	//tplb_UnselectAll( src );
	if (tplb_div != null)
		tplb_div.tplb_last_element = null;
}

function tplb_Clear( src )
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return;
	if ( table.rows.length == 0 )return;
	tplb_div = table.parentElement;		
	tplb_div.tplb_start_index = null;
	tplb_div.tplb_last_element = null;
		
	for ( var i = 0; i < table.rows.length; i++ )
		table.deleteRow(i--);
	tplb_GenerateState( src );
}

function tplb_GetStateString( src )
{
	var state = document.getElementById( src + "_state" );
	if( state == null )
		return '';
	return state.value;
}

function tplb_GetItemCount( src )
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return 0;
	return table.rows.length;
}

function tplb_FixScrollPosition()
{
	if( tplb_div.tplb_last_element.offsetTop - tplb_div.scrollTop < 0 )
		tplb_div.scrollTop =tplb_div.tplb_last_element.offsetTop;// tplb_element_height * tplb_div.tplb_start_index;
	else if( tplb_div.tplb_last_element.offsetTop - tplb_div.scrollTop > tplb_div.clientHeight - tplb_div.tplb_last_element.clientHeight )
		tplb_div.scrollTop =tplb_div.tplb_last_element.offsetTop-tplb_div.clientHeight+tplb_div.tplb_last_element.clientHeight;
}

function tplb_MoveUpSelectRows( src )
{
	var table = tplb_GetTableElement( src );
	
	if( table == null ) return;
	
	tplb_div = table.offsetParent;
	
	if( tplb_div.tplb_last_element == null )return;

	if( tplb_div.tplb_last_element.rowIndex < 0 )return;

	if( tplb_div.tplb_last_element.rowIndex == 0 || tplb_div.tplb_start_index  == 0 )
	{
		tplb_FixScrollPosition();
		return;
	}

	var tmp_name, tmp_value;

	if( tplb_div.tplb_start_index == tplb_div.tplb_last_element.rowIndex)
	{
		tmp_name = table.rows[ tplb_div.tplb_start_index -1 ].children[0].outerText;
		tmp_value = table.rows[ tplb_div.tplb_start_index -1 ].children[1].outerText;
		
		table.rows[ tplb_div.tplb_start_index -1 ].children[0].innerText = table.rows[ tplb_div.tplb_start_index ].children[0].outerText;
		table.rows[ tplb_div.tplb_start_index -1 ].children[1].innerText = table.rows[ tplb_div.tplb_start_index ].children[1].outerText;
		table.rows[ tplb_div.tplb_start_index ].children[0].innerText = tmp_name;
		table.rows[ tplb_div.tplb_start_index ].children[1].innerText = tmp_value;
		
		tplb_div.tplb_last_element = table.rows[ tplb_div.tplb_start_index -1 ];
		tplb_Select( tplb_div.tplb_last_element );
		tplb_UnSelect( table.rows[ tplb_div.tplb_start_index ] );
		tplb_div.tplb_start_index = tplb_div.tplb_last_element.rowIndex;
		tplb_FixScrollPosition();
		tplb_GenerateState( src );
	}
}

function tplb_MoveDownSelectRows( src )
{
	var table = tplb_GetTableElement( src );
	
	if( table == null ) return;
	
	tplb_div = table.offsetParent;
	
	if( tplb_div.tplb_last_element == null )return;

	if( tplb_div.tplb_last_element.rowIndex < 0 )return;

	if( tplb_div.tplb_last_element.rowIndex >= table.rows.length -1 || tplb_div.tplb_start_index  >= table.rows.length -1 )
	{
		tplb_FixScrollPosition();
		return;
	}
	
	var tmp_name, tmp_value;

	if( tplb_div.tplb_start_index == tplb_div.tplb_last_element.rowIndex )
	{
		tmp_name = table.rows[ tplb_div.tplb_start_index +1 ].children[0].outerText;
		tmp_value = table.rows[ tplb_div.tplb_start_index +1 ].children[1].outerText;
		
		table.rows[ tplb_div.tplb_start_index +1 ].children[0].innerText = table.rows[ tplb_div.tplb_start_index ].children[0].outerText;
		table.rows[ tplb_div.tplb_start_index +1 ].children[1].innerText = table.rows[ tplb_div.tplb_start_index ].children[1].outerText;
		table.rows[ tplb_div.tplb_start_index ].children[0].innerText = tmp_name;
		table.rows[ tplb_div.tplb_start_index ].children[1].innerText = tmp_value;
		
		tplb_div.tplb_last_element = table.rows[ tplb_div.tplb_start_index +1 ];
		tplb_Select( tplb_div.tplb_last_element );
		tplb_UnSelect( table.rows[ tplb_div.tplb_start_index ] );
		tplb_div.tplb_start_index = tplb_div.tplb_last_element.rowIndex;
		tplb_FixScrollPosition();
		tplb_GenerateState( src );
	}
}

function tplb_GetNumRowFromKey( key )
{
	if( tplb_div == null )return -1;

	if( tplb_div.tplb_start_index < tplb_current.rows.length + 1 )
		for ( var i = tplb_div.tplb_start_index + 1; i < tplb_current.rows.length; i++ )
		{
			var str = tplb_current.rows[i].children[0].outerText.substring(0, 1);
			if( str.toUpperCase() == key )
				return i;
}

	for ( var i = 0 ; i < tplb_div.tplb_start_index; i++ )
	{
		if (tplb_current.rows[i]) {
			var str = tplb_current.rows[i].children[0].outerText.substring(0, 1);
			if( str.toUpperCase() == key )
				return i;
		}
	}
	
	return -1;
}

function tplb_KeyDown(postbackFunc, evt)
{
	evt.returnValue = false;
	if(!str_IsStringEmpty(postbackFunc))
	{
		try{eval(postbackFunc+"(evt);")}catch(e){};
	}
	tplb_div = evt.srcElement;
	tplb_current = tplb_div .firstChild;
	
	if( evt.keyCode == 38 )
	{
		if( tplb_div.tplb_start_index <= 0)
			return;
		tplb_div.tplb_last_element = tplb_current.rows[ tplb_div.tplb_start_index -1 ];
	}
	else if( evt.keyCode == 40 )
	{
		if( tplb_div.tplb_start_index >= tplb_current.rows.length -1 )
			return;
		tplb_div.tplb_last_element = tplb_current.rows[ tplb_div.tplb_start_index +1 ];
	}
	else if( ( evt.keyCode >= 65 && evt.keyCode <= 90 ) || ( evt.keyCode >= 48 && evt.keyCode <= 57 ) )
	{
		var index = tplb_GetNumRowFromKey( String.fromCharCode(evt.keyCode) );
		if( index == -1 )
			return;
		tplb_div.tplb_last_element = tplb_current.rows[ index ];
	}
	else return;

	if (tplb_div.tplb_last_element)
	{
		if( tplb_div.tplb_start_index != tplb_div.tplb_last_element.rowIndex )
		{
			tplb_div.last_control_selection = true;	
			tplb_UnselectAll( tplb_current, evt );
		}
	
		tplb_Select( tplb_div.tplb_last_element );
	
		if( !evt.shiftKey )
			tplb_UnSelect( tplb_current.rows[ tplb_div.tplb_start_index ] );
		
		tplb_div.tplb_start_index = tplb_div.tplb_last_element.rowIndex;
		tplb_FixScrollPosition();	
		if( typeof(dom_findParent(tplb_div, "table").getAttribute("OnSelected")) != 'undefined' );
			eval( dom_findParent(tplb_div, "table").getAttribute("OnSelected") );
	}
}

function tplb_ContainsValue(src, val)
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return false;
	for ( var i = 0, ic=table.rows.length; i < ic; ++i)
		if (table.rows[i].children[1].outerText == val)
			return true;
	return false;
}

function tplb_ClearSelections( src )
{
	var table = tplb_GetTableElement(src);
	if (table == null) return;
	for (var i=0,ic=table.rows.length; i<ic; ++i)
		tplb_UnSelect(table.rows[i]);
	table.parentElement.scrollTop = 0;
	table.parentElement.scrollLeft = 0;
}

function tplb_UnselectByValue(src, value)
{
	var table = tplb_GetTableElement( src );
	if( table == null ) return false;
	for (var i = 0, ic=table.rows.length; i < ic; ++i)
		if( table.rows[i].children[1].outerText == value)
		{
			tplb_UnSelect(table.rows[i]);
			return true;
		}
	return false;
}
