function getCookieArg(argName,dflt)
{
	argVal = getArgument(argName)
	if (argVal==null)
	{
		cookie = getCookie(argName)
		if(cookie==null)
			argVal=dflt
		else
			argVal = cookie
	}
	
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	setCookie(argName,argVal,now)

	return(argVal)
}

function loadArgs()
{
	this.tile = getArgument('tile')
	
	this.bw = getCookieArg('bw','false')
	this.culvert = getCookieArg('culvert','true')
	this.habitat = getCookieArg('habitat','true')
	this.chanmod = getCookieArg('chanmod','true')
	this.fish = getCookieArg('fish','true')
	this.stream= getCookieArg('stream','true')
	this.road= getCookieArg('road','false')
	this.ortho= getCookieArg('ortho',this.orthodflt)
	this.topo= getCookieArg('topo',this.topodflt)
	this.streamid= getCookieArg('streamid','true')
	
	this.pathname=location.pathname
	if (this.pathname.substr(this.pathname.length-1,1)=='l')
		this.pathname = this.pathname.replace(/.html/g,".htm")
	else
		this.pathname = this.pathname.replace(/.htm/g,".html")
}

function copyArgs(myArgs)
{
	this.tile = myArgs.tile
	this.bw = myArgs.bw
	this.culvert= myArgs.culvert
	this.habitat= myArgs.habitat
	this.fish= myArgs.fish
	this.chanmod= myArgs.chanmod
	this.stream = myArgs.stream
	this.road = myArgs.road
	this.ortho = myArgs.ortho
	this.topo = myArgs.topo 
	this.streamid = myArgs.streamid
	this.pathname = myArgs.pathname
}

function setBroadbandDflts(broadBand)
{
	if (!broadBand)
	{
		this.orthodflt = 'true'
		this.topodflt = 'false'
	}
	else
	{
		this.orthodflt = 'false'
		this.topodflt = 'true'
	}
}

function getURL(myArgs)
{
	return('?tile='+this.tile+'&bw='+this.bw+'&culvert='+this.culvert+'&habitat='+this.habitat+'&fish='+this.fish+'&chanmod='+this.chanmod+'&stream='+this.stream+'&topo='+this.topo+'&streamid='+this.streamid+'&ortho='+this.ortho+'&road='+this.road)
}

function go()
{
	location.href = (location.protocol+'//'+location.host+this.pathname+this.getURL())
}

function goURL(newURL)
{
	location.href = (newURL+this.getURL())
}

function mapArgs(dflt)
{
	this.orthodflt = true
	this.topodflt = false
	this.tile = null
	this.topo = dflt
	this.streamid= dflt
	this.ortho = dflt
	this.bw = dflt
	this.habitat = dflt
	this.culvert = dflt
	this.fish = dflt
	this.chanmod = dflt
	this.stream = dflt
	this.road = dflt
	this.pathname = dflt
	this.loadArgs = loadArgs
	this.copyArgs = copyArgs
	this.setBroadbandDflts = setBroadbandDflts
	this.getURL = getURL
	this.go = go
	this.goURL = goURL
}



/***** Utilities *****/

function plusUnescape(str) { // the unescape function won't convert plus signs
	str = '' + str; // to spaces; like you see in search strings
	while (true) {
		var i = str.indexOf('+');
		if (i == -1) break;
		str = str.substring(0,i) + ' ' + str.substring(i+1,str.length);
	}
	return unescape(str);
}

function getArgument (theKey) {
	var args = new Array ();
	var argstring = window.location.search;
	if (argstring.charAt(0) != '?') return null; // no arguments
	argstring = argstring.substring(1, argstring.length);
	var argarray = argstring.split('&');
	for (var i=0; i < argarray.length; i++) {
		var singlearg = argarray[i].split('=');
		if (singlearg.length != 2) continue; // not a valid argument
		var argsKey = plusUnescape(singlearg[0]);
		var argsValue = plusUnescape(singlearg[1]);
		args [argsKey] = argsValue;
	}
	return args[('' + theKey)]
}

