// 2009-05-05 15:59 Xin
function isIE() {
	return window.all;
}

// extend XMLDocument for non-IE
if (document.implementation.hasFeature("XPath", "3.0")) {
	// selectNodes
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
		if (! xNode) {
			xNode = this;
		}
		var oNSResolver = this.createNSResolver(this.documentElement);
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var aResult = [];
		for (var i = 0; i < aItems.snapshotLength; i++) {
			aResult[i] = aItems.snapshotItem(i);
		}
		return aResult;
	}
	
	Element.prototype.selectNodes = function(cXPathString) {
		if (this.ownerDocument.selectNodes) {
			return this.ownerDocument.selectNodes(cXPathString, this);
		} else {
			throw "For XML Elements Only";
		}
	}
	
	// selectSingleNode
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
		if (! xNode) {
			xNode = this;
		}
		var xItems = this.selectNodes(cXPathString, xNode);
		if (xItems.length > 0) {
			return xItems[0];
		} else {
			return null;
		} 
	}
	
	Element.prototype.selectSingleNode = function(cXPathString) {
		if (this.ownerDocument.selectSingleNode) {
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		} else {
			throw "For XML Elements Only";
		} 
	} 
} 

// 2008-11-05 xin
var worker;
var titleList;
try {
	worker = new ActiveXObject("Microsoft.XMLDOM");
	titleList = new ActiveXObject("Microsoft.XMLDOM");
} catch (e) {
	//alert("Not IE");
	try {
		worker = document.implementation.createDocument("", "", null);
		titleList = document.implementation.createDocument("", "", null);
	} catch (e) {
		alert(e.message);
	}
}

curPage = 0;
index = 0;
curFileID = 1;	//maxFileID;
articleNum = 0;

worker.async = false;
titleList.async = false;
//titleList.load("/systemfiles/dummy.x.xml");
try {
	titleList.loadXML("<dummy></dummy>");
} catch (e) {
	var parser = new DOMParser();
	titleList = parser.parseFromString("<dummy></dummy>", "text/xml");
}


function clearDataNode()
{
	var articles = dataNode.selectNodes("//article");
	var len = articles.length;
	var i = 0;
	for (i = 0; i < len; i ++)
	{
		dataNode.removeChild(articles[i]);
	}
}

function isLastPage()
{
	if (curPage >= pageTotal)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function isFirstPage()
{
	if (curPage <= 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function getCurrentFile()
{
	return fileName + curFileID + ".xml";
}

function hasNextFile()
{
	if (curFileID < maxFileID)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function hasPrevFile()
{
	if (curFileID > 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function getNextFile()
{
	curFileID ++;
	retfileName = fileName + curFileID + ".xml";
	return retfileName;
}

function getPrevFile()
{
	curFileID --;
	retfileName = fileName + curFileID + ".xml";
	return retfileName;
}

// var pageSize=20;
// var pageTotal=11;
// var fileName="53_";
// var maxFileID=11;

function getCurrentFile()
{
	return fileName + curFileID + ".xml";
}

function getArticleListXMLFile() {
	return fileName + curPage + ".xml";
}

function displayArticleList() {
	document.getElementById("mytable").innerHTML = "";
	
	try {
		worker.load(getArticleListXMLFile());
	} catch (e) { // Google Chrome
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", getArticleListXMLFile(), false);
		xmlhttp.send(null);
		worker = xmlhttp.responseXML.documentElement; 
	}
	
	var rowNodes;
	var strArticleListHTML = "";
	strArticleListHTML = strArticleListHTML + '<table width="100%" border="0" cellspacing="0" cellpadding="3">';
	if (isIE()) {
		rowNodes = worker.selectNodes("data/article");
	} else {
		rowNodes = worker.selectNodes("//article");
		//alert(rowNodes);
	}
	for (i = 0; i < rowNodes.length; i++) {
		if (isIE()) {
			strArticleListHTML = strArticleListHTML + rowNodes[i].text;
		} else {
			strArticleListHTML = strArticleListHTML + rowNodes[i].selectSingleNode("value").firstChild.nodeValue;
		}
	}
	strArticleListHTML = strArticleListHTML + '</table>';

	//document.getElementById("articlelist").innerHTML = strArticleListHTML;
	document.getElementById("mytable").innerHTML = strArticleListHTML;
}

// backward compat
function nextPage()
{
	doNextPage();
	document.getElementById('pageTotalNum').innerHTML = document.getElementById('pageTotalNum').innerHTML.replace(/In\s+all(\d+)Page/, "of&nbsp;&nbsp;$1");
}

function doNextPage()
{
	if (isLastPage()) {
		alert("You are already at the last page");
		return;
	}
	curPage++;
	document.getElementById("pageSerialNum").innerHTML = "Page " + curPage + "";
	displayArticleList();
}

function doPrevPage()
{
	if (isFirstPage())
	{
		alert("You are already at the first page");
		return;
	}
	curPage--;
	document.getElementById("pageSerialNum").innerHTML = "Page " + curPage + "";
	displayArticleList();
}

function doLastPage()
{
	curPage = pageTotal;
	document.getElementById("pageSerialNum").innerHTML = "Page " + curPage + "";
	displayArticleList()
}

function doFirstPage()
{
	curPage = 1;
	document.getElementById("pageSerialNum").innerHTML = "Page " + curPage + "";
	displayArticleList()
}
