var imgState =new Array();
var txtStock =new Array();
var udtxtState =new Array();
var udtxtStock =new Array();
var udtxtInfo = new Array();
var udtxtInfo_2 = new Array();
var stockInfo = new Array();

var stocks = "";
var stockData = "";
var SinglePageCount = 1;
var topID = 0;
                          
var FITEMCOUNT = 6;
var BGBOTTOMHEIGHT = 38 ;
var dockh,undockh;
var undockedTop = 16;
var dockedTop = 5;

var tip = "由新浪财经提供股票数据";
var upFontColor = "216,20,53";
var downFontColor = "34,230,44";
var dockDownFontColor = "44,226,150";
var dockUpFontColor = "206,62,76";
var notupdownColor = "255,255,255";
var zxjNOcolor = "192,192,192";
var zxjColorfule = false;

var frm,btOK,radiogp,edtSt;
var showChart = false;
var chartHeight = 125;
var curCode = "";

function showStocks(pid)
{

  if(stockInfo.length<=0) return;

  LockRefresh;

  if(stockInfo.length <= SinglePageCount)
  {
    btUp.enabled = false;
    udbtUp.enabled =  false;
    btDown.enabled = false;
    udbtDown.enabled = false;
  }
  else
  {
    btUp.enabled = !( pid<=0 );
    udbtUp.enabled =  btUp.enabled ;
    btDown.enabled = !( (pid + SinglePageCount) >=  stockInfo.length);
    udbtDown.enabled = btDown.enabled;
  }

  pid = Math.max(0 , Math.min(stockInfo.length-SinglePageCount,pid) );
  udtxtTip.caption = tip;
  SinglePageCount = Math.min(stockInfo.length,SinglePageCount); 
 
  topID = pid;
 
  var cid = 0;
  for(var i=pid;i<pid + SinglePageCount ;i++)
  {
    udtxtStock[cid].caption =  stockInfo[i].txt + " ("+ stockInfo[i].code +")";
    udtxtInfo[cid].caption = "最新价 "+ stockInfo[i].zxj;  
    udtxtInfo_2[cid].caption = "昨收盘 " + stockInfo[i].zsp ;  
    //"最新价 "+ stockInfo[i].zxj + "  昨收盘 " + stockInfo[i].zsp ;
    var zdf = stockInfo[i].zdf;
    var zde = stockInfo[i].zde;
    if(zdf>0)
    {
      udtxtState[cid].fontColor = upFontColor;
      txtStock[cid].fontColor = dockUpFontColor;
      txtStock[cid].hotFontColor = dockUpFontColor;
      imgState[cid].Src = "images\\up.png"; 
      if( GetSetting("zdshow")=="0" )  
        udtxtState[cid].caption =  "+" + zdf + "%"
      else
        udtxtState[cid].caption =  "+" + zde ;
        
      if(zxjColorfule)
        udtxtInfo[cid].fontColor = upFontColor  
      else 
        udtxtInfo[cid].fontColor = zxjNOcolor;
    }
    else if(zdf<0)
    {
      udtxtState[cid].fontColor = downFontColor;
      txtStock[cid].fontColor = dockDownFontColor;
      txtStock[cid].hotFontColor = dockDownFontColor;
      imgState[cid].Src = "images\\down.png"; 

      if( GetSetting("zdshow")=="0" )  
        udtxtState[cid].caption =  zdf + "%"
      else
        udtxtState[cid].caption =  zde;
        
      if(zxjColorfule)
        udtxtInfo[cid].fontColor = downFontColor 
      else 
        udtxtInfo[cid].fontColor = zxjNOcolor;
    }
    else if(zdf==0)
    {
      udtxtState[cid].fontColor = notupdownColor;
      txtStock[cid].fontColor = notupdownColor;
      txtStock[cid].hotFontColor = notupdownColor;
      imgState[cid].Src = ""; 
      if( GetSetting("zdshow")=="0" )  
        udtxtState[cid].caption =  zdf + "%"
      else
        udtxtState[cid].caption =  zde;
      udtxtInfo[cid].fontColor = zxjNOcolor;
    }
    
    if(stockInfo[i].zxj=="0.00")
    {
      //停牌 
      udtxtStock[cid].alpha = 50;
      udtxtState[cid].fontColor = zxjNOcolor;
      txtStock[cid].fontColor = zxjNOcolor;
      txtStock[cid].hotFontColor = zxjNOcolor;
      imgState[cid].Src = ""; 
      udtxtState[cid].caption =  "--";
      txtStock[cid].caption =  stockInfo[i].txt + "\r\n( 停牌 )" ;
      udtxtInfo[cid].fontColor = zxjNOcolor;

      var stocktip = stockInfo[i].txt + "("+ stockInfo[i].code +")\r\n"
        +"--=停牌=--\r\n"
        +"发布时间: \r\n" + stockInfo[i].time
      
    }
    else
    {
      udtxtStock[cid].alpha = 100;
      var stocktip = stockInfo[i].txt + "("+ stockInfo[i].code +")\r\n"
        +"跌涨幅: "+ stockInfo[i].zdf + "%\r\n"
        +"跌涨额: "+ stockInfo[i].zde + "\r\n" 
        +"昨收盘: "+ stockInfo[i].zsp + "\r\n"
        +"今开盘: "+ stockInfo[i].jkp + "\r\n"
        +"最新价: "+ stockInfo[i].zxj + "\r\n"
        +"最高价: "+ stockInfo[i].zgj + "\r\n"
        +"最低价: "+ stockInfo[i].zdj + "\r\n"
        +"竞买价: "+ stockInfo[i].jmj + "\r\n"
        +"竞卖价: "+ stockInfo[i].jmj2 + "\r\n"
        +"成交量: "+ stockInfo[i].cjl + "\r\n"
        +"成交额: "+ stockInfo[i].cje + "\r\n"
        +"发布时间: \r\n" + stockInfo[i].time
        ;
      if( GetSetting("zdshow")=="0" )  
        txtStock[cid].caption =  stockInfo[i].txt + "\r\n" + stockInfo[i].zxj + "(" + stockInfo[i].zdf  + "%)" 
      else
        txtStock[cid].caption =  stockInfo[i].txt + "\r\n" + stockInfo[i].zxj + "(" + stockInfo[i].zde  + ")" ;
    }
    txtStock[cid].tagStr = stockInfo[i].code; 
    txtStock[cid].tooltip = stocktip;
    txtStock[cid].tag = i;

    udtxtStock[cid].tagStr = stockInfo[i].code; 
    udtxtStock[cid].tooltip = stocktip;
    
    udtxtState[cid].tag = i;
    udtxtState[cid].tagStr = stockInfo[i].code; 
    

    cid++;
  }
  
  setSkin();
  UnLockRefresh;
  
  WriteIni("topID",topID);
  

  if( trim(curCode)=="") 
  { 
    curCode = udtxtState[0].tagStr;
    imgSelect.top = udtxtState[0].top - 1; 
  
    if(showChart)
    {
      Print( " showStocks:  "+curCode );
      DownLoadFileAsync( "http://image.sinajs.cn/newchart/small/b"+ curCode +".gif?" + gettm() , widgetPath + "chart\\"+ curCode +".gif" ,"onDownChart2" ); 
  
    }
  }
}

