﻿document.write("<div id=\"popup\">");
document.write("<table id=\"name_table\" bgcolor=\"#FFFAFA\" border=\"0\" cellspacing=\"0\" cellpadding=\"3px\" onfocus=\"clearNames()\">");
document.write("<tbody id=\"name_table_body\">");
document.write("</tbody>");
document.write("</table>");
document.write("</div>");
document.write(" <style type=\"text/css\"> ");
document.write(".mouseOut {font-size:12px; background: #ffffff;color: #000000; }");
document.write(".mouseOver {font-size:12px;background: #3366cc;color: #ffffff; cursor: pointer; }</style>");

//建立跨浏览器的xmlHttp对象引用 
var xmlHttp; 
var k=-1; 
var txtvalue=null;
 var txtPriceName=null;
try 
{ 
xmlHttp=new XMLHttpRequest(); 
} 
catch(e) 
{ 
var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' ); 
var success = false; 
for(var i=0;i<XMLHTTP_IDS.length&&!success;i++) 
{ 
try { 
xmlHttp = new ActiveXObject(XMLHTTP_IDS[i]); 
success = true; 
} catch (e) {} 
} 
if (!success) { 
throw new Error('Unable to create XMLHttpRequest.'); 
} 
} 
//得到页面使用的引用变量 
function initVars(InputId) { 
inputField = document.getElementById(InputId); 
inputField.onkeyup=function(){findNames(event);GetStockCode();}
inputField.onkeydown=function(){
updown(event);
GetStockCode();
if(event.keyCode==13){
clearNames();
    return false;
}}
//txtPriceName.onfocus=function(){clearNames();}
inputField.onclick=function(){findNames(event);GetStockCode();}
inputField.onfocus=function(){getSelectPos(inputField);}
nameTable = document.getElementById("name_table"); 
completeDiv = document.getElementById("popup"); 
nameTableBody = document.getElementById("name_table_body"); 
document.getElementById("popup").style.display="block"; 
} 
//ajax前台触发函数 
function findNames(event) { 
var keyc=(window.navigator.appName=="Microsoft Internet Explorer")?event.keyCode:e.which; 
if(keyc!=40 && keyc!=38) 
{ 
//if(txtvalue==inputField.value){
//    return;
//}
if (inputField.value.length > 0) 
{
var url = "/index.php/Index/_getStockByCode/StockCode/" + inputField.value +"/rnd/"+Math.random().toString(); 
xmlHttp.open("GET", url, true); 
xmlHttp.onreadystatechange = callback; 
xmlHttp.send(null); 
} 
else 
{ 
clearNames(); 
} 
} 
} 
//ajax回调函数 
function callback() 
{ 
var names; 
if (xmlHttp.readyState == 4) { 
if (xmlHttp.status == 200) 
{ 
try 
{ 
//alert(xmlHttp.responseText);
//names=xmlHttp.responseXML.getElementsByTagName("name"); 
//alert(names.length);
if (window.ActiveXObject) { //for IE  
		var mydata = new ActiveXObject("Microsoft.XMLDOM");
		mydata.async = false;
		mydata.loadXML(xmlHttp.responseText);
		names=mydata.getElementsByTagName("name"); 
}else{
	names=xmlHttp.responseXML.getElementsByTagName("name"); 
}
//alert(names.length);

} 
catch(e) 
{ 
completeDiv.style.display="none"; 
clearNames(); 
} 
k=-1;//重新从第一项开始往下选择 
setNames(names); 
} 
else if (xmlHttp.status == 204) 
{ 
clearNames(); 
} 
} 
} 
//弹出层位置确定函数 
function divsOffset() 
{ 
var inputWidth=inputField.offsetWidth; 
var inputHeight=inputField.offsetHeight; 
var inputTop=inputField.offsetTop; 
var inputLeft=inputField.offsetLeft; 
nameTable.style.width=inputWidth+"px"; 
completeDiv.style.width=inputWidth+"px"; 
completeDiv.style.top=(getTop(inputField)+inputHeight)+"px"; 
completeDiv.style.left=getLeft(inputField)+"px"; 
completeDiv.style.border = "#817f82 1px solid"; 
completeDiv.style.position = "absolute"; 
//获取元素的绝对纵坐标 
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; 
} 
//alert(inputWidth+"----"+inputwidth+"----"+inputtopleft+"----"+inputtop); 
} 
//将返回数据添加到表格函数 
function setNames(the_names) { 
clearNames(); 
divsOffset(); 
if(the_names.length==0){
var nextNode="暂时没有任何结果"; 
var row =document.createElement("tr"); 
var cell =document.createElement("td"); 
cell.onmouseout = function() {this.className="mouseOut";}; 
cell.onmouseover =(function(tdi) {return function(){k=tdi;setStyle(k);}})(i); 
cell.setAttribute("align","left"); 
cell.setAttribute("bgcolor","#FFFAFA"); 
cell.setAttribute("border","0"); 
cell.setAttribute("style","font-size:15px"); 
//将nextNode添加到td 
var txtName = document.createTextNode(nextNode);
if(txtName)txtName.setAttribute("align","left"); 
cell.appendChild(txtName); 
row.appendChild(cell); 
nameTableBody.appendChild(row); 
}
for(var i=0;i<the_names.length;i++) 
{
var nextNode=the_names[i].firstChild.nodeValue; 
var row =document.createElement("tr"); 
var cell =document.createElement("td"); 
cell.onmouseout = function() {this.className="mouseOut";}; 
cell.onmouseover =(function(tdi) {return function(){k=tdi;setStyle(k);}})(i); 
cell.setAttribute("bgcolor","#FFFAFA"); 
cell.setAttribute("border","0"); 
cell.setAttribute("style","font-size:15px"); 
cell.onfocus =function() {populateName(this); }; 
cell.onkeyup=function(){populateName(this);}
cell.onkeydown=function(){populateName(this);}
//将nextNode添加到td 
var txtName = document.createTextNode(nextNode); 
if (txtName) cell.appendChild(txtName); 
row.appendChild(cell); 
nameTableBody.appendChild(row); 
} 
} 
//点击选中一条数据，添加到inputField 
function populateName(cell) { 
//填充数据到web页面，cell---->td对象 
if(cell.firstChild.nodeValue=='暂时没有任何结果'){
    return false;
}
inputField.value = cell.firstChild.nodeValue;
if(inputField.value!=null&&inputField.value!=""){
clearNames(); 
GetStockCode();
}
} 
//设置样式 
function setStyle(num) 
{ 
var tBody = nameTableBody.getElementsByTagName("td"); 
for(var i=0;i<tBody.length;i++) 
{ 
if(i>=0 && i<tBody.length&&i==num) 
{ 
tBody[i].className="mouseOver"; 
} 
else 
{ 
tBody[i].className="mouseOut"; 
} 
} 
} 
//获取键盘上下键 
function updown(e) 
{ 
var tBody = nameTableBody.getElementsByTagName("td"); 
if(tBody.length==0){return;}
var keyc=(window.navigator.appName=="Microsoft Internet Explorer")?event.keyCode:e.which; 

if(tBody==null) 
return; 
if(keyc==40) 
{ 
k++; 
if(k>=tBody.length) 
{ 
k=0; 
} 
if(tBody[k]==null||tBody[k].firstChild.nodeValue=='暂时没有任何结果'){
    return;
}
inputField.value=tBody[k].firstChild.nodeValue; 
} 
else if(keyc==38) 
{ 
k--; 
if(k<=-1) 
{ 
k=tBody.length-1; 
} 
if(tBody[k]==null||tBody[k].firstChild.nodeValue=='暂时没有任何结果'){
    return;
}
inputField.value=tBody[k].firstChild.nodeValue; 
} 
setStyle(k); 
} 
//清除列表数组 
function clearNames() { 
var ind = nameTableBody.childNodes.length; 
var completeDiv = document.getElementById("popup"); 
for (var i = ind - 1; i >= 0 ; i--) { 
nameTableBody.removeChild(nameTableBody.childNodes[i]); 
} 
completeDiv.style.border = "none"; 
} 


function GetStockCode(){
    var txt=inputField.value;
    if(txt.length>5){
        txt=txt.substring(0,6);
        inputField.value=txt;
        txtvalue=txt;
        //SendRequest(inputField.value);//读取股票数据
    } else {
        return false;
    }
}

function getSelectPos(obj){ 
var esrc = document.getElementById(obj); 
if(esrc==null){ 
   esrc=event.srcElement; 
} 
var rtextRange =esrc.createTextRange(); 
rtextRange.moveStart('character',esrc.value.length); 
rtextRange.collapse(true); 
rtextRange.select(); 
} 

