// Array Utility functions

// Gets an entry from an array where each entry has a name property
function getByName(name, array) {
	var found = undefined;
	for (i=0; i<array.length; i++) {	
		if (name == array[i].name) {
			found = array[i];
			break;
		}
	}
	
	//if (found) alert ('getByName - found: ' + found.name);
	//else alert ('getByName - NOT found: ' + found.name);
	
	return found;
}

// Gets a random entry from an array
function getRandomEntry(array) {
	var index = Math.floor(Math.random()*array.length)
	return array[index]
}

// Returns true if a string is defined and is not empty
function strValid(str) {
	return str != undefined && str != null && str.length > 0;
}


function removeAllChildren(targetElement) {
    if (targetElement && targetElement.childNodes) {
        for (var i = targetElement.childNodes.length -1; i >= 0; i--) {
            targetElement.removeChild(targetElement.childNodes[i]);
        }
    }
	//alert(targetElement.childNodes.length);
}

// Override getElementById for IE
// From
//http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
if (/msie/i.test (navigator.userAgent))
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].attributes['id'].value == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

