<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

// returns paging line, and sets 4 globals: nTotalRec, nStartRec, nEndRec, nTotalPg
function nGetPaging($nListingSQL,$nPage,$nShowPerPg,$nPassThru="",$nDivToUpdate="",$nPageInDiv=""){
	global $nSort,$nSortOrder,$nSearch;
	global $nTotalRec,$nStartRec,$nEndRec,$nTotalPg;
	if(!$nPage){ $nPage=1; }
	$nPrevPg = "";
	$nLeftDots = "";
	$nPagingCode = "";
	$nRightDots = "";
	$nNextPg = "";
	// set record globals
	$nTotalRec = nGetTotalRec($nListingSQL);
	$nTotalPg = 1; $nTotalRecCounter = $nTotalRec;
	while($nTotalRecCounter>$nShowPerPg){ $nTotalPg++; $nTotalRecCounter=$nTotalRecCounter-$nShowPerPg; }
	if($nPage>$nTotalPg){ $nPage=$nTotalPg; }
	if($nPage==1){ $nStartRec = 1; } else { $nStartRec = (($nPage-1)*$nShowPerPg)+1; }
	if($nPage<$nTotalPg){ $nEndRec = $nPage*$nShowPerPg; } else { $nEndRec = $nTotalRec; }
	if(!$nEndRec){ $nEndRec = 1; }
	// build paging line
	if($nPassThru){ $nPassThru="&".$nPassThru; }
	if($nPage>1){
		$nMoveLink = "?srt=$nSort&ord=$nSortOrder&pg=".($nPage-1)."$nPassThru";
		if($nDivToUpdate && $nPageInDiv){ $nMoveLink="javascript:loadContent('$nDivToUpdate','$nPageInDiv$nMoveLink');"; }
		$nPrevPg = "<li><a href=\"$nMoveLink\" class=\"prev\">Previous</a></li>";
	}
	if($nPage<$nTotalPg){
		$nMoveLink = "?srt=$nSort&ord=$nSortOrder&pg=".($nPage+1)."$nPassThru";
		if($nDivToUpdate && $nPageInDiv){ $nMoveLink="javascript:loadContent('$nDivToUpdate','$nPageInDiv$nMoveLink');"; }
		$nNextPg = "<li><a href=\"$nMoveLink\" class=\"next\">Next</a></li>";
	}
	if($nTotalPg>1){
		$nStartPg = 1; $nEndPg = $nTotalPg;
		if($nTotalPg>7){
			$nLeftSide = 2; $nRightSide = 2;
			if($nPage==1){ $nRightSide = 4; }
			if($nPage==2){ $nRightSide = 3; }
			if($nPage==$nTotalPg){ $nLeftSide = 4; }
			if($nPage==($nTotalPg-1)){ $nLeftSide = 3; }
			if($nPage>3){ $nLeftDots = "<li>...</li>"; $nStartPg = $nPage-$nLeftSide; } else { $nLeftDots = ""; }
			if($nPage<($nTotalPg-2)){ $nRightDots = "<li>...</li>"; $nEndPg = $nPage+$nRightSide; } else { $nRightDots = ""; }
		}
		for($nPgNum=$nStartPg;$nPgNum<=$nEndPg;$nPgNum++){
			$nMoveLink = "?srt=$nSort&ord=$nSortOrder&pg=$nPgNum$nPassThru";
			if($nDivToUpdate && $nPageInDiv){ $nMoveLink="javascript:loadContent('$nDivToUpdate','$nPageInDiv$nMoveLink');"; }
			if($nPgNum==$nPage){
				$nPagingCode.="<li><span>$nPgNum</span></li>";
			} else {
				$nPagingCode.="<li><a href=\"$nMoveLink\">$nPgNum</a></li>";
			}
		}
	}
	$nPagingCODE = <<<CODE
		<span class="page_no">Page $nPage of $nTotalPg &nbsp; &nbsp; </span>
		<ul class="pag_list"><li><a href="javascript:loadContent('$nDivToUpdate','$nPageInDiv?srt=$nSort&ord=$nSortOrder&spp=$nTotalRec$nPassThru');">View All</a></li></ul>
		<div align="center" style="float:left;width:250px;padding-left:70px;">
			<ul class="pag_list">
				$nPrevPg
				$nLeftDots
				$nPagingCode
				$nRightDots
				$nNextPg
			</ul>
		</div>
		<form name="pageForm" id="pageForm" action="$nPageInDiv" method="post" style="margin:0px;" onSubmit="return submitForm('$nDivToUpdate','$nPageInDiv?srt=$nSort&ord=$nSortOrder$nPassThru',this.name);return false;">
			<fieldset>
				<label>Go to page:</label>
				<div class="pag_inputs">
					<span class="input_go"><input type="text" name="pg" id="pg"></span>
					<span class="button"><input type="submit" name="nGo"></span>
				</div>
			</fieldset>
		</form>
CODE;
	return $nPagingCODE;
}
function nGetTotalRec($nListingSQL){
	global $DBName,$Link;
	if(!$nListingSQL){ return 0; }
	mysql_select_db($DBName); $Result = mysql_query($nListingSQL,$Link);
	$nNumRecords = mysql_num_rows($Result);
	return $nNumRecords;
}
?>