function getChildElementById ( parent, id )
{
	if ( parent == null )
	{
		return document.getElementById ( id );
	}
	
	if ( parent.id == id )
	{
		return parent;
	}
	
	var element = null;
	
	for ( var index = 0; index < parent.childNodes.length; index++ )
	{
		element = getChildElementById ( parent.childNodes [ index ], id );
		
		if ( element != null )
		{
			break;
		}
	}
	
	return element;
}

function setElementVisibility ( parent, id, visible, height )
{
	var element = getChildElementById ( parent, id );
	
	setElementVisibility2 ( element, visible, height );
	
	return element;
}

function setElementVisibility2 ( element, visible, height )
{
	element.style.visibility = visible ? 'visible' : 'hidden';
	element.style.height = visible ? ( height == null ? 'auto' : height ) : 0;
}

function linkElement ( element )
{
	if ( element.formerParentNode != null && element.formerParentNode != element.parentNode )
	{
		element.formerParentNode.appendChild ( element );
	}
}

function unlinkElement ( element )
{
	if ( element.formerParentNode == null )
	{
		element.formerParentNode = element.parentNode;
	}
	
	if ( element.parentNode != null )
	{
		element.parentNode.removeChild ( element );
	}
}

function fixIndices ( objectArray, indexAttribute )
{
	for ( var index = 0; index < objectArray.length; index++ )
	{
		objectArray [ index ] [ indexAttribute ] = index;
	}
}

function findInArray ( objectArray, attribute, value )
{
	for ( var index = 0; index < objectArray.length; index++ )
	{
		eval ( 'var objectValue = objectArray [ index ].' + attribute );
		
		if ( objectValue == value )
		{
			return objectArray [ index ];
		}
	}
	
	throw "object " + value + " was not found.";
}

function enableBlockButton ( parent, name, borderColor, color, link, text )
{
	var button;
	
	if ( parent == null )
	{
		button = document.getElementById ( name );
	}
	else
	{
		button = getChildElementById ( parent, name );
	}
	
	button.style.borderColor = borderColor;
	
	var html;
	
	if ( link != null )
	{
		html = '<a href="' + link + '" style="position: static; color: ' + color + ';">' + text + '</a>';
	}
	else
	{
		html = '<span style="position: static; color: ' + color + ';">' + text + '</span>';
	}
	
	button.innerHTML = html;
}

function showPopup ( message, showClose, left, top )
{
	showPopupEx ( message, showClose ? [ { "link": 'javascript:hidePopup ( )', "text": "close" } ] : null, top, left );
}

function showPopupEx ( message, buttons, left, top )
{
	var popupContainer	= setElementVisibility ( null, "popupContainer", true, 307 );
	var popup			= getChildElementById ( popupContainer, "popup" );
	var popupText		= getChildElementById ( popupContainer, "popupText" );
	var buttonContainer	= getChildElementById ( popupContainer, "closeButton" );
	
	popupText.innerHTML = message;
	
	if ( buttons != null )
	{
		var html = "";
		
		for ( var index = 0; index < buttons.length; index++ )
		{
			// We assume the link is okay to be embedded in double quotes.
			
			html += '<a href="' + buttons [ index ].link + '">' + buttons [ index ].text + '</a>';
			
			if ( index < buttons.length - 1 )
			{
				html += ' | ';
			}
		}
		
		buttonContainer.innerHTML = html;
		buttonContainer.style.top = popup.offsetHeight - 40;
		buttonContainer.style.visibility = "visible";
	}
	else
	{
		buttonContainer.style.visibility = "hidden";
	}
	
	if ( top != null && left != null )
	{
		popupContainer.style.left = left + popupContainer.parentNode.scrollLeft;
		popupContainer.style.top = top + popupContainer.parentNode.scrollTop;
	}
}

function hidePopup ( )
{
	var popupContainer	= setElementVisibility ( null, "popupContainer", false, null );
	var closeButton		= getChildElementById ( popupContainer, "closeButton" );
	closeButton.style.visibility = "hidden";
}

function trim ( value )
{
	return value.replace ( /^\s*|\s*$/g, "" );
}

function confirmLink ( link, message, newWindow, cancelText, continueText )
{
	message += '<a href="javascript:hidePopup ( )">' + cancelText + '</a> | <a href="' + link + '"' + ( newWindow ? ' onclick="hidePopup ( )" target="_blank"' : '' ) + '>' + continueText + '</a>';
	showPopup ( message, false, 100, 100 );
}

