/*分页 开始*/
var g_isInitSplitPage = false;
var g_pageSize = 10;
var g_nPage = 1;
var g_nTotalPage = 0;
var gMaxSplitPageDisplaySize = 5;
var aIdStr = new Array("td_split_page_previous_batch","td_split_page_previous","td_split_page_next","td_split_page_next_batch","td_split_page_index_");
function modifyTdStyleByHover(tObj,isOver)
{
	if(isOver)
	{
		tObj.initColor = tObj.style.backgroundColor;
		tObj.style.backgroundColor = "#0CF";
		tObj.style.color = "#fff";
	}
	else
	{		
		tObj.style.backgroundColor = tObj.initColor;
		tObj.style.color = "#000";
	}
}
//初始化分页表格
function initSplitPageTable(pObj,totalPage,nPage,getDataByPageIndexMehtodName,relativeTabId)
{
	if(totalPage < 1)
	{
		g_isInitSplitPage = false;
		return;
	}
	gMaxSplitPageDisplaySize = 5;
	pObj.innerHTML = '';
	var tab  = document.createElement("table");	
	tab.className = "splitPageTab";
	tab.border = "0";
	tab.cellPadding = "0";
	tab.cellSpacing = "0";
	
	var row = tab.insertRow();
	//row.style.backgroundColor = "#ffe8ad";
	if(relativeTabId != undefined && relativeTabId != "")
		initSplitPageNavBySize(totalPage,nPage,row,getDataByPageIndexMehtodName,relativeTabId);
	else
		initSplitPageNavBySize(totalPage,nPage,row,getDataByPageIndexMehtodName);
	pObj.appendChild(tab);
}
function initSplitPageNavBySize(pSize,pIndex,pRow,getDataByPageIndexMehtodName,relativeTabId)
{
	if(pSize < gMaxSplitPageDisplaySize)
		gMaxSplitPageDisplaySize = pSize;
	
	var index = 1;									

	var cell = pRow.insertCell();
	cell.id = aIdStr[0];
	cell.pindex = "1_"+pSize;	
	cell.onclick = function(){jumpNextOrPreviousSplitPageSign(this,false,true,aIdStr);};
	cell.onmouseover = function(){modifyTdStyleByHover(this,true);};
	cell.onmouseout = function(){modifyTdStyleByHover(this,false);};
	cell.innerHTML = "<<";
	
	cell = pRow.insertCell();
	cell.id = aIdStr[1];
	cell.pindex = "1_"+pSize;	
	cell.onclick = function(){jumpNextOrPreviousSplitPageSign(this,false,false,aIdStr);};
	cell.onmouseover = function(){modifyTdStyleByHover(this,true);};
	cell.onmouseout = function(){modifyTdStyleByHover(this,false);};
	cell.innerHTML = "<";
	var pCell = null;
	for(var i = 0; i < pSize; i++)
	{//alert(i + " < " + gMaxSplitPageDisplaySize + ",,pSize = " + pSize);
		if(i < gMaxSplitPageDisplaySize)
		{
			cell = pRow.insertCell();
			cell.id = aIdStr[4]+i;
			cell.onmouseover = function(){modifyTdStyleByHover(this,true);};
			cell.onmouseout = function(){modifyTdStyleByHover(this,false);};
			if(relativeTabId != undefined)
				cell.relativeTabId = relativeTabId;
			cell.onclick = function(){jumpToPageNoByPageNumber(this,getDataByPageIndexMehtodName);};
			cell.selected = false;
			cell.pindex = (i+1);	
			cell.innerHTML = (i+1);		
			if(g_nPage == i + 1)
				pCell = cell;	
		}
		else
		{
			break;
		}
	}
	var boderColor = "#ccc";
	cell = pRow.insertCell();
	cell.id = aIdStr[2];
	cell.pindex = "1_"+pSize;	
	cell.onclick = function(){jumpNextOrPreviousSplitPageSign(this,true,false,aIdStr);};
	cell.onmouseover = function(){modifyTdStyleByHover(this,true);}
	cell.onmouseout = function(){modifyTdStyleByHover(this,false);}
	cell.innerHTML = ">";
	
	cell = pRow.insertCell();
	cell.id = aIdStr[3];
	cell.pindex = "1_"+pSize;	
	cell.onclick = function(){jumpNextOrPreviousSplitPageSign(this,true,true,aIdStr);};
	cell.onmouseover = function(){modifyTdStyleByHover(this,true);}
	cell.onmouseout = function(){modifyTdStyleByHover(this,false);}
	cell.innerHTML = ">>";
	
	cell = pRow.insertCell();
	cell.style.cssText = "border:#ccc solid 0px;cursor:default;";
	cell.innerHTML = "&nbsp;";	
	
	cell = pRow.insertCell();
	cell.style.cssText = "cursor:default;border-color:"+boderColor+"!important;border-right:#ccc solid 0px!important;";
	var btnStr = '<input id="txt_split_page_no" type="text" style="width: 40px;margin-left:0.4em;text-align:center;margin-right:0.4em; font-size: 0.8em; border-top:#ccc solid 1px;border-left:#ccc solid 1px;border-bottom:#ccc solid 1px;border-right:#ccc solid 1px;" onkeypress="executeJumpToPageNo(this,&quot;'+getDataByPageIndexMehtodName+'&quot;)"/>';
	var cText = getLanTextByKey("T_JUMP_TO_PAGE_TITLE").replace("%1",btnStr);
	cell.innerHTML = cText.replace("%2",pSize);
	
	cell = pRow.insertCell();
	cell.style.cssText = "border-color:"+boderColor+"!important;border-left:#ccc solid 0px!important;";
	cell.innerHTML = '<a class="normalbtn" href="javascript:void(0);" onclick="jumpToPageNo(-1,&quot;'+getDataByPageIndexMehtodName+'&quot;);">'+getLanTextByKey("T_PAGE_OK_BUTTON")+'</a>';
	
	setTimeout(function (){if(pCell != null)setSelectedTdBk(pCell);},20);
}
function checkInputKeyCode(oControl)
{
	if (event.keyCode == 13) 
		jumpToPageNo(-1);
}
function executeJumpToPageNo(oControl,callBackethodName)
{
	if (event.keyCode == 13) 
	{
		jumpToPageNo(-1,callBackethodName);
	}
}

