var intTimerId;
var str2q;
var str3q;
function SWInit(strSelectField)
{
	var objSWL = eval('document.getElementById(\'' + strSelectField + '\')');
	var intIdx = parseInt(objSWL.length/4);
	str2q = objSWL.options[intIdx*2].text;
	str3q = objSWL.options[intIdx*3].text;

}
//--------------------------------------------------------------------------------------
// Function: SmartWordHelper
// Evaluate each key up in the SmartWord box
// strSortOrder = "DESC" or strSortOrder = "ASC"
//--------------------------------------------------------------------------------------		
function SmartWordHelper(strFormName, strSelectField, strSortOrder)
{
	var intIndex;
	var strOption;


//	if (typeof(window.event) !== 'undefined')
//	{
		//-----------
		// Up Arrow
		//-----------
		if (window.event.keyCode == 38)
	    {
			for (intIndex=0; intIndex < eval('document.'+strFormName+'.'+strSelectField+'.options.length'); intIndex++) {
				strOption = eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
				if (strOption == eval('document.'+strFormName+'.'+strSelectField+'.options[document.'+strFormName+'.'+strSelectField+'.selectedIndex].text'))
				{
					break;
				}
			}
			intIndex = Math.max(0,intIndex-1);
			eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].selected = true');	    
			eval('document.'+strFormName+'.txtSmartWordHelp.value = document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
			window.clearTimeout(intTimerId);
		}
		
		//-------------
		// Down Arrow
		//-------------    
		else if (window.event.keyCode == 40)
		{
			for (intIndex=0; intIndex < eval('document.'+strFormName+'.'+strSelectField+'.options.length'); intIndex++) {
				strOption = eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
				if (strOption == eval('document.'+strFormName+'.'+strSelectField+'.options[document.'+strFormName+'.'+strSelectField+'.selectedIndex].text'))
				{
					break;
				}
			}
			intIndex = Math.min(intIndex+1,eval('document.'+strFormName+'.'+strSelectField+'.options.length-1'));
			eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].selected = true');	
			eval('document.'+strFormName+'.txtSmartWordHelp.value = document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
			window.clearTimeout(intTimerId);
		}
		else
		{
			//---------------------------------------------------------
			// Set a delay, before moving the Smartword list
			// This allows the user to type in a number of characters
			// before the list is moved
			//---------------------------------------------------------    	
			intTimerId = eval('window.setTimeout("SmartWordMove(\''+strFormName+'\',\''+strSelectField+'\',\''+strSortOrder+'\')",500)'); 
			eval('document.'+strFormName+'.txtSmartWordHelp.onkeyup = new Function("window.clearTimeout(intTimerId);SmartWordHelper(\''+strFormName+'\',\''+strSelectField+'\',\''+strSortOrder+'\')")');
		}
//	}
}	
//--------------------------------------------------------------------------------------
// Function: SmartWordMove
// Select an item in the Smartword list that most closely matches the text entered on
// in the Smartword input box
// strSortOrder = "DESC" or strSortOrder = "ASC"
//--------------------------------------------------------------------------------------			
function SmartWordMove(strFormName, strSelectField, strSortOrder)
{
	var intLength = document.getElementById('txtSmartWordHelp').value.length;
	var intIndex;
	var strOption;
	var intLimit = 100;
	var intSelIndex;
	var objSWH = document.getElementById('txtSmartWordHelp');
	var strSW = objSWH.value.toUpperCase();
	var objSelField = eval('document.getElementById(\'' + strSelectField + '\')');
	
	if (intLength > 0)
	{
		//-----------------------------------------------------
		//	Find the 'Quarter' Smart Words and compare it to the 
		//	entered text.  
		//-----------------------------------------------------
		intSelIndex = parseInt(objSelField.length/2);
		strOption = document.getElementById(strSelectField).options[intSelIndex].text;
if (strSortOrder == "DESC"){

		if (strOption.substr(0,intLength).toUpperCase() > strSW.toUpperCase())
		{

				//	The smart word we are looking for is later in the list
//alert('The smart word we are looking for is later in the list');
				for(intIndex=intSelIndex+1; intIndex<objSelField.length; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else if (strOption.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
		{
				//	The smart word we are looking for is earlier in the list
				for(intIndex=0; intIndex<=intSelIndex-1; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else
		{
			//	We found the smart word.
			objSelField.options[intSelIndex].selected = true;
		}
}
else
{
		if (strOption.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
		{

				//	The smart word we are looking for is later in the list
				for(intIndex=intSelIndex+1; intIndex<objSelField.length; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() > strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else if (strOption.substr(0,intLength).toUpperCase() > strSW)
		{
				//	The smart word we are looking for is earlier in the list
				for(intIndex=0; intIndex<=intSelIndex-1; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() > strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else
		{
			//	We found the smart word.
			objSelField.options[intSelIndex].selected = true;
		}
}		
	}
	else
	{
		objSelField.options[0].selected = true;
	}
	
	//-----------------------------------------------------
	// Force the located SmartWord to the top of the list
	//-----------------------------------------------------
	//if (typeof(document.all) !== 'undefined')
	if (typeof(document.all) != 'undefined')
	{
		document.all.spnSmartWord.innerHTML = document.all.spnSmartWord.innerHTML;
	}	
	
	//-----------------------------
	// SmartWordMove has finished
	// Clear timer
	//-----------------------------
	window.clearTimeout(intTimerId);
}

function MozSmartWordHelper(event,strFormName, strSelectField, strSortOrder)
{
	var intIndex;
	var strOption;
	var objSWL = eval('document.getElementById(\'' + strSelectField + '\')');
	

//	if (typeof(window.event) !== 'undefined')
//	{
		//-----------
		// Up Arrow
		//-----------
		if (event.keyCode == 38 && objSWL.selectedIndex != 0)
	    {
			objSWL.options[objSWL.options.selectedIndex-1].selected = true;
			document.getElementById('txtSmartWordHelp').value = objSWL.options[objSWL.options.selectedIndex].text;
/*
			for (intIndex=0; intIndex < eval('document.'+strFormName+'.'+strSelectField+'.options.length'); intIndex++) {
				strOption = eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
				if (strOption == eval('document.'+strFormName+'.'+strSelectField+'.options[document.'+strFormName+'.'+strSelectField+'.selectedIndex].text'))
				{
					break;
				}
			}
			intIndex = Math.max(0,intIndex-1);
			eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].selected = true');	    
			eval('document.'+strFormName+'.txtSmartWordHelp.value = document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
			window.clearTimeout(intTimerId);
*/
		}
		
		//-------------
		// Down Arrow
		//-------------    
		else if (event.keyCode == 40 && objSWL.selectedIndex != objSWL.length-1)
		{
			objSWL.options[objSWL.options.selectedIndex+1].selected = true;
			document.getElementById('txtSmartWordHelp').value = objSWL.options[objSWL.options.selectedIndex].text;
/*
			for (intIndex=0; intIndex < eval('document.'+strFormName+'.'+strSelectField+'.options.length'); intIndex++) {
				strOption = eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
				if (strOption == eval('document.'+strFormName+'.'+strSelectField+'.options[document.'+strFormName+'.'+strSelectField+'.selectedIndex].text'))
				{
					break;
				}
			}
			intIndex = Math.min(intIndex+1,eval('document.'+strFormName+'.'+strSelectField+'.options.length-1'));
			eval('document.'+strFormName+'.'+strSelectField+'.options[intIndex].selected = true');	
			eval('document.'+strFormName+'.txtSmartWordHelp.value = document.'+strFormName+'.'+strSelectField+'.options[intIndex].text');
			window.clearTimeout(intTimerId);
*/
		}
		else if (event.keyCode ==13)
		{
			//	Do nothing at the moment
		}
		else
		{
			//---------------------------------------------------------
			// Set a delay, before moving the Smartword list
			// This allows the user to type in a number of characters
			// before the list is moved
			//---------------------------------------------------------    	
			MozSmartWordScan(strFormName,strSelectField,strSortOrder);
		}
//	}
}	



function MozSmartWordScan(strFormName, strSelectField, strSortOrder)
{

	var intLength = document.getElementById('txtSmartWordHelp').value.length;
	var intIndex;
	var strOption;
	var intLimit = 100;
	var intSelIndex;
	var objSWH = document.getElementById('txtSmartWordHelp');
	var strSW = objSWH.value.toUpperCase();
	var objSelField = eval('document.getElementById(\'' + strSelectField + '\')');
	
	if (intLength > 0)
	{
		//-----------------------------------------------------
		//	Find the 'Quarter' Smart Words and compare it to the 
		//	entered text.  
		//-----------------------------------------------------
		intSelIndex = parseInt(objSelField.length/2);
		strOption = document.getElementById(strSelectField).options[intSelIndex].text;
if (strSortOrder == "DESC"){

		if (strOption.substr(0,intLength).toUpperCase() > strSW.toUpperCase())
		{

				//	The smart word we are looking for is later in the list
//alert('The smart word we are looking for is later in the list');
				for(intIndex=intSelIndex+1; intIndex<objSelField.length; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else if (strOption.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
		{
				//	The smart word we are looking for is earlier in the list
				for(intIndex=0; intIndex<=intSelIndex-1; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else
		{
			//	We found the smart word.
			objSelField.options[intSelIndex].selected = true;
		}
}
else
{
		if (strOption.substr(0,intLength).toUpperCase() < strSW.toUpperCase())
		{

				//	The smart word we are looking for is later in the list
				for(intIndex=intSelIndex+1; intIndex<objSelField.length; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() > strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else if (strOption.substr(0,intLength).toUpperCase() > strSW)
		{
				//	The smart word we are looking for is earlier in the list
				for(intIndex=0; intIndex<=intSelIndex-1; intIndex++)
				{
					if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() == strSW.toUpperCase())
					{
						objSelField.options[intIndex].selected = true;
						break;
					}
					else if (objSelField.options[intIndex].text.substr(0,intLength).toUpperCase() > strSW.toUpperCase())
					{
						objSelField.options[intIndex-1].selected = true;
						break;
					}
				}
		}
		else
		{
			//	We found the smart word.
			objSelField.options[intSelIndex].selected = true;
		}
}
	}
	else
	{
		objSelField.options[0].selected = true;
	}
	window.clearTimeout(intTimerId);
}
