﻿// JScript File

function SanitizeQueryString()
{
	var keys = ["p", "r"];

	var str = window.location.search;
	for (var i = keys.length - 1; i >= 0; i--)
	{
		str = StripQueryParam(keys[i], str);
	}

	return str;
}

function AddQueryParam(key, value, qs)
{

	qs = StripQueryParam(key, qs);
	qs = key+"="+value+ "&" + qs;
	qs = qs.replace("?","");
	
	return qs;
}

function StripQueryParam(parameter, qs)
{
	//qs = "&" + qs; //this creates an extra & in the qs, but it's needed to detect and remove parameter when it's the first 
	qs - qs.replace("?", "");

	var p = escape(unescape(parameter));
	if(qs.indexOf(parameter+"=") == 0)
	{
		qs = qs.substring( qs.indexOf("&")+1 );
		return qs
	}
	
	/**original code not reliable; if qs contains p and pid, removing p will match pid as well, causing collision; 
	 * i.e. i=23700&pid=4450 becomes i=23700id=4450 -- the &p are stripped
	 * The fix looks for a key + "=" match, so in the expression, p becomes p= and much more reliable.
	**/
	//var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?", "i"); 
	var regex = new RegExp("[?&]" + p + "=(?:([^&]*))?", "i");
	qs = qs.replace(regex, "");
	
	return qs;
}

function GetQueryParam(parameter, qs)
{
	qs = "&" + qs;
	var p = escape(unescape(parameter));
	var regex = new RegExp("[?&]" + p + "=(?:([^&]*))?", "i");

	var match = regex.exec(qs);
	var value = "";
	if (match != null)
	{
		value = match[1];
	}
	return value;
}


function FilterQty(oSender)
{
	oSender.value = oSender.value.replace(validations["Number"], "");
}


function BuildAndPage(page_path)
{
	var qs = window.location.search;
	qs = AddQueryParam("p", GetQueryParam("p", page_path), qs);
	qs = AddQueryParam("r", GetQueryParam("r", page_path), qs);
		
	if( qs.charAt(qs.length-1) == "&" ){ qs = qs.substring(0, qs.length-1) }
	
	window.location = location.pathname + "?" + qs;
}




function ChangeCurrentPage(sPageSelectId, sResultsPerPageSelectId) 
{
	var page_path =
		"p=" + (parseInt(document.getElementById(sPageSelectId).value) + 1) +
		"&r=" + document.getElementById(sResultsPerPageSelectId).value
	;
	BuildAndPage(page_path);
}

function ChangeResultsPerPage(sPageSelectId, sResultsPerPageSelectId) 
{
	page_path =
		"p=" +
		(
			parseInt
			(
				document.getElementById(sPageSelectId)
				? document.getElementById(sPageSelectId).value
				: "0"
			) + 1
		) +
		"&r=" + document.getElementById(sResultsPerPageSelectId).value
	;
	BuildAndPage(page_path);
}

function ChangeCurrentPageClick(sPageSelectId, sResultsPerPageSelectId, bNext) 
{
	try {
		if (!document.getElementById(sPageSelectId)) { return false; } //true if there is only one page; i.e. no page select menu

		if (bNext) {
			if (document.getElementById(sPageSelectId).selectedIndex + 1 >= document.getElementById(sPageSelectId).length) { return false; }
			document.getElementById(sPageSelectId).selectedIndex = document.getElementById(sPageSelectId).selectedIndex + 1;
		}
		else {
			if (document.getElementById(sPageSelectId).selectedIndex - 1 < 0) { return false; }
			document.getElementById(sPageSelectId).selectedIndex = document.getElementById(sPageSelectId).selectedIndex - 1;
		}


		page_path =
			"p=" + (document.getElementById(sPageSelectId).selectedIndex + 1) +
			"&r=" + document.getElementById(sResultsPerPageSelectId).value
		;

		BuildAndPage(page_path);
		return false;
	}
	catch (e) {
		alert(e);
		return false;
	}
}

