// _STKCODE_CodeList[i], _STKCODE_NameList[i]
// _STKCODE_CodeList[i]

Function.prototype.andThen = function( g )
{
	var f = this;
	return function()
	{
		f();
		g();
	}
}

function CallbackManager()
{
	this.callback = function(){};
	this.registerCallback = function( callbackFunction ){
		this.callback = ( this.callback ).andThen( callbackFunction );
	}
}

function KeyBoard( _STKCODE_PYList )
{//alert( "1" + this._STKCODE_codeCtrl );

	this.stkcode_textlist = new Array();
	this.stkcode_valuelist = new Array();
	this.stkcode_pinyinlist = _STKCODE_PYList ? _STKCODE_PYList : new Array();
	this.id = 0;
	this.dynamic = false;
	this.affectCtrl = null;

	this._STKCODE_codeCtrl = null;	// the HTML ELEMENT to receive the stock code
	this._STKCODE_nameCtrl = null;	// the HTML ELEMENT to receive the stock name
	this._STKCODE_num = 0;			// Number of Stock Code to display
	this._STKCODE_code = "";		// temp stock code
	this._STKCODE_orgCode = "";		// Original stock code
	this._STKCODE_selCodeList = null;		// <Select> element object
	this._STKCODE_divSelectCode = null;		// <div> element object
	this._STKCODE_callback_string = "";

	this.callbackManager = new CallbackManager();
	//this.display = null;

	var me = this;
	attachEvent( "onunload", function(){ me.release() } );
}

KeyBoard.prototype.test = function()
{
	alert( "KeyBoard.prototype.test" );
}

// 添加一条记录
KeyBoard.prototype.add = function( text, value )
{//alert( "2" + this._STKCODE_codeCtrl );
	if( typeof( text ) == "object" )
	{
		this.stkcode_textlist = text;
	}
	else
	{
		this.stkcode_textlist[ this.stkcode_textlist.length ] = text;
	}

	if( typeof( value ) == "object" )
	{
		this.stkcode_valuelist = value;
	}
	else
	{
		this.stkcode_valuelist[ this.stkcode_valuelist.length ] = value;
	}
}

KeyBoard.prototype.draw = function()
{//alert( "3" + this._STKCODE_codeCtrl );
	// 下拉列表的框架
	var stkcode_divSelectCode_id = "_STKCODE_divSelectCode" + this.id;
	var stkcode_selCodeList_id = "_STKCODE_selCodeList" + this.id;


	document.write('<div id="' + stkcode_divSelectCode_id + '" style="position:absolute;display:none;">\n');
  document.write('<table style="border:none 1px #000;" border="0" cellpadding="0" cellspacing="0"><tr><td>\n');
	document.write('<select id="' + stkcode_selCodeList_id + '" size="10" style="width:180px;background-color:#ffffee;"\n');
	document.write('>\n');
	document.write('</select>\n');
	document.write('</td></tr></table>\n');
	document.write('</div>\n');



	//
	this._STKCODE_selCodeList = document.getElementById(stkcode_selCodeList_id);
	this._STKCODE_divSelectCode = document.getElementById(stkcode_divSelectCode_id);

	var me = this;
	this._STKCODE_selCodeList.onclick = function() { me.onDblClick(); }
	this._STKCODE_selCodeList.ondblclick = function() { me.onDblClick(); }
	this._STKCODE_selCodeList.onchange = function() { me.onChange(); };
	this._STKCODE_selCodeList.onkeypress = function() { me.onKeyPress(); }


	this._STKCODE_num = 10;
	if (this.stkcode_valuelist.length < this._STKCODE_num)
	{
		this._STKCODE_num = this.stkcode_valuelist.length;
	}
	this._STKCODE_selCodeList.innerHTML = null;


	if ( this.affectCtrl != null )
	{
		//this.affectCtrl.onfocus = function() { me.showCodeList(); }
		this.affectCtrl.onkeydown = function() { me.showCodeList(); }
		this.affectCtrl.onkeyup = function() { me.filterCodeList(); }
	}

}

KeyBoard.prototype.onDblClick = function()
{//alert( "4 onDbleClick" );

	var me = this;
	document.detachEvent("onclick", function(){ me.hideCodeList() });

	this._STKCODE_divSelectCode.style.display = "none";
	this.setValue();
	this.callback();

	this.callbackManager.callback();
 	this.affectCtrl.blur();
	//document.frm1.stock_code.focus();
	//document.getElementById("fundcode").focus();
	this._STKCODE_codeCtrl.focus();
}