function clearAllItem()
{
  LockRefresh;
  
  showChart = false;
  
  for(var i=0;i<6;i++)
  {
    imgState[i].src = "";
    
    txtStock[i].caption = "";
    txtStock[i].tagStr = "";
    
    udtxtStock[i].caption = "";
    udtxtStock[i].tagStr = "";
    udtxtStock[i].tooltip = "";
    udtxtInfo[i].caption = "";
    udtxtInfo_2[i].caption = "";
    udtxtState[i].caption = "";
  }
  imgChart.Src = "";
  imgSelect.Visible = false;
  
  UnLockRefresh;

}

function refreshData()
{
  if(stocks=="") 
  {
    clearAllItem();
    udtxtTip.caption = "请点击右下角加号按钮添加股票"; 
    btUp.enabled = false;
    udbtUp.enabled =  false;
    btDown.enabled = false;
    udbtDown.enabled = false;

    return;
  }
  
  udtxtTip.caption = "正在刷新数据，请稍后..."; 
  txtTip.caption = "刷新...";
  getSinaStockAsync( stocks ,"onGetTxt" );   
  
  if( trim(curCode)!="") 
  { 
    if(showChart)
    {
      Print("refreshData: "+ curCode );
      DownLoadFileAsync( "http://image.sinajs.cn/newchart/small/b"+ curCode +".gif?" + gettm() , widgetPath + "chart\\"+ curCode +".gif" ,"onDownChart2" ); 
  
    }
  }
  

}

