var calTimer = null;
var cmbTimer = null;
var comboTimer = null;
var suggestTimer = null;
var suggestTimer2 = null;
var suggestLastKey = null;
var comboLastKey = null;
var pageLoaded = false;

function ComboBox_Hide(strFieldId)
{
  // hides the ComboBox ListBox
  
  var objPanel = Core_ID('document', strFieldId + '_PNL');
  var objTextBox = Core_ID('document', strFieldId);
  var objListBox = Core_ID('document', strFieldId + '_DDL');
  objPanel.style.visibility = 'hidden';
  if (objTextBox.obj.value.length == 0)
  {
	  objListBox.obj.options[0].selected = true;
  }
  var i;
	for (i=0; i<objListBox.obj.options.length; i++)
	{
		if (objListBox.obj.options[i].selected)
		{
		  if (objTextBox.obj.value != objListBox.obj.options[i].text)
		  {
		    //objTextBox.obj.value = '';
		    objListBox.obj.options[0].selected = true;
		  }
		}
	}
}

function ComboBox_OnBlur(objTextBox)
{
  // will hide the ComboBox ListBox if necessary
  
  var strID = objTextBox.id.replace('_DDL', '');
  var objPanel = Core_ID('document', strID + '_PNL');
  if (objPanel.style.visibility == 'visible')
  {
    comboTimer = setTimeout("ComboBox_Hide('" + strID + "')",200);
  }
}

function ComboBox_OnChange(objListBox)
{
  // assign the value fo the TextBox to that of the value selected in the ListBox
  
	var objTextBox = Core_ID('document', objListBox.id.replace('_DDL',''));
	var objPanel = Core_ID('document', objListBox.id.replace('_DDL', '_PNL'));
	var i;
	for (i=0; i<objListBox.options.length; i++)
	{
		if (objListBox.options[i].selected)
		{
			objTextBox.obj.value = objListBox.options[i].text;
			objTextBox.obj.focus();
		}
	}
	ComboBox_Hide(objTextBox.obj.id);
}

function ComboBox_OnClick(strImg, intOffsetX, intOffsetY)
{
  // toggles the visibility of the ListBox
  
  var objTextBox = Core_ID('document', strImg.replace('_IMG', ''));
  var objPanel = Core_ID('document', strImg.replace('_IMG', '_PNL'));
  if (objPanel.style.visibility == 'visible')
  {
    ComboBox_Hide(objTextBox.obj.id);
  }
  else
  {
  	ComboBox_Show(objTextBox.obj.id, intOffsetX, intOffsetY);
  }
}

function ComboBox_OnFocus(objField)
{
  // if focus is placed upon either TextBox or ListBox, ListBox should remain visible
  clearTimeout(comboTimer);
}

function ComboBox_ListBox_OnFocus(objField)
{
  // if focus is placed upon either TextBox or ListBox, ListBox should remain visible
  clearTimeout(comboTimer);
}

function ComboBox_OnKeyDown(objTextBox)
{
  // a tab off of the TextBox should not go to the ListBox
  
	if (event.keyCode == 9)
	{
	  ComboBox_Hide(objTextBox.id)
	}
}

function ComboBox_OnKeyUp(objTextBox, intOffsetX, intOffsetY)
{
  // select the appropriate value in the ListBox based upon the entered text
  
	var objListBox = Core_ID('document', objTextBox.id + '_DDL');
	var objPanel = Core_ID('document', objTextBox.id + '_PNL');
	var key = "~!@#$";
	
  var defaultValue = objTextBox.value;
	var valLen = defaultValue.length;
	var i;
	
	if (event.keyCode < 32 && event.keyCode != 8)
	{
	  comboLastKey = event.keyCode;
		return true;
	}
	
	if (objTextBox.value == '')
	{
	  objListBox.obj.selectedIndex = -1;
	  comboLastKey = event.keyCode;
		return true;
	}
	
  var objRange = document.selection.createRange().duplicate();
	objRange.text = key;	
	var cursorPosition = objTextBox.value.indexOf(key);
	
	// a double backspace means that they really want to backspace
	
	if (event.keyCode == 8 && event.keyCode == comboLastKey && cursorPosition > 0)
	{
	  cursorPosition--;
	}
	var valToCursor = objTextBox.value.substring(0, cursorPosition);
	comboLastKey = event.keyCode;

  if (valToCursor.length == 0)
  {
    objTextBox.value = '';
	  objListBox.obj.selectedIndex = -1;
    return true;
  }
	
	ComboBox_Show(objTextBox.id, intOffsetX, intOffsetY);
	
	for(i=0; i<objListBox.obj.options.length; i++)
	{
		strTest = objListBox.obj.options[i].text;

		if (strTest.toUpperCase().indexOf(valToCursor.toUpperCase()) == 0)
		{
			objTextBox.value = strTest;
			objListBox.obj.options[i].selected = true;
	    // move the caret back
		  objRange.collapse();
		  objRange.moveStart('character', cursorPosition);
      objRange.moveEnd('character', objTextBox.value.length);
		  objRange.select();
		  return false;
		}
	}
	objTextBox.value = objTextBox.value.replace(key, '');
	objTextBox.value = objTextBox.value.replace(objTextBox.value.substring(cursorPosition, valLen), '');
	
}

