// JavaScript Document
/* ********************************************************************* */
/*	Function: getPageData                                                 */
/*		Get the next block of data when paginating.                        */
/* Input:                                                                */
/*		target: Where the resultant data is to be displayed.               */
/*		restart: If set to "new', data from a new query is to be diaplayed */
/*	Output:                                                               */
/*		None. A call to AJAX is made to retrieve the data.                 */
/* ********************************************************************* */
function getPageData(target, restart) {

if(restart == 'new') {
		document.getElementById("txtStartRow").value = 0;		
	}
	
	var obj = document.getElementById(target);
	var serverPage = "getCemeteryData.php";
	
	getCemeteryData(serverPage);
	
}

/* Clear an input text field */
function blank(fieldToClear) {
	document.getElementById(fieldToClear).value = "";
}

/* ********************************************************************* */
/*	Function getNext()                                                    */
/*		Get the next block of maxRows.                                     */
/*	Input:                                                                */
/*		None.                                                              */
/*	Output:                                                               */
/*		Pass control to getData to  completee the processing               */
/* ********************************************************************* */

function getNext() {
	
	/* startRow: Row in the database to start the retrieval.              */
	var startRow = parseInt(document.getElementById("txtStartRow").value);
	/* maxRows: Maximum rows to retrieve                                   */
	var maxRows = parseInt(document.getElementById("txtMaxRows").value);
	/* totalRows: Total number of rows in the base SELECT sql.             */
	var totalRows = parseInt(document.getElementById("txtTotalRows").value);
	
	startRow += maxRows;
	
	if(startRow < totalRows) {
		var newMax = totalRows - startRow;
		/* Determine there are less then maxRows to retrieve                  */
		newMax = (newMax < maxRows)?newMax:maxRows; 
		document.getElementById("txtStartRow").value = startRow;
		getPageData('results', 'next');
	}
}

/* ********************************************************************* */
/*	Function getPrevious()                                                */
/*		Get the previous block of maxRows.                                 */
/*	Input:                                                                */
/*		None.                                                              */
/*	Output:                                                               */
/*		Pass control to getData to  completee the processing               */
/* ********************************************************************* */

function getPrevious(){
	var startRow = parseInt(document.getElementById("txtStartRow").value);
	var totalRows = parseInt(document.getElementById("txtTotalRows").value);
	var maxRows = parseInt(document.getElementById("txtMaxRows").value);
	
	if(startRow >= maxRows) {
		startRow = startRow - maxRows;
		document.getElementById("txtStartRow").value = startRow;
		getPageData('results', 'previous');
	}
}

function setStartRow(startRowValue,serverpage) {
	document.getElementById("txtStartRow").value = startRowValue;	
	getCemeteryData(serverpage);
}

/* **************************************************************** */
/* Function: getCemeteryData                                        */
/* Purpose: Retrieve Cemetery Data using AJAX                       */
/* Input: php server page used to contruct a table of data          */
/*	Output: Server page returned data                                */
/* **************************************************************** */

function getCemeteryData(serverpage) {

//	User input
	var surname = document.getElementById("txtLast").value;
	var first = document.getElementById("txtFirst").value;
	var location = document.getElementById("txtLocation").value;
	var cemeteryid = document.getElementById("txtCemeteryID").value;
	var sortparm = document.getElementById("txtSortParm").value;
	var startrow = document.getElementById("txtStartRow").value;
	
	parmStr = "surname=" + surname;
	parmStr += "&first=" + first;
	parmStr += "&location=" + location;
	parmStr += "&cemeteryid=" + cemeteryid;
	parmStr += "&sortparm=" + sortparm;
	parmStr += "&startrow=" + startrow;

	var target = document.getElementById('result');
	
	processAJAXPost(serverpage, parmStr, target);

}


