var isIE = navigator.appVersion.indexOf( "MSIE" ) != -1;

var isIE8 = navigator.appVersion.indexOf( "MSIE 8" ) != -1;

var classAttributeName = ( isIE & !isIE8 ? "className" : "class" );

function log( value )
{
	if( element = document.getElementById( "Log" ) )
	{
		element.appendChild( document.createTextNode( value + "\n" ) );
	}
}

function logClear()
{
	if( element = document.getElementById( "Log" ) )
	{
		while( element.hasChildNodes() )
		{
			element.removeChild( this.element.firstChild );
		}
	}
}

function addInputSubmitEvent( form, input )
{
	input.onkeydown = function(e)
	{
		e = e || window.event;
		if (e.keyCode == 13) {
		form.submit();
		return false;
		}
	};
}

function addInputSubmitEventsToForms()
{
	if( isIE )
	{
		var forms = document.getElementsByTagName('form');
		
		for( var i = 0 ; i < forms.length ; i++ )
		{
			var inputs = forms[i].getElementsByTagName( "input" );
	
			for( var j = 0 ; j < inputs.length ; j++ )
			{
				addInputSubmitEvent( forms[i], inputs[j] );
			}
		}
	}
}

function getElementsByClassName( node, tag, className )
{
	elements = new Array();
	
	if( nodes = node.getElementsByTagName( tag ) )
	{
		for( i = 0 ; i < nodes.length ; i++ )
		{		
			if( nodes[i].getAttribute( classAttributeName ) == className )
			{
				elements.push( nodes[i] );
			}
		}
	}
	
	return elements;
}

function appendNodeAttribute2( node, name, value )
{
	var v = node.getAttribute( name );
	
	if( v != null )
	{
		node.setAttribute( name, v + value );
	}
	else
	{
		node.setAttribute( name, value );
	}
}

function appendNodeAttribute( node, name, value )
{
	var v = node.getAttribute( name );
	
	if( v != null )
	{
		node.setAttribute( name, v + value );
	}
	else
	{
		node.setAttribute( name, value );
	}
}

function setListAction( formId, actionName, actionValue )
{
	if( document.getElementById )
	{
		var form = document.getElementById( formId );
	
		form.list_action.value = actionName;
		form.list_action_value.value = actionValue;
		
		form.submit();
	}
}

function getFormElementValue( formId, elementName ) // Only tested with radiobuttons
{
	var form = null;

	if( document.getElementById )
	{
		form = document.getElementById( formId );
		
		var element = form.elements[elementName];
		
		if( element.length > 0 )
		{
			var i;
			
			for( i = 0 ; i < element.length ; i++ )
			{
				if( element[i].checked )
				{
					return element[i].value;
				}
			}
		}
	}
	
	return null;
}

function valueInRange( value, min, max )
{
	return value >= min && value <= max;
}

function stringByJoiningArrayElements( elements, glue, lastElementGlue )
{
	var string = "";
	
	var l = elements.length;
	
	for( i = 0 ; i < l ; i++ )
	{
		string += ( i > 0 ? ( i == l - 1 ? lastElementGlue : glue ) : "" ) + elements[i];
	}
		
	return string;
}

function firstIndexOfValueInArray( value, array )
{
	for( var i = 0 ; i < array.length ; i++ )
	{
		if( value == array[i] )
		{
			return i;
		}
	}
	
	return -1;
}

function removeValueFromArray( value, array )
{
	var i;
	
	while( ( i = firstIndexOfValueInArray( value, array ) ) >= 0 )
	{	
		array.splice( i, 1 );
	}
}

function valueInArray( value, array )
{
	for( var i = 0 ; i < array.length ; i++ )
	{
		if( value == array[i] )
		{
			return true;
		}
	}
	
	return false;
}

function numericSort( a, b )
{
	return a - b;
}