function ComboBox_OnLoad(strImg)
{
  // reposition the Image and the ListBox to be around the TextBox

  var objImage = Core_ID('document', strImg);
  var objTextBox = Core_ID('document', strImg.replace('_IMG', ''));

  objImage.style.height = objTextBox.obj.offsetHeight;
  objImage.style.width = objTextBox.obj.offsetHeight;

}

function ComboBox_Show(strFieldId, intOffsetX, intOffsetY)
{
  // shows the ComboBox ListBox in the proper position
  
  var objTextBox = Core_ID('document', strFieldId);
  var objPanel = Core_ID('document', strFieldId + '_PNL');
    
  objPanel.style.top = objTextBox.y + objTextBox.obj.offsetHeight - intOffsetY;
  objPanel.style.left = objTextBox.x - intOffsetX;
  objPanel.style.visibility = 'visible';
}

function Core_Browser()
{
  var rObj = new Object();
  rObj.isDOM = (document.getElementById) ? true : false;
  rObj.isNS4 = (document.layers) ? true : false;
  rObj.isIE  = (document.all) ? true : false;
  rObj.isIE4 = rObj.isIE && !rObj.isDOM;
  rObj.isMac = (navigator.appVersion.indexOf("Mac") != -1);
  rObj.isIE4Mac = rObj.isIE4 && rObj.isMac;
  rObj.isOK  = (rObj.isDOM || rObj.isNS4 || (rObj.isIE4 && !rObj.isMac));
  return rObj;
}

function Core_ChangeStyleRule (strSelector, strRuleName, strNewValue)
{
  for (var i=0; i < document.styleSheets.length; i++) 
  {
    if (document.styleSheets[i].rules != undefined)
    {
      for (var j=0; j < document.styleSheets[i].rules.length; j++)
      {
        if (document.styleSheets[i].rules[j].selectorText.toLowerCase() == strSelector) 
        {
          document.styleSheets[i].rules[j].style[strRuleName] = strNewValue;
        }
      } 
    } 
    else if (document.styleSheets[i].cssRules != undefined) 
    {
      for (var j=0; j < document.styleSheets[i].cssRules.length; j++)
      {
        if (document.styleSheets[i].cssRules[j].selectorText.toLowerCase() == strSelector)
        {
          document.styleSheets[i].cssRules[j].style[strRuleName] = strNewValue;
        }
      }
    }
  }
}

function Core_ID(frame, object) {

  var rObj = new Object();
  var objBrowser = Core_Browser();
  if (objBrowser.isDOM) {
      rObj.obj = eval(frame + ".getElementById('" + object + "')");
      rObj.style = eval(frame + ".getElementById('" + object + "').style");
  } else if (objBrowser.isIE4) {
      rObj.obj = eval(frame + ".all['" + object + "']");
      rObj.style = eval(frame + ".all['" + object + "'].style");
  } else if (objBrowser.isNS4) {
      rObj.obj = eval(frame + ".layers['" + object + "']");
      rObj.style = eval(frame + ".layers['" + object + "']");
  } else {
      rObj.obj = new Object();
      rObj.style = new Object();
  }
  
  var tObj = rObj.obj;
	rObj.x = 0;
	rObj.y = 0;
	rObj.width = rObj.obj.offsetWidth;
	rObj.height = rObj.obj.offsetHeight;

	if (tObj.offsetParent)
	{
		while (tObj.offsetParent)
		{
			rObj.x += tObj.offsetLeft;
			rObj.y += tObj.offsetTop;
			tObj = tObj.offsetParent;
		}
	}
	else if (tObj.x)
	{
		rObj.x = tObj.x;
    rObj.y = tObj.y;
  }
  
  return rObj;

}

