

/*
Written by Peter Tyrrell, Andornot Consulting 2003
Inmagic Next & Previous links
Modified Nov 2003 to include First and Last: First | Previous | Next | Last
Modified April 2004 to include Rewind()
Modified June 2004 to accommodate Andornot WebPublisher Control
Modified June 2004 to allow global values
Modified Oct 2004 to allow page number ranges
*/

/*
GLOBAL VARIABLES
*/

// global page url
var currentUrl = window.location.href;

if (currentUrl.indexOf("?") != -1)
{
	currentUrl = currentUrl.substr(0, currentUrl.indexOf("?")); //snip href minus query string
}

// global link text values
var npFirstText = "First";
var npPreviousText = "Previous";
var npNextText = "Next";
var npLastText = "Last";

//global page range values
var npPageRange = 10;
var npPageRangePreText = "";
var npPageRangePostText = "";

//global show MR count in link text
var npShowMR = true;

//global css class names
var npLinkCss = "nextprev_link";
var npTextCss = "nextprev_text";


/*
Next/previous BLOCK section 
Use in reports where multiple records shown at a time
*/

function nextprev_First()
{
	var params = window.dbtw_params;
	var first_write = npFirstText;
	var first_link = "";
	var first_params = "";
	
	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);
	
	// determine whether a link should be made
	if (rn == 0)
	{
		first_link += "<span class='" + npTextCss + "'>"+first_write;
		first_link += "</span>";
	}
	else
	{
		var first_params = params.replace((/RN=(\d+)/), "RN=" + mr);
		first_link += "<a href='" + currentUrl;
		first_link +=  "?AC=PREV_BLOCK" + first_params;
		first_link += "' class='" + npLinkCss + "'>"; 
		first_link += first_write;
		first_link += "</a>";
	}
	return first_link;

}

function nextprev_Previous()
{
	var params = window.dbtw_params;
	var prev_write = npPreviousText;
	var prev_link = "";		

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);
	
	//comparison number for prev block
	var prev_total = (rn - mr);
	
	// determine if a link should be made
	if (prev_total >= 0 && mr != 0)
	{
		prev_link += "<a href='" +  currentUrl;
		prev_link +=  "?AC=PREV_BLOCK" + params;
		prev_link += "' class='" + npLinkCss + "'>";
		prev_link += prev_write;
		if (npShowMR == true) {prev_link += " " + mr;}
		prev_link += "</a>";
	}
	else if (mr == 0)
	{
		prev_link += "<span class='" + npTextCss + "'>"+prev_write;
		if (npShowMR == true) {prev_link;}
		prev_link += "</span>";
	}
	else
	{
		prev_link += "<span class='" + npTextCss + "'>"+prev_write;
		if (npShowMR == true) {prev_link += " " + mr;}
		prev_link += "</span>";
	}

	return prev_link;

} //end fxn

function nextprev_Next(record_count)
{
	var params = window.dbtw_params;
	var next_write = npNextText;
	var next_link = "";
	var rc = parseInt(record_count);		

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);
	
	//comparison number for next block
	var next_total = (rc - mr - rn);

	// determine if a link should be made
	if (next_total > 0 && mr != 0)
	{
		next_link += "<a href='" + currentUrl;
		next_link +=  "?AC=NEXT_BLOCK" + params;
		next_link += "' class='" + npLinkCss + "'>";
		next_link += next_write;
		if (npShowMR == true) {next_link += " " + mr;}
		next_link += "</a>";	
	}
	else if (mr == 0)
	{
		next_link += "<span class='" + npTextCss + "'>"+next_write;
		if (npShowMR == true) {next_link;}
		next_link += "</span>";	
	}	
	else
	{
		next_link += "<span class='" + npTextCss + "'>"+next_write;
		if (npShowMR == true) {next_link += " " + mr;}
		next_link += "</span>";	
	}
	
	return next_link;

} //end fxn

