// JavaScript Document
try {
    document.execCommand("BackgroundImageCache", false, true)
} 
catch (e) {
}
function SortKanbanData(){
    var isAdvance = true;
    var sortMode = "0";
    BindKanbanData(isAdvance, '当天单位净值', sortMode);
}

function BindKanbanData(isAdvance, sort, sortMode){

	           
    var netValues = getKanbanNetValues();
    var tables = [];
    var tablesTemp = [];
    tables.push('<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#C8D7E8"><tr bgcolor="#F7FAFD" align="center">   <td rowspan="2" >当日排名</td>    <td rowspan="2" >基金代码</td><td rowspan="2">基金名称</td><td colspan="2">'+today+'</td><td colspan="2" bgcolor="#F7FAFD">'+yesterday+'</td><td rowspan="2" align="center">日增长值</td><td rowspan="2" align="center">日增长率</td><td height="19" colspan="3" align="center">回报率</td><td rowspan="2" >购买</td><td rowspan="2">赎回 </td> <td rowspan="2" bgcolor="#F7FAFD" >&nbsp;</td></tr><tr bgcolor="#F7FAFD" align="center"><td height="19">单位净值</td><td >累计净值</td><td >单位净值</td><td >累计净值</td><td>最近一周</td><td>最近一月</td><td>最近一年</td></tr>');
    var order = 1;
    for (var i = 0; i < netValues.length; i++) {
        if ((sort == "当天单位净值" || sort == "当天累计净值") && netValues[i][2] == 0) {
            PushTR2(i, "--", netValues, tablesTemp, isAdvance);
        }
        else 
            if ((sort == "增长值" || sort == "增长率") && (netValues[i][2] == 0 || netValues[i][4] == 0)) {
                PushTR2(i, "--", netValues, tablesTemp, isAdvance);
            }
            else 
                if ((sort == "单位净值" || sort == "累计净值") && netValues[i][4] == 0) {
                    PushTR2(i, "--", netValues, tablesTemp, isAdvance);
                }
                else {
                    PushTR2(i, order, netValues, tables, isAdvance);
                    order++;
                }
    }
    tables.push(tablesTemp.join(''));
    tables.push('</table>');
   // document.getElementById("kdivLock").innerHTML = tables.join('');
}