function  onGetTxt(fileTxt,errorCode)
{
  txtTip.caption = "";  
  if(errorCode<1)
  {
    udtxtTip.caption = "无法连接服务器";
    return; 
  }
  
  stockData = fileTxt;  

  stockInfo.splice(0, stockInfo.length);
  var sl = stockData.split('";');
  ;
  var str = '';
  for(var i=0;i<sl.length;i++)
  {
    str = trim(sl[i]);
    if(str=='') continue;
    
   
    if( str.substr(0,('var hq_str_').length)!='var hq_str_'  ) continue;
    
    stockInfo.push(new NewStock());
    var cid = stockInfo.length-1 ;

    if( str.length-1 == str.indexOf('"')  ) 
    {
      stockInfo[cid].txt =  '未知股票代码';
      stockInfo[cid].code =  getMidTxt(str,'var hq_str_','=');;
      continue;
    }

  
    stockInfo[cid].code =  getMidTxt(str,'var hq_str_','=');
    str = delSubTxt(str,'"');
    str = str.split(",");
    
    stockInfo[cid].txt =  str[0];

    stockInfo[cid].zsp = str[2];
    stockInfo[cid].jkp = str[1];
    stockInfo[cid].zxj = str[3];
    stockInfo[cid].zgj = str[4];
    stockInfo[cid].zdj = str[5];
    stockInfo[cid].jmj = str[6];
    stockInfo[cid].jmj2 = str[7];
    stockInfo[cid].cjl = str[8];
    stockInfo[cid].cje = str[9];
    stockInfo[cid].time = str[30] + " " + str[31];
    
    
    
    try{
    stockInfo[cid].zde = (stockInfo[cid].zxj - stockInfo[cid].zsp).toFixed(2) ;
    }catch(e){ }
    
    try{
      stockInfo[cid].zdf = (stockInfo[cid].zde/stockInfo[cid].zsp *100).toFixed(2) ;
    }catch(e){ }

  }
  
  showStocks(topID);
  
}

function setSkin()
{
  var num = parseInt( GetSetting("valSinglePageCount") );
  if((num>0)&&(num<=FITEMCOUNT))
    SinglePageCount = num
  else
    SinglePageCount = 3;

  var sl = stocks.split(",");
  SinglePageCount = Math.max(1,Math.min(sl.length,SinglePageCount)); 

  LockRefresh;
  var tp = 0;     
  for(var i=0;i<FITEMCOUNT;i++)
  {
    tp = i*33 + dockedTop;
    imgState[i].top = tp;
    txtStock[i].top = tp+3;
    imgState[i].Visible = i<SinglePageCount;
    txtStock[i].Visible = i<SinglePageCount; 

    tp = i*33+undockedTop;
    udtxtState[i].top = tp - 2;
    udtxtState[i].Visible = i<SinglePageCount;

    udtxtStock[i].top = tp;
    udtxtStock[i].Visible = i<SinglePageCount;
    
    udtxtInfo[i].top = tp + 16;
    udtxtInfo[i].Visible = i<SinglePageCount;

    udtxtInfo_2[i].left = 115;
    udtxtInfo_2[i].top = tp + 16;
    udtxtInfo_2[i].Visible = i<SinglePageCount;
     

  }
  tp = SinglePageCount*33+dockedTop;
  imgLine.top = tp;
  imgNav.top = tp+6;
  btAdd.Top = tp + 8;
  btRef.Top = tp + 21;
  btUp.Top = tp + 9;
  btDown.Top = tp + 8;
  txtTip.Top = tp + 23;
  
  tp = SinglePageCount*33+undockedTop;
  udimgLine.clipTop = (6-SinglePageCount)*33;

  imgChart.top = tp ;
  imgChart.Visible = showChart;
  
  if(showChart)
    tp = tp + chartHeight;

  udbtAdd.top =  tp ; 
  udbtRef.top =  tp ; 
  udbtChart.top =  tp ;
  udtxtTip.top =  tp + 2;
  udbtUp.Top = tp ;
  udbtDown.Top = tp - 1;
    
  dockh =  BGBOTTOMHEIGHT + (SinglePageCount)*33 + dockedTop;
  if(!showChart)
    undockh =  BGBOTTOMHEIGHT + (SinglePageCount)*33 + undockedTop 
  else
  {
     undockh =  BGBOTTOMHEIGHT + (SinglePageCount)*33 + undockedTop + chartHeight;
  }

    
  if(dockedOnSidebar)
    widgetHeight = dockh
  else
    widgetHeight = undockh;
    
  var h = widgetHeight;
    
  bg2.Height = h - bg1.Height - bg3.Height; 
  bg3.top = bg1.Height + bg2.Height;
  
  udbg2.Height = h - udbg1.Height - udbg3.Height; 
  udbg3.top = udbg1.Height + udbg2.Height;
  
  UnLockRefresh;
  
}