function nextprev_Last(record_count)
{
	var params = window.dbtw_params;
	var last_write = npLastText;
	var last_link = "";
	var last_params = "";
	var rc = parseInt(record_count);

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);
	
	//comparison number for next block
	var last_total = (rc - mr - rn);
		
	// determine if a link should be made
	if (last_total <= 0)
	{
		last_link = "<span class='" + npTextCss + "'>"+last_write;
		last_link += "</span>";
	}
	else
	{
		var last_RN = (Math.floor(((rc-1) / mr)) - 1) * mr;
		var last_params = params.replace((/RN=(\d+)/), "RN=" + last_RN);
		
		last_link += "<a href='" + currentUrl;
		last_link +=  "?AC=NEXT_BLOCK" + last_params;
		last_link += "' class='" + npLinkCss + "'>";
		last_link += last_write;
		last_link +="</a>";
	}
	
	return last_link;
}


function nextprev_PageRange(record_count)
{
	var params = window.dbtw_params;
	var rc = parseInt(record_count);
	var i = 0;
	var j = 0;
	var pre_params = "";
	var post_params = "";
	var pre_link = "";
	var post_link = "";	
	var current_write = "";
		
	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);	
	
	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);	
	
	// determine current page	
	var current_page = (rn / mr) + 1
	
	if ( rc % mr == 0)
	{
		var total_page = rc / mr
	}
	else 
	{
		var total_page = ((rc - (rc%mr)) / mr) + 1
	}	

	// build links before current page
	if ((total_page - current_page) < (Math.round(npPageRange / 2) - 1))
	{
		var PreSlider = (npPageRange - (total_page - current_page)) - 1;
	}
	else
	{
		var PreSlider = Math.round(npPageRange / 2);
	}
	var PreCount = 0;
	for (i = current_page - PreSlider; i < current_page; i++)
	{		
		if (i <= 0)
		{
			continue;	
		}
		else
		{
			pre_params = params.replace((/RN=(\d+)/), "RN=" + (mr * i));
			pre_link += "<a href='" + currentUrl;
			pre_link +=  "?AC=PREV_BLOCK" + pre_params;
			pre_link += "' class='" + npLinkCss + "'>"; 
			pre_link += npPageRangePreText + i + npPageRangePostText;
			pre_link += "</a> ";
			
			PreCount++;
		}
	}	
	
	// build links after current page
	var PostSlider = npPageRange - PreCount;
	for (j = current_page + 1; j < (current_page + PostSlider); j++)
	{		
		if (j > total_page)
		{
			break;	
		}
		else
		{
			post_params = params.replace((/RN=(\d+)/), "RN=" + (mr * (j-2)));
			
			post_link += "<a href='" + currentUrl;
			post_link +=  "?AC=NEXT_BLOCK" + post_params;
			post_link += "' class='" + npLinkCss + "'>";
			post_link += npPageRangePreText + j + npPageRangePostText;
			post_link +="</a> ";	
					
		}
	}
	
	current_write = "<span class='" + npTextCss + "'>" + npPageRangePreText + current_page + npPageRangePostText + "</span> "
	
	return pre_link + current_write + post_link;	
}


/*
Next/previous RECORD section
Use in full display where one record shown at a time
*/

function nextprevRecord_First()
{
	var params = window.dbtw_params;
	var first_write = npFirstText;
	var first_link = "";

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	
	// determine if a link should be made
	if (rn > 0)
	{
		var first_params = params.replace((/RN=(\d+)/), "RN=1");
		first_link += "<a href='" + currentUrl;
		first_link +=  "?AC=PREV_RECORD" + first_params;
		first_link += "' class='" + npLinkCss + "'>";
		first_link += first_write + "</a>";
	}
	else
	{
		first_link = "<span class='" + npTextCss + "'>"+first_write+"</span>";	
	}

	return first_link;

} //end fxn

