if (window.NOF_RSN_ARRAY == null) {
    window.NOF_RSN_ARRAY = new Array();
}

var nof_null_rsn = new RSNavigator("",0,0,0);

function GetRSN(name) {
    if (window.NOF_RSN_ARRAY[name] != null) {
        return window.NOF_RSN_ARRAY[name];
    } else {
        return nof_null_rsn;
    }
}

function RSNavigator(queryName, currentStartRow, resultsPerPage, recordCount) {
    this.queryName        = queryName;
    this.currentStartRow  = currentStartRow;
    this.resultsPerPage   = resultsPerPage;
    this.recordCount      = recordCount;
    this.pagesNo          = Math.ceil(recordCount/resultsPerPage);
    this.lastPageStartRow = ((resultsPerPage * this.pagesNo + 1) <= recordCount) ?
                            resultsPerPage * this.pagesNo + 1 : resultsPerPage * (this.pagesNo -1) + 1;

    RSNavigator.prototype.First = function NOF_RSNavigator_First() {
        if (this.currentStartRow > 1) {
            this.reload(1);
        }
    };

    RSNavigator.prototype.Previous = function NOF_RSNavigator_Previous(){
        if (this.currentStartRow - this.resultsPerPage >= 1) {
            this.reload(this.currentStartRow - this.resultsPerPage);
        }
    };

    RSNavigator.prototype.Next = function NOF_RSNavigator_Next(){
        if (this.currentStartRow + this.resultsPerPage <= this.recordCount) {
            this.reload(this.currentStartRow + this.resultsPerPage);
        }
    };

    RSNavigator.prototype.Last = function NOF_RSNavigator_Last() {
        if ( this.lastPageStartRow >  this.currentStartRow) {
            this.reload(this.lastPageStartRow);
        }
    };

    RSNavigator.prototype.reload = function NOF_RSNavigator_reload(rowNumber) {
        var startRowParam = "NOF_StartRow_"+ this.queryName;
        var newRowArg = startRowParam + "=" + rowNumber;
        if (location.search != "") {
            var paramIndex = location.search.indexOf( startRowParam );
            if (paramIndex != -1) {
                eval("re = /" + startRowParam + "=\\d+/")
                location.href = location.href.replace(re, newRowArg);
            } else {
                location.href = location.href + "&" + newRowArg;
            }
        } else {
            location.href = window.location.href +  "?" + newRowArg;
        }
    };
}

function Sort(rsName, direction, dataField) {
    var sortOrderParamName = "NOF_SORT_ORDER_" + rsName;
    var sortOrderParam = sortOrderParamName + "=" + dataField;
    var sortDirParamName = "NOF_SORT_DIR_" + rsName ;
    var sortDirParam = sortDirParamName + "=" + direction;
    if (location.search != "") {
        var paramIndex = location.search.indexOf(sortOrderParamName);
        if (paramIndex != -1) {
			oldUrl = location.href;
			oldParams =	oldUrl.substr(oldUrl.lastIndexOf('?')+1).split('&');
			for (i=0 ; i<oldParams.length; i++) {
				if (oldParams[i].indexOf(sortOrderParamName) != -1) {
            		eval("re = /" + sortOrderParamName + "=[a-zA-Z0-9_]+/")
					oldParams[i] = oldParams[i].replace(re, sortOrderParam);
				} else if (oldParams[i].indexOf(sortDirParamName) != -1) {
            		eval("re = /" + sortDirParamName + "=[a-zA-Z0-9_]+/")
					oldParams[i] = oldParams[i].replace(re, sortDirParam);
				}
			}
            newUrl = oldUrl.substr(0, oldUrl.lastIndexOf('?')+1);
			for (i=0 ; i<oldParams.length ; i++) {
				if (i > 0) {
					newUrl += '&';
				}
				newUrl += oldParams[i];
			}
			location.href = newUrl;
        } else {
            location.href = location.href + "&" + sortOrderParam + "&" + sortDirParam;
        }
    } else {
        location.href = window.location.href + "?" + sortOrderParam + "&" + sortDirParam;
    }
}

function SortToggle(rsName, currentDirection, dataField) {
    var newDirection = (currentDirection == 'ASC' ? 'DESC' : 'ASC');
    Sort(rsName, newDirection, dataField);
}