function loadsetting()
{
  stocks = GetSetting("stocks");//  ReadIni("stocks");
//  SaveSetting("stocks");
//  WriteIni("stocks",stocks);
  
//  showChart = parseInt( GetSetting("showChart") );
  //curCode = ReadIni("curCode");
  topID = parseInt(ReadIni("topID")); 
  zxjColorfule = parseInt( GetSetting("zxjColorfule")  );
  
  var rtm = parseInt( GetSetting("refreshtm")  ) * 1000 * 60;
  timer0.enabled = rtm!=0 ; 
  timer0.interval = rtm;
  
  if(isNan(topID))
    topID = 0;
  
  curCode = ""; 
  if(stocks=="")
  {
    //如果没有设置任何股票代码
  }

  
  setSkin();
}


function widgetOnInit()
{
 
  imgState[0] = imgState0;
  imgState[1] = imgState1;
  imgState[2] = imgState2;
  imgState[3] = imgState3;
  imgState[4] = imgState4;
  imgState[5] = imgState5;
  
  txtStock[0] = txtStock0;
  txtStock[1] = txtStock1;
  txtStock[2] = txtStock2;
  txtStock[3] = txtStock3;
  txtStock[4] = txtStock4;
  txtStock[5] = txtStock5;
  
  
  udtxtState[0] = udtxtState0;
  udtxtState[1] = udtxtState1;
  udtxtState[2] = udtxtState2;
  udtxtState[3] = udtxtState3;
  udtxtState[4] = udtxtState4;
  udtxtState[5] = udtxtState5;
  
  udtxtStock[0] = udtxtStock0;
  udtxtStock[1] = udtxtStock1;
  udtxtStock[2] = udtxtStock2;
  udtxtStock[3] = udtxtStock3;
  udtxtStock[4] = udtxtStock4;
  udtxtStock[5] = udtxtStock5;
  
  udtxtInfo[0] = udtxtInfo0; 
  udtxtInfo[1] = udtxtInfo1; 
  udtxtInfo[2] = udtxtInfo2; 
  udtxtInfo[3] = udtxtInfo3; 
  udtxtInfo[4] = udtxtInfo4; 
  udtxtInfo[5] = udtxtInfo5; 

  udtxtInfo_2[0] = udtxtInfo20; 
  udtxtInfo_2[1] = udtxtInfo21; 
  udtxtInfo_2[2] = udtxtInfo22; 
  udtxtInfo_2[3] = udtxtInfo23; 
  udtxtInfo_2[4] = udtxtInfo24; 
  udtxtInfo_2[5] = udtxtInfo25; 


  loadsetting();
  
   
  

}

function value0_scriptFuncName(panel)
{
/*
   var lb = TLabel.Create(widget);
   lb.parent = panel;
   lb.caption = "请选择要显示的证劵指数:";
   lb.rc = Rect(35,5,200,20);
   
   var cbox = TCheckListBox.Create(widget);
   cbox.parent = panel;
   cbox.style = 1; 
   cbox.columns = 2;
   cbox.itemHeight = 24; 
   cbox.rc = Rect(35,25,325,105);
   cbox.items.add("上证指数");
   cbox.items.add("深证成指");
   cbox.items.add("新综指");
   cbox.items.add("上证180");
   cbox.items.add("上证50");
   cbox.items.add("中小扳指");
   cbox.items.add("沪深300");
   cbox.items.add("深证100R");
   
    */

}

function OnAllSettingsSaved()
{
  loadsetting();
  refreshData();
}

function widgetOnDock()
{
  widget.pageIndex = 1;
  setSkin();
}

function widgetOnUnDock()
{
  widget.pageIndex = 2;
  setSkin();
    
}

function NewStock()
{
  this.code = "";
  this.txt = "";
  this.zdf = "";
  this.zde = "";
  this.zsp = "";
  this.jkp = "";
  this.zxj = "";
  this.zgj = "";
  this.zdj = "";
  this.cjl = "";
  this.cje = "";
  this.time = "";

  this.jmj = "";
  this.jmj2 = "";
  
}




///////////////////////////////////////////
////获取某字符串的中间一段 
//////////////////////////////////////////
function getMidTxt(str,frttxt,endtxt)
{
  try
  {
     var n = str.indexOf(frttxt);
  }
  catch(e)
  {
    return "";
  }
   if(n<0)
   {
     result = "";
     return;
   }
     
   str = str.substr( n + frttxt.length);

   n = str.indexOf(endtxt);
   if(n<0)
   {
     result = "";
     return;
   }
   
   return trim(str.substr(0,n));

}

/////////////////////////////////////////////////////
//返回删除subtxt前面所有文字之后的str字符 
/////////////////////////////////////////////////////
function delSubTxt(str,subtxt)
{
   try
   {
     var n = str.indexOf(subtxt);
   }catch(e){
     return "";
   }
   if(n<0)
   {
     result = str;
     return;
   }
     
   str = str.substr( n + subtxt.length);
   return trim(str);

}