KeyBoard.prototype.hideCodeList = function()
{//alert( "5 hideCodeList" );

	var me = this;
	if (this._STKCODE_codeCtrl == null)
	{
		document.detachEvent( "onclick", function(){ me.hideCodeList() } );
		this._STKCODE_divSelectCode.style.display = "none";
		return;
	}
	var	obj;
	obj = event.srcElement;
	if ((obj != this._STKCODE_codeCtrl) && (obj != this._STKCODE_selCodeList))
	{
		document,detachEvent( "onclick", function(){ me.hideCodeList() } );
		this._STKCODE_divSelectCode.style.display = "none";
		this.callback();
	}
}

KeyBoard.prototype.setValue = function()
{//alert( "6 setValue" );
	if (this._STKCODE_codeCtrl)
	{
		var selectedIndex = this._STKCODE_selCodeList.selectedIndex;
		this._STKCODE_codeCtrl.value = this._STKCODE_selCodeList.options[ selectedIndex ].value;
	}

	if (this._STKCODE_nameCtrl)
	{
		var selectedIndex = this._STKCODE_selCodeList.selectedIndex;
		this._STKCODE_nameCtrl.value = this.getName(this._STKCODE_selCodeList.options[selectedIndex].value);
	}
}

KeyBoard.prototype.callback = function()
{//alert( "7" + this._STKCODE_codeCtrl );
	if (this._STKCODE_orgCode != this._STKCODE_codeCtrl.value)
	{
		setTimeout(this._STKCODE_callback_string);
	}
}

KeyBoard.prototype.onChange = function()
{//alert( "8" + this._STKCODE_codeCtrl );
	this.setValue();
}

KeyBoard.prototype.onKeyPress = function()
{//alert( "9" + this._STKCODE_codeCtrl );
	if (event.keyCode == 27)			// <Esc>
	{
		this.hideCodeList();
	}
	else if (event.keyCode == 13)			// <Enter>
	{
		this.onDblClick();
	}

}

KeyBoard.prototype.showCodeList = function( _callback_string, nameCtrl )
{//alert( "10" + this._STKCODE_codeCtrl );
	if (!_callback_string)
		this._STKCODE_callback_string = "";
	else
		this._STKCODE_callback_string = _callback_string;
	this._STKCODE_nameCtrl = nameCtrl;

	this._STKCODE_codeCtrl = event.srcElement;

	this._STKCODE_orgCode = this._STKCODE_codeCtrl.value;

	var pos = this.getPosition( this._STKCODE_codeCtrl );
	this._STKCODE_divSelectCode.style.top = pos[0] + this._STKCODE_codeCtrl.clientHeight;
	this._STKCODE_divSelectCode.style.left = pos[1];

	this._STKCODE_divSelectCode.style.display = "";
	this.filterCodeList();

	var me = this;
	document.attachEvent("onclick", function(){ me.hideCodeList() });
}

KeyBoard.prototype.getPosition = function( ctrl )
{//alert( "11" + this._STKCODE_codeCtrl );
	var pos = new Array();
	pos[0] = ctrl.offsetTop;
	pos[1] = ctrl.offsetLeft;

	var parent = ctrl.offsetParent;
	while ( parent )
	{
		pos[0] += parent.offsetTop;
		pos[1] += parent.offsetLeft;

		parent = parent.offsetParent;
	}

	return pos;
}

