
// 简单查询
function GetQueryData(num)
{
	var sqlFilter = GetSimpleSqlFilter();
		    
	var page = parseInt(document.getElementById("pagenum").value);
	
	page = page + num;
		
	if(page > 0) list.GetServerData(sqlFilter, page, GetServerData_CallBack);

	// set the current value
	document.getElementById("pagenum").value = page;
}

// 简单查询回调函数
function GetServerData_CallBack(response)
{	
	var result = response.value;
	
	var page = parseInt(document.getElementById("pagenum").value);
	
	if(result != null)
	{										
		document.getElementById('tablebox02').innerHTML =  result;
		
		document.getElementById("pagelist").innerHTML = GetPageList(page);
	}
}

// 翻页
function GetPageList(page)
{
	var PAGESIZE = 4;
	
	var rowCount = document.getElementById("hidRowCount").value;
	
	var pageCount = parseInt(rowCount / PAGESIZE, 0);
	
	if(rowCount % PAGESIZE != 0 ) pageCount = pageCount + 1;
	
	var pageList = "";
							
	// 第一页
	if ((page == 1) && (page < pageCount))
	{
		pageList = pageList + " <img src='img/btn_previous.gif' alt='上一页' /> <a href='#' onclick='GetQueryData(1)' target='_self'><img src='img/btn_next.gif' alt='下一页' /></a> ";
	}
	
	// 页码大于1并且页码小于总页数
	if ((page > 1) && (page < pageCount))
	{
		pageList = pageList + "<a href='#' onclick='GetQueryData(-1)' target='_self'><img src='img/btn_previous.gif' alt='上一页' /></a>  <a href='#' onclick='GetQueryData(1)' target='_self'><img src='img/btn_next.gif' alt='上一页' /></a> ";
	}
	
	// 页码等于页数并且页码大于1
	if ((page == pageCount) && (page > 1))
	{
		pageList = pageList + "<a href='#' onclick='GetQueryData(-1)' target='_self'><img src='img/btn_previous.gif' alt='上一页' /> </a>  <img src='img/btn_next.gif' alt='上一页' />";
	}
		
	return pageList;
}

// 获取查询过滤条件
function GetSimpleSqlFilter()
{
	var sql = "";
	
	var bankName = document.getElementById("bankName").value;
    
    var cashType = document.getElementById("cashType").value;
    
    var highYield = document.getElementById("highYield").value;
    
    var hxScore = document.getElementById("hxScore").value;
    
    var productName = document.getElementById("productName").value;
    
    var isEnd = document.getElementById("isEnd").value;
  
    if(bankName != null && bankName != "-1") sql += "fld_bank='" + bankName + "' AND ";
    
    if(cashType != null && cashType != "-1") sql += "fld_cashtype='" + cashType + "' AND ";
    
    if(highYield != null && highYield != "-1") sql += "fld_highYield=' " + highYield + "' AND ";
    
    if(hxScore != null && hxScore != "-1") sql += "fld_score='" + hxScore + "' AND ";
    
    if(productName != null && productName != '') sql += "fld_bankname like %' " + productName + "'% AND ";
 
    if(isEnd != null && isEnd != -1)
    {
		if(isEnd == 1)
		{
			sql += "fld_issenddate < getdate() AND ";
		}
		else if(isEnd == 0)
		{
			sql += "fld_issenddate >= getdate() AND ";
		}
		
    }
    
    var endPos = sql.lastIndexOf("AND");
    
    if(endPos == -1)
    {
		//alert('你还没有选择,不能查询');
		
		return -1;
    }
    else
    {
		sql = sql.substring(0, endPos);
		return sql;
	}
}

// 清除所有选项
function ClearAllCheckOption()
{
	var coll = document.getElementsByTagName("INPUT");
	
	for (var i = 0; i <coll.length; i++)
	{
		if (coll[i].type == "checkbox" && coll[i].checked)
		{
			coll[i].checked = false;
		}
	}
}