function ColorArray()
{
	this.length = 0;
	
	this.r = new Array();
	this.g = new Array();
	this.b = new Array();
	
	this.addRgbColor = function addRgbColor( r, g, b )
	{
		this.r.push( r );
		this.g.push( g );
		this.b.push( b );
		
		this.length++;
	}
	
	this.addHexColor = function addHexColor( r, g, b )
	{
		this.r.push( parseInt( r, 16 ) );
		this.g.push( parseInt( g, 16 ) );
		this.b.push( parseInt( b, 16 ) );
		
		this.length++;
	}
	
	this.interpolate = function interpolate( steps )
	{
		if( this.length == 2 )
		{
			var r1 = this.r[0];
			var r2 = this.r[1];
			var g1 = this.g[0];
			var g2 = this.g[1];
			var b1 = this.b[0];
			var b2 = this.b[1];
			
			var rd = r2 - r1;
			var rs = Math.round( rd / ( steps + 1 ) );
			var gd = g2 - g1;
			var gs = Math.round( gd / ( steps + 1 ) );
			var bd = b2 - b1;
			var bs = Math.round( bd / ( steps + 1 ) );
			
			this.r = new Array();
			this.g = new Array();
			this.b = new Array();
			
			this.length = 0;
			
			this.addRgbColor( r1, g1, b1 );
			
			for( var i = 1 ; i <= steps ; i++ )
			{
				this.addRgbColor( r1 + ( rs * i ), g1 + ( gs * i ), b1 + ( bs * i ) );
			}
			
			this.addRgbColor( r2, g2, b2 );			
		}
	}
	
	this.colorAtIndex = function colorAtIndex( index )
	{
		return "rgb(" + this.r[index] + "," + this.g[index] + "," + this.b[index] + ")";
	}
	
	this.getList = function getList()
	{
		var l = "";
		
		for( var i = 0 ; i < this.length  ; i++ )
		{
			l += this.r[i] + "," + this.g[i] + "," + this.b[i];
			
			if( i < this.length - 1 )
			{
				l += ":";
			}
		}
				
		return l;
	}
}

function highlightButton( id )
{
	if( document.getElementById )
	{
		if( node = document.getElementById( id ) )
		{
			var c = node.getAttribute( classAttributeName );
			
			if( c && !c.match( /(^| )highlighted( |$)/ ) )
			{
				node.setAttribute( classAttributeName, node.getAttribute( classAttributeName ) + " " + "highlighted" );
			
				node.style.color = "white";
				
				var colors = new ColorArray();
				
				colors.addHexColor( "A7", "A7", "A6" );
				colors.addRgbColor( 255, 255, 255 );
				colors.interpolate( 6 );
								
				animateColor( id, colors.getList(), colors.length, 2, true );
			}
		}
	}
}

function animateColor( nodeId, colors, numberOfColors, index, ascending )
{
	if( document.getElementById )
	{
		if( object = document.getElementById( nodeId ) )
		{
			var a = colors.split( ":" );
			
			object.style.color = "rgb(" + a[index] + ")";

			index = ascending ? index + 1 : index - 1;
			
			if( index == numberOfColors )
			{
				ascending = false;
				index = numberOfColors - 2;
			}
			else if( index == -1 )
			{
				ascending = true;
				index = 1;
			}
			
			window.setTimeout( "animateColor('" + nodeId + "','" + colors + "'," + numberOfColors + "," + index + "," + ascending + ")", 100 );
		}
	}
}

function hideNodeWithId( nodeId )
{
	if( document.getElementById )
	{
		if( node = document.getElementById( nodeId ) )
		{
			node.style.display = "none";
		}
	}
}

function showNodeWithId( nodeId )
{
	if( document.getElementById )
	{
		if( node = document.getElementById( nodeId ) )
		{
			node.style.display = "block";
		}
	}
}

function showNodeWithIdAndSetFocus( nodeId )
{
	showNodeWithId( nodeId );
	focusOnFirstEmptyInputInNodeWithId( nodeId );
}

function focusOnFirstEmptyInputInNodeWithId( nodeId )
{
	if( document.getElementById )
	{
		focusOnFirstEmptyInput( document.getElementById( nodeId ) );
	}
}

function focusOnFirstEmptyInput( node )
{
	if( node && node.nodeType == 1 )
	{
		for( var i = 0 ; i < node.childNodes.length ; i++ )
		{
			var n = node.childNodes[i];
			
			if( n.nodeType == 1 && ( n.nodeName == "TEXTAREA" || n.nodeName == "INPUT" ) )
			{
				if( n.value == "" )
				{
					n.focus();
					
					return true;
				}
			}
			
			if( focusOnFirstEmptyInput( node.childNodes[i] ) )
			{
				return true;
			}
		}
	}
	
	return false;
}