//////////////////////////////////////////
//替换str内所有的srctxt为destxt 
/////////////////////////////////////////
function ReplaceTxt(str,srctxt,destxt)
{
   try{
     var n = str.indexOf(srctxt);
   }catch(e)
   {
     return "";
   }

   while(n>0)
   {
     if(n<0)
     {
       return str;
     }
     var str1 = str.substr(0,n);
     var str2 = str.substr( n + srctxt.length );
     str = str1 + destxt + str2;
   
     n = str.indexOf(srctxt);
   }
   
   
   return str;
}

function trim(str)
{
  // 用正则表达式将前后空格用空字符串替代。
  return str.replace(/(^\s*)|(\s*$)/g, "");
}

function udbtDownOnClick(sender)
{
  topID++;
  curCode = "";
  showStocks(topID);
  hideBtDel();  
}

function udbtUpOnClick(sender)
{
  topID--;
  curCode = "";
  showStocks(topID);
  hideBtDel();  
}



function btAddOnClick(sender)
{
  custPopMenu.clear;
  custPopMenu.addMenuItem("添加自定义股票...","addStockMenuClick",0);

  var pnt = custPopMenu.addMenuItem("添加证劵指数","addStockMenuClick",-999);
  custPopMenu.addMenuItemChild(pnt,"添加以下全部证劵指数","addStockMenuClick",-1);
  custPopMenu.addMenuItemChild(pnt,"-","",-999);
  custPopMenu.addMenuItemChild(pnt,"添加 上证指数","addStockMenuClick",2);
  custPopMenu.addMenuItemChild(pnt,"添加 深证成指","addStockMenuClick",3);
  custPopMenu.addMenuItemChild(pnt,"添加 新综指","addStockMenuClick",4);
  custPopMenu.addMenuItemChild(pnt,"添加 上证180","addStockMenuClick",5);
  custPopMenu.addMenuItemChild(pnt,"添加 上证50","addStockMenuClick",6);
  custPopMenu.addMenuItemChild(pnt,"添加 中小板指","addStockMenuClick",7);
  custPopMenu.addMenuItemChild(pnt,"添加 沪深300","addStockMenuClick",8);
  custPopMenu.addMenuItemChild(pnt,"添加 深证100R","addStockMenuClick",9);
  custPopMenu.addMenuItemChild(pnt,"添加 A股指数","addStockMenuClick",10);
  custPopMenu.addMenuItemChild(pnt,"添加 B股指数","addStockMenuClick",11);
  custPopMenu.addMenuItemChild(pnt,"添加 基金指数","addStockMenuClick",12);
  custPopMenu.addMenuItemChild(pnt,"添加 成份A指","addStockMenuClick",13);
  custPopMenu.addMenuItemChild(pnt,"添加 成份B指","addStockMenuClick",14);
  
  var pnt = custPopMenu.addMenuItem("添加行业指数","addStockMenuClick",-999);
  custPopMenu.addMenuItemChild(pnt,"添加以下全部行业指数","addStockMenuClick",-2);
  custPopMenu.addMenuItemChild(pnt,"-","",-999);
  custPopMenu.addMenuItemChild(pnt,"添加 地产指数","addStockMenuClick",20);
  custPopMenu.addMenuItemChild(pnt,"添加 金融指数","addStockMenuClick",21);
  custPopMenu.addMenuItemChild(pnt,"添加 服务指数","addStockMenuClick",22);
  custPopMenu.addMenuItemChild(pnt,"添加 建筑指数","addStockMenuClick",23);
  custPopMenu.addMenuItemChild(pnt,"添加 金属指数","addStockMenuClick",24);
  custPopMenu.addMenuItemChild(pnt,"添加 造纸指数","addStockMenuClick",25);
  custPopMenu.addMenuItemChild(pnt,"添加 采掘指数","addStockMenuClick",26);
  custPopMenu.addMenuItemChild(pnt,"添加 医药指数","addStockMenuClick",27);
  custPopMenu.addMenuItemChild(pnt,"添加 综企指数","addStockMenuClick",28);
  custPopMenu.addMenuItemChild(pnt,"添加 制造指数","addStockMenuClick",29);
  custPopMenu.addMenuItemChild(pnt,"添加 纺织指数","addStockMenuClick",30);
  custPopMenu.addMenuItemChild(pnt,"添加 机械指数","addStockMenuClick",31);
  custPopMenu.addMenuItemChild(pnt,"添加 IT指数","addStockMenuClick",32);                   
  
  custPopMenu.popup;    
}