// 检查时候选择
function Check_OnSubmit()
{
	var bankName = document.getElementById("bankname").value;
    
    var cashType = document.getElementById("cashtype").value;
    
    var highYield = document.getElementById("highyield").value;
    
    var hxScore = document.getElementById("hxscore").value;
    
    var productName = document.getElementById("productname").value;
    
    var isEnd = document.getElementById("isend").value;
    
    if(bankName == "-1" && cashType == "-1" && highYield == "-1" && hxScore == "-1" && isEnd == "-1" && productName == "") 
    {
		alert('你还没有选择选项!');
		
		return false;
    }
    
   window.document.Form1.submit();
}


// 最多可以选择5项产品比较
function CompareProduct(value)
{
	var coll = document.getElementsByTagName("INPUT");
	
	var url = "compare.aspx?id=";
	
	var count = 0;
	
	var fld_ids = "";
	
	for (var i = 0; i < coll.length; i++)
	{
		if ((coll[i].type == "checkbox") && coll[i].checked && (coll[i].name == value))
		{
			fld_ids += coll[i].value + ',';
			
			count++;			
		}
	}

	if(count > 0 && count < 5)
	{
		url += fld_ids;
		
		window.open(url);
	}
	else if (count >= 5)
	{
	   alert('你最多能选择4项产品比较!');
	   
	   return false;
	}
	else
	{
		alert('你还没有选择要比较的产品!');
		return false;
	}
}

//上一天
function showPreviousDay(num)
{
   var MonthFirstDay=new Date();
   
   var preDay = new Date(MonthFirstDay-86400000*num);
   
   var M = Number(preDay.getMonth()) + 1;
   
   return preDay.getFullYear() + "-" + M + "-" + preDay.getDate(); 
}

//今天
function showToDay()
{
var Nowdate = new Date();
 M = Number(Nowdate.getMonth()) + 1
return Nowdate.getYear() + "-" + M + "-"+Nowdate.getDate();
}

// 日期列表
function InitBeginEndDate()
{
	//InitDate("begindate");
	//InitDate("enddate");
	
	SetLinkByCashType('1');
}

// 日期列表
function InitDate(objID)
{		
	for(var i = 1; i < 100; i++)
	{
		var preDay = showPreviousDay(i-1);
		
		var opt = document.createElement("OPTION");

		opt.value = preDay;
		opt.text = preDay;
		document.getElementById(objID).options.add(opt);
	}
}

// 理财产品排行榜
function SetLinkByCashType(rcashType)
{
	// rcashType -1:全部产品 1-人民币 0-外币
	
	// 月度排行
	SetMonthLinkByType(rcashType);
	
	// 季度排行
	SetQuarterLinkByType(rcashType);
	
	// 半年度
	SetHalfLinkByType(rcashType);
	
	
	// 年度
	SetYearLinkByType(rcashType);
}

// 年度排行榜
function SetYearLinkByType(cashType)
{
	var Nowdate = new Date();
	var Y = Number(Nowdate.getFullYear());
	
	var yearSort = "年度排行榜：";
	
	// 本年度
	yearSort += "<a target='_self' href='rank.aspx?sdate=" + Y + "-01-01&edate=" + Y + "-12-" +  GetDays(Y, 12) +"&ct=" + cashType + "'>[" + Y + "年]</a>";
	
	yearSort += "<a target='_self' href='rank.aspx?sdate=" + (Y-1) + "-01-01&edate=" + (Y-1) + "-12-"+  GetDays(Y-1, 3) + "&ct=" + cashType + "'>[" + (Y-1) + "年]</a>";
	
	document.getElementById("yearSort").innerHTML = yearSort;
}