function toggleNodesWithIds( nodeId1, nodeId2 )
{
	if( document.getElementById )
	{
		if( ( node1 = document.getElementById( nodeId1 ) ) && ( node2 = document.getElementById( nodeId2 ) ) )
		{
			if( node2.style.display == "none" || node2.style.display == "" )
			{
				node2.style.display = "block";
				node1.style.display = "none";
			}
			else
			{
				node1.style.display = "block";
				node2.style.display = "none";
			}
		}
	}
}

function toggleNodesText( nodeId, text1, text2 )
{
	if( document.getElementById )
	{
		if( node = document.getElementById( nodeId ) )
		{
			if( node.firstChild.data == text1 )
			{
				node.firstChild.data = text2;
			}
			else
			{
				node.firstChild.data = text1;
			}
		}
	}
}

function setNodeText( nodeId, text )
{
	if( document.getElementById )
	{
		if( node = document.getElementById( nodeId ) )
		{
			node.firstChild.data = text;
		}
	}
}

function setOpacity( node, opacity )
{
	opacity = ( opacity == 100 ) ? 99.999 : opacity;
	
	node.style.filter = "alpha(opacity:" + opacity + ")";
	node.style.KHTMLOpacity = opacity/100;
	node.style.MozOpacity = opacity/100;
	node.style.opacity = opacity/100;
}

function locationInNode( node, x, y )
{
	var w = node.clientWidth;
	
	if( w > 0 )
	{
		var t = 0;
		var l =	0;
		
		if( isIE )
		{
			var rect = node.getBoundingClientRect();
			
			t = rect.top + getScrollTop();
			l = rect.left + getScrollLeft();
		}
		else
		{
			t = node.offsetTop;
			l =	node.offsetLeft;
		}
				
		if( x >= l && x <= l + node.clientWidth )
		{
			if( y >= t && y <= t + node.clientHeight )
			{
				return true;
			}
		}
	}
	
	return false;
}

function getScrollTop()
{
	var v1 = document.documentElement.scrollTop;
	var v2 = document.body.scrollTop;
	
	return v1 > v2 ? v1 : v2;
}

function getScrollLeft()
{
	var v1 = document.documentElement.scrollLeft;
	var v2 = document.body.scrollLeft;
	
	return v1 > v2 ? v1 : v2;
}

function getWindowWidth()
{
	if (self.innerWidth)
	{
		return self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
}

function getWindowHeight()
{
	if( self.innerHeight )
	{
		return self.innerHeight;
	}
	else if( document.documentElement && document.documentElement.clientHeight )
	{
		return document.documentElement.clientHeight;
	}
	else if( document.body )
	{
		return document.body.clientHeight;
	}
}

function getBodyNode()
{
	var nodes = document.getElementsByTagName( "body" );
	
	if( nodes.length == 1 )
	{
		return nodes[0];
	}
	
	return null;
}

function getFullBodyWidth()
{
	var w, ww;
	
	if( document.body.scrollHeight > document.body.offsetHeight )
	{
		w = document.body.scrollWidth;
	}
	else
	{
		w = document.body.offsetWidth;
	}

	ww = getWindowWidth();
	
	return w >= ww ? w : ww;
}

function getFullBodyHeight()
{
	var h, wh;

	if( document.body.scrollHeight > document.body.offsetHeight )
	{
		h = document.body.scrollHeight;
	}
	else
	{
		h = document.body.offsetHeight;
	}
	
	wh = getWindowHeight();
	
	return h >= wh ? h : wh;
}

function setCookie(name, value, expires, path, domain, secure){

 var curCookie = name + "=" + escape(value) + 
	((expires) ? "; expires=" + expires.toGMTString() : "") + 
	((path) ? "; path=" + path : "") + 
	((domain) ? "; domain=" + domain : "") + 
	((secure) ? "; secure" : "");
		
	document.cookie = curCookie; 
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}