KeyBoard.prototype.filterCodeList = function()
{//alert( "12" + this._STKCODE_codeCtrl );
  this._STKCODE_divSelectCode.style.display = "";
	if (event.keyCode > 36 && event.keyCode < 41)
	{
		this.focusSelCodeList( event.keyCode );
		return;
	}
	else if (event.keyCode == 27)	// <ESC>
	{
		this._STKCODE_divSelectCode.style.display = "none";
		this._STKCODE_codeCtrl.focus();
		return;
	}
	else if ( event.keyCode == 13 )
	{
		this.onDblClick();
		return;
	}

	this._STKCODE_code = this._STKCODE_codeCtrl.value;
	this._STKCODE_selCodeList.options.length = 0;
	var j = 0, i = 0;

	// ---------------------- 初始区域 ( start )----------------------
	var lc_select_texts = this.stkcode_textlist;
	var lc_select_values = this.stkcode_valuelist;
	var lc_select_pinyins = this.stkcode_pinyinlist;

	var lc_max_options_length = this._STKCODE_num;  // 列表最大存放股票的数量
	var lc_stocks_length = lc_select_texts.length; // 总共有多少股票
	var lc_dynamic = this.dynamic;
	var lc_options = this._STKCODE_selCodeList.options;
	// ---------------------- 初始区域 ( end )----------------------


	if ( this._STKCODE_code == "" )  // 当为空时将内容全部列出
	{
		for (i = 0; i < lc_max_options_length; i ++)
		{
			if ( lc_dynamic )
			{
				var text = this.format( lc_select_values[i] );
				lc_options[i] = new Option( text + lc_select_texts[i], lc_select_values[i] );
			}
			else
			{
				lc_options[i] = new Option( lc_select_texts[i], lc_select_values[i] );
			}
		}
	}
	else
	{
		// 优先匹配value
		this._STKCODE_code = this._STKCODE_code.toLowerCase();
		var lc_stock_code = this._STKCODE_code;

		var lc_stk_code_length = this._STKCODE_code.length;


		for (i = 0; i < lc_stocks_length; i ++)
		{
			var temp = lc_select_values[i].substr( 0, lc_stk_code_length ).toLowerCase();
			if ( temp == lc_stock_code && j < 10)
			{
				if ( lc_dynamic )
				{
					var text = this.format( lc_select_values[i] );
					lc_options[j] = new Option( text + lc_select_texts[i], lc_select_values[i] );
				}
				else
				{
					lc_options[j] = new Option( lc_select_texts[i], lc_select_values[i] );
				}

				j ++;
			}
			else
			{
				if (j > 10) break;
			}
		}

		// 其次匹配pinyinlist
		if ( lc_options.length == 0 )
		{
			if ( lc_select_pinyins.length == lc_stocks_length )
			{
				lc_options.length = 0;
				for (i = 0; i < lc_stocks_length; i ++)
				{
					var temp = lc_select_pinyins[i].substr( 0, lc_stk_code_length ).toLowerCase();

					if ( temp == lc_stock_code && j < 10 )
					{
						if ( lc_dynamic )
						{
							var text = this.format( lc_select_pinyins[i] );
							lc_options[j] = new Option( text + lc_select_texts[i], lc_select_values[i] );
						}
						else
						{
							lc_options[j] = new Option( lc_select_texts[i], lc_select_values[i] );
						}
						j ++;
					}
					else
					{
						if (j > 10)
							break;
					}
				}
			}
		}
	}

	if ( lc_options.length > 0 )
	{
		lc_options[0].selected = true;
	}
}

KeyBoard.prototype.focusSelCodeList = function( key )
{//alert( "13" + this._STKCODE_codeCtrl );
	this._STKCODE_selCodeList.focus();
}

KeyBoard.prototype.getName = function( stkcode )
{//alert( "14" + this._STKCODE_codeCtrl );
	for ( var i = 0; i < this.stkcode_valuelist.length; i ++ )
	{
		if ( this.stkcode_valuelist[i] == stkcode )
			return this.stkcode_textlist[i];
	}
}

// 使其具有得长度为8
KeyBoard.prototype.format = function( text )
{
	var text_length = text.length;
	var delta = 9 - text_length;
	if ( delta <= 0 ) delta = 1;
	for( var i = 0; i < delta; i++ ) text += " ";

	return text;
}

KeyBoard.prototype.release = function()
{
	this.stkcode_textlist = new Array();
	this.stkcode_valuelist = new Array();
	this.stkcode_pinyinlist = new Array();
	this.id = 0;
	this.dynamic = false;
	this.affectCtrl = null;

	this._STKCODE_codeCtrl = null;	// the HTML ELEMENT to receive the stock code
	this._STKCODE_nameCtrl = null;	// the HTML ELEMENT to receive the stock name
	this._STKCODE_num = 0;			// Number of Stock Code to display
	this._STKCODE_code = "";		// temp stock code
	this._STKCODE_orgCode = "";		// Original stock code
	this._STKCODE_selCodeList = null;		// <Select> element object
	this._STKCODE_divSelectCode = null;		// <div> element object
	this._STKCODE_callback_string = "";
	this.callbackManager = new CallbackManager();


	var me = this;
	detachEvent( "onunload", function(){ me.release() } );
}