// Ensure we're working with a relatively standards compliant user agent
function isBrowserCompliant()
{
	return document.getElementById && document.createElement && document.createTextNode;
}


function getFields( list , field )
{
	if( field == null )
		field = "label";
	var labels = new Array();
	for( var i=0 ; i < list.length ; i++ )
	{
		labels[ labels.length ] = list[i][field];
	}
	return labels;
}


function getItemById( id , list , field )
{
	if( field == null )
		field = "input";
	for( var i=0 ; i < list.length ; i++ )
	{
		if( list[i][field] == id )
			return list[i];
	}
	return false;
}


function updateState( node , className , nodeList , override )
{
	var curState = node.className;
	
	//prevent multiple mouseover handling when already handled
	if( curState == className )
		return false;
		
	//we can only update non selected states
	if( curState != "selected" || override )
	{
		var combinedClass = "";
		if( node.prevClassName != null && node.prevClassName.length > 0 )
			combinedClass += node.prevClassName + "-";
		if( className != null )
			combinedClass += className;
		node.className = combinedClass;
	}
	
	if( nodeList != null && className == "selected" )
	{
		//remove all other selected!
		for( var i=0 ; i < nodeList.length ; i++ )
		{
			//alert( nodeList[i] );
			if( nodeList[i] != node.id )
			{
				labelRef = document.getElementById( nodeList[i] );
				if( labelRef.className == "selected" )
				{
					if( labelRef.prevClassName != null )
						labelRef.className = labelRef.prevClassName
					else
						labelRef.className = null;
				}
			}

		}
	}
	return true;
}


function initBases( displayItems , txtId , imgId , dependantItems )
{
	var hasInputs = false;
	for( var i=0 ; i < displayItems.length ; i++ )
	{
		var curInfo = displayItems[i];
		var label = document.getElementById( curInfo.label );
		var input = document.getElementById( curInfo.input );
		if( input != null )
			hasInputs = true;
		
		label.onmouseover = function()
		{
			if( !updateState( this , "highlight" ) )
				return;
		};
		
		label.onmouseout = function()
		{
			if( !updateState( this , null ) )
				return;
		};
		
		if( input != null )
		{
			input.onclick = function()
			{
				var label = document.getElementById( getItemById(this.id,displayItems,"input" ).label );

			    if( !updateState( label , "selected" , getFields( displayItems ) ) )
				    return true;
				
			    return true;
			}
		}
	}
	
	//make sure state is preserved onload 
	if( hasInputs )
		initInputState( getFields( displayItems , "input" ) , 0 );
	else
		updateDisplayItem( tempProps , txtId , imgId , false );
}


function initInputState( inputList , defaultInd , allowMultiple )
{
	if( allowMultiple == null )
		allowMultiple = false;
 
	var foundChecked = false;
	var firstInput = null;
	for( var j=0 ; j < inputList.length ; j++ )
	{
		var inputRef = document.getElementById( inputList[j] );
		
		if( j == defaultInd )
			firstInput = inputRef;
		
		if( inputRef.checked )
		{
			foundChecked = true;
			//make sure the style is correct
			inputRef.onclick();
			if( !allowMultiple )
				break;
		}
	}
	
	
	if( !foundChecked && firstInput != null )
	{
		firstInput.checked = true;
		firstInput.onclick();
	}
}