function setSelectedTdBk(obj)
{
	var index = obj.pindex; 
	var selectTd = null;//alert(index);
	for(var i = 0; i < gMaxSplitPageDisplaySize; i++)
	{		
		selectTd = document.getElementById(aIdStr[4]+ i);		
		selectTd.initColor = (selectTd.pindex == index)?"#cbf3fd":"#fff";
		//selectTd.bkColor = (selectTd.pindex == index)?"#cbf3fd":"#fff";
		selectTd.style.backgroundColor = (selectTd.pindex == index)?"#cbf3fd":"#fff";
		//console.log(selectTd.style.backgroundColor);
	}	
}
function setSelectTdSignBk(obj,index)
{
	var aParas = (obj.pindex).split("_");	
	var start = parseInt(aParas[0],10);
	var no = index - start + 1;
	
	for(var i = 0; i < gMaxSplitPageDisplaySize; i++)
	{		
		var selectTd = document.getElementById(aIdStr[4]+ i);
		selectTd.style.backgroundColor = (index == selectTd.pindex )?"#cbf3fd":"#fff";
		selectTd.initColor = (index == selectTd.pindex )?"#cbf3fd":"#fff";
	}
	
}
function jumpNextOrPreviousSplitPageSign(obj,isNext,isBatch,aIdStr)
{
	//console.log(obj+",obj.pindex="+obj.pindex+","+isNext+","+isBatch+","+aIdStr);
	var aParas = (obj.pindex).split("_");	
	var start = parseInt(aParas[0],10);
	var total = parseInt(aParas[1],10);
	var skipSpan = (isBatch)?gMaxSplitPageDisplaySize:1;
	var end = -1;
	
	if(isNext == true)
	{
		if((start + gMaxSplitPageDisplaySize + skipSpan -1) <= total)
		{
			end = start + gMaxSplitPageDisplaySize + skipSpan - 1;
			start = start + skipSpan;
		}
		else
		{
			if(start + gMaxSplitPageDisplaySize+1 == total)
			{
				start = -1;
				end = -1;	
				return;
			}
			else
			{
				end = total;
				start = total - gMaxSplitPageDisplaySize +1;
			}
		}
	}
	else if(isNext == -1)
	{
		if(start + gMaxSplitPageDisplaySize - 1 < total)
		{
			end = start + gMaxSplitPageDisplaySize - 1;
		}
		else
		{			
			end = total;
			if((total - gMaxSplitPageDisplaySize + 1) >= 0)
				start = total - gMaxSplitPageDisplaySize + 1;
		}
	}
	else
	{
		if(start <= 1)
		{
			start = -1;
			end = -1;	
			return;
		}
		else
		{
			if(start - skipSpan >= 0)
			{
				end = start + gMaxSplitPageDisplaySize - skipSpan -1;	
				start = start - skipSpan ;				
			}
			else
			{
				start = 1;
				end = skipSpan;	
			}
		}
	}
	
	for(var i = 0; i < aIdStr.length - 1; i++)
	{
		var td = document.getElementById(aIdStr[i]);
		td.pindex = start+"_"+total;
	}
	//alert("start:"+start + "=="+end);
	for(var i = start,j = 0; i <= end; i++,j++)
	{
		var td = document.getElementById(aIdStr[aIdStr.length - 1]+j);
		td.innerHTML = i;
		td.pindex = i;
	}	
	setSelectTdSignBk(obj,g_nPage);
}
var g_selectedCell = null;
function jumpToPageNoByPageNumber(cell,getDataByPageIndexMehtodName)
{
	cell.disabled = true;
	g_isInitListUI = true;
	g_selectedCell = cell;
	if(cell.relativeTabId != undefined)
		delTipsIdFromStockTips(cell.relativeTabId);
	delTipsIdFromStockTips("icon_edit");
	delTipsIdFromStockTips("icon_del");
	console.log(getDataByPageIndexMehtodName);
	console.log("=cell.pindex="+cell.pindex);
	jumpToPageNo(cell.pindex,getDataByPageIndexMehtodName);	
	setSelectedTdBk(cell);
}
function jumpToPageNo(pageIndex,getDataByPageIndexMehtodName)
{
	var obj = document.getElementById("td_split_page_previous_batch");
	var aParas = (obj.pindex).split("_");	
	var start = parseInt(aParas[0],10);
	var total = parseInt(aParas[1],10);
	if(pageIndex == -1)
	{
		var nPageIndex = parseInt(document.getElementById("txt_split_page_no").value,10);
		if(nPageIndex < 1)
			nPageIndex = 1;
		else if(nPageIndex > total)
			nPageIndex = 65535;
		g_nPage = nPageIndex;
		document.getElementById("txt_split_page_no").value = "";
	}
	else
		g_nPage = pageIndex;
	
	if(g_nPage > 0 & g_nPage < 6)
	{
		start = 1;		
	}
	else if(g_nPage > 5 & g_nPage < total - 4)
	{
		start = g_nPage;	
	}
	else if(g_nPage > total - 5 & g_nPage < total + 1)
		start = total - 4;
	else if(g_nPage > total)
		start = ((total - 4) > 0)?(total - 4):1;
	
	
	obj.pindex = start + "_" + total;
	if(pageIndex == -1)
		jumpNextOrPreviousSplitPageSign(obj,-1,false,aIdStr);
	var callBackMehod = "modifyAjaxPageUI();";
	if(checkMethodName(callBackMehod))
		eval(callBackMehod);
	eval(getDataByPageIndexMehtodName+'('+g_nPage+');');
}
/*分页 结束*/