// 半年排行榜
function SetHalfLinkByType(cashType)
{
	var Nowdate = new Date();
	var Y = Nowdate.getFullYear();
	var M = Number(Nowdate.getMonth()) + 1;
	
	var halfSort = "半年排行榜：";
	
	// 只有上半年
	if(M <= 6)
	{
		halfSort += "<a target='_self' href='rank.aspx?sdate=" + Y + "-01-01&edate=" + Y + "-06-" + GetDays(Y, 6) + "&ct=" + cashType + "'>[" + Y + "年上半年]</a>" 
	}
	else
	{
		halfSort += "<a target='_self' href='rank.aspx?sdate=" + Y + "-01-01&edate=" + Y + "-06-" + GetDays(Y, 6) +"&ct=" + cashType + "'>[" + Y + "年上半年]</a>" 
		
		halfSort += "<a target='_self' href='rank.aspx?sdate=" + Y + "-07-01&edate=" + Y + "-12-" +  GetDays(Y, 3) +"&ct=" + cashType + "'>[" + Y + "年下半年]</a>" 
	}
	
	document.getElementById("halfSort").innerHTML = halfSort;
}

// 季度排行
function SetQuarterLinkByType(cashType)
{
	var num = GetCurrentQuarter();
	
	var quarterSort = "季度排行榜：";
	
	for(var i = 1; i <= num; i++)
	{
		quarterSort += "<a target='_self' href='rank.aspx?" + GetDateByQuarter(i) + "&ct=" + cashType + "'>[" + Y + "年第" + i + "季度]</a>";
	}
	
	document.getElementById("quarterSort").innerHTML = quarterSort;
}

// 月度排行榜
function SetMonthLinkByType(cashType)
{
	var Nowdate = new Date();
	Y = Nowdate.getFullYear();
	M = Number(Nowdate.getMonth()) + 1;
	
	var monthSort = "月度排行榜：";
	
	var startdate = "";
	var enddate = "";
	
	for(var k = 1; k < M; k++)
	{
		startdate = Y + "-" + k + "-01";
		enddate = Y + "-" + k + "-" +  GetDays(Y, k);
		monthSort += "<a target='_self' href='rank.aspx?sdate=" + startdate + "&edate=" + enddate + "&ct=" + cashType +"'>[" + Y + "年" + k + "月]</a>";
	}
	
	document.getElementById("monthSort").innerHTML = monthSort;
}

// 获取当前的季度值
function GetCurrentQuarter()
{
	var Nowdate = new Date();
	Y = Nowdate.getFullYear();
	M = Number(Nowdate.getMonth()) + 1;
	
	// 一季度
	if( M >= 1 && M <= 3)
	{
		return 1;
	}
	else if( M > 3 && M <= 6)
	{
		return 2;
	}
	else if( M > 6 && M <= 9)
	{
		return 3;
	}
	else if( M > 9 && M <= 12)
	{
		return 4;
	}
}

// 根据季度获取前后的时间段
function GetDateByQuarter(num)
{
	var Nowdate = new Date();
	Y = Nowdate.getFullYear();
	
	var startdate = "";
	var enddate = "";
	var retdate = "";
	
	// 一季度
	if( num == 1)
	{
		startdate = Y + "-01-01";
		enddate = Y + "-03-" + GetDays(Y, 3);
	}
	else if( num  == 2)
	{
		startdate = Y + "-04-01";
		enddate = Y + "-06-" +  GetDays(Y, 6);
	}
	else if( num == 3)
	{
		startdate = Y + "-07-01";
		endate = Y + "-09-" +  GetDays(Y, 9);
	}
	else if( num == 4)
	{
		startdate = Y + "-10-01";
		enddate = Y + "-12-" +  GetDays(Y, 12);
	}
	
	retdate = "sdate=" + startdate + "&edate=" + enddate;
	
	return retdate;
}

// 获取某年某月的天数
function GetDays(selYear, selMon)
{
	var d_start = new Date(selYear, selMon-1, 1);
	var d_end   = new Date(selYear, selMon, 1);
	var days = (d_end - d_start)/(1000 * 60 * 60 * 24);
	     
    return days;
}