function nextprevRecord_Previous()
{
	var params = window.dbtw_params;
	var prev_write = npPreviousText;
	var prev_link = "";

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// determine if a link should be made
	if (rn > 0)
	{
		prev_link += "<a href='" +  currentUrl;
		prev_link +=  "?AC=PREV_RECORD" + params;
		prev_link += "' class='" + npLinkCss + "'>";
		prev_link += prev_write + "</a>";
	}
	else
	{
		prev_link = "<span class='" + npTextCss + "'>"+prev_write+"</span>";	
	}

	return prev_link;

} //end fxn

function nextprevRecord_Next(record_count)
{
	var params = window.dbtw_params;
	var next_write = npNextText;
	var next_link = "";
	var rc = parseInt(record_count);

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);
	
	// determine if a link should be made
	if (rn+1 < rc)
	{
		next_link += "<a href='" + currentUrl;
		next_link +=  "?AC=NEXT_RECORD" + params;
		next_link += "' class='" + npLinkCss + "'>";
		next_link += next_write + "</a>";
	}
	else
	{
		next_link = "<span class='" + npTextCss + "'>"+next_write+"</span>";	
	}

	return next_link;

} //end fxn

function nextprevRecord_Last(record_count)
{
	var params = window.dbtw_params;
	var last_write = npLastText;
	var last_link = "";
	var rc = parseInt(record_count);

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);
	
	// determine if a link should be made
	if (rn+1 < rc)
	{
		var last_params = params.replace((/RN=(\d+)/), "RN=" + (rc-2));
		last_link += "<a href='" + currentUrl;
		last_link +=  "?AC=NEXT_RECORD" + last_params;
		last_link += "' class='" + npLinkCss + "'>";
		last_link += last_write + "</a>";
	}
	else
	{
		last_link = "<span class='" + npTextCss + "'>"+last_write+"</span>";	
	}

	return last_link;

} //end fxn



/*
 This back_to_block method written by Terry McBride tttmcbride@yahoo.com
 This method takes you from a display of an expanded record back to  the list from the position that the current expanded record would be in.
*/

function back_to_block() 
{
	var params = window.dbtw_params;

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);

	// find the lower multiple of mr and add another mr to it to fool PREV_BLOCK
	var remainder = (rn % mr);
	var start = (rn - remainder + mr); 

	var i = params.indexOf("RN=") + 3;
	var j = params.indexOf("&", i);

	var url = currentUrl + "?AC=PREV_BLOCK" + params.substring (0, i) + start + params.substring(j, params.length);

	window.location.href = url; 

}
	
function back_to_block_cookie(Textbase) 
{
	var params = window.dbtw_params;

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);

	// find the lower multiple of mr and add another mr to it to fool PREV_BLOCK
	var remainder = (rn % mr);
	var start = (rn - remainder + mr); 

	var i = params.indexOf("RN=") + 3;
	var j = params.indexOf("&", i);

	var url = currentUrl + "?AC=PREV_BLOCK" + params.substring (0, i) + start + params.substring(j, params.length);
	
	document.cookie = Textbase + "_DbtwLastResult=" + url + "; path=/";
	
}


function back_to_record_cookie(Textbase, RecordNumber)
{
	var params = window.dbtw_params;
	var rn = parseInt(RecordNumber) - 1;
	var i = params.indexOf("RN=") + 3;
	var j = params.indexOf("&", i);
	
	// reconstruct the link with only the RN value replaced
	var url = currentUrl + "?AC=GET_RECORD" + params.substring (0, i) + rn + params.substring(j, params.length);
	document.cookie = Textbase+ "_DbtwFullRecord=" + url + "; path=/";
}

function record_count_cookie(Textbase, RecordCount)
{
	document.cookie = Textbase + "_RecordCount=" + RecordCount + "; path=/";
}




/*
Kept for backwards compatibility
*/

