function isIE6()
{
	return (navigator.appName == 'Microsoft Internet Explorer') && navigator.appVersion.indexOf('MSIE 6') != -1;
}

function isIE7()
{
	return (navigator.appName == 'Microsoft Internet Explorer') && navigator.appVersion.indexOf('MSIE 7') != -1;
}

function isIE()
{
	return (navigator.appName == 'Microsoft Internet Explorer');
}

// http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function cookie_exists( cookie_name )
{
	return (document.cookie && document.cookie.match(new RegExp('\b' + cookie_name + '\b')));
}

function Hash()
{
	this.length = 0;
	this.items = new Array();

	this.remove = function( key )
	{
		if( typeof(this.items[key]) != undefined )
		{
			this.length--;
			delete this.items[key];
		}
	}

	this.set = function( key, value )
	{
		if( typeof(this.items[key]) == undefined )
		{
			this.length++;
		}
		this.items[key] = value;
	}

	this.get = function( key )
	{
		return this.items[key];
	}

	this.hasKey = function( key )
	{
		return (typeof(this.items[key]) != undefined);
	}
}

function create_XMLHttpRequest()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}

function unique_get( url )
{
	return  url.replace(/&rnd=\d+/, '') + '&rnd=' + Math.floor(Math.random() * 1001);
}

function init_character_counter( input, label_widget, warning_widget, warning_display )
{
	setInterval(
		function() {
			var max_length = null;
			var cur_length = null;
			if( input.nodeName.toUpperCase() == 'TEXTAREA' )
			{
				max_length = parseInt(input.getAttribute('maxlength'));
				cur_length = input.value.length;
				if( input.value.length >= max_length )
				{
					if( warning_widget != null )
					{
						warning_widget.style.display = warning_display;
					}

					if( input.value.length > max_length )
					{
						input.value = input.value.substring(0, max_length);
						input.scrollTop = input.scrollHeight;
					}
				}
				else if( warning_widget != null )
				{
					warning_widget.style.display = 'none';
				}
			}
			else
			{
				max_length = input.maxLength;
				cur_length = input.value.length;
			}
			label_widget.innerHTML = (max_length - cur_length);
		},
		250);
}

function check_all( widget )
{
	var input;
	for( var i = 0; i < widget.form.elements.length; i++ )
	{
		input = widget.form.elements[i];
		if( input.type == 'checkbox' && input != widget && input.disabled == false )
		{
			input.checked = widget.checked;
		}
	}
}

function element_position( el )
{
	if ( el == null )
	{
		console.log('The element that you are trying to obtain an offset for does not exist or is inside a hidden div.');
	}
	var offsetTop = 0;
	var offsetLeft = 0;
	while( el.offsetParent )
	{
		offsetTop += el.offsetTop;
		offsetLeft += el.offsetLeft;
		el = el.offsetParent;
	}

	return { 'x': offsetLeft, 'y': offsetTop };
}

function ie6_rerender( el )
{
	if( document.compatMode && !isIE7() )
	{
		el.parentNode.style.zoom = '1';
		el.parentNode.style.zoom = 'normal';
	}
}


