//Functions for navigating views//Sean Power//26 Nov 2007var vPosition = 1; //the position in the view to retrieve fromvar entryCountDocument = 10; //number of entries to retireve & display - documentsvar entryCountCategory = 30; //number of entries to retireve & display - Categoriesvar previousPageScript = "";	//the script to call from a back button to get back to a page - this could be a cat page or a search or somethingvar currentPageURL = ""; //the current page url, this will allow us to determine if we're using categories or anything elsevar viewName = ""; //current view to lookupvar categoryView = ""; //used for the view we want to lookup the categories from//Sets the start position in the view to the next pagefunction setPositionDown (type) {	var countNum = entryCountDocument;	if (type == "C") {		countNum = entryCountCategory;	}	vPosition = vPosition + countNum;} //end setPositionDown//Sets the start postion in the view to the previous pagefunction setPositionUp (type){	var countNum = entryCountDocument;	if (type == "C") {		countNum = entryCountCategory;	}	vPosition = vPosition - countNum;	if (vPosition < 1) {		vPosition = 1;	}} //end setPositionUp//retrieves the documents from the viewfunction retrieveViewCategories (requestStr){	var xmlhttp = new XMLHttpRequest ();		if (!requestStr) requestStr = categoryView + "?ReadViewEntries&CollapseView&Start=" + vPosition + "&Count=" + entryCountCategory;			currentPageURL = requestStr;	previousPageScript = "vPosition="+ vPosition +";viewName=\"" + viewName + "\"; categoryView=\"" + categoryView + "\";retrieveViewCategories (\"" + requestStr +"\")";	xmlhttp.open ("POST", requestStr, true) //open the view ready to send the request		//Add a listener so we know when the agent has finished retrieving information	function viewCategoryHandler ()	{		//only process if we get a ready state of 4		if (xmlhttp.readyState == 4)		{			//Process the returned XML - i.e. Put it into a html table/div and shove it into the screen			var luVals;			//if we have a category element then get them			 if (xmlhttp.responseXML.getElementsByTagName ("viewentry").length >0 )	 		{		 		luVals = xmlhttp.responseXML.getElementsByTagName ("viewentry");		 		parseCategories (luVals, xmlhttp.responseXML);			}						hideLoading ();		}	}	//tell the http request what to run when we're ready	xmlhttp.onreadystatechange = viewCategoryHandler;		//Draw the loading DIV so the user knows we're doing something	showLoading ();     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');    	xmlhttp.send(null);	} //end RetrieveViewDocuments//retrieves the documents from the viewfunction retrieveViewDocuments (category){	var xmlhttp = new XMLHttpRequest ();		var requestStr = viewName + "?ReadViewEntries&Start=" + vPosition + "&Count=" + entryCountDocument;		//if we have category then we need to lookup by that too	if (category)	{		requestStr += "&RestrictToCategory="+ category;	}	//if we have a currenturl then set it to previous, other wise just set this to current	if (currentPageURL) previousPageURL = currentPageURL;	currentPageURL = requestStr;		xmlhttp.open ("POST", requestStr, true) //open the view ready to send the request		//Add a listener so we know when the agent has finished retrieving information	function viewRequestHandler ()	{		//only process if we get a ready state of 4		if (xmlhttp.readyState == 4)		{			//Process the returned XML - i.e. Put it into a html table/div and shove it into the screen			var luVals;			//if we have a category element then get them			 if (xmlhttp.responseXML.getElementsByTagName ("viewentry").length >0 )	 		{		 		luVals = xmlhttp.responseXML.getElementsByTagName ("viewentry");		 		parseEntryDocuments (luVals, xmlhttp.responseXML);			} else			{				var viewData = document.getElementById ("ViewData");				if (vPosition > 1) viewData.innerHTML =  "No more documents for that selection."; else viewData.innerHTML = "No documents for that selection";			}			hideLoading ();		}	}	//tell the http request what to run when we're ready	xmlhttp.onreadystatechange = viewRequestHandler;		//Draw the loading DIV so the user knows we're doing something	showLoading ();     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');    	xmlhttp.send(null);	} //end RetrieveViewDocumentsfunction parseCategories (luVals, oDomDoc){		//workout the html to put in it	var viewData = document.getElementById ("ViewData");	var viewNav = createViewBreadCrumbHTML (oDomDoc, "C");	var cCat;	var catHTML = "<div class='ViewHeader'>Select Category</div>" ;	var elData;		//do some columns - if we've got less than 10 items then done worry about it	var colLen = luVals.length/2;	if (colLen < 10) colLen = luVals.length;		catHTML += htmlStartTag ("table", "ViewCategories", "", "") + "<tr><td>";	for (var i=0; i< colLen; i++)	{			elData = luVals[i].getElementsByTagName ("entrydata");			if (elData[0].lastChild && elData[0].lastChild.firstChild)			{				catHTML += "<a href='#' onclick='vPosition=1;retrieveViewDocuments(\"" + elData[0].lastChild.firstChild.nodeValue.replace (/'/g, "&#39;")  + "\"); return false;'>" + elData[0].lastChild.firstChild.nodeValue + "</a><br>"			}	}	catHTML += ce;	if (colLen != luVals.length)	{	catHTML += "<td>";	for (var i=Math.round (colLen); i< luVals.length; i++)	{			elData = luVals[i].getElementsByTagName ("entrydata");			if (elData[0].lastChild && elData[0].lastChild.firstChild)			{					catHTML += "<a href='#' onclick='vPosition=1;retrieveViewDocuments(\"" + elData[0].lastChild.firstChild.nodeValue.replace (/'/g, "&#39;")  + "\"); return false;'>" + elData[0].lastChild.firstChild.nodeValue + "</a><br>"			}	}	}	catHTML += re + te;		viewData.innerHTML  = catHTML + viewNav;	resetScreen ();}function parseEntryDocuments (luVals, oDomDoc, searchResults){	//will have a list of the documents and then the view navigator (ie, next previous), this could go at the top too if wanted	var viewData = document.getElementById ("ViewData");	var html = "";	var docObj;	documentList = new Array ();	var docCount = 0;	var viewNav ="";		if (searchResults != true)	{		viewNav = createViewBreadCrumbHTML (oDomDoc, "D");	}	for (var i=0; i < luVals.length; i++){		docObj = new BookDetails ();		docObj.initialise (luVals [i]);		documentList [docCount] = docObj;		docObj.listNumber = docCount;		docCount ++;			html += docObj.generateViewHTML (i+vPosition);		html += "<div class='RowSpacer'>"+ sp + "</div>";	}		viewData.innerHTML  = html+ viewNav;	resetScreen ();}//formulate the bottom section of the viewfunction createViewBreadCrumbHTML (oDomDoc, type){	var bLink = ""; //back link if we're coming from a search or category or something		var pageArray = new Array (); //an array of pages - this is of the vPositions for the pages - i dont want it to grow to big so i'm limiting it to the first, 3 b4 and after current and last	var lastLink = ""; //a link to go to the last page	var firstLink = ""; //a link to the first page	var cPageHTML = "";	//if we're on page one and there is only one page then dont display anything		//links for 3up and 3 down from current position	var up1Link = "";	var up2Link = "";	var up3Link = "";	var down1Link = "";	var down2Link = "";	var down3Link = "";		var entryNum = entryCountDocument;	if (type == "C") entryNum = entryCountCategory;		var numEntries = oDomDoc.getElementsByTagName("viewentries")[0].getAttribute("toplevelentries"); 	var pageCount = numEntries/entryNum;	var currentPage = vPosition/entryNum;	//work out the page array	pageArray[0] = 1 //start	pageArray[1] = vPosition - (entryNum*3)	pageArray[2] = vPosition - (entryNum*2)	pageArray[3] = vPosition - (entryNum)	pageArray[4] = vPosition	pageArray[5] = vPosition + (entryNum) 	pageArray[6] = vPosition + (entryNum*2) 	pageArray[7] = vPosition + (entryNum*3)	//find the last	for (var i = 1; i<=numEntries; i+=entryNum)	{			if (i+entryNum >= numEntries) pageArray[8] = i;				}			var functionToCall = "";	if (type == "D")	{		//if we've got a previousPageScript then we set the back link		if (previousPageScript) bLink = "<a href='#' onclick='" + previousPageScript + "; return false;'> RETURN TO CATEGORIES </a>";		//need to determin if we were using a category.		if (currentPageURL.match("RestrictToCategory"))		{			//we had a category so we need to retrieve it			var splitString = currentPageURL.substring (currentPageURL.indexOf ("Category=") + 9);			functionToCall = "retrieveViewDocuments (\"" + splitString + "\")";			}else		{			functionToCall = "retrieveViewDocuments ()";			}	}else	{		functionToCall = "retrieveViewCategories ()";		}		var tCounter = Math.round (pageCount);	if (pageCount <= tCounter)	{		pageCount = tCounter;	}else	{		pageCount = tCounter+ 1;	}	tCounter = Math.round (currentPage)	if (currentPage < tCounter)	{		currentPage = tCounter;	}else	{		currentPage = tCounter+ 1;	}		var html = "";	//lets do the 1 up link	if (currentPage < pageCount) up1Link = "<a href='#' onclick='vPosition="+ pageArray[5]+";" + functionToCall + " ; return false;'> " + (currentPage + 1) + "</a>";	//lets do the 2 up link	if (currentPage+1 < pageCount) up2Link = "<a href='#' onclick='vPosition="+ pageArray[6]+";" + functionToCall + " ; return false;'> " + (currentPage + 2) + "</a>";	//lets do the 3 up link	if (currentPage+2 < pageCount) up3Link = "<a href='#' onclick='vPosition="+ pageArray[7]+";" + functionToCall + " ; return false;'> " + (currentPage + 3) + " </a>";	//we need a last page work out start	if (currentPage+3 < pageCount) lastLink = "<a href='#' onclick='vPosition="+pageArray[8]+";" + functionToCall + " ; return false;'> LAST</a>";		//lets do the 1 down link	if (currentPage > 1) down1Link = "<a href='#' onclick='vPosition="+ pageArray[3]+"; " + functionToCall + "; return false;'> " + (currentPage - 1) + "</a>";	//lets do the 2 down link	if (currentPage > 2) down2Link = "<a href='#' onclick='vPosition="+ pageArray[2]+"; " + functionToCall + "; return false;'> " + (currentPage - 2) + "</a>";	//lets do the 3 down link	if (currentPage > 3) down3Link = "<a href='#' onclick='vPosition="+ pageArray[1]+"; " + functionToCall + "; return false;'> " + (currentPage - 3) + "</a>";	//if we have so many pages that it gets over 4 digits we're going to run into space problems so we'll cut it down to 2 buttons each way	if (currentPage+3 >=1000) up3Link = "";	if (currentPage-3 >=1000) down3Link = "";		//first page	if (currentPage-3 > 1) 	firstLink = "<a href='#' onclick='vPosition="+pageArray[0]+";" + functionToCall + " ; return false;'> FIRST</a>";	if (currentPage != 1 || pageCount != 1)  cPageHTML = "<span class='currentpage'>" + currentPage + "</span>";	html += htmlStartTag ("div", "pageswitcher FloatLeft","","") + bLink + de; 	html += htmlStartTag ("div", "ralign pageswitcher","","");	html += firstLink + down3Link + down2Link + down1Link + cPageHTML + up1Link + up2Link + up3Link + lastLink;		html += de;	return html;	}