﻿function SortData(obj)
{
	ClearTitle();
	var sortMode = "0";
	if (obj.getAttribute("order") != "") {	sortMode = obj.getAttribute("order"); }
	BindData(false, obj.title, sortMode);
	var title = obj.title.replace("当天", "");
	if (sortMode != "1") 
	{
		obj.setAttribute("order", "1"); 
		obj.innerHTML = title + "<img src='/Image/arrowDown.gif' align='absmiddle' border='0' />";
	}
	else
	{
		obj.setAttribute("order", "0");
		obj.innerHTML = title + "<img src='/Image/arrowUp.gif' align='absmiddle' border='0' />";
	}
//window.parent.ChangeMode(); 
}

function ClearTitle()
{
	var tableHeader = document.getElementById("tableHeader");
	var links = tableHeader.getElementsByTagName("A");
	for(var i=0; i<links.length; i++)
	{
		links[i].innerHTML = links[i].title.replace("当天", "");
	}
}

function sortbyCode(x, y) { if(x[0] < y[0]) { return 1; } else { return -1; }}
function sortbyName(x, y) { return y[1].localeCompare(x[1]); }
function sortbyCNV(x, y) { if(x[2] < y[2]) { return 1; } else { return -1; }}
function sortbyCTNV(x, y) { if(x[3] < y[3]) { return 1; } else { return -1; }}
function sortbyPNV(x, y) { if(x[4] < y[4]) { return 1; } else { return -1; }}
function sortbyPTNV(x, y) { if(x[5] < y[5]) { return 1; } else { return -1; }}
function sortbyDisValue(x, y) { if(x[6] < y[6]) { return 1; } else { return -1; }}
function sortbyPercent(x, y) { if(x[7] < y[7]) { return 1; } else { 	return -1; }}
function sortbyPricePercent(x, y) { if(x[8] < y[8]) { return 1; } else { 	return -1; }}

function BindData(isAdvance, sort, sortMode)
{
    var netValues = eval(document.getElementById("mainContent").innerHTML); 
	if (sort == "代码")
	{
		netValues.sort(sortbyCode);
	}
	if (sort == "名称")
	{
		netValues.sort(sortbyName);
	}
	if (sort == "当天单位净值")
	{
		netValues.sort(sortbyCNV);
	}
	if (sort == "当天累计净值")
	{
		netValues.sort(sortbyCTNV);
	}
	if (sort == "单位净值")
	{
		netValues.sort(sortbyPNV);
	}
	if (sort == "累计净值")
	{
		netValues.sort(sortbyPTNV);
	}
	if (sort == "增长值")
	{
		netValues.sort(sortbyDisValue);
	}
	if (sort == "增长率")
	{
		netValues.sort(sortbyPercent);
	}
	if (sort == "溢价率")
	{
		netValues.sort(sortbyPricePercent);
	}	
	if (sortMode == "1")
	{
		netValues.reverse();
	}
	var tables = [];
	var tablesTemp = [];
	tables.push('<table id="tblContent" onclick="tblCellClick(this, event);" class="tableBody" cellpadding="0" cellspacing="0" border="1" width="900px" bordercolor="#cccccc"><tr style="height: 1px">');
	tables.push('<th style="width: 50px;"></th><th style="width: 80px;"></th><th style="width: 150px;"></th><th style="width: 80px;"></th><th style="width: 80px;"></th><th style="width: 80px;"></th><th style="width: 80px;"></th><th style="width: 80px;"></th><th style="width: 80px;"></th><th style="width: 60px;"></th><th style="width: 70px;"></th></tr>');

	var order = 1;
	for(var i=0;i<netValues.length;i++)
	{
		if ((sort == "当天单位净值" || sort == "当天累计净值") && netValues[i][2] == 0)
		{
			PushTR(i, "--", netValues, tablesTemp, isAdvance);
		}
		else if ((sort == "增长值" || sort == "增长率") && (netValues[i][2] == 0 || netValues[i][4] == 0))
		{
			PushTR(i, "--", netValues, tablesTemp, isAdvance);
		}
		else if ((sort == "单位净值" || sort == "累计净值") && netValues[i][4] == 0)
		{
			PushTR(i, "--", netValues, tablesTemp, isAdvance);
		}
		else
		{
			PushTR(i, order, netValues, tables, isAdvance);
			order++;
		}
	}
	tables.push(tablesTemp.join(''));
	tables.push('</table>');
	document.getElementById("divLock").innerHTML = tables.join('');
}