var last_dropdown = null;
function dropdown( dropdown_id, anchor_widget )
{
	var dropdown = document.getElementById(dropdown_id);
	if( last_dropdown != null )
	{
		last_dropdown.hide();
	}
	var offsetTop = 0;
	var offsetLeft = 0;
	var el = anchor_widget;
	while( el.offsetParent )
	{
		offsetTop += el.offsetTop;
		offsetLeft += el.offsetLeft;
		el = el.offsetParent;
	}

	if( arguments[2] != undefined )
	{
		offsetTop += arguments[2];
	}

	if( arguments[3] != undefined )
	{
		offsetLeft += arguments[3];
	}

	var old_class = anchor_widget.className;
	anchor_widget.restoreClass = function() { anchor_widget.className = old_class; ie6_rerender(anchor_widget); };

	anchor_widget.className += ' selected';

	el.insertBefore(dropdown, el.lastChild);

	dropdown.style.display = 'block';

	var docdimensions = document_dimensions();

	if( offsetTop + dropdown.offsetHeight < docdimensions.winheight + docdimensions.top )
	{
		dropdown.style.top = (offsetTop + anchor_widget.offsetHeight).toString() + 'px';
	}
	else
	{
		dropdown.style.top = (docdimensions.winheight + docdimensions.top - dropdown.offsetHeight).toString() + 'px';
	}

	if( offsetLeft + dropdown.offsetWidth < docdimensions.winwidth + docdimensions.left )
	{
		dropdown.style.left = offsetLeft.toString() + 'px';
	}
	else
	{
		dropdown.style.left = (docdimensions.winwidth + docdimensions.left - dropdown.offsetWidth - 18).toString() + 'px';
	}

	ie6_rerender(anchor_widget);

	last_dropdown = dropdown;
	last_anchor_widget = anchor_widget;
	dropdown.hide =
		function( e )
		{
			// If e is given, it should be an event from an onmouseout and will be used to
			// determine if the mouse left the dropdown or just moved between child elements
			var relatedTarget = null;
			if( e != undefined )
			{
				relatedTarget = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
			}

			if( (relatedTarget == null) ||
			    (this.contains && !this.contains(e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement)) ||
			    (this.compareDocumentPosition && ((this.compareDocumentPosition(relatedTarget) & 16) != 16)) )
			{
				this.style.display = 'none';
				anchor_widget.restoreClass();
			}
		};

	dropdown.onmouseout = function() {};

	dropdown.onmouseover =
		function()
		{
			if( anchor_widget.hidetimeout != undefined )
			{
				// Remove autohide, since user has moused over the dropdown
				window.clearTimeout(anchor_widget.hidetimeout);
			}
			anchor_widget.onmouseout = function() {};
			last_dropdown = null;
			this.onmouseout = function( e ) { this.hide(e ? e : window.event); };
		};

	anchor_widget.onmouseout =
		function( e )
		{
			var event = e ? e : window.event;

			var relatedTarget = null;
			if( event != undefined )
			{
				relatedTarget = event.relatedTarget ? event.relatedTarget : event.type == 'mouseout' ? event.toElement : event.fromElement;
			}

			if( (relatedTarget == null) ||
			    (this.contains && !this.contains(event.relatedTarget ? event.relatedTarget : event.type == 'mouseout' ? event.toElement : event.fromElement)) ||
			    (this.compareDocumentPosition && ((this.compareDocumentPosition(relatedTarget) & 16) != 16)) )
			{
				if( this.hidetimeout )
				{
					window.clearTimeout(this.hidetimeout);
				}
				this.hidetimeout = setTimeout(function() { dropdown.hide(); }, 100);
			}
		};
}

function getElementsByClassName( oElm, strTagName, strClassName )
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(\\s|^)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function cycle_class_names( node_list, class_list )
{
	var re_string = '';
	for( i in class_list )
	{
		re_string += '\\b' + class_list[i] + '\\b|';
	}
	re_string = re_string.substring(0, re_string.length - 1);

	var re = new RegExp(re_string, 'g');

	for( i in node_list )
	{
		var el = node_list[i];

		el.className = el.className.replace(re, '');

		el.className += ' ' + class_list[i % class_list.length];
	}
}

function form_vars_encoded( form )
{
	var url_string = '';
	for( i in form.elements )
	{
		var el = form.elements[i];
		var tagName = null;
		try
		{
			tagName = el.tagName;
		}
		catch( e )
		{
		}
		if( (tagName == 'INPUT' && el.type == 'text') || (tagName == 'TEXTAREA') )
		{
			url_string += encodeURIComponent('&' + el.name + '=' + el.value);
		}
	}

	return encodeURIComponent(url_string);
}

/* page change tracking */
var page_element_status = new Hash();
var page_saved = false;

function page_change_toggle( el_id )
{
	if( page_element_status.hasKey(el_id) )
	{
		page_element_status.set(el_id, !page_element_status.get(el_id));
	}
	else
	{
		page_element_status.set(el_id, true);
	}
}

function page_change_set( el_id, changed )
{
	page_element_status.set(el_id, changed);
}