function PushTR2(i, order, netValues, tables, isAdvance){
    var dayColor, weekColor, monthColor, yearColor;
 
    dayColor = "";
	weekColor="";
	weekColor="";
	monthColor="";
	yearColor="";

        if (netValues[i][7] > 0) {
            dayColor = "style='color:red'";
        } else if (netValues[i][7] < 0) {
                dayColor = "style='color:green'";
            }
        if (netValues[i][14] > 0) {
            weekColor = "style='color:red'";
        } else if (netValues[i][14] < 0) {
                weekColor = "style='color:green'";
            }
         if (netValues[i][15] > 0) {
            monthColor = "style='color:red'";
        } else if (netValues[i][15] < 0) {
                monthColor = "style='color:green'";
            }
         if (netValues[i][16] > 0) {
            yearColor = "style='color:red'";
        } else if (netValues[i][16] < 0) {
                yearColor = "style='color:green'";
            }			
			   
    disValue = netValues[i][6];
    disValueColor = "";
    if (disValue) {
        if (disValue > 0) {
            disValueColor = "style='color:red'";
        }
        else 
            if (disValue < 0) {
                disValueColor = "style='color:green'";
            }
    }
    
    tables.push('<tr bgcolor="#FFFFFF" type="code" code="' + netValues[i][0] + '">');
    tables.push('<td height="30" title="当日排名">' + (netValues[i][7] == 10000 ? "--" : netValues[i][17]) + '</td>');
    tables.push('<td title="基金代码"><a target="_blank" href="http://supermarket.jjmmw.com/main/view/fundcode/' + netValues[i][0] + '.shtml">' + netValues[i][0] + '</a></td>');
    tables.push('<td title="基金名称"><a target="_blank" href="http://supermarket.jjmmw.com/main/view/fundcode/' + netValues[i][0] + '.shtml">' + netValues[i][1] + '</td>');
    tables.push("<td title='最新单位净值'>" + (netValues[i][2] == 10000 ? "--" : FormatNumber2(netValues[i][2], 4)) + "</td>");
    tables.push("<td title='最新累计净值'>" + (netValues[i][2] == 10000 ? "--" : FormatNumber2(netValues[i][3], 4)) + "</td>");
    tables.push("<td title='上个交易日单位净值' style='color: #666'>" + (netValues[i][4] == 10000 ? "--" : FormatNumber2(netValues[i][4], 4)) + "</td>");
    tables.push("<td title='上个交易日累计净值' style='color: #666'>" + (netValues[i][4] == 10000 ? "--" : FormatNumber2(netValues[i][5], 4)) + "</td>");
    tables.push("<td title='日增长值' " + dayColor + ">" + (netValues[i][2] == 10000  || netValues[i][6] == 10000 ||netValues[i][6] == null ? "--" : FormatNumber2(netValues[i][6], 4)) + "</td>");
    tables.push("<td title='日增长率' " + dayColor + ">" + (netValues[i][2] == 10000  || netValues[i][7] == 10000 || netValues[i][7] == null ? "--" : (FormatNumber2(netValues[i][7]*100, 2) + "%")) + "</td>");
    tables.push("<td title='最近一周回报率' " + weekColor + ">" + (netValues[i][14] == 10000  ? "--" : (FormatNumber2(netValues[i][14]*100, 2) + "%")) + "</td>");
    tables.push("<td title='最近一月回报率' " + monthColor + ">" + (netValues[i][15] == 10000  ? "--" : (FormatNumber2(netValues[i][15]*100, 2) + "%")) + "</td>");
    tables.push("<td title='最近一年回报率' " + yearColor + ">" + (netValues[i][16] == 10000  ? "--" : (FormatNumber2(netValues[i][16]*100, 2) + "%")) + "</td>");
    
    var buyTip = "", buyHref = "";
    if (netValues[i][9] != "") {
        buyTip = "title='" + netValues[i][9] + "'  class='xiaodian'";
    }
    else {
        buyTip = "class='notipCss'";
    }
    
    if (netValues[i][10] != "") {
        buyHref = "/LSGG_C_" + netValues[i][10] + "_" + netValues[i][0] + ".aspx";
    }
    if (buyHref != "") {
        tables.push("<td " + buyTip + ">" +  "<font" + (netValues[i][8] == 0 ? " color='red'" : " color='#000000'")+">"  + (netValues[i][8] == 1 ? "开放" : netValues[i][8] == 0 ? "暂停" : "--") + "</font>" + "</td>");
    }
    else {
        tables.push("<td " + buyTip + ">" + "<font" + (netValues[i][8] == 0 ? " color='red'" : " color='#000000'")+">"  + (netValues[i][8] == 1 ? "开放" : netValues[i][8] == 0 ? "暂停" : "--")+ "</font>" + "</td>");
    }
    
    var redeemTip = "", redeemHref = "";
    if (netValues[i][12] != "") {
        redeemTip = "title='" + netValues[i][12] + "' class='tipCss'";
    }
    else {
        redeemTip = "class='notipCss'";
    }
    
    if (netValues[i][13] != "") {
        redeemHref = "/LSGG_C_" + netValues[i][13] + "_" + netValues[i][0] + ".aspx";
    }
    if (redeemHref != "") {
        tables.push("<td " + redeemTip + ">" + "<font" + (netValues[i][11] == 0 ? " color='red'" : " color='#000000'")+">" + (netValues[i][11] == 1 ? "开放" : netValues[i][11] == 0 ? "暂停" : "--") + "</font>" + "</td>");
    }
    else {
        tables.push("<td " + redeemTip + ">" +"<font" + (netValues[i][11] == 0 ? " color='red'" : " color='#000000'")+">"+ (netValues[i][11] == 1 ? "开放" : netValues[i][11] == 0 ? "暂停" : "--") + "</font>" +"</td>");
    }
    
    if (isAdvance) {
        var week, weekColor = "", month, monthColor = "", quarter, quarterColor = "", half, halfColor = "", year, yearColor = "";
        week = netValues[i][8];
        month = netValues[i][9];
        quarter = netValues[i][10];
        half = netValues[i][11];
        year = netValues[i][12];
        if (week) {
            if (week > 0) {
                weekColor = "style='color:red'";
            }
            else 
                if (week < 0) {
                    weekColor = "style='color:green'";
                }
        }
        if (month) {
            if (month > 0) {
                monthColor = "style='color:red'";
            }
            else 
                if (month < 0) {
                    monthColor = "style='color:green'";
                }
        }
        if (quarter) {
            if (quarter > 0) {
                quarterColor = "style='color:red'";
            }
            else 
                if (quarter < 0) {
                    quarterColor = "style='color:green'";
                }
        }
        if (half) {
            if (half > 0) {
                halfColor = "style='color:red'";
            }
            else 
                if (half < 0) {
                    halfColor = "style='color:green'";
                }
        }
        if (year) {
            if (year > 0) {
                yearColor = "style='color:red'";
            }
            else 
                if (year < 0) {
                    yearColor = "style='color:green'";
                }
        }
        
    }
    tables.push('<td>');
    tables.push('<a href="http://supermarket.jjmmw.com/main/fund/history/'+ netValues[i][0]+'/'+netValues[i][0]+'.shtml" title="历史净值查询" target="_blank" class="blue">走势图</a>');
    tables.push('&nbsp;<a target="_blank" href="http://surpass.jjmmw.com/cgi-bin/sailipper/SaiLevel?function=queryCommonResult&fundcode=' + netValues[i][0] + '">评级</a>');
    tables.push('&nbsp;<a href="#"><img src="new_web/images/delete.gif" code="'+netValues[i][0]+'" onclick="delItem(this);" alt="删除此基金" width="12" height="12" /></a>');
    tables.push('</td>');
    tables.push("</tr>");
}