function Core_Screen() 
{
  var rObj = new Object();
  var objBrowser = Core_Browser();

  if (typeof(window.innerWidth) == 'number') 
  {
    rObj.width  = window.innerWidth;
    rObj.height = window.innerHeight;
    rObj.left   = window.pageXOffset;
    rObj.top    = window.pageYOffset;
  } 
  else 
  {
    if (document.documentElement && document.documentElement.clientWidth) 
    {
      rObj.left   = document.documentElement.scrollLeft;
      rObj.top    = document.documentElement.scrollTop;
      rObj.width  = document.documentElement.clientWidth;
      rObj.height = document.documentElement.clientHeight;
    } 
    else 
    {
      if (document.body && document.body.clientWidth)
      {
        rObj.left   = document.body.scrollLeft;
        rObj.top    = document.body.scrollTop;
        rObj.width  = document.body.clientWidth;
        rObj.height = document.body.clientHeight;
      }
      else
      {
        rObj.left   = 0;
        rObj.top    = 0;
        rObj.width  = 600;
        rObj.height = 400;
      }
    }
  }

  rObj.right  = rObj.left + rObj.width;
  rObj.bottom = rObj.top  + rObj.height;
  return rObj;
}

function DateBox_OnFocus(objField)
{
  clearTimeout(calTimer);
  calendarControl.show(objField);
}

function DateBox_OnKeyPress(objField) 
{

  calendarControl.hide(); // hide popup calendar when manually entering date

  TextBox_OnKeyPress_WipeSelection(objField);

	var strTemp = objField.value;	  //Value of the input field
	var arrDate;                    //Array Holding Month, Day, and Year
	var intSectionCount;            //Number of Sections in the Date

	arrDate = strTemp.split('/');
	intSectionCount = arrDate.length;
	
	//handle manual entry of a '/'
	if ((event.keyCode == 47) && intSectionCount < 3)
	{
		if (strTemp.charAt(strTemp.length-1) == '/') 
			event.returnValue = false;
	}

	//handle numeric keys
	else if (event.keyCode >= 48 && event.keyCode <= 57) 
	{
		var intMonth;
		var intDay;
		var chrCurrentKey;
		
		chrCurrentKey = event.keyCode - 48
	
		var s;
		s = arrDate[arrDate.length - 1].toString();
			
		if (arrDate.length == 1)	//Month Field
		{
			intMonth = s + chrCurrentKey.toString();
			if (intMonth > 12)
				strTemp = strTemp + '/';
			else if (s.length == 1){
				strTemp = strTemp + chrCurrentKey + '/';
				event.returnValue = false;
			}
		}
		else if (arrDate.length == 2)	//Day Field
		{
			intDay = s + chrCurrentKey.toString();
			if (intDay > 31)
				strTemp = strTemp + '/';
			else if (s.length == 1)
			{
				strTemp = strTemp + chrCurrentKey + '/';
				event.returnValue = false;
			}
		}
		else if (arrDate.length == 3 && arrDate[arrDate.length-1].length < 4)	//Year Field
			event.returnValue = true;
		else
			event.returnValue = false;
	}
	else
		event.returnValue = false;
		
	objField.focus;
	objField.value = strTemp;				
	return;
}