function addStockMenuClick(sender)
{
  var tag = sender.tag;
  switch (tag)
  {
    case 0:
      addCustStock(); 

      break;
      
    case 2:   addStockByCode("sh000001");     break;
    case 3:   addStockByCode("sz399001");     break;
    case 4:   addStockByCode("sh000017");     break;
    case 5:   addStockByCode("sh000010");     break;
    case 6:   addStockByCode("sh000016");     break;
    case 7:   addStockByCode("sz399101");     break;
    case 8:   addStockByCode("sh000300");     break;
    case 9:   addStockByCode("sz399004");     break;
    case 10:   addStockByCode("sh000002");     break;
    case 11:   addStockByCode("sh000003");     break;
    case 12:   addStockByCode("sh000011");     break;
    case 13:   addStockByCode("sz399002");     break;
    case 14:   addStockByCode("sz399003");     break;

    case 20:   addStockByCode("sz399200");     break;
    case 21:   addStockByCode("sz399190");     break;
    case 22:   addStockByCode("sz399210");     break;
    case 23:   addStockByCode("sz399150");     break;
    case 24:   addStockByCode("sz399137");     break;
    case 25:   addStockByCode("sz399134");     break;
    case 26:   addStockByCode("sz399120");     break;
    case 27:   addStockByCode("sz399139");     break;
    case 28:   addStockByCode("sz399230");     break;
    case 29:   addStockByCode("sz399130");     break;
    case 30:   addStockByCode("sz399132");     break;
    case 31:   addStockByCode("sz399138");     break;
    case 32:   addStockByCode("sz399170");     break;
    case -1: 
      addStockByCode("sh000001,sz399001,sh000017,sh000010,sh000016,sz399101,sh000300,sz399004,sh000002,sh000003,sh000011,sz399002,sz399003");
      break; 
    case -2: 
      addStockByCode("sz399200,sz399190,sz399210,sz399150,sz399137,sz399134,sz399120,sz399139,sz399230,sz399130,sz399132,sz399138,sz399170"); 
      break; 

    default:  
  }


}

function  addStockByCode(code)
{
  if(stocks=="")
    stocks = code
  else
    stocks = stocks + "," + code ;
//  WriteIni("stocks",stocks);
  SetSetting( "stocks",stocks );
  SaveSetting("stocks");
  
  WriteIni("topID",1024);
  loadsetting();
  refreshData();
}

function addCustStock()
{
  frm = TForm.create(widget);
  frm.width = 230;
  frm.height = 160;
  frm.Position = poScreenCenter;
  frm.caption = '添加股票';
  frm.font.name='宋体';
  frm.font.size=9;
  frm.ShowHint=true;
  frm.BorderStyle=bsDialog;
    
  radiogp = TRadioGroup.create(widget);
  radiogp.parent=frm;
  radiogp.Caption='股票类型';
  radiogp.rc = Rect(10,10,205,50); 
  radiogp.columns = 2;
  radiogp.items.add("沪市");    
  radiogp.items.add("深市");
  radiogp.itemindex = 0;    

  var lb = TLabel.Create(widget);
  lb.parent = frm;
  lb.caption = "请输入股票代码:";
  lb.rc = Rect(15,70,205,50); 

  edtSt = TEdit.Create(widget);
  edtSt.parent = frm;
  edtSt.rc = Rect(110,66,105,30);
   
  btCan = TButton.Create(widget);
  btCan.parent = frm;
  btCan.caption = "取消";
  btCan.rc = Rect(141,95,75,25); 
  btCan.ModalResult = 2;
  btCan.cancel = true;
        
  btOK = TButton.Create(widget);
  btOK.parent = frm;
  btOK.caption = "确定";
  btOK.rc = Rect(55,95,75,25);
  //btOK.ModalResult = 1;
  btOK.OnClickEX = "btOKOnClick"; 

  if(frm.ShowModal==1)
  {
    var str = "";
    str = edtSt.text;
    if(radiogp.itemindex == 0)
      str = "sh" + str
    else
      str = "sz" + str;
    
    addStockByCode(str);  
  }
  frm.free;

}

function btOKOnClick(sender)
{
  if( trim(edtSt.text)=="" )
  {
    widget.alert("请输入股票代码");
    return;
  }

  var str = "";
  str = edtSt.text;
  if(radiogp.itemindex == 0)
    str = "sh" + str
  else
    str = "sz" + str;

  frm.caption = "正在检测请稍后..."; 
  getSinaStockAsync(str ,"onCheckGetTxt" );     
  
}
 
