// swapImageByAbbr -- replace an image
// imageObj is the image object to swap, e.g. document.myimage
// abbr is an index into associative array hash, whose value will be the new src
function swapImageByAbbr (imageObj, abbr, hash) {
	if (document.images)
		imageObj.src = hash[abbr];
}

// toggleLegend(url, inputObj) -- if the legend window is open, close it and
// set inputObj (probably a hidden form field) to "no".  If the legend window
// is closed, open it to url and set inputObj to "yes"
function toggleLegend (url, inputObj) {
	if (!legendWindow || legendWindow.closed) {
		popupLegendWindow(url);
		if (inputObj) inputObj.value = "yes";
	} else {
		legendWindow.close();
		if (inputObj) inputObj.value = "no";
	}
}

// popupLegendWindow -- pop up a window to the right of the browser for the
// supplied URL (presumably a VoyagerJr legend)
var legendWindow;
function popupLegendWindow (url) {
	if (legendWindow && !legendWindow.closed) {
		return;  // window already open
	}
	legendWindow = window.open(url, "jvvjrlegend", "width=280,height=360,menubar=yes,toolbar=no,location=no,scrollbars=yes,resizable=yes");
	var scwidth = 800;
	if (screen.width)
		scwidth = screen.width;
	var x, y;
	y = window.screenY + 100;
	if (window.screenX + window.outerWidth + 280 > scwidth)
		x = scwidth - 280;  // as little overlap as possible
	else
		x = window.screenX + window.outerWidth;  // along the side
	
	if (legendWindow)  // not defined if popups blocked, frex
		legendWindow.moveTo(x, y);
}


// launchHelp(url) -- opens url in a new window ... TODO


// makeCGIURL(formObj, scriptname, [ name1, value1, ... nameN, valueN ]) --
// construct a URL by constructing name/value pairs from each (sensible)
// element in formObj and the passed array object/literal, and then sticking
// those on to scriptname.  Any element named in myValues won't be included
// from the form.  (As a consequence, you can't add to a multiple select,
// but you can just tack those on after.) This function is useful for
// constructing link hrefs to CGI scripts based on currently-selected
// form contents.
function makeCGIURL (formObj, scriptname, nameValPairs) {
	var url = scriptname;
	if (url.charAt(url.length-1) != "?") url += "?";
	var glue = ";";
	var myValues = new Array;;  // user supplied values
	var pieces = new Array();
	
	for (var i = 0; i < nameValPairs.length; i+= 2)
		myValues[nameValPairs[i]] = nameValPairs[i+1];
	
	// construct the user's name/value pairs
	for (var key in myValues) 
		pieces.push(key + "=" + myValues[key]);

	// construct the form's name/value pairs
	var element;
	for (var i = 0; i <  formObj.elements.length; i++) {
		element = formObj.elements[i];
		if (myValues[element.name])
			continue;  // element overridden

		// make sure this is a sensible element to use
		// unsensible elements include (submit) buttons
		if (element.type == "hidden" || element.type == "text" || element.type == "textarea" || element.type == "password" || ((element.type == "radio" || element.type == "checkbox") && element.checked))
			pieces.push(element.name + "=" + element.value);
		// select boxes
		else if (element.type == "select-one" || element.type == "select-multiple") {
			for (var j = 0; j < element.options.length; j++)
				if (element.options[j].selected == true)
					pieces.push(element.name + "=" + element.options[j].value);
		}
	}
	url += pieces.join(glue);
	return url;
}

// makeCGIURLClosure(formObj, scriptname) -- returns a function that takes
// an associative array of values to use in building a URL from formObj to
// submit to scriptname.  This function works like makeCGIURL with the first
// two parameters fixed
function makeCGIURLClosure(formObj, scriptname) {
	return function () {
		//var closureArgs = [formObj, scriptname];
		//arguments.unshift(formObj, scriptname);
		//makeCGIURL.apply(makeCGIURL, closureArgs.concat(arguments));
		makeCGIURL(formObj, scriptname, arguments);
	}
}


// conditionalObjVisibility(obj, cond) -- if cond is true, set obj's
// style.visibility property visible; if cond is false, set it to hidden
function conditionalObjVisibility(obj, cond) {
	if (cond)
		obj.style.visibility = 'visible';
	else
		obj.style.visibility = 'hidden';
}


// CyclingImage(imgObj, 'img1', 'img2', ...)  -- creates a cycling image
// object.  To change the image that imgObj displays, call myCycImg.cycle()
// To preload all of the images, call myCycImg.preload()
function CyclingImage (imgObj) {
	this.imageObject = imgObj;
	this.src = new Array();
	for (var i = 1; i < arguments.length; i++)
		this.src[i-1] = arguments[i];
	this.cursrc = 0;
	if (imgObj)
		imgObj.src = this.src[this.cursrc];

	this.cycle = function () {
		if (this.imageObject['src']) {
			this.cursrc = (this.cursrc + 1) % this.src.length;
			this.imageObject.src = this.src[this.cursrc];
		}
	}

	this.preload = function () {
		this.imgObj = new Array();
		for (var i = 0; i < this.src.length; i++) {
			this.imgObj[i] = new Image;
			this.imgObj[i].src = this.src[this.cursrc];
		}
	}
}
