// _STKCODE_CodeList[i], _STKCODE_NameList[i] // _STKCODE_CodeList[i] 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.prevSecCode = ""; 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; // \n'); //document.write(' onDblClick="_STKCODE_selCodeList_onDblClick();" onChange="_STKCODE_selCodeList_onChange();" onKeyPress="_STKCODE_selCodeList_onKeyPress();">\n'); document.write('\n'); document.write('\n'); document.write('\n'); // this._STKCODE_selCodeList = document.getElementById(stkcode_selCodeList_id); this._STKCODE_divSelectCode = document.getElementById(stkcode_divSelectCode_id); //alert(this._STKCODE_divSelectCode.innerHTML); var me = this; this._STKCODE_selCodeList.ondblclick = function() { me.onDblClick(); } this._STKCODE_selCodeList.onchange = function() { me.onChange(); }; this._STKCODE_selCodeList.onkeypress = function() { me.onKeyPress(); } this._STKCODE_selCodeList.onkeydown = function() {me.selectEnter();}; //选中后回车 this._STKCODE_num = 100; 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.clearCode();me.showCodeList(); } this.affectCtrl.onkeyup = function() { me.filterCodeList(); } this.affectCtrl.onblur = function() {me.loseCode();} //this.affectCtrl.onreset = function() {me.clearCode();} //CHONG this.affectCtrl.onkeydown = function(){me.inEnter();} } /*for (var i = 0; i < this._STKCODE_num; i ++) { if ( this.dynamic ) { var text = this.format( this.stkcode_textlist[i] ); this._STKCODE_selCodeList.options[i] = new Option( text + this.stkcode_valuelist[i], this.stkcode_valuelist[i]); } else { this._STKCODE_selCodeList.options[i] = new Option(this.stkcode_textlist[i], this.stkcode_valuelist[i]); } } if (this._STKCODE_selCodeList.length > 0) { this._STKCODE_selCodeList.options[0].selected = true; }*/ //alert( this._STKCODE_num ); } KeyBoard.prototype.onDblClick = function() {//alert( "4" + this._STKCODE_codeCtrl ); //alert( this._STKCODE_codeCtrl ); var me = this; document.detachEvent("onclick", function(){ me.hideCodeList() }); this._STKCODE_divSelectCode.style.display = "none"; this.setValue(); this.callback(); } KeyBoard.prototype.hideCodeList = function() {//alert( "5" + this._STKCODE_codeCtrl ); 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" + this._STKCODE_codeCtrl ); if (this._STKCODE_codeCtrl) { var selectedIndex = this._STKCODE_selCodeList.selectedIndex; if (selectedIndex<0) { return; } 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() { if (event.keyCode == 27) // { this.hideCodeList(); } else if (event.keyCode == 13) // { //alert( "9" + this._STKCODE_codeCtrl ); this.onDblClick(); } else if (event.keyCode == 8) // { this._STKCODE_code = this._STKCODE_code.substr(0, this._STKCODE_code.length - 1); alert( "9" + this._STKCODE_code.substr(0, this._STKCODE_code.length - 1) ); } else if (event.keyCode >= 48 && event.keyCode <= 122) { this._STKCODE_code = this._STKCODE_code + string(event.keyCode); } } KeyBoard.prototype.inEnter = function(e) { var ieKey=event.keyCode; if (ieKey == 13) { //alert("sss"); this.onDblClick(); if (document.getElementById(this.KB_INPUT_ID)!=null) { document.getElementById(this.KB_INPUT_ID).click(); } } } KeyBoard.prototype.selectEnter = function(e) { var ieKey=event.keyCode; if (ieKey == 13) { this.onDblClick(); if (document.getElementById(this.KB_INPUT_ID)!=null) { document.getElementById(this.KB_INPUT_ID).click(); } } } KeyBoard.prototype.clearCode = function() { if(this.affectCtrl.value.length>=6) { if (this.affectCtrl.value.length==6) { this.prevSecCode = this.affectCtrl.value; } this.affectCtrl.value = ""; } } KeyBoard.prototype.loseCode = function() { if (this.affectCtrl.value.length!=6) { this.affectCtrl.value = this.prevSecCode; } } KeyBoard.prototype.showCodeList = function( _callback_string, nameCtrl ) { 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; if(this._STKCODE_codeCtrl.up!=null){ var pos = this.getPosition( this._STKCODE_codeCtrl ); this._STKCODE_divSelectCode.style.top = pos[1] + this._STKCODE_codeCtrl.clientHeight-170 ; this._STKCODE_divSelectCode.style.posLeft = pos[0]+2; }else{ //alert( "10" + this._STKCODE_codeCtrl.outerHTML ); var pos = this.getPosition( this._STKCODE_codeCtrl ); this._STKCODE_divSelectCode.style.top = pos[1] + this._STKCODE_codeCtrl.clientHeight ; this._STKCODE_divSelectCode.style.posLeft = pos[0]+2; } this._STKCODE_divSelectCode.style.display = ""; this.filterCodeList(); var me = this; document.attachEvent("onclick", function(){ me.hideCodeList() }); } KeyBoard.prototype.truePosition = function (element) { var pos = this.cumulativeOffset(element); //alert(pos); if(window.opera) { return pos; } var iebody = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body; var dsocleft = document.all ? iebody.scrollLeft : window.pageXOffset; var dsoctop = document.all ? iebody.scrollTop : window.pageYOffset; var posReal = this.realOffset(element); var top = pos[1] - posReal[1] + dsoctop; var left = pos[0] - posReal[0] + dsocleft; //alert(left+","+top); return [left, top]; } KeyBoard.prototype.realOffset = function (element) { var t = 0, l = 0; do { t += element.scrollTop || 0; l += element.scrollLeft || 0; element = element.parentNode; } while (element); return [l, t]; } KeyBoard.prototype.cumulativeOffset = function (element) { var t = 0, l = 0; do { t += element.offsetTop || 0; l += element.offsetLeft || 0; //alert(element.id); element = element.offsetParent; } while (element); return [l, t]; } KeyBoard.prototype.getPosition = function( ctrl ) { /*** del 2007/05/23 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; } pos[0] -=82; alert(pos); */ var pos = this.truePosition(ctrl); //alert(pos); return pos; } KeyBoard.prototype.filterCodeList = function() {//alert( "12" + this._STKCODE_codeCtrl ); if (event.keyCode > 36 && event.keyCode < 41) { this.focusSelCodeList( event.keyCode ); return; } if (event.keyCode == 27) // { this._STKCODE_divSelectCode.style.display = "none"; this._STKCODE_codeCtrl.focus(); return; } this._STKCODE_code = this._STKCODE_codeCtrl.value; this._STKCODE_selCodeList.innerHTML = ""; var j = 0, i = 0; if ( this._STKCODE_code == "" ) { for (i = 0; i < this._STKCODE_num; i ++) { if ( this.dynamic ) { var text = this.format( this.stkcode_valuelist[i] ); this._STKCODE_selCodeList.options[i] = new Option( text + this.stkcode_textlist[i], this.stkcode_valuelist[i]); } else { this._STKCODE_selCodeList.options[i] = new Option(this.format8(this.stkcode_valuelist[i])+this.format(this.stkcode_textlist[i])+this.format(this.stkcode_pinyinlist[i]), this.stkcode_valuelist[i]); } } } else { // 优先匹配value this._STKCODE_code = this._STKCODE_code.toLowerCase(); for (i = 0; i < this.stkcode_valuelist.length; i ++) { var temp = (this.stkcode_valuelist[i] + "").substr(0, this._STKCODE_code.length).toLowerCase(); if ( temp == this._STKCODE_code && j < 100) { if ( this.dynamic ) { var text = this.format( this.stkcode_valuelist[i] ); this._STKCODE_selCodeList.options[j] = new Option( text + this.stkcode_textlist[i], this.stkcode_valuelist[i]); } else { this._STKCODE_selCodeList.options[j] = new Option(this.format8(this.stkcode_valuelist[i])+this.format(this.stkcode_textlist[i])+this.format(this.stkcode_pinyinlist[i]) , this.stkcode_valuelist[i]); } j ++; } else { if (j > 100) break; } } // 其次匹配pinyinlist if( this._STKCODE_selCodeList.options.length == 0 ) { if ( this.stkcode_pinyinlist.length == this.stkcode_valuelist.length ) { //this._STKCODE_code = this._STKCODE_code.toLowerCase(); for (i = 0; i < this.stkcode_valuelist.length; i ++) { var temp = (this.stkcode_pinyinlist[i] + "").substr(0, this._STKCODE_code.length).toLowerCase(); if ( temp == this._STKCODE_code && j < 100) { //this._STKCODE_selCodeList.options[j] = new Option(this.stkcode_textlist[i], this.stkcode_valuelist[i]); if ( this.dynamic ) { var text = this.format( this.stkcode_pinyinlist[i] ); this._STKCODE_selCodeList.options[j] = new Option( text + this.stkcode_textlist[i], this.stkcode_valuelist[i]); } else { this._STKCODE_selCodeList.options[j] = new Option(this.format8(this.stkcode_valuelist[i])+this.format(this.stkcode_textlist[i])+this.format(this.stkcode_pinyinlist[i]), this.stkcode_valuelist[i]); } j ++; } else { if (j > 100) break; } } } } } if (this._STKCODE_selCodeList.length > 0) { this._STKCODE_selCodeList.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 = 14 - this.getStrLen( text ); if ( delta <= 0 ) delta = 2; for( var i = 0; i < delta; i++ ) text += " "; return text; } KeyBoard.prototype.format8 = function( text ) { var text_length = text.length; var delta = 8 - this.getStrLen( text ); if ( delta <= 0 ) delta = 2; for( var i = 0; i < delta; i++ ) text += " "; return text; } KeyBoard.prototype.getStrLen = function( text ) { var iCount, strTemp; iCount = 0 ; for ( var i = 0 ; i < text.length ; i ++ ) { strTemp = escape(text.charAt(i)); if (strTemp.indexOf("%u",0) == -1) // 表示是汉字 { iCount = iCount + 1 ; } else { iCount = iCount + 2 ; } } return iCount ; }