function PushTR(i, order, netValues, tables, isAdvance)
{
	var percentColor,disValueColor,percent,disValue;
	percent = netValues[i][7];
	percentColor = "";
	if (percent)
	{
		if (percent > 0)
		{
			percentColor = "style='color:red'";
		}
		else if (percent < 0)
		{
			percentColor = "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 style="cursor:pointer" type="code" code="'+netValues[i][0]+'" onmouseout="changebg(this)" onmouseover="changebg1(this)">');
	tables.push("<td title='序号'>"+order+"</td>");
	tables.push('<td title="基金代码">' + netValues[i][0]+'</td>');
	tables.push('<td title="基金名称"><a target="_blank" href="/fundFiles/'+netValues[i][0]+'.html">' +netValues[i][1]+'</td>');
	tables.push("<td ttile='最新单位净值'>" +(netValues[i][2] == 0 ? "--" : FormatNumber(netValues[i][2], 4))+"</td>");
	tables.push("<td ttile='最新累计净值'>" +(netValues[i][2] == 0 ? "--" : FormatNumber(netValues[i][3], 4))+"</td>");
	tables.push("<td ttile='上周单位净值' style='color: #666'>" +(netValues[i][4] == -1985 ? "--" : FormatNumber(netValues[i][4], 4))+"</td>");
	tables.push("<td ttile='上周单位净值' style='color: #666'>" +(netValues[i][5] == -1985 ? "--" : FormatNumber(netValues[i][5], 4))+"</td>");
	tables.push("<td ttile='最新增长率' "+disValueColor+">" +(netValues[i][6] == -1985 || netValues[i][4] == 0 ? "--" : FormatNumber(netValues[i][6], 4))+"</td>");
	tables.push("<td ttile='最新增长值' "+percentColor+">" +(netValues[i][7] == -198500 || netValues[i][4] == 0 ? "--" : (FormatNumber(netValues[i][7], 2)+"%"))+"</td>");
	tables.push("<td ttile='溢价率'>" + (parseFloat(netValues[i][8]) == -1985 ? "--" :  (FormatNumber(netValues[i][8], 2)+"%")) +"</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 "+weekColor+">" +(netValues[i][8] != "--" ? (netValues[i][8]+"%") : netValues[i][8])+"</td>");
		tables.push("<td "+monthColor+">" +(netValues[i][9] != "--" ? (netValues[i][9]+"%") : netValues[i][9])+"</td>");
		tables.push("<td "+quarterColor+">" +(netValues[i][10] != "--" ? (netValues[i][10]+"%") : netValues[i][10])+"</td>");
		tables.push("<td "+halfColor+">" +(netValues[i][11] != "--" ? (netValues[i][11]+"%") : netValues[i][11])+"</td>");
		tables.push("<td "+yearColor+">" +(netValues[i][12] != "--" ? (netValues[i][12]+"%") : netValues[i][12])+"</td>");
	}
	tables.push('<td>');
	tables.push('<a target="_blank" href="/FundFiles/'+netValues[i][0]+'.html">详情</a>');
	tables.push('</td>');	
	tables.push("</tr>");
}



function DisplayOtherMode()
{
	var otherMode = document.getElementById("otherMode");
	if (otherMode.style.display == "none")
	{
		otherMode.style.display = "";
	}
	else
	{
		otherMode.style.display = "none";
	}
}
function hightMyFund(obj)
{
	var myCodes = obj.split(";");
	var tblContent = document.getElementById("tblContent");
	var trs = tblContent.getElementsByTagName("TR");
	for (var i=0;i<trs.length;i++)
	{
		if (trs[i].getAttribute("type") == "code")
		{
			trs[i].bgColor = "";
			for(var k=0;k<myCodes.length;k++)
			{
				if (myCodes[k] == trs[i].getAttribute("code"))
				{
					trs[i].bgColor = "#c6d7e9";
					break;
				}
			}
		}
	}
}

function SortListTableVarMode(text, value)
{
	var tblContent = document.getElementById("tblContent");
	var trs = tblContent.getElementsByTagName("TR");
	
	if( currCodes == null && value == "-1" ) value = "0";
	
	if (value == "0")
	{
		for (var i=0;i<trs.length;i++)
		{
			if (trs[i].getAttribute("type") == "code")
			{
				trs[i].style.display = "";
			}
		}
		
		currCodes = null;
	}
	else
	{
	    var codes;
	    if( value == "-1" )
	    {
	        codes = currCodes;
	    }
	    else
	    {
	        codes = value.split(";");
	        if( currCodes != null )
	        { 
	            codes = CombineArray(currCodes, codes); 
	        } 
	    }
		
		for (var i=0;i<trs.length;i++)
		{
			if (trs[i].getAttribute("type") == "code")
			{
				trs[i].style.display = "none";
				for(var k=0;k<codes.length;k++)
				{
					if (codes[k] == trs[i].getAttribute("code"))
					{
						trs[i].style.display = "";
						break;
					}
				}
			}
		}	
	}
}

function ModeState(obj)
{
	if (obj.className == "ModeLink")
	{
		obj.className = "ModeLinkOver";
	}
	else
	{
		obj.className = "ModeLink";
	}
}

var isLock = true;
function LockTitle(bool)
{
	isLock = bool;
	var divLock = document.getElementById("divLock");
	if (bool)
	{
		divLock.style.overflow = "auto";
	}
	else
	{
		divLock.style.overflow = "";
	}
}

function ResizeNetValueList(size)
{
	var divLock = null;
	if (document.getElementById)
	{
		divLock = document.getElementById("divLock");
	}
	else
	{
		eval('divLock = divLock;');
	}
	if (size == 1985)
	{
		divLock.style.height = "auto";
	}
	else if (size == -1985)
	{
		divLock.style.height = "266px";
	}
	else
	{
		var height = parseInt(divLock.style.height.replace("px", ""));
		height = height + size;
		if (height > 100)
		{
			divLock.style.height = height + "px";
		}
		SetCookie("NetValueListHeight", divLock.style.height);
	}
	window.parent.ResizeIframe();
}

var activedRow;
var activedCell;
var activedCellIndex;
function tblCellClick(tbl, evt)
{
//clear
	if (activedRow && activedRow.tagName == "TR")
	{
		activedRow.style.backgroundColor = "#FFFFFF";
	}

//set
	evt = evt ? evt : (window.event ? window.event : null);
	var activeCell = evt.srcElement ? evt.srcElement : evt.target;
	if (activeCell && activeCell.tagName == "TD")
	{
	//set row
		var srcRow = activeCell.parentNode;//parentElement;
		srcRow.style.backgroundColor = "#F0FFFF";
		
		var indexCell = activeCell.cellIndex;
		if (activedCellIndex != indexCell)
		{
			//clear
			if (activedCellIndex)
			{
				for(var i=1;i<tbl.rows.length;i++)
				{
					var td = tbl.rows[i].cells[activedCellIndex];
					if (td)
						td.style.backgroundColor = "";
				}
			}
		
			//set cells
			for(var i=1;i<tbl.rows.length;i++)
			{
				var td = tbl.rows[i].cells[indexCell];
				if (td)
					td.style.backgroundColor = "#F0FFFF";
			}
		}
		
		activedRow = srcRow;
		activedCell = activeCell;
		activedCellIndex = activedCell.cellIndex;
	}
}

function changebg(obj) { obj.style.backgroundColor = "#FFFFFF"; }
function changebg1(obj){ obj.style.backgroundColor = "#F0FFFF"; }

function FormatNumber(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;
　　}
　}
} 