// $Revision: 3 $ $Date: 2011-05-23 14:43:45+01:00 $ $Workfile: TT_Lov.js $

var en_dropID = 0;
var en_textID = 1;
var KEY_TAB =9;

function ttlov_formKeyPress(p_object, p_event, shortCutKey)
// doesn't work in Netscape as it doesn't trap Alt key.
{
	var m_keyCode = window.event.keyCode;
	var m_altKey  = window.event.altKey;
	var shortCutKey2 = parseInt(shortCutKey)+32;
				
	if (m_altKey&&((m_keyCode==m_altKey)||(m_keyCode==shortCutKey)))
	// alt+[lL]
	{
		//var thisLov = document.getElementById(p_object.parentElement.id + "_" + cd[en_dropID]);
		var thisLov = document.getElementById(p_object);		
		thisLov.focus();
				
		return false;
	}
}

function ttlov_vlChange(p_droObject,p_lovObject, p_event,p_autoPostBack)
{
	var thisDrop = document.getElementById(p_droObject);
	var thisText = document.getElementById(p_lovObject);
	
	thisText.value = thisDrop.value;
	if(thisDrop.onchangeFunction !=null)
		eval(thisDrop.onchangeFunction);
		
    if(p_autoPostBack=='True')
	{
	    var linkId =p_droObject.replace(/_/g,"\$");
	    linkId =linkId.replace("$ct","_ct");
	    __doPostBack(linkId,'');
	}
	return false;
}

function ttlov_autoFill(p_lovObject, p_droObject,p_event, p_invalidLov, p_autoPostBack)
{
	// 47 is the delete key, keys below this are things like cursor keys that should not perform this check.
	// However keyCode 0 is OK for simulated events
	if (window.event.keyCode<47
		&& window.event.keyCode!=8  
		&& window.event.keyCode!=46
		&& window.event.keyCode!=0 )	
		return;
		
	try
	{
		var i;
		var thisDrop = document.getElementById(p_droObject);
		var thisText = document.getElementById(p_lovObject);	
		var m_value = new String(thisText.value);

		//8 is backspace //46 is delete
		if(window.event.keyCode ==8 && thisText.value !='' ||
			window.event.keyCode ==46 && thisText.value !='')
			return;

		var found = false;
		var matchIndex;
		for (i=0; i<thisDrop.options.length; i++)
		{
			if (m_value.toUpperCase()==thisDrop.options[i].value.toUpperCase().substr(0,m_value.length))
			{
				// Remember index of first code match, or an exact match only
				if (!found)
				{
					matchIndex = i;
				}
				// Only breakout if exact match, otherwise keep looking
				if (m_value.toUpperCase() == thisDrop.options[i].value.toUpperCase())
				{
					matchIndex = i;
					found = true;
					break;
				}
				found = true;
			}
		}

		if (found) 
		{
			thisText.value = thisDrop.options[matchIndex].value;
			thisDrop.value = thisDrop.options[matchIndex].value;

			if (window.event.keyCode != 0)
			{
				var m_startPos = m_value.length;
				var m_endPos = thisText.value.length;

				ttlov_selectInputText(thisText, m_startPos, m_endPos);
				if (p_autoPostBack == 'True')
				{
					var linkId = p_droObject.replace(/_/g, "\$");
					linkId = linkId.replace("$ct", "_ct");
					__doPostBack(linkId, '');
				}
				if (thisDrop.onchangeFunction != null)
					eval(thisDrop.onchangeFunction);

				if (thisText.onchange != null)
					thisText.fireEvent("onChange");
			}
		}


		if(!found  && p_invalidLov == 'False')
		{
			for (i=0; i<thisDrop.options.length; i++)
			{
				if(thisText.value.length <= thisDrop.options[i].innerText.length)
				{//Only check if the entered value is less than actual value
					var tmp  = thisDrop.options[i].innerText.substr(0, thisText.value.length);

					if( tmp.toLowerCase() ==thisText.value.toLowerCase())
					{
					    found= true;
						thisText.value = thisDrop.options[i].value;
						thisDrop.value = thisDrop.options[i].value;	
						var m_startPos = m_value.length;
						var m_endPos   = thisText.value.length;		
							
						
						if(thisDrop.onchangeFunction !=null)
					        eval(thisDrop.onchangeFunction);
    					
				        if(thisText.onchange !=null)
					        thisText.fireEvent("onChange");
					    	
					    ttlov_selectInputText(thisText, m_startPos, m_endPos);
					    
					    if(p_autoPostBack=='True')
						{
							var linkId =p_droObject.replace(/_/g,"\$");
							linkId =linkId.replace("$ct","_ct");
							__doPostBack(linkId,'');
						}
						break;
					}
				}
			}
			
			if(!found)
			{
			    //
			    //still not found then clear the value
			    thisText.value ="";
			    thisDrop.value = thisDrop.options[0].value
			    if(thisDrop.onchangeFunction !=null)
					  eval(thisDrop.onchangeFunction);
    					
				if(thisText.onchange !=null)
					  thisText.fireEvent("onChange");
			}
		}
	}
	catch(ex){};
}

function ttlov_selectInputText(p_inputObj, p_startPos, p_endPos)
{
	if (p_inputObj.setSelectionRange)
	{
		p_inputObj.focus();
		p_inputObj.setSelectionRange(p_startPos, p_endPos);
	}
	else
	{
		if (p_inputObj.createTextRange)
		{
			var m_range = p_inputObj.createTextRange();
			m_range.collapse(true);
			m_range.moveEnd('character', p_endPos);
			m_range.moveStart('character', p_startPos);
			m_range.select();
		}
	}	
}

function ttlov_dropKeyPress(lovId,dropId)
{
	if(event.keyCode==KEY_TAB)
	{		
		var thisText = document.getElementById(lovId);
		thisText.focus();
	}
}