function onCheckGetTxt(fileTxt,errorCode)
{
  if((errorCode<1)||(fileTxt==""))
  {
    alert("连接服务器失败，请稍后重试");
    return;
  } 

  var n = fileTxt.indexOf('="";');
  if( n>0 )
  {
    frm.caption = "添加股票"; 
    alert("股票代码错误，请填写正确的股票代码.\r\n【提示】:请检查股票类型是否正确");
    return;
  }
  frm.ModalResult = 1;
} 



function hideBtDel()
{
  if(udbtDel.Visible)
  {
    udbtDel.Visible = false;
    udbtDel.tag = -1;
  }
  if(btDel.Visible)
  {
    btDel.Visible = false;
    btDel.tag = -1;
  }
}



function widgetOnStart()
{
  refreshData();
}





function udbtDelOnClick(sender)
{
  if( sender.tag < 0 ) return;
  var sl = stocks.split(",");
  sl.splice(sender.tag, 1);
  var s = "";
  for(var i=0;i<sl.length;i++)
  {
    if(sl[i]!="")
      s = s + sl[i] + ",";
  }
 
  if( s.substr(s.length-1,1)=="," );
    s = s.substr(0,s.length-1) ;
  stocks = s;
  
  if(stocks=="")
    clearAllItem();  
      
//  WriteIni("stocks",stocks);
  SetSetting( "stocks",stocks );
  SaveSetting( "stocks");

  /////////
  
  hideBtDel();
  loadsetting();
  refreshData();  
}

function delAllItem()
{
  stocks = "";
  clearAllItem();  
//  WriteIni("stocks",stocks);
  SetSetting( "stocks",stocks );
  SaveSetting( "stocks");

  /////////
  
  hideBtDel();
  loadsetting();
  refreshData();  

}


function txtStock0OnMouseEnter(sender)
{
  LockRefresh;
  
  if(sender.caption=="") return;
  btDel.tag = sender.tag;
  if(!btDel.Visible)
  {
    btDel.top = sender.Top ;
    btDel.Visible = true;
  }
  else if(btDel.top != sender.Top)
    btDel.top = sender.Top ;
    
  
  
  UnLockRefresh;  
}

function txtStock0OnMouseDown(sender)
{
//  
}

function txtStock0OnClick(sender)
{
  OpenLink( "http://finance.sina.com.cn/realstock/company/"+ sender.tagStr  +"/nc.shtml" ); 
}

function udbtChartOnClick(sender)
{

  showChart = !showChart;
  //SetSetting("showChart",showChart );
  //SaveSetting("showChart");
  if(showChart)
  {
    if( !fileExists( widgetPath + "chart\\" + curCode + ".gif" ) )
    {
      if(curCode=="")  
        curCode = udtxtState[0].tagStr;

      Print( curCode );
      
      if(curCode!="")  
        DownLoadFileAsync( "http://image.sinajs.cn/newchart/small/b"+ curCode +".gif?" + gettm() , widgetPath + "chart\\"+ curCode +".gif" ,"onDownChart2" );
    
    }
    else
      imgChart.Src = "chart\\" + curCode + ".gif";
  }
  

  
  setSkin();
}

function udtxtStock0OnClick(sender)
{
  OpenLink( "http://finance.sina.com.cn/realstock/company/"+ sender.tagStr  +"/nc.shtml" ); 
   
}

function  onDownChart(fileName,errorCode)
{
  if(errorCode<1)
  {
    udtxtTip.caption = "无法连接服务器";
    return; 
  }
  
  LockRefresh;
  
  udtxtTip.caption = tip;
  imgChart.Src = "";
  imgChart.Src = fileName;
  showChart = true;

  //SetSetting("showChart",showChart );
  //SaveSetting("showChart");

  
  UnLockRefresh;
  
  setSkin();  

}

function  onDownChart2(fileName,errorCode)
{
  if(errorCode<1)
  {
    udtxtTip.caption = "无法连接服务器";
    return; 
  }
  
  LockRefresh;
  
  udtxtTip.caption = tip;
  imgChart.Src = "";
  imgChart.Src = fileName;
  showChart = true;
  
  UnLockRefresh;

}


function gettm()
{
  return (new Date()).getTime();
}

function imgChartOnDbClick(sender)
{
  udbtChartOnClick(udbtChart);
}

function udtxtState0OnMouseEnter(sender)
{
  LockRefresh;
  
  if(sender.caption=="") return;
  udbtDel.tag = sender.tag;
  if(!udbtDel.Visible)
  {
    udbtDel.top = sender.Top ;
    udbtDel.Visible = true;
  }
  else if(udbtDel.top != sender.Top)
    udbtDel.top = sender.Top ;
    
  
  
  UnLockRefresh;  
}