function page_has_changed()
{
	if( page_saved )
	{
		return false;
	}

	for( key in page_element_status.items )
	{
		if( page_element_status.get(key) )
		{
			return true;
		}
	}

	return false;
}

function html_to_text( text_or_element )
{
	var text = '';

	if( typeof(text_or_element) == 'string' )
	{
		var div_el = document.createElement('div');
		div_el.innerHTML = text_or_element;
		text = html_to_text(div_el);
	}
	else
	{
		for( var i = 0; i < text_or_element.childNodes.length; i++ )
		{
			var el = text_or_element.childNodes[i];
			if( el.nodeName == 'BR' )
			{
				text += '\n';
			}
			else if( el.nodeName == 'P' || el.nodeName == 'DIV' )
			{
				text += '\n' + html_to_text(el) + '\n';
			}
			else if( el.nodeType == 3 )
			{
				text += el.nodeValue.replace(/ {2,}/g, ' ').replace(/^\t+/g, '').replace(/\t+/g, ' ').replace(/\n/g, '');
			}
			else
			{
				text += html_to_text(el);
			}
		}
	}

	return text;
}

function hover_element(element_id, positioning_element_id, left_inc, top_inc)
{
	var pos = element_position(document.getElementById(positioning_element_id));

	var new_y = parseInt(pos['y']) + parseInt(top_inc);
	var new_x = parseInt(pos['x']) + parseInt(left_inc);

	var element_id = document.getElementById(element_id);

	element_id.style.position='absolute';
	element_id.style.top='' + new_y + 'px';
	element_id.style.left='' + new_x + 'px';

	document.body.insertBefore(element_id, document.body.lastChild);

	element_id.style.display='none';
}

function add_onload( func )
{
	var old_onload = window.onload;

	window.onload = function() {
		if( typeof(old_onload) == 'function' )
		{
			old_onload();
		}

		try
		{
			func();
		}
		catch( exception )
		{
			// do nothing
		}
	};
}

function resizeFileWidget( widget_name )
{
	var container = document.getElementById('_fileInputContainer_' + widget_name);
	var textInput = document.getElementById('_text_' + widget_name);
	var button = document.getElementById('_browseButton_' + widget_name);

	//alert(container.clientWidth - 10 - Math.max(85, button.clientWidth));

	textInput.style.width = (container.clientWidth - 10 - Math.max(85, button.clientWidth)) + 'px';
}

function document_dimensions()
{
	// Scroll position.
	var top = 0;
	var left = 0;
	if( document.documentElement && document.documentElement.scrollTop )
	{
		top = document.documentElement.scrollTop;
		left = document.documentElement.scrollLeft;
	}
	else if( document.body && document.body.scrollTop )
	{
		top = document.body.scrollTop;
		left = document.body.scrollLeft;
	}
	else if( window.pageYOffset )
	{
		top = window.pageYOffset;
		left = window.pageXOffset;
	}
	else if( window.scrollY )
	{
		top = window.scrollY;
		left = window.scrollX;
	}


	// Document height/width
	if( document.documentElement.scrollHeight )
	{
		// Firefox.
		height = document.documentElement.scrollHeight;
		width = document.documentElement.scrollWidth;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight )
	{
		// All except explorer on the mac.
		height = document.body.scrollHeight;
		width = document.body.scrollWidth;
	}
	else
	{
		// works in Explorer 6 Strict, Mozilla (not FF) and Safari
		height = document.body.offsetHeight;
		width = document.body.offsetWidth;
  	}

  	// Window height /width
  	var window_width = 0;
  	var window_height = 0;

  	if( window.innerHeight != undefined )
	{
		window_width = window.innerWidth;
		window_height = window.innerHeight;
	}
	else if( typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0 )
	{
		window_width = document.documentElement.clientWidth;
		window_height = document.documentElement.clientHeight;
	}
	else
	{
		window_width = document.getElementsByTagName('body')[0].clientWidth;
		window_height = document.getElementsByTagName('body')[0].clientHeight;
	}

  	return { 'docheight' : height, 'docwidth' : width, 'left' : left, 'top' : top, 'winwidth' : window_width, 'winheight' : window_height };
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}