/********************************/
/* GENERAL JAVASCRIPT FUNCTIONS */
/********************************/

// Get an object by it's ID - cross browser function
function get_object_by_ID(objectId) {
	var object;
	// check W3C DOM, then MSIE 4, then NN 4
	// Adapted from http://developer.apple.com/internet/javascript/dynamicforms.html
	if(document.getElementById && document.getElementById(objectId))
		object = document.getElementById(objectId);
	else if (document.all && document.all(objectId))
		object = document.all(objectId);
	else if (document.layers && document.layers[objectId])
		object = document.layers[objectId];
	else
		return false;
	return object;
}

// Get a style object from its ID
function get_style_object(objectId) {
	var object = get_object_by_ID(objectId);
	if (object)
		return object.style;
	return false;
}

// Set an object (identified by it's id attribute) to be displayed or not, using CSS
function set_object_display(objectId, visible) {
	var style_object = get_style_object(objectId);
 
	if (visible)
		style_object.display = 'block';
	else
		style_object.display = 'none';
	return true;
}

// Utility function to hide several objects in one go
function hide_objects(objectIdArray) {
	for (var i=0; i<objectIdArray.length; i++) {
		set_object_display(objectIdArray[i], false);
	}
	return true;
}
// Utility function to show several objects in one go
function show_objects(objectIdArray) {
	for (var i=0; i<objectIdArray.length; i++) {
		set_object_display(objectIdArray[i], true);
	}
	return true;
}


// Set an object (identified by it's id attribute) to be displayed or not, using CSS
// If it is currently hidden, it will be displayed and vice-versa.
function toggle_object_display(objectId) {
	var style_object = get_style_object(objectId);
 
	if (style_object.display == 'none')
		style_object.display = 'block';
	else
		style_object.display = 'none';
	return true;
}

// Set an object (identified by it's id attribute) to be visible or not, using CSS(2)
function set_object_visibility(objectId, visible) {
	var style_object = get_style_object(objectId);
 
	if (visible)
		style_object.visibility = 'visible';
	else
		style_object.visibility = 'hidden';
	return true;
}

// Set an object's CSS class
function set_CSS_class(object, newClassName) {
	object.className = newClassName;
	return true;
}
// Get an object's CSS class
function get_CSS_class(object) {
	return object.className;
}

// Set the given select options to the given array
function set_select_options(selectElement, optionsArray) {
	selectElement.length = 0;
	for (var i=0; i<optionsArray.length; i++) {
		selectElement[i] = new Option(optionsArray[i], i);
	}
	return true;
}

// Open Product Window - Opens a new window with the product details of the given product ID
function open_product_window(productID) {
	var url = '/product_details.php?prod_id=' + productID;
	window.open(url, '');
	return true;
}

// Opens a popup window and bring it to the front.
function open_popup_window(url, name, features) {
	var newWin = window.open(url, name, features);
	newWin.focus();
}

// Test membership of an item in an array
function in_array(theItem, theArray) {
	for (var i = 0; i < theArray.length; i++) {
		if (theItem == theArray[i])
			return true;
	}
	return false;
}

// Computes the difference of arrays. Returns an array of array1 - array2.
function array_diff(array1, array2) {
	var diff = new Array();
	for (var i=0; i<array1.length; i++) {
		if (!in_array(array1[i], array2))
			diff[diff.length] = array1[i];
	}
	return diff;
}

// Preload images: provide as a list of names and filenames
function preload_images() {
	if (document.images) {
		var argv = preload_images.arguments;
		var argc = argv.length;
		if (argc>0 && (argc%2)==0) {
			if (!document.preloadedImages)
				document.preloadedImages = new Array();
			for (var i=0; i<argc; i=i+2) {
				document.preloadedImages[argv[i]] = new Image();
				document.preloadedImages[argv[i]].src = argv[i+1];
			}
		}
	}
}

// Swap an image (with one already preloaded)
function swap_image(imgName, newImgFile) {
	if (document.images && document.preloadedImages)
		document.images[imgName].src = document.preloadedImages[newImgFile].src;
}

// Sets the value of a field in a form
function set_field_value(field, val) {
	var x = get_object_by_ID(field);
	
	x.value = val;
}

