
//全局变量
var _delay = 50;
var _quest_input=null;
var _showContainer=null;
var _showFrame=null;
var _showTable=null;
var _currLine=null;
var RootPath = "/";
	
function getData()
{	
	//debugger;			
	if(_quest_input == null)
		return false;
	if( _quest_input.value != "" && ( _quest_input.questMode == null || _quest_input.questMode == "S" ))
	{
		_showFrame.location.href= RootPath + 'BackDataService.aspx?Type=' + _quest_input.questType  + '&Cat=' + _quest_input.questCat + '&Mode=S' + '&SearchCode=' + _quest_input.value;
	}
	else if ( _quest_input.value != "" && _quest_input.questMode == "M" )
	{
		var sInput = _quest_input.value.split(',');
		var SearchCode = sInput[sInput.length - 1];
		var checkedOptions = _quest_input.value.substring( 0 , _quest_input.value.length - SearchCode.length - 1 );
		_showFrame.location.href= RootPath + 'BackDataService.aspx?Type=' + _quest_input.questType  + '&Cat=' + _quest_input.questCat + '&Mode=M' + '&SearchCode=' + SearchCode + '&checkedOptions=' + checkedOptions;
	}
	else
	{
		_showContainer.style.visibility = "hidden";
	}
}

function keyDown(obj,evt)
{
	//debugger;
	_quest_input = obj;
	//============Edited by Moon 2007-9-3========
	/*
	if(_quest_input.questType=="Base")
	{
		_showFrame = document.frames[_quest_input.questCat + "_show"];
		_showContainer = document.getElementById(_quest_input.questCat + "_show");
		_showTable = _showFrame.document.getElementById(_quest_input.questCat + "_table");
	}
	else
	{
		_showFrame = document.frames[_quest_input.questType + "_show"];
		_showContainer = document.getElementById(_quest_input.questType + "_show");
		_showTable = _showFrame.document.getElementById(_quest_input.questType + "_table");
	}
	*/
	_showFrame = document.frames["help_show"];
	_showContainer = document.getElementById("help_show");
	_showTable = _showFrame.document.getElementById("help_table");
	//===========================================
	//alert(evt.keyCode);
	switch (evt.keyCode) {
		case 37://left
			_showFrame.prevPage();
			break;
		case 39://right
			_showFrame.nextPage();
			break;
		case 38: //up
			setTimeout(preLine,_delay);
			return false;
			break;
		case 40: //down
			setTimeout(nextLine,_delay);
			return false;
			break;
		case 32://space
			if( _quest_input.questMode == null || _quest_input.questMode == "S" )
			{
				blurObj();
			}
			else if( _quest_input.questMode == "M" )
			{
				if ( _currLine != null )
					_showFrame.checkLine(_currLine);
			}
			return false;
			break;
		case 9: //Tab	
		case 13: //Enter
			blurObj();
			return false;
			break;
		default:
			//_showContainer.style.visibility = "hidden";
			this.setLine(null);
			setTimeout(getData,_delay);
			//_showContainer.focus();
			//setTimeout(adjustPosition,_delay);
			break;
	}
}
function nextLine()
{
	if(_showTable == null || _showTable.rows.length <= 1)
		;
	else if (_currLine == null || _currLine.rowIndex == _showTable.rows.length - 1 || _currLine.rowIndex < 0) {
		this.setLine(_showTable.rows[1]);
	}
	else {
		this.setLine(_showTable.rows[_currLine.rowIndex + 1]);
	}
}
function preLine()
{
	if(_showTable == null || _showTable.rows.length <= 1)
		;
	else if (_currLine == null || _currLine.rowIndex == 1 || _currLine.rowIndex < 0) {
		this.setLine(_showTable.rows[_showTable.rows.length - 1]);
	}
	else {
		this.setLine(_showTable.rows[_currLine.rowIndex - 1]);
	}
}
function blurObj()
{
	if(_showContainer != null && !inFrame())
	{	
		if(_showFrame.mode != 'M' && _currLine != null)
			_quest_input.value = _currLine.cells[0].innerHTML;						
		disappear();
	}
	//_quest_input=null;
	//_showContainer=null;
}
function disappear()
{
	_showContainer.style.visibility = "hidden";
}
function inFrame()
{
	var mouseX = event.clientX;
	var mouseY = event.clientY;
	if(	   mouseX >= _showContainer.offsetLeft 
		&& mouseX <= _showContainer.offsetLeft + _showContainer.offsetWidth
		&& mouseY >= _showContainer.offsetTop
		&& mouseY <= _showContainer.offsetTop + _showContainer.offsetHeight)
	{
		return true;
	}
	else
		return false;
}
function colorLine(line) 
{
	line.className = "selected";
}
function discolorLine(line) 
{
	line.className = "";
}
function setLine(line) 
{
	if (line != null) 
	{
		if (_currLine != null) 
		{
			discolorLine(_currLine);
		}
		_currLine = line;
		this.colorLine(line);
		/*
		if( _quest_input.questMode == null || _quest_input.questMode == "S" )
		{
			_quest_input.value = line.cells[0].innerHTML;
		}
		else if ( _quest_input.questMode == "M" )
		{
			_quest_input.value = line.cells[1].innerHTML;
		}
		*/
	}
	else 
	{
		_currLine = null;
	}
}
//以下是调整位置的函数
function adjustPosition()
{
	_showContainer.style.visibility = "hidden";

	divObj = _showContainer;
	//debugger;

	//还原
	divObj.style.position = "absolute";
	divObj.style.top = getTop(_quest_input) + 20;
	divObj.style.left = getLeft(_quest_input);
	//控件的绝对位置
	var absolutePosition = getTop(divObj);
	
	//空间的高度
	var controlHeight = divObj.clientHeight;
	
	if ( absolutePosition + controlHeight > document.body.clientHeight )
	{
		
		//20 是div.style.margin-top的值，由于使用CSS，无法取到。
		//24 是toolbar的高度。
		var newTop = absolutePosition - controlHeight - 20;					
		divObj.style.top = newTop + "px";
	}
	if(_quest_input.value != "")
		_showContainer.style.visibility = "visible";

}

function getTop(e)
{ 
	var offset=e.offsetTop; 
	if(e.offsetParent!=null) offset+=getTop(e.offsetParent); 
	return offset; 
}

function getLeft(e)
{ 
	var offset=e.offsetLeft; 
	if(e.offsetParent!=null) offset+=getLeft(e.offsetParent); 
	return offset; 
}

function adjustSelect()   
{
	var   e   =   event.srcElement;   
	var   r   =   e.createTextRange();   
	r.moveStart('character',e.value.length);   
	r.collapse(true);   
	r.select();   
}
			/*
			function frameKeyDown()
			{
				switch (event.keyCode) {
					case 37://left
						_showFrame.prevPage();
						break;
					case 39://right
						_showFrame.nextPage();
						break;
					case 38: //up
						setTimeout(preLine,_delay);
						return false;
						break;
					case 40: //down
						setTimeout(nextLine,_delay);
						return false;
						break;
					case 32://space
						if( _quest_input.questMode == null || _quest_input.questMode == "S" )
						{
							blurObj();
						}
						else if( _quest_input.questMode == "M" )
						{
							if ( _currLine != null )
								_showFrame.checkLine(_currLine);
						}
						return false;
						break;
					case 9: //Tab	
					case 13: //Enter
						blurObj();
						return false;
						break;
				}
			}
			*/