/*
	(c) Copyright Mobile Bridges Ltd 2005. All Rights Reserved.

	Project: mPhpLib
	Version: 1.0
	Date:    21-10-2005

	Description: Eventhandlers for Pager.

	Version		Date		Author			Initials	Description
	1.0			051021		Ed Schimmel     EAS			Start of development
	1.1			061109		Ed Schimmel		EAS			Change in OnPreviousPage()
														and OnNextPage()
	1.2			061113		Ed Schimmel		EAS			Added feature to activate loading of parent
*/

function OnFirstPage(inName, inLoadParent)
{
	var theFirstRow = document.getElementById("FirstRow"+inName);
	var theLastRow = document.getElementById("LastRow"+inName);
	var theRowsPerPage = document.getElementById("RowsPerPage"+inName);

	theFirstRow.value = 0;
	theLastRow.value = parseInt(theRowsPerPage.value,10) - 1;

	if (inLoadParent)
		SetActionLoad();

	theFirstRow.form.submit();
}

function OnPreviousPage(inName, inLoadParent)
{
	var theFirstRow = document.getElementById("FirstRow"+inName);
	var theLastRow = document.getElementById("LastRow"+inName);
	var theRowsPerPage = document.getElementById("RowsPerPage"+inName);

	var theFirstRowValue = parseInt(theFirstRow.value,10);
	var theRowsPerPageValue = parseInt(theRowsPerPage.value,10);

	var theLastRowValue = theFirstRowValue - 1;
	if (theLastRowValue >= 0)
	{
    	theFirstRow.value -= theRowsPerPageValue;
		theLastRow.value = theLastRowValue;

		if (theFirstRow.value < 0)
			theFirstRow.value = 0;

		if (inLoadParent)
			SetActionLoad();

		theFirstRow.form.submit();
	}
}

function OnNextPage(inName, inLoadParent)
{
	var theFirstRow = document.getElementById("FirstRow"+inName);
	var theLastRow = document.getElementById("LastRow"+inName);
	var theRowsPerPage = document.getElementById("RowsPerPage"+inName);
	var theNrOfRows = document.getElementById("NrOfRows"+inName);

	var theLastRowValue = parseInt(theLastRow.value,10);
	var theRowsPerPageValue = parseInt(theRowsPerPage.value,10);
	var theNrOfRowsValue = parseInt(theNrOfRows.value,10);

	var theFirstRowValue = theLastRowValue + 1;
	if (theFirstRowValue < theNrOfRowsValue)
	{
		theFirstRow.value = theFirstRowValue;
		theLastRow.value = theLastRowValue + theRowsPerPageValue;

		if (theLastRow.value >= theNrOfRowsValue)
			theLastRow.value = theNrOfRowsValue - 1;

		if (inLoadParent)
			SetActionLoad();

		theFirstRow.form.submit();
	}
}

function OnLastPage(inName, inLoadParent)
{
	var theFirstRow = document.getElementById("FirstRow"+inName);
	var theLastRow = document.getElementById("LastRow"+inName);
	var theRowsPerPage = document.getElementById("RowsPerPage"+inName);
	var theNrOfRows = document.getElementById("NrOfRows"+inName);

	var theRowsPerPageValue = parseInt(theRowsPerPage.value,10);
	var theNrOfRowsValue = parseInt(theNrOfRows.value,10);

	theFirstRow.value = parseInt((theNrOfRowsValue - 1)/ theRowsPerPageValue,10) * theRowsPerPageValue;
	theLastRow.value = theNrOfRowsValue - 1;

	if (inLoadParent)
		SetActionLoad();

	theFirstRow.form.submit();
}

function OnGotoPage(inName, inLoadParent)
{
	var thePage = document.getElementById("Page"+inName);

	var theFirstRow = document.getElementById("FirstRow"+inName);
	var theLastRow = document.getElementById("LastRow"+inName);
	var theRowsPerPage = document.getElementById("RowsPerPage"+inName);
	var theNrOfRows = document.getElementById("NrOfRows"+inName);

	var thePageValue = parseInt(thePage.value,10);
	var theRowsPerPageValue = parseInt(theRowsPerPage.value,10);
	var theNrOfRowsValue = parseInt(theNrOfRows.value,10);

	var theValue = (thePageValue - 1) * theRowsPerPageValue;
	if ((theValue >= 0) && (theValue < theNrOfRowsValue))
    {
		theFirstRow.value = theValue;
		theLastRow.value = theValue + theRowsPerPageValue;

		if (theLastRow.value > theNrOfRowsValue)
			theLastRow.value = theNrOfRowsValue;

		if (inLoadParent)
			SetActionLoad();

		theFirstRow.form.submit();
	}
}

function SetActionLoad()
{
	var theAction = document.getElementById("theAction");
	if (theAction)
		theAction.value = "Load";
}
