
if (typeof(log)=="undefined") {	log=function(msg)	{	alert("LOG ["+arguments.callee+"]: "+msg+" ");	}; if (window!=parent) try {parent.log(msg);}catch(e){}	}

//
// params.js
//
// Standalone JS object

//
// Description:	Helper function - constructs a params object
// Definition:	object getParams()
// Examples:	var oParamCtrl = getParams();
// Notes:	Never returns anything other than a params object
//
function getParams()
{
	var cmdLine = "";
	if (location.search.length > 1) cmdLine = location.search.substring(1);
	var params = new ParamList(cmdLine);
	return params;
}
//
// Description:	Constructor - creates a parameter list object.
// Definition:	object ParamList(string)
// Examples:	var oParamCtrl = new ParamList(location.search)
// 		var oParamCtrl = new ParamList("param1=value1&param2=&param3=value3")
// Notes:	Never returns anything other than a params object
//
function ParamList(sCmdLine)
{
	this.debugmode = false;

if (typeof(sCmdLine)!="string")	cmdLine = "";	// If parameter is not of type "string" assume empty string
else {
	cmdLine = sCmdLine;
	if (cmdLine.substring(0,1)=="?")	cmdLine = cmdLine.substring(1); // remove the '?' if there is one
}
var tempArray = cmdLine.split("&");
var i = 0;

	this.cmdLine = cmdLine;
	this.paramName = new Array()
	this.paramdata = new Array()
	this.paramIdx = -1; // initialize iterator index
	if (tempArray.length>0)
	{
		for (var i=0;i<tempArray.length;i++)
		{
			var pos=tempArray[i].indexOf("=");
			if (pos!=-1) {
				name=unescape(tempArray[i].substring(0,pos));
				this.paramName[i]=name;
				if ((pos+1)<tempArray[i].length)
					this.paramdata[name]=unescape(tempArray[i].substring(pos+1));
				else
					this.paramdata[name]="";
			}
			try {
				this.length = i+1;
			}
			catch(e)
			{
			}
		}
	}
	//
	// Methods:
	//
	this.getFirst	= ParamList_getFirst;		// string getFirst()	- returns the name of the first parameter (or null is no params)
	this.getNext	= ParamList_getNext;		// string getNext()	- returns the name of the next parameter (or null is no more params)
	this.getParamName=ParamList_getParamName;	// string getParamName(paramIdx) - returns the name of the #th parameter (starting at 0)

	this.getParamValue=ParamList_getParamValue;	// getParamValue(paramName, insensitive)	- get the value for the first parameter named as paramName. If insensitive is true the the case of the paramName is ignored
	this.setParamValue=ParamList_setParamValue;	// setParamValue(name, paramValue, insensitive)	- set the value for the first parameter named as paramName. If insensitive is true then parameter name is uppercased
	this.removeParam = ParamList_removeParam;	// removeParam(name)	- remove the parameter and its value

	this.getQueryString=ParamList_getQueryString;	// Build/rebuild a querystring from the contents for the parameter control object

	this.debugMessage = ParamList_debugMessage;	// debugMessage(string) - if debugmode == true display a debug message

	return this;
}
// Start of paramList object methods
// {
function ParamList_getParamValue(paramName, insensitive)
{
var novalue;
	if (typeof(insensitive)!="undefined") {
		var param = paramName.toUpperCase();
		for (i=0;i<this.paramName.length;i++)
		{
			if (this.paramName[i].toUpperCase()==param)
				return this.paramdata[this.paramName[i]];
		}
	}
	if (typeof(this.paramdata[paramName])=="undefined")
		return novalue;
	return this.paramdata[paramName];
}
function ParamList_getParamName(paramIdx)
{
	if (typeof(this.paramName[paramIdx])=="undefined")
		return null; // no more params
	return this.paramName[paramIdx];
}
function ParamList_getFirst()
{
	this.paramIdx = -1;
	return this.getNext();
}
function ParamList_getNext()
{
	this.paramIdx++;
	return this.getParamName(this.paramIdx);
}
function ParamList_setParamValue(name, paramValue, insensitive)
{
	if (typeof(insensitive)=="undefined") insensitive = false;
	if ((typeof(name)!="string") || (name!=""))
	{
		if (insensitive==true)
		{
			this.setParamValue(paramName.toUpperCase(), paramValue, false);
		}
		else
		{
			if (typeof(this.paramdata[name])=="undefined")
			{
				this.debugMessage("New parameter "+name+" added");
				if (this.paramName[this.paramName.length-1]=="")
					this.paramName[this.paramName.length-1]=name;
				else
					this.paramName[this.paramName.length] = name;
			}
			else this.debugMessage("Parameter "+name+" updated");
			this.paramdata[name] = paramValue;
		}
	}
	else this.debugMessage("Parameters must have a name (value is '"+paramValue+"')");
}
function ParamList_getQueryString()
{
var queryString = "";
	for (var i=0;i<this.paramName.length;i++)
	{
		if ( this.paramName[i] == "" ) continue;
		if ( typeof(this.paramdata[this.paramName[i]]) == "undefined") continue;

		if (i>0) queryString+="&"; else queryString="?";
		queryString += escape(this.paramName[i])+"="+escape(this.paramdata[this.paramName[i]]);
	}
	return queryString;
}
function ParamList_debugMessage(msg)
{
	if (this.debugmode==true) alert(msg);
}
function ParamList_removeParam(removeMe)
{
var novalue;
	this.paramdata[removeMe]=novalue;
	delete this.paramdata[removeMe];
	for (var i=0;i<this.paramName.length;i++)
	{
		if (this.paramName[i] == removeMe)
		{
			this.paramName.splice(i, 1); // unset the param name
			
			return true;
		}
	}
	return false;
}
// }
// End of paramList object methods

//
// Helper/Alterantive function
//
// String manipulation function
// From a query String (params) remove a parameter and the associated value by name (removeMe)
//
function removeParam(params, removeMe)
{
var ret_val = params;
	var pos = params.indexOf(removeMe+"=");
	if (pos == -1) {
		return params;
	}
	var end = params.indexOf("&", pos);
	if (end == -1) {
		ret_val=params.substr(0, pos-1);
		// check to see that there is nore than one parameter
		if (ret_val.substr(1,2)=="&")
			ret_val="?"+ret_val.substr(2,ret_val.length-1)
		return ret_val;
	}
	var startStr=params.substr(0, pos-1);
	var endStr=params.substr(end,params.length);
	ret_val=startStr+endStr;
	// check to see that there is nore than one parameter
	if (ret_val.substr(0,1)=="&")
		ret_val="?"+ret_val.substr(1,ret_val.length-1)
	if (ret_val.substr(1,2)=="&")
		ret_val="?"+ret_val.substr(2,ret_val.length-1)
	if (ret_val.length<3)
		ret_val="";
	return ret_val;
}
if (typeof(verboseLoad)!="undefined") if (verboseLoad==true) log("params.js loaded");