/*
function nextprev_block(record_count)
{
	var params = window.dbtw_params;
	var next_write = "";
	var next_link = "";
	var prev_write = "";
	var prev_link = "";
	var nextprev_link = "";
	var rc = parseInt(record_count);

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	// MR is records at a time
	var mrs = params.match(/MR=(\d+)/);
	var mr = parseInt(mrs[1]);
	
	//comparison number for next block
	var next_total = (rc - mr - rn);
	
	//comparison number for prev block
	var prev_total = (rn - mr);
	
	// determine if a "Previous x Records" link should be made
	if (prev_total >= 0)
	{
		prev_write = "Previous " + mr + " Records";	

		//prev_link = "<form>";
		prev_link += "<a class='NavBar_Links' href='" + currentUrl;
		//prev_link += "<input type='button' value='" + prev_write + "' onClick='dbtw_prev(\"";
		prev_link +=  "?AC=PREV_BLOCK" + params;
		prev_link += "\")' >";
		prev_link += prev_write + "</a>&nbsp;";
		//prev_link += "</form>";
	}
	
	// determine if a "Next x Records" link should be made
	if (next_total < mr && next_total > 0)
	{
		next_write = "Next " + next_total + " Records";
	}
	else if (next_total >= mr)
	{
		next_write = "Next " + mr + " Records";
	}

	if (next_write != "")
	{
		//next_link = "<form>";
		next_link += "<a class='NavBar_Links' href='" + currentUrl;
		//next_link += "<input type='button' value='" + next_write + "' onClick='dbtw_next(\"";
		next_link +=  "?AC=NEXT_BLOCK" + params;
		next_link += "\")' >";
		next_link += next_write + "</a>";
		//next_link += "</form>";
	}

	// build the final string to write into HTML
	if (next_link == "" && prev_link == "")
	{
		nextprev_link = "";	
	}
	else if (next_link != "" && prev_link == "")
	{
		nextprev_link += next_link;
	}
	else if (next_link == "" && prev_link != "")
	{
		nextprev_link += prev_link;	
	}
	else
	{ 
		nextprev_link += prev_link + "|&nbsp;" + next_link;		
	}

		
	return nextprev_link;

} //end fxn


function nextprev_record(record_count)
{
	var params = window.dbtw_params;
	var next_write = "";
	var next_link = "";
	var prev_write = "";
	var prev_link = "";
	var nextprev_link = "";
	var rc = parseInt(record_count);

	// find the record number
	var rns = params.match(/RN=(\d+)/);
	var rn = parseInt(rns[1]);

	
	// determine if a "Previous Record" link should be made
	if (rn > 0)
	{
		prev_write = "Previous Record";

		//prev_link = "<form>";
		prev_link += "<a class='NavBar_Links' href='" + currentUrl;
		//prev_link += "<input type='button' value='" + prev_write + "' onClick='dbtw_prev(\"";
		prev_link +=  "?AC=PREV_RECORD" + params;
		prev_link += "\")' >";
		prev_link += prev_write + "</a>&nbsp;";
		//prev_link += "</form>";
	}
	
	// determine if a "Next Record" link should be made
	if (rn+1 < rc)
	{
		next_write = "Next Record";

		//next_link = "<form>";
		next_link += "<a class='NavBar_Links' href='" + currentUrl;
		//next_link += "<input type='button' value='" + next_write + "' onClick='dbtw_next(\"";
		next_link +=  "?AC=NEXT_RECORD" + params;
		next_link += "\")' >";
		next_link += next_write + "</a>";
		//next_link += "</form>";
	}

	// build the final string to write into HTML
	if (next_link == "" && prev_link == "")
	{
		nextprev_link = "";	
	}
	else if (next_link != "" && prev_link == "")
	{
		nextprev_link += next_link;
	}
	else if (next_link == "" && prev_link != "")
	{
		nextprev_link += prev_link;	
	}
	else
	{ 
		nextprev_link += prev_link + "|&nbsp;" + next_link;		
	}

		
	return nextprev_link;

} //end fxn

*/