var updateFlags = new Object ( );
var updateCheckers = new Array ( );

function addUpdateChecker ( checker )
{
	updateCheckers.push ( checker );
}

function goToLink ( link, message, newWindow )
{
	var updateNeeded = false;
	
	for ( var index in updateFlags )
	{
		if ( updateFlags [ index ] )
		{
			updateNeeded = true;
			break;
		}
	}
	
	if ( ! updateNeeded )
	{
		for ( var index = 0; index < updateCheckers.length; index++ )
		{
			if ( updateCheckers [ index ] ( ) )
			{
				updateNeeded = true;
				break;
			}
		}
	}
	
	if ( updateNeeded )
	{
		confirmLink ( link, message == null ? updateNeededMessage : message, newWindow, 'Cancel', 'Continue without Saving' );
	}
	else
	{
		if ( newWindow )
		{
			window.open ( link, null );
		}
		else
		{
			window.location = link;
		}
	}
}

var updateNeededMessage = '<b>Changes Not Saved</b><br />Changes on this page have not been saved yet and continuing will cause them to be lost.<br /><br />';

function getCredentials ( )
{
	var cred = readCookie ( 'cred' );

	if ( cred != null )
	{
		cred = unescape ( cred );
	}
	else
	{
		cred = '';
	}
	
	return cred;
}

function jumpToAnchor ( name )
{
	if ( window.location.hash != null )
	{
		window.location.hash = name;
	}
	else
	{
		var location = window.location.href;
		location.replace ( /(#.*)$/, '' );
		window.location.replace ( location + '#' + name );
	}
}

function isCompatibleBrowser ( )
{
	var browser = 'Other';
	var os = 'Other';
	
	var userAgent = navigator.userAgent.toLowerCase ( );
	
	if ( userAgent.indexOf ( "firefox" ) != -1 ) 
	{ 
		browser = 'Firefox';
	}
	else if ( userAgent.indexOf ( "safari" ) != -1 ) 
	{ 
		browser = 'Safari';
	}
	else if ( userAgent.indexOf ( "msie" ) != -1 ) 
	{ 
		browser = 'IE';
	}
	else if ( userAgent.indexOf ( "mozilla/5.0" ) != -1 ) 
	{ 
		browser = 'Mozilla';
	}
	else if ( userAgent.indexOf ( "netscape" ) != -1 ) 
	{ 
		browser = 'Netscape';
	}
	else if ( userAgent.indexOf ( "opera" ) != -1 ) 
	{ 
		browser = 'Opera';
	}
	else if ( userAgent.indexOf ( "konqueror" ) != -1 ) 
	{ 
		browser = 'Konqueror';
	}
	
	if ( userAgent.indexOf ( "linux" ) != -1 ) 
	{ 
		os = 'Linux';
	}
	else if ( userAgent.indexOf ( "windows" ) != -1 ) 
	{ 
		os = 'Windows';
	}
	else if ( userAgent.indexOf ( "mac " ) != -1 ) 
	{ 
		os = 'Mac OS';
	}
	
	var compatible = false;
	var error = null;
	
	if ( browser == 'Firefox' )
	{
		compatible = true;
	}
	else if ( browser == 'Safari' ) 
	{ 
		var version = userAgent;
		version = new Number ( version.replace ( /^.*safari\/([0-9]+).*$/, "$1" ) );
		
		if ( version > 300 )
		{
			compatible = true;
		}
		else
		{
			error = 'Support for Safari is limited to versions 1.3 and above. It appears that you are using an older version with which there are known incompatibilities. You may experience problems if you continue.';
		}
	}
	else if ( browser == 'IE' ) 
	{ 
		var version = userAgent;
		version = new Number ( version.replace ( /^.*msie\s+([0-9]+).*$/, "$1" ) );
		
		if ( version >= 6 )
		{
			compatible = true;
		}
		else
		{
			error = 'Support for Internet Explorer is limited to versions 6 and above. It appears that you are using an older version with which there are known incompatibilities. You may experience problems if you continue.';
		}
	}
	else
	{
		error = 'Slideblox currently supports Internet Explorer, Safari and Firefox. It appears that you are using another browser. You may experience problems if you continue.';
	}
		
	if ( compatible && ( os != 'Windows' && os != 'Mac OS' ) )
	{
		compatible = false;
		error = 'While your browser appears to be compatible with Slideblox, your operating system is not yet on the approved list. If you choose to continue, we\'d be interested in hearing how things go.';
	}
	
	if ( ! compatible )
	{
		throw error;
	}
}

// http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