function udtxtState0OnMouseDown(sender)
{
  imgSelect.top = sender.Top -1 ;
  imgSelect.Visible = true;
   
  curCode = sender.tagStr;
    
  if(!showChart)
    return;
    
  udtxtState0OnDbClick(sender);

}

function widgetOnLeave()
{
  hideBtDel();
}

function widgetOnDeActive()
{
  imgSelect.Visible = false;
  hideBtDel();
}

function udtxtState0OnDbClick(sender)
{
  curCode = sender.tagStr;

  udtxtTip.caption = "正在下载曲线图表，请稍后...";

  Print( curCode );
  
  DownLoadFileAsync( "http://image.sinajs.cn/newchart/small/b"+ curCode +".gif?" + gettm() , widgetPath + "chart\\"+ curCode +".gif" ,"onDownChart" ); 
  
}





function widgetOnPopupMenu()
{
  nMenu3.clear;
  
  custPopMenu.addMenuItemChild(nMenu3,"添加自定义股票...","addStockMenuClick",0);

  var pnt = custPopMenu.addMenuItemChild(nMenu3,"添加证劵指数","addStockMenuClick",-999);
  custPopMenu.addMenuItemChild(pnt,"添加以下全部证劵指数","addStockMenuClick",-1);
  custPopMenu.addMenuItemChild(pnt,"-","",-999);
  custPopMenu.addMenuItemChild(pnt,"添加 上证指数","addStockMenuClick",2);
  custPopMenu.addMenuItemChild(pnt,"添加 深证成指","addStockMenuClick",3);
  custPopMenu.addMenuItemChild(pnt,"添加 新综指","addStockMenuClick",4);
  custPopMenu.addMenuItemChild(pnt,"添加 上证180","addStockMenuClick",5);
  custPopMenu.addMenuItemChild(pnt,"添加 上证50","addStockMenuClick",6);
  custPopMenu.addMenuItemChild(pnt,"添加 中小板指","addStockMenuClick",7);
  custPopMenu.addMenuItemChild(pnt,"添加 沪深300","addStockMenuClick",8);
  custPopMenu.addMenuItemChild(pnt,"添加 深证100R","addStockMenuClick",9);
  custPopMenu.addMenuItemChild(pnt,"添加 A股指数","addStockMenuClick",10);
  custPopMenu.addMenuItemChild(pnt,"添加 B股指数","addStockMenuClick",11);
  custPopMenu.addMenuItemChild(pnt,"添加 基金指数","addStockMenuClick",12);
  custPopMenu.addMenuItemChild(pnt,"添加 成份A指","addStockMenuClick",13);
  custPopMenu.addMenuItemChild(pnt,"添加 成份B指","addStockMenuClick",14);
  
  var pnt = custPopMenu.addMenuItemChild(nMenu3,"添加行业指数","addStockMenuClick",-999);
  custPopMenu.addMenuItemChild(pnt,"添加以下全部行业指数","addStockMenuClick",-2);
  custPopMenu.addMenuItemChild(pnt,"-","",-999);
  custPopMenu.addMenuItemChild(pnt,"添加 地产指数","addStockMenuClick",20);
  custPopMenu.addMenuItemChild(pnt,"添加 金融指数","addStockMenuClick",21);
  custPopMenu.addMenuItemChild(pnt,"添加 服务指数","addStockMenuClick",22);
  custPopMenu.addMenuItemChild(pnt,"添加 建筑指数","addStockMenuClick",23);
  custPopMenu.addMenuItemChild(pnt,"添加 金属指数","addStockMenuClick",24);
  custPopMenu.addMenuItemChild(pnt,"添加 造纸指数","addStockMenuClick",25);
  custPopMenu.addMenuItemChild(pnt,"添加 采掘指数","addStockMenuClick",26);
  custPopMenu.addMenuItemChild(pnt,"添加 医药指数","addStockMenuClick",27);
  custPopMenu.addMenuItemChild(pnt,"添加 综企指数","addStockMenuClick",28);
  custPopMenu.addMenuItemChild(pnt,"添加 制造指数","addStockMenuClick",29);
  custPopMenu.addMenuItemChild(pnt,"添加 纺织指数","addStockMenuClick",30);
  custPopMenu.addMenuItemChild(pnt,"添加 机械指数","addStockMenuClick",31);
  custPopMenu.addMenuItemChild(pnt,"添加 IT指数","addStockMenuClick",32);   
}

function nMenu4OnClick()
{
  if(! widget.confirm2("确认删除所有股票?") )
    return;
  delAllItem();   
}

function widgetOnClick()
{
  if(stocks!="")
    imgSelect.Visible = true;
}