function DateBox_OnBlur()
{
	
  // get input parameters
	var objField   = arguments[0];
  var intLimit   = 0;
  if (arguments.length > 1)
    intLimit  = arguments[1];

  calTimer = setTimeout("calendarControl.hide()",200);

  // skip routine if nothing entered
	if (objField.value == "")
	  return true;

	var blnError = false;
	var arrDate = objField.value.split('/');

	// must be three components to the date
	if (arrDate.length != 3)		
	  return TextBox_OnBlur_Error(objField, 'Please enter a valid date.');

  // make sure a four digit year is being used
  
  switch (arrDate[2].length)
	{
    case 1:
      arrDate[2] = '200' + arrDate[2];
      break;
    case 2:
		  if (arrDate[2] > 50)
			  arrDate[2] = '19' + arrDate[2];
		  else	
			  arrDate[2] = '20' + arrDate[2];
			break;
		case 3:
      arrDate[2] = '2' + arrDate[2];
      break;
		case 4:
      break;
		default:
		  blnError = true;
		  break; 
	}

  // zero pad month and day
  
	if (arrDate[0].length < 2)
		arrDate[0] = '0' + arrDate[0];

	if (arrDate[1].length < 2)
		arrDate[1] = '0' + arrDate[1];

	// month must be in proper range
	if (arrDate[0] > 12 || arrDate[0] < 1)			
		blnError = true;			

	// day must be in proper range
	if (arrDate[1] > 31 || arrDate[1] < 1)
	  blnError = true;
	  
	if( (arrDate[0]=="04" || arrDate[0]=="06" || arrDate[0]=="09" || arrDate[0]=="11") && arrDate[1] > "30")
		blnError = true;
	else if (arrDate[0]=="02") 
	{
		if (arrDate[1] > 29)
			blnError = true;
		
		if (arrDate[1] == 29 && (arrDate[2] % 4) != 0 )
			blnError = true;
	}

  // bail if anything wrong
	if (blnError == true)
		return TextBox_OnBlur_Error(objField, 'Please enter a valid date.');

  // rebuild new date string
  strDate = arrDate[0] + '/' + arrDate[1] + '/' + arrDate[2];
  
  // check for future or past date
	if (intLimit != 0)
	{
	  var dateCurrent = new Date();
	  var dateTest = new Date(strDate);
	  if (isNaN(dateTest.valueOf())) 
		  return TextBox_OnBlur_Error(objField, 'Please enter a valid date.');

	  // date must be one that is in the past 
	  if (intLimit < 0)
	  {
	    if (dateCurrent.getTime() < dateTest.getTime())
		    return TextBox_OnBlur_Error(objField, 'Please enter a date in the past.');
		}
	  // date must be one that is in the future 
	  if (intLimit > 0)
	  {
	    if (dateCurrent.getTime() > dateTest.getTime())
		    return TextBox_OnBlur_Error(objField, 'Please enter a date in the future.');
		}
  }
  
  objField.value = strDate;
	return true;
	
}

function DropDownList_SelectItem(objField, strValue, blnValue)
{
  for (var i=0; i<objField.options.length; i++)
  {
    if (blnValue)
    {
      if (strValue == objField.options[i].value)
      {
        objField.options[i].selected = true;
      }
    }
    else
    {
      if (strValue == objField.options[i].text)
      {
        objField.options[i].selected = true;
      }
    }
  }
}

