
////////////////////////////////////////////////////////////////////////////////
// These functions handle URLs in the following "HTML form GET method" format:
//
//    <ASP_URL>     := <beginning_part>[?<querystring>][#docLoc]
//    <querystring> := <key_pair>[{&<key_pair>}]
//    <key_pair>    := <name>=<value>
//
//  Note the characters {'?','&', '='} must not appear in <beginning_part>.
//  All <name>s should generally be alphanumeric only, but both <name> and
//  <value> are assumed to be encoding using JavaScript's escape() functions.
//  This module uses escape() and unescape() to maintain this state.
//  Finally, note all named tokens must have length > 1. In particular note
//  that if emptystring is specified for a <value> in the replace function,
//  that signifies a delete of the assocated <key_pair>.

function request_querystring(strKeyName)
{
	var strURL;
	strURL = document.location.href
	return request_querystringEx(strKeyName, strURL)
}

function request_keys(strURL)
{
	if(!strURL)
		strURL = document.location.href
	
	strQS = request_querystringEx(null, strURL)
	
	straKeyPairs = strQS.split("&")
	straKeys = new Array()
	for(i=0; i<straKeyPairs.length; i++)
		straKeys.push(straKeyPairs[i].split("=")[0])
	
	return straKeys
}

function getScriptName()
{
    // https://something.com/somewhere/else/script.aspx?q=2&d=2
    // https://something.com/somewhere/else/script.aspx
    // https://something.com/somewhere/else/
    url = document.location.href
    
	qMark = url.indexOf("?")
	if(qMark < 0)
	{
        lastSlash = url.lastIndexOf("/")
        dot = url.lastIndexOf(".")
    }
    else
    {
        lastSlash = url.lastIndexOf("/", qMark-1)
        dot = url.lastIndexOf(".", qMark-1)
    }
    
    if(dot < 0)
        return "Default"
    else
        return url.substring(lastSlash+1, dot)
}

function request_querystringEx(strKeyName, strURL)
{
	////If the current URL is an <ASP_URL>, this function returns the <value>
	//  associated with the given <name>, strKeyName. Or if strKeyName is
	//  undefined, returns the entire <querystring>
	
	if(!strKeyName)
	{
		iQMark = strURL.indexOf("?")
		
		if(iQMark > 0 && iQMark < strURL.length-1)
		{
			var strReturn = strURL.substr(iQMark+1)
			if(strReturn.indexOf("#") > 0)
				return strReturn.substr(0, strReturn.indexOf("#"))
			else
				return strReturn
		}
		else
			return ""
	}
	
	iKeyPairStart = 1 + strURL.indexOf("?" + strKeyName + "=")
	if(iKeyPairStart == 0)
		iKeyPairStart = 1 + strURL.indexOf("&" + strKeyName + "=")
	
	if(iKeyPairStart == 0)
	{
		//Key not found
		return ""
	}
	else
	{
		strRightPart = strURL.substring(iKeyPairStart, strURL.length)
		iValueStart = 1 + strRightPart.indexOf("=")
		
		if(iValueStart == 0)
		{
			//Malformed URL
			return ""
		}
		
		strRightPart = strRightPart.substring(iValueStart, strRightPart.length)
		
		iAmp = strRightPart.indexOf("&")
		
		if(iAmp == -1)
		{
			// Must be the last key_pair in the querystring.
			if(strRightPart.indexOf("#") > 0)
				return unescape(strRightPart.substr(0, strRightPart.indexOf("#")))
			else
				return unescape(strRightPart)
		}
		else
		{
			return unescape(strRightPart.substring(0, iAmp))
		}
	}
}

function strURLReplaceQSPair(strName, strValue)
{
	strURL = document.location.href
	return strURLReplaceQSPairEx(strName, strValue, strURL)
}

function strURLReplaceQSPairEx(strName, strValue, strURL)
{
	////If the current URL is an <ASP_URL>...
	//  - If strName is a <name>, this function replaces the associated
	//    <value> to strName with the new <value>, strValue.
	//  - Otherwise, this function adds a new <key_pair> to the <ASP_URL>
	//    with the given <name> and <value>, strName and strValue.
	//
	//  NOTE: Returns emptystring if the URL is malformed

	strOldValue = request_querystringEx(strName, strURL)
	
	if(strOldValue == "")
	{
		//No matching pair, so add it:
		if(strValue != "")
		{
			//Add new value to the beginning of the <querystring>:
			iQMark = strURL.indexOf("?")

			if(iQMark == -1)
			{
				strURL = strURL + "?" + escape(strName) + "=" + escape(strValue)
			}
			else
			{
				strRightSide = strURL.substring(iQMark + 1, strURL.length)
				strLeftSide  = strURL.substring(0, iQMark + 1)
				
				strURL = strLeftSide + escape(strName) + "=" + escape(strValue) + "&" + strRightSide
			}
		}
		//else: actually is a delete, so leave unchanged.
	}
	else
	{
		//Replace the existing value:
		iQMark = strURL.indexOf("?")

		iValueStart = strURL.indexOf(escape(strName) + "=") + (escape(strName) + "=").length
		strLeftSide = strURL.substring(0, iValueStart)
		strRightSide = strURL.substring(iValueStart, strURL.length)

		iAmp = strRightPart.indexOf("&")
		
		if(iAmp == -1)
		{
			// Must be the last key_pair in the querystring.
			if(strValue != "")
			{
				strURL = strLeftSide + strValue
			}
			else
			{
				//actually want to delete the <name> from the querystring.
				//also delete the lefthand seperator, either '?' or '&':
				iKeypairStart = strLeftSide.lastIndexOf(escape(strName) + "=")
				strLeftSide = strLeftSide.substring(0, iKeypairStart)
				strURL = strLeftSide
			}
		}
		else
		{
			if(strValue != "")
			{
				strRightSide = strRightSide.substring(iAmp, strRightSide.length)
				strURL = strLeftSide + escape(strValue) + strRightSide
			}
			else
			{
				//actually want to delete the <name> from the querystring.
				//at this point there exists at least one other <key-pair> 
				//to the right. So delete the righthand token, either '?' or
				//'&', as well as this <key-pair>:
				strRightSide = strRightSide.substring(iAmp + 1, strRightSide.length)
				iKeypairStart = strLeftSide.lastIndexOf(escape(strName) + "=")
				strLeftSide = strLeftSide.substring(0, iKeypairStart)
				strURL = strLeftSide + strRightSide
			}
		}
	}

	return strURL
}