function FormatNumber2(srcStr, nAfterDot){
    var srcStr, nAfterDot;
    var resultStr, nTen;
    srcStr = "" + srcStr + "";
    strLen = srcStr.length;
    dotPos = srcStr.indexOf(".", 0);
    if (dotPos == -1) {
        resultStr = srcStr + ".";
        for (i = 0; i < nAfterDot; i++) {
            resultStr = resultStr + "0";
        }
        return resultStr;
    }
    else {
        if ((strLen - dotPos - 1) >= nAfterDot) {
            nAfter = dotPos + nAfterDot + 1;
            nTen = 1;
            for (j = 0; j < nAfterDot; j++) {
                nTen = nTen * 10;
            }
            resultStr = Math.round(parseFloat(srcStr) * nTen) / nTen;
            return resultStr;
        }
        else {
            resultStr = srcStr;
            for (i = 0; i < (nAfterDot - strLen + dotPos + 1); i++) {
                resultStr = resultStr + "0";
            }
            return resultStr;
        }
    }
}

function delItem(obj){
	var code = obj.getAttribute('code');
	var kanbanCodes = GetCookie('kanbanCodes');
	
	var sign = "0";
	var codesArry = kanbanCodes.split(";");
	for (var k = 0; k < codesArry.length; k++) {
	
		if (codesArry[k] == code) {
			delete codesArry[k];
		}
	}

		kanbanCodes=codesArry.join(";");
SetCookie('kanbanCodes',kanbanCodes);
	if(kanbanCodes==null)alert("你好没有设置看板的基金！");

	SortKanbanData();
}
function addItem(fundcode){
	var kanbanCodes=GetCookie('kanbanCodes');
	
	if (kanbanCodes==null){
		SetCookie('kanbanCodes', fundcode);
		SortKanbanData();	
		return;
	}else if(kanbanCodes.indexOf(fundcode) == -1){
			kanbanCodes = kanbanCodes + ";" + fundcode;
		SetCookie('kanbanCodes', kanbanCodes);
		SortKanbanData();
		return;	
	}
}
function getKanbanNetValues(){
		var codes=GetCookie('kanbanCodes');
		var kanbanValues = [];
		if (codes != null) {
			var codesArry = codes.split(";");
			var ifm = document.getElementsByTagName("iframe")[0];
			var netValues = ifm.contentWindow.getmainContent();
			for (var n = 0; n < codesArry.length; n++) {
				for (var k = 0; k < netValues.length; k++) {
					if (codesArry[n] == netValues[k][0]) {
						kanbanValues.push(netValues[k]);
					}
				}
				
			}
		}
		return kanbanValues;
		
}
function changeDisplay(obj,obj2){
	
	    if (obj.style.display == "none") {
        obj.style.display = "";
		changeIamge(obj2,'n');
    }else if(obj.style.display == ""){
		changeIamge(obj2,'y');
		obj.style.display = "none";
	}
}
function changeIamge(obj, sign){
	if (sign == 'y') {
		obj.src = "new_web/images/tt2.gif";
	}else if(sign == 'n'){
		obj.src = "new_web/images/tt1.gif";
	}
	
}