function SpecialNumberBox_OnKeyPress(objField, strFormat) 
{

  TextBox_OnKeyPress_WipeSelection(objField);
  
  // remove any non numeric char from string
  var re = /\D/g;
	var strJustNumbers = objField.value.replace(re, '');

	// remove any non (implied) numeric char from the format string
	re = /[^#]/g;
	var strFormatNumbers = strFormat.replace(re, '');

	// obtain a list of non numeric chars that are allowed as specified in the format string
	re = /#/g;
	var strSpecialChars = strFormat.replace(re, '');

  // don't allow any more chars to be entered if the maximum number of numbers has been already entered
	if (strJustNumbers.length >= strFormatNumbers.length)
	{
	  event.returnValue = false;
	  return true;
	}

  // allow any numeric char
	if (event.keyCode >= 48 && event.keyCode <= 57)
	{
	  return true;
	}

  // see if one of special chars has been entered
  for (var i=0; i < strSpecialChars.length; i++) 
  {
    if (event.keyCode == strSpecialChars.charCodeAt(i)) 
    {
      return true;
    }
  }
  
  // everything else must be bad
 	event.returnValue = false;
	return true;
	
}

function SpecialNumberBox_OnBlur(objField, strFormat, strPad)
{

  // don't do anything if field is empty
	if (objField.value.length == 0)
		return true;

  // remove any non numeric chars from string
  var re = /\D/g;
	var strJustNumbers = objField.value.replace(re, '');

	// remove any non (implied) numeric char from the format string
	re = /[^#]/g;
	var strFormatNumbers = strFormat.replace(re, '');

  if (strJustNumbers.length < strFormatNumbers.length) {
    if (strPad.length == 0)
    {
  	  return TextBox_OnBlur_Error(objField, 'This field does not contain the appropriate number of digits.');
  	}
  	else
  	{
  	  while (strJustNumbers.length < strFormatNumbers.length)
  	  {
  	    strJustNumbers += strPad;
  	  }
  	}
  }

  // rebuild string by putting numbers into format
  var j = 0;
  var strFormatted = '_';  // put this temp char into string so concatenation is forced
  for (var i=0; i < strJustNumbers.length; i++)
  {
    while (strFormat.substring(j, j+1) != '#')
    {
      strFormatted += strFormat.substring(j, j+1);
      j++;
    }
    strFormatted += strJustNumbers.substring(i, i+1);
    j++;
  }
    
  // reassign formatted value to field
  objField.value = strFormatted.substring(1);
	return true;
	
}

function ImageButton_OnMouseOut(objImage, blnSelected)
{

  if (blnSelected) return;
  
  if (!objImage.disabled)
  {
    if (ImageButtonNormal.length > 0)
    {
      objImage.src = objImage.src.replace(ImageButtonHighlighted, ImageButtonNormal);
    }
    else
    {
      objImage.src = objImage.src.replace(ImageButtonHighlighted, '');
    }

    objImage.style.cursor = 'default'; 
  }
}

function ImageButton_OnMouseOver(objImage, blnSelected)
{

  if (blnSelected) return;

  if (!objImage.disabled)
  {
    if (ImageButtonNormal.length > 0)
    {
      objImage.src = objImage.src.replace(ImageButtonNormal, ImageButtonHighlighted);
    }
    else
    {
      objImage.src = objImage.src.replace(ImageButtonExtension, ImageButtonHighlighted + ImageButtonExtension);
    }
    objImage.style.cursor = 'hand'; 
  }
}

function ImageLink_OnMouseOut(strImage)
{
  var objImage = Core_ID('document', strImage);
  if (ImageButtonNormal.length > 0)
  {
    objImage.obj.src = objImage.obj.src.replace(ImageButtonHighlighted, ImageButtonNormal);
  }
  else
  {
    objImage.obj.src = objImage.obj.src.replace(ImageButtonHighlighted, '');
  }
}

function ImageLink_OnMouseOver(strImage)
{
  var objImage = Core_ID('document', strImage);
  if (ImageButtonNormal.length > 0)
  {
    objImage.obj.src = objImage.obj.src.replace(ImageButtonNormal, ImageButtonHighlighted);
  }
  else
  {
    objImage.obj.src = objImage.obj.src.replace(ImageButtonExtension, ImageButtonHighlighted + ImageButtonExtension);
  }
}

function ListBox_ToggleAll(objField, selectedValue)
{
  for (var i=0; i<objField.options.length; i++)
  {
    objField.options[i].selected = selectedValue;
  }
}

function NumberBox_OnKeyPress(objField, intDecimals, blnPositives, blnGroup) 
{

  TextBox_OnKeyPress_WipeSelection(objField);

  var strTest = objField.value;
  var intDecPos = strTest.indexOf('.');
  
  // if it is a numeric character and 
  // we have not exceeded the number of digits allowed after the decimal point
	if (event.keyCode >= 48 && event.keyCode <= 57)
	{
	  if (intDecPos > -1 && strTest.length > (intDecPos + intDecimals)) 
	  {
		  event.returnValue = false;
	  }
	  return true;
	}

	switch (event.keyCode)
	{
	  case 45:
	  
	    // if they are trying to enter a hyphen, it can only be the first character
	    if (blnPositives)
	    {
	      event.returnValue = false;
	    }
	    else
	    {
	      if (strTest.length > 0 || strTest.indexOf('-') > -1)
	      {
	        event.returnValue = false;
	      } 
	    }
      break;
      
	  case 46:
	  
	    // if they are trying to enter a decimal point, only allow one
	    if (intDecimals == 0) 
	    {
	      event.returnValue = false;
	    }
	    else
	    {
	      if (intDecPos > -1)
	      {
	        event.returnValue = false;
	      }
	    }
	    break;
	    
 	  default:
	    event.returnValue = false;
	    break;
	}
		 
	return true;
	
}

function NumberBox_OnBlur(objField, intDecimals, blnPositives, blnGroup)
{

  // skip routine if nothing entered
	if(objField.value == '')
		return true;

  // remove any unsavory characters
	var re = /[^\d\-\.]/g;
	if (blnPositives)
	{
	  re = /[^\d\.]/g;
	}
	
	var strTest = objField.value.replace(re, '');
  var intDecPos = strTest.indexOf('.');
	var strWholePart = strTest;
	var strDecimalPart = '0';
	var intCommaBlock = 3;

  // cut up number into pieces
  if (intDecimals > 0)
  {
    if (intDecPos > -1)
    {
		  strWholePart = strTest.substr(0, intDecPos);
		  strDecimalPart = strTest.substring(intDecPos+1, strTest.length);
		}
		if (strDecimalPart.length < intDecimals)
		{
		  var tmpDec = strDecimalPart;
	    for (var i=0; i<intDecimals-tmpDec.length; i++)
	    {
	      strDecimalPart += '0';
	    }
	  }
	  if (strDecimalPart.length > intDecimals)
	  {
	    strDecimalPart = strDecimalPart.substring(0, intDecimals)
	  }
	}
	
	if (strWholePart.substring(0,1) == '-')
	{
	  intCommaBlock = 4;
	}
		
	// add in commas if number to left of decimal should be grouped in threes
 	strTest = '';
  if (blnGroup)
  {
	  while (strWholePart.length > intCommaBlock) 
	  {
		  strTest = ',' + strWholePart.substr(strWholePart.length - 3,3) + strTest;
		  strWholePart = strWholePart.substr(0, strWholePart.length - 3);
	  }
	}
	strTest = strWholePart + strTest;			
	
	// add in the decimal portion
	if (intDecimals > 0) 
	{
	  strTest += '.' + strDecimalPart;
	}

	if (intDecimals == 0)
	{
	  strTest = parseInt(strTest);
	}
	
	objField.value = strTest;
	return true;
	
}

function Form_OnLoad()
{
  // place focus on the first field in the first form
  if (document.forms.length > 0)
  {
    var objForm = document.forms[0];
    for (i=0;i<objForm.length;i++)
    {
      if (objForm.elements[i].type=="text" ||
          objForm.elements[i].type=="textarea" ||
          objForm.elements[i].type.toString().charAt(0)=="s")
      {
        if (!objForm.elements[i].disabled)
        {
          objForm.elements[i].focus();
          break;
        }
      }
    }
  }
}

function StateBox_OnKeyPress(objField) 
{

  TextBox_OnKeyPress_WipeSelection(objField);

  // allow any a-z char
	if (event.keyCode >= 97 && event.keyCode <= 122)
	{
	  return true;
	}

  // allow any A-Z char
	if (event.keyCode >= 65 && event.keyCode <= 90)
	{
	  return true;
	}

  event.returnValue = false;
	return true;
	
}

function StateBox_OnBlur(objField) 
{
  objField.value = objField.value.toUpperCase();
	return true;
}

function SuggestBox_Hide(strFieldId)
{
  // hides the SuggestBox ListBox
  
  var objPanel = Core_ID('document', strFieldId + '_PNL');
  var objTextBox = Core_ID('document', strFieldId);
  var objListBox = Core_ID('document', strFieldId + '_DDL');
  objPanel.style.visibility = 'hidden';
  if (objTextBox.obj.value.length == 0)
  {
    SuggestBox_Reset(strFieldId);
  }
}

function SuggestBox_OnBlur(objTextBox)
{
  // will hide the SuggestBox ListBox if necessary
  
  var strID = objTextBox.id.replace('_DDL', '');
  var objPanel = Core_ID('document', strID + '_PNL');
  if (objPanel.style.visibility == 'visible')
  {
    suggestTimer = setTimeout("SuggestBox_Hide('" + strID + "')",200);
  }
}

function SuggestBox_OnChange(objListBox)
{
  // assign the value of the TextBox to that of the value selected in the ListBox
  
	var objTextBox = Core_ID('document', objListBox.id.replace('_DDL',''));
	var objHidden = Core_ID('document', objListBox.id.replace('_DDL','_HDN'));
	var i;
	for (i=0; i<objListBox.options.length; i++)
	{
		if (objListBox.options[i].selected)
		{
		  objHidden.obj.value = objListBox.options[i].value;
			objTextBox.obj.value = objListBox.options[i].text;
			objTextBox.obj.focus();
		}
	}
	SuggestBox_Hide(objTextBox.obj.id);
}

function SuggestBox_OnClick(objListBox)
{
  SuggestBox_OnChange(objListBox);
}

function SuggestBox_OnFocus(objField)
{
  // if focus is placed upon TextBox, ListBox should remain visible
  clearTimeout(suggestTimer);
}

function SuggestBox_ListBox_OnFocus(objField)
{
  // if focus is placed upon ListBox, ListBox should remain visible
  clearTimeout(suggestTimer);
}

function SuggestBox_OnKeyDown(objTextBox)
{
  // a tab off of the TextBox should not go to the ListBox
  
	if (event.keyCode == 9)
	{
	  SuggestBox_Hide(objTextBox.id)
	}
}

function SuggestBox_OnKeyUp_Contains(objTextBox, intOffsetX, intOffsetY, strLookupKey, strFilterKey, strValueKey, strTextKey)
{

  clearTimeout(suggestTimer2);
  suggestTimer2 = setTimeout("SuggestBox_OnKeyUp_Contains_Lookup(document.MainForm." + objTextBox.id + ", " + intOffsetX + ", " + intOffsetY + ", '" + strLookupKey + "', '" + strFilterKey + "', '" + strValueKey + "', '" + strTextKey + "')",500);

}

function SuggestBox_OnKeyUp_Contains_Lookup(objTextBox, intOffsetX, intOffsetY, strLookupKey, strFilterKey, strValueKey, strTextKey)
{
  // select the appropriate value in the ListBox based upon the entered text
  
	var objListBox = Core_ID('document', objTextBox.id + '_DDL');
	var objHidden = Core_ID('document', objTextBox.id + '_HDN');
	
  var defaultValue = objTextBox.value;
	var i;
		
	if (objTextBox.value == '')
	{
	  SuggestBox_Reset(objTextBox.id);
		return true;
	}
	
  
	// fetch items beginning with selected value
	
	var valToFilter = '';
	var objFilter = Core_ID('document', strFilterKey);
	if (objFilter.obj.type=="text" ||
      objFilter.obj.type=="hidden")
  {
    valToFilter = objFilter.obj.value;
  }
  else if(objFilter.obj.type.toString().charAt(0)=="s")
  {
    if (objFilter.obj.selectedIndex >= 0)
    {
      valToFilter = objFilter.obj.options[objFilter.obj.selectedIndex].value;
    }
  }
  
  if (defaultValue.length > 2)
  {
    var ajaxUrl = "AjaxFilterList.aspx?lookup=" + escape(strLookupKey) + "&by=" + escape(defaultValue) + "&values=" + escape(valToFilter);
    var xdoc = new ActiveXObject("Microsoft.XMLDOM");
    xdoc.async = false;
    xdoc.load(ajaxUrl);
    SuggestBox_Reset(objTextBox.id);
    var rows = xdoc.getElementsByTagName("Table");
    for(var i = 0; i<rows.length; ++i)
    {
      var optRow = new Option();
      optRow.value = rows[i].getElementsByTagName(strValueKey)[0].firstChild.nodeValue;
      optRow.text = rows[i].getElementsByTagName(strTextKey)[0].firstChild.nodeValue;
      objListBox.obj.options[objListBox.obj.options.length] = optRow;
    }
	  SuggestBox_Show(objTextBox.id, intOffsetX, intOffsetY);
    if (objListBox.obj.options.length > 0)
    {
      //objListBox.obj.options[0].selected = true;
	    //objTextBox.value = objListBox.obj.options[0].text;
	    //objHidden.obj.value = objListBox.obj.options[0].value;
  	  
		  return false;
    }
  }
}

function SuggestBox_OnKeyUp_BeginsWith(objTextBox, intOffsetX, intOffsetY, strLookupKey, strFilterKey, strValueKey, strTextKey)
{

  // select the appropriate value in the ListBox based upon the entered text
  
	var objListBox = Core_ID('document', objTextBox.id + '_DDL');
	var objHidden = Core_ID('document', objTextBox.id + '_HDN');
	var key = "~!@#$";
	
  var defaultValue = objTextBox.value;
	var valLen = defaultValue.length;
	var i;
		
	if (event.keyCode < 32 && event.keyCode != 8)
	{
		suggestLastKey = event.keyCode;
		return true;
  }
	if (objTextBox.value == '')
	{
	  SuggestBox_Reset(objTextBox.id);
	  suggestLastKey = event.keyCode;
		return true;
	}
	
	var objRange = document.selection.createRange().duplicate();
	objRange.text = key;	
	var cursorPosition = objTextBox.value.indexOf(key);
	
	// a double backspace means that they really want to backspace
	
	if (event.keyCode == 8 && event.keyCode == suggestLastKey && cursorPosition > 0)
	{
	  cursorPosition--;
	}
	var valToCursor = objTextBox.value.substring(0, cursorPosition);
	suggestLastKey = event.keyCode;

  if (valToCursor.length == 0)
  {
    objTextBox.value = '';
  	SuggestBox_Reset(objTextBox.id);
    return true;
  }
  
	// fetch items beginning with selected value
	
	var valToFilter = '';
	var objFilter = Core_ID('document', strFilterKey);
	if (objFilter.obj.type=="text" ||
      objFilter.obj.type=="hidden")
  {
    valToFilter = objFilter.obj.value;
  }
  else if(objFilter.obj.type.toString().charAt(0)=="s")
  {
    if (objFilter.obj.selectedIndex >= 0)
    {
      valToFilter = objFilter.obj.options[objFilter.obj.selectedIndex].value;
    }
  }
  
  var ajaxUrl = "AjaxFilterList.aspx?lookup=" + escape(strLookupKey) + "&by=" + escape(valToCursor) + "&values=" + escape(valToFilter);
  var xdoc = new ActiveXObject("Microsoft.XMLDOM");
  xdoc.async = false;
  xdoc.load(ajaxUrl);
  SuggestBox_Reset(objTextBox.id);
  var rows = xdoc.getElementsByTagName("Table");
  for(var i = 0; i<rows.length; ++i)
  {
    var optRow = new Option();
    optRow.value = rows[i].getElementsByTagName(strValueKey)[0].firstChild.nodeValue;
    optRow.text = rows[i].getElementsByTagName(strTextKey)[0].firstChild.nodeValue;
    objListBox.obj.options[objListBox.obj.options.length] = optRow;
  }
	SuggestBox_Show(objTextBox.id, intOffsetX, intOffsetY);
  if (objListBox.obj.options.length > 0)
  {
    objListBox.obj.options[0].selected = true;
	  objTextBox.value = objListBox.obj.options[0].text;
	  objHidden.obj.value = objListBox.obj.options[0].value;
	  
	  // move the caret back
		objRange.collapse();
		objRange.moveStart('character', cursorPosition);
    objRange.moveEnd('character', objTextBox.value.length);
		objRange.select();
		return false;
  }
  
	objTextBox.value = objTextBox.value.replace(key, '');
	objTextBox.value = objTextBox.value.replace(objTextBox.value.substring(cursorPosition, valLen), '');
	
}

function SuggestBox_Reset(strFieldId)
{
  var objListBox = Core_ID('document', strFieldId + '_DDL');
  var objHidden = Core_ID('document', strFieldId + '_HDN');
  objListBox.obj.options.length = 0;
  objListBox.obj.selectedIndex = -1;
  objHidden.obj.value = '';
}

function SuggestBox_Show(strFieldId, intOffsetX, intOffsetY)
{
  // shows the SuggestBox ListBox in the proper position
  
  var objTextBox = Core_ID('document', strFieldId);
  var objPanel = Core_ID('document', strFieldId + '_PNL');
    
  objPanel.style.top = objTextBox.y + objTextBox.obj.offsetHeight - intOffsetY;
  objPanel.style.left = objTextBox.x - intOffsetX;
  objPanel.style.visibility = 'visible';
}

function TextBox_OnFocus(objField)
{
  // select all the text within the TextBox
  objField.select();
}

function TextBox_OnKeyPress_WipeSelection(objField)
{
  /*
  Determines if entire field is selected and if so, will allow it to be wiped clean
  */
  
  var strTemp = objField.value;
  var objRange = document.selection.createRange();	
  
	if (objRange.text.length == strTemp.length) 
	{
		strTemp = strTemp.replace(objRange.text, "");					
    objField.value = strTemp;
  }
}

function TextBox_OnBlur_Error(objField, strMessage)
{
  /*
  Launches a popup dialog box if entered data is inaccurate for any reason
  */
  
	alert(strMessage);
	objField.focus();
	objField.select();
	return true;
}
