﻿function dataGrid(){
	this.strTableStyle = "table";
	this.strHeaderRowStyle = "headerRow";
	this.strHeadCaptionStyle = "headerCaption";
	this.strFilterRowStyle = "filterRow";
	this.strOddRowStyle = "oddRow";
	this.strEvenRowStyle = "evenRow";
	this.markFilterStyle = "markFilter";
	
	this.strTableId = "";
	this.strRoot = "";
	this.strNameSpace = "";
	
	this.blnFilter = true;
	this.blnPaging = false;
	this.blnSort = false;
	this.blnPaging = false;
	
	this.strOverRowFunction = "";
	this.strOutRowFunction = "";
	this.strClickRowFunction = "";
	this.strGotoPageFunction = "";
	this.intRecordPage = 10;
	this.intPageCount = 5;
	this.intPage = 0;
	this.intTotalRecord = 0;
	//this.strPaging = new Array("|<","<<",">>",">|");
	
	this.strShowField = null;
	this.intType = null;
	this.strFilter = new Array();
	this.intSort = new Array();
	this.strHeaderCaption = null;
	this.intHeaderWidth = null;
	
	var intDataField = 0,intShowField = 0;
	
	//=========================================
	this.parseXML = function(pstrXML){
		var intCount;
		var objXML = loadXMLstr(pstrXML);
		var objPaging = objXML.getElementsByTagName("paging").item(0);
		var objItems = objXML.getElementsByTagName("item");
		
		this.intPage = parseInt(objPaging.getAttribute("page"));
		this.intTotalRecord = parseInt(objPaging.getAttribute("totalrecord"));
		//alert("Total record : " + this.intTotalRecord);
		return this.getTable(objItems);
	}
	//=========================================
	this.getTable = function(pobjItems){
		var strData = "",strRowData = "",intCount;
		strData = "<table cellspacing='2' cellpadding='2' border='1' bordercolor='#FFFFFF' " +
			"width='100%'" + " id='" + this.strTableId + "' class='" + this.strTableStyle + "'><tbody>";
		if(pobjItems.length > 0)
			for(intCount = 0;intCount < pobjItems.length;intCount++)
				strRowData += this.getRow(intCount,pobjItems.item(intCount));
		else
			strRowData = "<tr><td colspan="+ this.strHeaderCaption.length +">&nbsp;</td></tr>";
		strData += this.getHeader() + this.getFilter() + 
			strRowData + this.getRule() + this.getPaging() + "</tbody></table>";
		return strData;
	}
	//=========================================
	this.getRule = function(){
		var strData = "",intCount;
		for(intCount = 0;intCount < this.intHeaderWidth.length;intCount++)
			strData += "<th><img src='space.gif' width='" + 
				this.intHeaderWidth[intCount] + "px' height='0px'/></th>";
		return "<tr id=row" + this.strNameSpace + "rule " + 
			"height=0px class='ruleRow'>" + strData + "</tr>";
	}
	//=========================================
	this.getHeader = function(){
		var strData = "",intCount;
		for(intCount = 0;intCount < this.strHeaderCaption.length;intCount++)
			strData += "<td class='" + this.strHeadCaptionStyle + "'>" + 
				this.strHeaderCaption[intCount] + "</td>";
		return "<tr id=row" + this.strNameSpace + "header " + 
			"class='" + this.strHeaderRowStyle + "'>" + strData + "</tr>";
	}
	//=========================================
	this.getFilter = function(){
		if(this.blnFilter == false) 
			return "";
		var strData = "",intCount;
		for(intCount = 0;intCount < this.strHeaderCaption.length;intCount++)
			if(this.strHeaderCaption[intCount] != "")
				strData += "<td align=center><input id=txt" + this.strNameSpace + 
					this.strShowField[intCount] + "Filter type=text " + 
					"value='" + this.strFilter[intCount] + "' size=1 style='width:100%'></td>";
			else
				strData += "<td>&nbsp;</td>";
		return "<tr id=row" + this.strNameSpace + "filter class=filterRow>" + strData + "</tr>"
	}
	//=========================================
	//this.getSort = function(){
	//	if(this.blnSort == false) 
	//		return "<tr><td class=sortRow colspan=" + this.strHeaderCaption.length + ">&nbsp;</td></tr>";
	//	var strData = "",intCount;
	//	for(intCount = 0;intCount < this.strHeaderCaption.length;intCount++)
	//		if(this.strHeaderCaption[intCount] != "")
	//			strData += "<td align=center><select id=txt" + this.strNameSpace + 
	//				this.strShowField[intCount] + "Sort type=text " + 
	//				"value='" + this.strFilter[intCount] + "' size=1 style='width:95%'>" + 
	//				"<option value='0'></option>" +
	//				"<option value='1'>A - Z</option>" +
	//				"<option value='-1'>Z - A</option>" +
	//				"</td>";
	//		else
	//			strData += "<td>&nbsp;</td>";
	//	return "<tr id=row" + this.strNameSpace + "filter class=sortRow>" + strData + "</tr>"
	//}
	//=========================================
	this.getPaging = function(){
		if(this.blnPaging == false) 
			return "<tr class=pagingRow><td colspan=" + this.strHeaderCaption.length + ">&nbsp;</td></tr>";
		var strData = "";
		strData = "<td align=left colspan=" + this.strHeaderCaption.length + ">" + 
			getPaging(this.intTotalRecord,this.intPage,this.intPageCount,this.intRecordPage,
			this.strGotoPageFunction) + "</td>";
		return "<tr id=row" + this.strNameSpace + "paging class=pagingRow>" + strData + "</tr>"
	}
	//=========================================
	this.getRow = function(pintNumber,pobjNode){
		var strData = "",intCount,strItemData = "",strTempRowData = "",strTempNodeData;
		for(intCount = 0;intCount < this.strHeaderCaption.length;intCount++)
			if(this.strHeaderCaption[intCount] != ""){
				strTempNodeData = pobjNode.getAttribute(this.strShowField[intCount]);
				strTempNodeData = decodeHtml(strTempNodeData);
				if((parseInt(this.intType[intCount]) == 2) || (parseInt(this.intType[intCount]) == 3)){
					if(parseInt(this.intType[intCount]) == 2)
						strTempNodeData = numberFormat(strTempNodeData);
					else
						strTempNodeData = numberFormatVN(strTempNodeData);
					strTempRowData += "<td align=right>";
				}
				else
					strTempRowData += "<td align=left>";
				if(this.strFilter[intCount] != "")
					strTempRowData += this.markFilter(strTempNodeData,this.strFilter[intCount]) + "</td>";
				else
					strTempRowData += strTempNodeData + "</td>";
			}
			else
				strTempRowData += "<td align=left>&nbsp;</td>";
		strItemData += this.getHidden(pintNumber,pobjNode);
		
		strData = "<tr id=row" + this.strNameSpace + "data" + (pintNumber + 1) + 
			" onmouseover='" + this.strOverRowFunction + "' " + 
			"onmouseout='" + this.strOutRowFunction + "' " + 
			"onclick='" + this.strClickRowFunction + "' ";
		if((pintNumber % 2) == 0)
			strData += "class='" + this.strOddRowStyle + "'>" + strTempRowData + strItemData + "</tr>";
		else
			strData += "class='" + this.strEvenRowStyle + "'>" + strTempRowData + strItemData + "</tr>";

		return strData;
	}
	//=========================================
	/*
	this.getRow = function(pintNumber,pobjNode){
		var strData = "",intCount,strItemData = "",strTempRowData = "";
		for(intCount = 0;intCount < this.strHeaderCaption.length;intCount++)
			if(this.strHeaderCaption[intCount] != "")
				if(this.strFilter[intCount] != "")
					strTempRowData += "<td align=left>" + 
						this.markFilter(pobjNode.getAttribute(this.strShowField[intCount]),
						this.strFilter[intCount]) + "</td>";
				else
					strTempRowData += "<td align=left>" + 
						pobjNode.getAttribute(this.strShowField[intCount]) + "</td>";
			else
				strTempRowData += "<td align=left>&nbsp;</td>";
		strItemData += this.getHidden(pintNumber,pobjNode);
		
		strData = "<tr id=row" + this.strNameSpace + "data" + (pintNumber + 1) + 
				" onmouseover='" + this.strOverRowFunction + "' " + 
				"onmouseout='" + this.strOutRowFunction + "' " + 
				"onclick='" + this.strClickRowFunction + "' ";
		if((pintNumber % 2) == 0)
			strData += "class='" + this.strOddRowStyle + "'>" + strTempRowData + strItemData + "</tr>";
		else
			strData += "class='" + this.strEvenRowStyle + "'>" + strTempRowData + strItemData + "</tr>";

		return strData;
	}
	*/
	//=========================================
	this.getHidden = function(pintNumber,pobjNode){
		var intCount,strData = "";
		pintNumber++;
		for(intCount = 0;intCount < pobjNode.attributes.length;intCount++)
			strData += "<input type=hidden id='txt" + this.strNameSpace + "f" + intCount +
				pintNumber + "' value='" + pobjNode.attributes[intCount].value + "'>";
		return strData;
	}
	//=========================================
	this.markFilter = function(pstr,pstrFilter){
		var objRegExp = new RegExp(pstrFilter,"gi");
		return pstr.replace(objRegExp,"<span class='" + 
			this.markFilterStyle + "'>" + pstrFilter + "</span>");
	}
}
//======================================================================================================================
var strFirst = "",strPrevious = "",strNext = "",strLast = "",strPage = "";
var strFirstKey = "f",strPreviousKey = "p",strNextKey = "n",strLastKey = "l";
//===================================================
function getPaging(pintTotalRecord,pintPage,pintPageCount,pintRecordPage,pstrGOTOfunction){
	if(pintTotalRecord < pintRecordPage) return "";
	
	var intPagegroup,intLastPagegroup,blnLastPagegroup,intTotalPage;
	var strTemp = "",intCount;
	pagingInit(1);//style
	
	if((pintTotalRecord % pintRecordPage) == 0)
		intTotalPage = parseInt(pintTotalRecord/pintRecordPage);
	else
		intTotalPage = parseInt(pintTotalRecord/pintRecordPage) + 1;
	
	if(intTotalPage == 1) return "";
	
	//nhom trang hien tai
	if((pintPage % pintPageCount) == 0)
		intPagegroup = parseInt(pintPage/pintPageCount);
	else
		intPagegroup = parseInt(pintPage/pintPageCount) + 1;
	//nhom trang cuoi
	if((intTotalPage % pintPageCount) == 0)
		intLastPagegroup = parseInt(intTotalPage/pintPageCount);
	else
		intLastPagegroup = parseInt(intTotalPage/pintPageCount) + 1;
	//hien tai la nhom trang cuoi 
	if(intPagegroup == intLastPagegroup)
		blnLastPagegroup = true;
	else
		blnLastPagegroup = false;
	for(intCount = 1;intCount <= pintPageCount;intCount++){
		if(parseInt(((intPagegroup - 1)*pintPageCount + intCount)) == parseInt(pintPage))
			strTemp = strTemp + parseInt((intPagegroup - 1)*pintPageCount + intCount) +
				"&nbsp;&nbsp;";
		else
			strTemp = strTemp + "<a href='javascript:" + pstrGOTOfunction + "(" +  
				parseInt((intPagegroup - 1)*pintPageCount + intCount) + ")' class=paging>" +
				parseInt(((intPagegroup - 1)*pintPageCount + intCount)) + "</a>&nbsp;&nbsp;";
		if(parseInt(((intPagegroup - 1)*pintPageCount + intCount)) == parseInt(intTotalPage)) break;
		//alert("in for" + strTemp);
	}
	if(pintPage > 1) strTemp = "<a onfocus='" + pstrGOTOfunction + "(" + parseInt(pintPage - 1) +
		")' href='javascript:" + pstrGOTOfunction + "(" + parseInt(pintPage - 1) +
		")' accesskey=" + strPreviousKey + " class=paging>" + strPrevious + "</a>&nbsp;&nbsp;" + strTemp;
	
	if(pintPage > pintPageCount) strTemp = "<a onfocus='" + pstrGOTOfunction + "(1)' " +
		"href='javascript:" + pstrGOTOfunction + "(1)' " + 
		" accesskey=" + strFirstKey + " class=paging>" + strFirst + "</a>&nbsp;&nbsp;" + strTemp;
		
	if(pintPage < intTotalPage) strTemp = strTemp + "<a onfocus='" + pstrGOTOfunction + "(" +
		parseInt((pintPage + 1)) + ")' href='javascript:" + pstrGOTOfunction + "(" +
		parseInt((pintPage + 1)) + ")' accesskey=" + strNextKey + " class=paging>" + 
		strNext + "</a>&nbsp;&nbsp;";
		
	if(blnLastPagegroup == false) strTemp = strTemp + "<a onfocus='" + pstrGOTOfunction + "(" +
		parseInt(intTotalPage) + ")' href='javascript:" + pstrGOTOfunction + "(" +
		parseInt(intTotalPage) + ")' accesskey=" + strLastKey + " class=paging>" + 
		strLast + "[" + intTotalPage + "]</a>";
	
	strTemp = strPage + " : " + strTemp + "<br>"
	
	return strTemp;	
}
//==================================================
function pagingInit(pintStyle){
	switch (pintStyle){
		case 1:{
			strPage = "Page";
			strFirst = "First";
			strPrevious = "Précédent";
			strNext = "Suivant";
			strLast = "Last";
			break;
		}
		case 2:{
			strPage = "Trang";
			strFirst = "Đầu";
			strPrevious = "Trước";
			strNext = "Sau";
			strLast = "Cuối";
			break;
		}
		case 3:{
			strPage = "Trang";
			strFirst = "|<";
			strPrevious = "<<";
			strNext = ">>";
			strLast = ">|";
			break;
		}
	}
}
