﻿var sumfind=0;
var maxfind=8;
function find()
{
    var  link_color2 = "#FF0000",
    layer_background_color2 = "#FFFFFF",
    layer_hover_color2 = "#FFF3F3",
    title_text_color2 = "#FF0000",
    ad_text_color2 = "#333333",
    link_text_color2 = "#FF0000";

    //add style
    var stylev=[["A.vLink8888:link","color:"+link_color2+";border-bottom: none;cursor: hand;"],["A.vLink8888:visited","color:"+link_color2+";border-bottom: none;cursor: hand;"],["A.vLink8888:hover","color:"+link_color2+";cursor: hand;"],["A.vLink8888:active","color:"+link_color2+";border-bottom: none;cursor: hand;"]];
    if(!document.styleSheets.length){document.createStyleSheet()}
    var sheetv=document.styleSheets[0];
    var bIEv=(navigator.appName.indexOf("Microsoft")!=-1);
    if(bIEv){for(var i=0;i<stylev.length;i++){sheetv.addRule(stylev[i][0],stylev[i][1])}}
    else{for(var i=0;i<stylev.length;i++){sheetv.insertRule(stylev[i][0]+" { "+stylev[i][1]+" } ",1)}}

    var txt= document.getElementById("artibody").outerHTML;
    var str="";
    if(stockList)
    {
        var strStockList=stockList;
        var arrData=strStockList.split('@');
        for(var i=0;i<arrData.length&&arrData[i]!="";i++)
        {
            if(sumfind<=maxfind)
            {
                var arr=arrData[i].split('{|}');
                txt=changeoper(txt,arr);
            }
            else
            {
                document.getElementById("artibody").outerHTML=txt;
                return;
            }
        }
        document.getElementById("artibody").outerHTML=txt;
    }
   // alert(document.getElementById("artibody").innerHTML);
  //  alert(txt);
}


function changeoper(txt,arr)
{
    txt=replaceStock(txt,arr[0],arr[0]);
    txt=replaceStock(txt,arr[1],arr[0]);
    return txt;
}

function replaceStock(txt,stockstr,code)
{    
    var re2 =new RegExp("<([^>]*)>","gi");
    txt2=txt.replace(re2,"arr2[]");       
    if(txt2.indexOf(stockstr)!=-1)
    {        
        sumfind+=1;
        var str="<a class=\"vLink8888\"  href=\"http://stock.secon.cn/HtmlPages/"+code+".htm\" target=\"_blank\">"+stockstr+"</a>";    
        
        //HTML标签
        var re =new RegExp("<(.[^>]*)>","gi");
        var arr=txt.match(re);
        if(arr!=null)
        {
            for(var i=0;i<arr.length;i++)
            {
               txt=txt.replace(arr[i],"arr["+i+"]");
            }
        }

        //替换不在a标签其他的所有
        if(txt.indexOf(stockstr)!=-1)
        {
            var re2=new RegExp(stockstr,"gi");
            txt=txt.replace(re2,str);            
        }
        
        //替换原来在A标签里的内容   
        if(arr!=null)
        {
            for(var i=0;i<arr.length;i++)
            {
                txt=txt.replace("arr["+i+"]",arr[i]);
            }
        }
    }
    return txt;
}