/********************
*
*	decodeParams
*
*	Very handily decode the parameters
*	passed to the location within the
*	query string
*
*	Hats off, Paul.
*
*	-But I'm not wearing any hats, Fred.
*
*********************/

	function decodeParams(incoming) {
		tempP = new Array();

		if(incoming.charAt(0)=='?')incoming=incoming.substring(1,incoming.length);

		while(incoming.length>0){
			tempPMid = incoming.indexOf('=');
			tempPEnd = incoming.indexOf('&');
			if(tempPEnd==-1)tempPEnd = incoming.length;

			if(tempPMid>-1 && tempPMid<tempPEnd-1){
				tempPKey = unescape(incoming.substring(0,tempPMid));
				tempPVal = unescape(incoming.substring(tempPMid+1,tempPEnd));
				tempP[tempPKey]=tempPVal;
			}
			incoming = incoming.substring(tempPEnd+1,incoming.length);
		}

		return tempP;
	}



/********************
*
*	getCookie and setCookie
*
*	Two functions, one to set the cookie,
*	and one to get the cookie.
*	Rocket science? One thinks not.
*
*********************/

	function getCookie(name) {
		var index = document.cookie.indexOf(name + "=");
		if (index == -1) return null;
		index = document.cookie.indexOf("=", index) + 1;
		var endstr = document.cookie.indexOf(";", index);
		if (endstr == -1) endstr = document.cookie.length;
		return unescape(document.cookie.substring(index, endstr));
	}

	function setCookie(name, value) {
		var today = new Date();
		var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days
		if (value != null && value != "")
		document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
	}

	function deleteCookie(name) {
		document.cookie=name + "=" + escape("1") + "; expires=-1";
	}



/********************
*
*	copyArray
*
*	Duplicates the passed array and returns
*	it. Eliminates redirection arrays and
*	creates a brand new duplicate.
*
*********************/


	function copyArray(whichArray){

		var i;
		tempArray = new Array();
		for (key in whichArray){
			tempArray[key] = whichArray[key];
		}
		return tempArray;

	}

	function parseArray(whichString){

		var i;
		var tempArray = new Array();
		tempArray = whichString.split(",");
		return tempArray;

	}



/********************
*
*	popupWindow
*
*	Open and set the properties of a popup
*	window by using the funciton below.
*
*	Usage
*	newWin = new popupWindow();
*	newWin.width = 10;
*	newWin.open();
*
*********************/

	function popupWindow(){

		this.left = 100;
		this.height = Math.floor((screen.availHeight / 3) * 2);
		this.fullscreen = 0;
		this.channelmode = 0;
		this.menubar = 1;
		this.name = "PopupWindow";
		this.resizable = 1;
		this.scrollbars = 1;
		this.status = 1;
		this.toolbar = 1;
		this.location = 0;		// added by Paul - allows control of the address bar! See also below.
		this.top = 100;
		this.url = "#";
		this.width = Math.floor((screen.availWidth / 3) * 2);
		this.open = function() {return window.open(this.url, this.name, "left=" + this.left + "," + "channelmode=" + this.channelmode + "," + "height=" + this.height + "," + "menubar=" + this.menubar + "," + "resizable=" + this.resizable + "," + "scrollbars=" + this.scrollbars + "," + "status=" + this.status + "," + "toolbar=" + this.toolbar + "," + "location=" + this.location + "," + "top=" + this.top + "," + "width=" + this.width, true)};
	}


/********************
*
*	openPopup
*
*	Uses a basic set of parameters from
*	above to open a popup window
*
*********************/


	function openPopup(whichLocation){
		newPopup = new popupWindow();
		if (frame['main'] && frame['main'].require && frame['main'].require["jaws"]){

			// code inserted by paul here

			newPopup.location = 1;
			newPopup.left = 0;
			newPopup.top = 0;
			newPopup.width = screen.availWidth - 10;
			newPopup.height = screen.availHeight - 180;

			newPopup.url = whichLocation;
		} else {
			newPopup.url = "launch.htm?launchURL=" + whichLocation;
		}
		newPopup.open();
		return 1;
	}



/********************
*
*	findReplace
*
*	A function built to implement the simple
*	find and replace functionality that javascript
*	has inexplicably omitted
*
*********************/

function findReplace (theString, searchString, replaceString, occurrences, backward){
	if (searchString == replaceString) {
		return theString;
	}
	var found = 0;
	if (backward == true){
		var pos = theString.lastIndexOf(searchString);
		while (pos>= 0) {
			found++;
			var startString = theString.substr(0, pos);
			var endString = theString.substr(pos + searchString.length);
			theString = startString + replaceString + endString;
			pos = theString.lastIndexOf(searchString, startString.length);
			if (found == occurrences) {
				pos = -1;
			}
		}
	} else {
		var pos = theString.indexOf(searchString);
		while (pos>= 0) {
			found++;
			var startString = theString.substr(0, pos);
			var endString = theString.substr(pos + searchString.length);
			theString = startString + replaceString + endString;
			pos = theString.indexOf(searchString, pos + replaceString.length);
			if (found == occurrences) {
				pos = -1;
			}
		}
	}
	return theString;
}


