/** toggleDisplay - takes an HTML element and toggles the display between 'none' and 'block'
 *
 *	@param		elementID - the id of the element to be toggled
 *
 */
function toggleDisplay(elementID)
{
	divObject = document.getElementById(elementID);
	if(divObject.style.display != "none")
	{
		divObject.style.display = "none";
	}
	else
	{
		divObject.style.display = "block";
	}
	
}

/** whiteBlocks - will change the backgroundColor to white for all the items in the passed array which holds element id's
 *
 *	@param			blockArray - an array with element id's as its values
 *
 */
function whiteBlocks(blockArray)
{
	for(i = 0; i < blockArray.length; i++)
	{
		document.getElementById(blockArray[i]).style.backgroundColor = '#FFFFFF';
	}
}
function highlightBlock(blockID,focusID,blockArray)
{
	whiteBlocks(blockArray);
	document.getElementById(blockID).style.backgroundColor = '#E8E3D3';
	if(focusID != ""){document.getElementById(focusID).focus();}
}

function hideBlocks(blockArray)
{
	for(i = 0; i < blockArray.length; i++)
	{
		document.getElementById(blockArray[i]).style.display = 'none';
	}
}
function showBlock(blockID,blockArray)
{
	hideBlocks(blockArray);
	document.getElementById(blockID).style.display = 'block';
}

Array.prototype.contains = function(element)
{
	for(var h = 0; h < this.length; h++)
	{
		if(this[h] == element)
		{
			return true;
		}
	}
	return false;
}

function open_window(path,width,height,window_name,scrollbars)
{
	if ( width === undefined ) { width = '800'; }
	if ( height === undefined ) { height = '710'; }
	if ( window_name === undefined ) { window_name = 'macu_window'; }
	if ( scrollbars === undefined ) { scrollbars = '1'; }
	window.open (path,window_name,"status=0,toolbar=0,menubar=0,location=0,resizable=0,height=" + height + ",width=" + width + ",scrollbars=yes");
}

/* from web */
Array.prototype.Contains = function(mxd,strict) {
for(i in this) {
if(this[i] == mxd && !strict) return true;
else if(this[i] === mxd) return true;
}
return false;
}
	
/* setDisplay - sets the style display property of a given element based on element id
 *
 * @param	elementID		The id of the element to set the display
 * @param	mode			The mode to set the display to
 *
 */
function setDisplay(elementID,mode){document.getElementById(elementID).style.display = mode;}

function getURL(location){ window.location = location; }

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
var Url = {
 
	// public method for url encoding
	encode : function (string) { return escape(this._utf8_encode(string)); },
 
	// public method for url decoding
	decode : function (string) { return this._utf8_decode(unescape(string)); },
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {	utftext += String.fromCharCode(c); }
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);	}
		} return utftext;
	},
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = ""; var i = 0; var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) { string += String.fromCharCode(c); i++; }
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3; }
		}
		return string;
	}
}

function set_plus(element_plus)
{	var plus_element = document.getElementById(element_plus);
	if(plus_element.innerHTML == '-'){ plus_element.innerHTML = '+'; }
	else if(plus_element.innerHTML == '+'){ plus_element.innerHTML = '-'; }
}

function set_select(eid,ov){ o = document.getElementById(eid); for(i=0; i <= o.options.length - 1; i++){if(o.options[i].value == ov){ o.options[i].selected = true; }}}

function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
