/**
 * ====================================================================
 *
 * Copyright 2002 USSearch, Inc.   Title, ownership rights, and
 * intellectual property rights in and to this software remain with
 * USSearch, Inc. USSearch, Inc. hereby reserves all rights
 * in and to this software. This software may not be copied, modified,
 * or used without a license from USSearch, Inc. This software is
 * protected by international copyright laws and treaties, and may be
 * protected by other laws. Violation of copyright laws may result in
 * civil liability and criminal penalties.
 *
 * ====================================================================
 */

/**
 * Javascript library for page navigation
 * 
 * The only thing we need to sync up here with the PHP code are the
 * following variables:
 *
 * SORT_FIELD_NAME
 * START_ROW_FIELD_NAME
 * NUM_ROWS_FIELD_NAME
 * 
 */

// Sync up the following variable names with PHP Paginator class

var SORT_FIELD_NAME = 'paginatorSort';
var START_ROW_FIELD_NAME = 'paginatorStartRow';
var NUM_ROWS_FIELD_NAME = 'paginatorNumRows';

/**
 * Method called when user changes the "Show:" drop down box
 *  - Sets the number of rows to show per page 
 *  - Refresh page (submit form)
 */
function setNumRows(aSelectBox, aStartRowFieldName) {
    var numRows = aSelectBox.options[aSelectBox.selectedIndex].value;
    aSelectBox.form.elements[aStartRowFieldName].value = "1";
	refreshPage(aSelectBox.form);
}

/**
 * Method called when user clicks on the column headers
 *  - Sets a hidden field to the selected sort value
 *  - Sets the start row to start back on the first record
 *  - Refresh page (submit form)
 */
function setSort(aSort) {
    var form = getPageNavForm();
	form.elements[SORT_FIELD_NAME].value = aSort;
    form.elements[START_ROW_FIELD_NAME].value = 1;
	refreshPage(form);
}


/**
 * Method called when user changes the "Jump To:" drop down box
 *  - Sets a hidden field to the start row to show 
 *  - Refresh page (submit form)
 */
function setStartRow(aSelectBox) {
    var startRow = aSelectBox.options[aSelectBox.selectedIndex].value;
    aSelectBox.form.elements[START_ROW_FIELD_NAME].value = startRow;
    refreshPage(aSelectBox.form);
}

function setStartRowToStart() {
    var form = getPageNavForm();
    form.elements[START_ROW_FIELD_NAME].value = 1;
}

/**
 * Method called when user clicks on the "NEXT >>" link/button
 *  - Gets the current form
 *  - Gets the current start row and adds the number of rows
 *  - Sets the start row to the new start row
 *  - Refresh Page (submit form)
 */
function nextPage() {
    var form = getPageNavForm();
    var currStartRow = form.elements[START_ROW_FIELD_NAME].value;
    var selectBox = form.elements[NUM_ROWS_FIELD_NAME];
    var numRows = selectBox.options[selectBox.selectedIndex].value;
    form.elements[START_ROW_FIELD_NAME].value = parseInt(currStartRow) + parseInt(numRows);
    refreshPage(form);
}

/**
 * Method called when user clicks on the "<< PREV" link/button
 *  - Gets the current form
 *  - Gets the current start row and substracts the number of rows
 *  - Sets the start row to the new start row
 *  - Refresh Page (submit form)
 */
function previousPage() {
    var form = getPageNavForm();
    var currStartRow = form.elements[START_ROW_FIELD_NAME].value;
    var selectBox = form.elements[NUM_ROWS_FIELD_NAME];
    var numRows = selectBox.options[selectBox.selectedIndex].value;
    form.elements[START_ROW_FIELD_NAME].value = parseInt(currStartRow) - parseInt(numRows);
    refreshPage(form);
}

/**
 * Method called when user clicks on the "<< FIRST" link/button
 *  - Gets the current form
 *  - Sets the start row back to "1"
 *  - Refresh Page (submit form)
 */
function firstPage() {
    var form = getPageNavForm();
    form.elements[START_ROW_FIELD_NAME].value = 1;
    refreshPage(form);
}

/**
 * TODO:
 * Method called when user clicks on the "LAST >>" link/button
 *  - Gets the current form
 *  - Sets the start row back to "1"
 *  - Refresh Page (submit form)
 */
function lastPage() {
    var form = getPageNavForm();
    form.elements[START_ROW_FIELD_NAME].value = 1;
    refreshPage(form);
}

/**
 * Convenience method to jump 
 */
function jumpTo(aSelectBox) {
	refreshPage(aSelectBox.form);
}

/**
 * Convenience method to get the correct form for page navigation
 * The check is to loop through all the forms in the page
 * and find the form with one of the form field names provided
 */
function getPageNavForm() {
    if( document.forms.length>0 ) {
        for( var ii=0; ii<document.forms.length; ii++ ) {
            for( var jj=0; jj<document.forms[ii].elements.length; jj++ ) {
                if( document.forms[ii].elements[jj].name==SORT_FIELD_NAME ) {
                    return document.forms[ii];
                }
            }
        }
    }
}

/**
 * Convenience method to submit the form and refresh the page
 */
function refreshPage(aForm) {
    aForm.submit();
}

