// The absolute core and most common javascript utilites, which 
// make writing JavaScript a bit more bearable.
var DEBUG = false;

// Equivalent of the perl "exists" keyword. This works everywhere I know of
function exists(object, key) { return (typeof object[key] != 'undefined') }

// Sets a property of an object, but only if it has been changed.
// We do not check to see if the object actually HAS the property,
// since we are trying to be fast.
function set(object, property, value) {
	if ( ! object ) {
		if ( DEBUG ) alert( 'Missing object with property "' + property + '"' );
		return false;
	}
	if ( object[property] == value ) return true;
	object[property] = value;
	return true;
}

// Convenient access to browser capability detection.
// We don't do "browser" detection as such, just adapt to each
// browser's capabilities as we find them.
var has = {
	all:            exists(document, "all"),
	getElementById: exists(document, "getElementById"),
	style:          exists(new Image(), "style"),
	innerHTML:      exists(document, "innerHTML")
	};

// Does the browser support the things needed to qualify as supporting
// our definition of level 2 JavaScript features.
has["javascript2"] = has.getElementById && has.style && has.innerHTML;

// Create an index (hash) of an array.
// Basically create an object where the keys are the values in the array,
// and the values are all true
function array2hash(array) {
	var hash = new Object();
	for ( var i in array ) { 
		hash[array[i]] = true; 
	}
	return hash;
}

// Finds a html element in the current document by id
function id(name) {
	var object = has.getElementById ? document.getElementById(name) // Standards (prefered)
		: has.all ? document.all[name]                          // Older IE
		: null;
	if ( DEBUG && ! object ) alert("Failed to find element with id '" + name + "'");
	return object;
}

// Find an image in the current document by name or by id
function image(name) {
	// Look for it by name
	var images = document.images;
	for ( var i = 0; i < images.length; i++ ) {
		if ( images[i].name == name ) return images[i];
	}

	// Look for it by id. If we find something make sure it
	// is ACTUALLY an Image by checking for the 'hspace' and 'src'
	// properties, which nothing else has both of.
	var img = id(name);
	if ( ! img ) return null;
	if ( (exists(img, 'hspace') && exists(img, 'src')) ) return img;
	if ( DEBUG ) alert( "Element with name '" + name + "' is not an image" );
	return null;
}

// Precache an image, without creating any variables in the DOM
function precache_image( src ) {
	(new Image()).src = src;
	return true;
}

// Basic visibility "show" function
function show(name) {
	var e = id(name);
	if ( ! e ) return false;
	if ( ! exists( e, "style" ) ) return false;
	if ( ! exists( e.style, "display" ) ) return false;
	set( e.style, "display", "" );
	return true;
}

// Basic visibility "show" function
function hide(name) {
	var e = id(name);
	if ( ! e ) return false;
	if ( ! exists( e, "style" ) ) return false;
	if ( ! exists( e.style, "display" ) ) return false;
	set( e.style, "display", "none" );
	return true;
}
