var gDropDowns = new Array();

function InArray( MyString, MyArray )
{
	var VarInArray = false;
	for( i = 0; i < MyArray.length; i++ )
	{
		if( MyArray[i] == MyString )
		{
			return true;
		}
	}

	return false;
}

// if localeCompare() is not defined (Safari <3), or does not work properly (Safari 3+), define a stand-in
if( String.prototype.localeCompare == null || String.prototype.localeCompare('Argentina','Venezuela') >= 0 )
{
	String.prototype.localeCompare =
	function( sOther )
	{
		a = Normalize( this );
		b = Normalize( sOther );
		if( a < b ) return -1;
		else if ( a > b ) return 1;
		else return 0;
	}
}

function Normalize(str)
{
	var nstr = str;

	nstr = nstr.replace(/[&#8730;Ã„&#8730;Â†&#8730;Ã…&#8730;Â°&#8730;Ã‰&#8730;Â£&#8730;Ã‘&#8730;Â§&#8730;Ã–&#8730;Â•&#8730;Ãœ&#8730;Â¶]/,'a');
	nstr = nstr.replace(/[&#8730;Ã¡&#8730;ÃŸ]/,'c');
	nstr = nstr.replace(/[&#8730;Ãª&#8730;&#8734;]/,'d');
	nstr = nstr.replace(/[&#8730;Ã &#8730;(R)&#8730;Ã¢&#8730;(c)&#8730;Ã¤&#8730;TM&#8730;Ã£&#8730;Â´]/,'e');
	nstr = nstr.replace(/[&#8730;Ã¥&#8730;Â¨&#8730;Ã§&#8730;&#8800;&#8730;Ã©&#8730;Ã†&#8730;Ã¨&#8730;Ã˜]/,'i');
	nstr = nstr.replace(/[&#8730;Ã­&#8730;&#8804;&#8730;Ã¬&#8730;&#8805;&#8730;Ã®&#8730;Â¥&#8730;Ã±&#8730;&#8706;Â¬Ã¥Â¬Ãº&#8730;Ã²&#8730;&#8719;]/,'o');
	nstr = nstr.replace(/[&#8730;Ã´&#8730;&#960;&#8730;Ã¶&#8730;&#8747;&#8730;Ãµ&#8730;Âª&#8730;Ãº&#8730;Âº]/,'u');
	nstr = nstr.replace(/[&#8730;Ã¹&#8730;&#937;Â¬Ã¼&#8730;Ã¸]/,'y');
	nstr = nstr.replace(/&#8730;Ã¼/,'s');
	return nstr;
}

function LocaleSort(a,b)
{
	return a.localeCompare(b);
}

function AddEvent( obj, type, fn )
{
	// IE
	if( obj.attachEvent )
	{
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		return obj.attachEvent( 'on'+type, obj[type+fn] );
	}
	// W3C DOM
	else
	{
		return obj.addEventListener( type, fn, false );
	}
}

function RemoveEvent( obj, type, fn )
{
	// IE
	if( obj.detachEvent )
	{
		var Result = obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
		return Result;
	}
	// W3C DOM
	else
	{
		return obj.removeEventListener( type, fn, false );
	}
}

function DropDownMouseOut( Identifier )
{
	// start the time, if there is an auto hide duration
	if( gDropDowns[Identifier].AutoHideDuration > 0 )
	{
		return DropDownHandler( Identifier, gDropDowns[Identifier].AutoHideDuration, false );
	}

	return false;
}

function SetDropDown( Identifier, IsHovering )
{
	return DropDownHandler( Identifier, gDropDowns[Identifier].AutoHideDuration, IsHovering );
}

function ToggleDropDown( Identifier )
{
	return DropDownHandler( Identifier, 0, ( gDropDowns[ Identifier ] == null || !gDropDowns[ Identifier ].Hovering ) );
}

function GoToDropDown(e,Identifier)
{
	if( !e ){ e = window.event; }
	if( e )
	{
		var KeyCode = ( e.keyCode ? e.keyCode : e.which );
		var PressedKey = String.fromCharCode( KeyCode );
		if( document.getElementById( Identifier + '-' + PressedKey ) )
		{
			document.getElementById(Identifier + '-' + PressedKey).scrollIntoView();
		}
	}
}

function DropDownHandler( Identifier, TimeOutDuration, IsHovering )
{
	if( gDropDowns[ Identifier ] == null )
	{
		gDropDowns[ Identifier ] = { Hovering:false, TimeoutID:null };
	}

	// if we are changing state
	if( gDropDowns[ Identifier ].Hovering != IsHovering )
	{
		var Element = document.getElementById( Identifier + '-List' );

		if( IsHovering )
		{
			// hide all other active drop downs
			for( DropDownIdentifier in gDropDowns )
			{
				if( DropDownIdentifier != Identifier )
				{
					var DropDownElement = document.getElementById( DropDownIdentifier + '-List' );
					if( DropDownElement.style.display != 'none' )
					{
						document.onkeypress = null;
						DropDownElement.style.display = 'none';
					}
					if( gDropDowns[DropDownIdentifier].TimeoutID != null )
					{
						clearTimeout( gDropDowns[ DropDownIdentifier ].TimeoutID );
						gDropDowns[DropDownIdentifier].TimeoutID = null;
					}
					gDropDowns[DropDownIdentifier].Hovering = false;
				}
			}

			Element.style.display = 'block';
			if( gDropDowns[ Identifier ].TimeoutID != null )
			{
				clearTimeout( gDropDowns[ Identifier ].TimeoutID );
				gDropDowns[ Identifier ].TimeoutID = null;
			}
			AddEvent( document, 'keydown', gDropDowns[ Identifier ].KeyHandler );
		}
		else if( TimeOutDuration > 0 )
		{
			gDropDowns[ Identifier ].TimeoutID = setTimeout('HideDropDown("' + Identifier + '")', TimeOutDuration );
		}
		else
		{
			RemoveEvent( document, 'keydown', gDropDowns[ Identifier ].KeyHandler );

			Element.style.display = 'none';
			if( gDropDowns[ Identifier ].TimeoutID != null )
			{
				clearTimeout( gDropDowns[ Identifier ].TimeoutID );
				gDropDowns[ Identifier ].TimeoutID = null;
			}
		}

		gDropDowns[ Identifier ].Hovering = IsHovering;
	}
}

function HideDropDown( Identifier )
{
	if( !gDropDowns[ Identifier ].Hovering )
	{
		var Element = document.getElementById( Identifier + '-List' );
		document.onkeypress = null;
		Element.style.display = 'none';
	}

	gDropDowns[ Identifier ].TimeoutID = null;
}

function DropDownIsActive( Identifier )
{
	return ( gDropDowns[ Identifier ] != null && gDropDowns[ Identifier ].TimeoutID != null );
}

/*
 * Create a <div> that contains all of the necessary elements and functionality
 * for a drop-down menu.
 * 
 * Identifier - desired form element id/name
 *
 * Title - initial/default value
 *
 * Values - array of objects that define the list values and optional href URLs
 *
 * ChainedDropDowns - array of drop-down Identifiers to "chain" to this drop-down
 *  so that when a value is selected for this drop-down, the chained drop-downs
 *  are reset to their default value and their lists are filtered based on the
 *  selected value of this drop-down (through related object property values)
 *
 * AutoHideDuration - delay before hiding drop-down after mouseout (0=never)
 *
 * IsSubmittable- displays the submit button
 *
 */
function CreateDropDown( Identifier, Title, Values, FormName, ChainedDropDowns, AutoHideDuration, IsSubmittable )
{
	var UniqueValues = new Array();
	for( var index = 0; index < Values.length; index++)
	{
		if( !InArray( Values[index][Identifier], UniqueValues ) )
		{
			UniqueValues.push( Values[index][Identifier] );
		}
	}

	gDropDowns[ Identifier ] =
	{
		Identifier:Identifier,
		Title:Title,
		Values:Values,
		FormName:FormName,
		ChainedDropDowns:ChainedDropDowns,
		AutoHideDuration:( AutoHideDuration == null ? 0 : AutoHideDuration ),
		IsSubmittable:IsSubmittable,
		TimeoutID:null,
		KeyHandler:new Function( "e", "if(!e){e=window.event;} GoToDropDown(e,'" + Identifier + "')" ),
		Hovering:false
	};

	document.writeln('<input type="hidden" id="' + Identifier + '" name="' + Identifier + '" value="" />');
	document.writeln('<div class="DropDown">');

	document.writeln('<div class="Arrow" onclick="ToggleDropDown(\'' + Identifier + '\',0,true);" onmouseover="if( DropDownIsActive(\'' + Identifier + '\') ){ SetDropDown(\'' + Identifier + '\',true); }" onmouseout="DropDownMouseOut(\'' + Identifier + '\');"></div>');

	document.writeln('<div class="Selection" onclick="ToggleDropDown(\'' + Identifier + '\',0,true);" onmouseover="if( DropDownIsActive(\'' + Identifier + '\') ){ SetDropDown(\'' + Identifier + '\',true); }" onmouseout="DropDownMouseOut(\'' + Identifier + '\');">' );
	document.writeln('<div id="' + Identifier + '-Value" class="Value">' + Title + '</div>' );
	document.writeln( '</div>' );

	document.writeln('<div id="' + Identifier + '-List" class="List">');
	document.writeln('</div>');

	document.writeln('</div>');

	BuildDropDownList( Identifier );
}

/*
 * TextValue = null - build default lists
 */
function BuildDropDownLists( Identifier, TextValue )
{
	if( TextValue == null )
	{
		BuildDropDownList( Identifier );
	}

	var ChainedDropDowns = gDropDowns[ Identifier ].ChainedDropDowns;
	if( ChainedDropDowns != null )
	{
		// rebuild chained drop-down lists
		for( var ChainIndex = 0; ChainIndex < ChainedDropDowns.length; ChainIndex++ )
		{
			var ChainedIdentifier = ChainedDropDowns[ChainIndex];

			BuildDropDownList( ChainedIdentifier, Identifier, TextValue );
		}
	}
}

function BuildDropDownList( Identifier, MatchIdentifier, MatchValue )
{
	var Values = gDropDowns[ Identifier ].Values;
	var UniqueValues = new Array();
	for( var ValuesIndex = 0; ValuesIndex < Values.length; ValuesIndex++)
	{
		if( MatchIdentifier == null || MatchValue == null || Values[ValuesIndex][MatchIdentifier] == MatchValue )
		{
			if( Values[ValuesIndex][Identifier] != '' && !InArray( Values[ValuesIndex][Identifier], UniqueValues ) )
			{
				UniqueValues.push( Values[ValuesIndex][Identifier] );
			}
		}
	}

	UniqueValues.sort(LocaleSort);
	var ListHTML = '<ul>';
	var PreviousLetter = '';
	for( var index = 0; index < UniqueValues.length; index++ )
	{
		var DropDownElementValue = UniqueValues[index];
		var FirstLetter = (DropDownElementValue.length>0 ? DropDownElementValue.charAt(0) : '');
		if(FirstLetter != PreviousLetter)
		{
			var IsID = ' id="' + Identifier + '-' + FirstLetter + '" ';
			PreviousLetter = DropDownElementValue.charAt(0);
		}
		else
		{
			var IsID='';
		}
		ListHTML += '<li ' + IsID  +'onClick="SelectOption(\'' + Identifier + '\',\'' + UniqueValues[index].replace('\'','\\\'') + '\'); " onMouseOver="SetDropDown(\'' + Identifier + '\',true);" onMouseOut="DropDownMouseOut(\'' + Identifier + '\');"><a onclick="return false"; href="#">' + UniqueValues[index] + '</a></li>';
	}
	ListHTML += '</ul>';

	var ListDiv = document.getElementById( Identifier + '-List' );
	if( UniqueValues.length > 8 )
	{
		ListDiv.style.height = '160px';
	}
	else
	{
		ListDiv.style.height = 'auto';
	}
	ListDiv.innerHTML = ListHTML;

	var Element;
	Element = document.getElementById( Identifier );
	Element.value = '';

	Element = document.getElementById( Identifier + '-Value' );
	Element.innerHTML = gDropDowns[ Identifier ].Title;
}

function SelectOption( Identifier, TextValue )
{
	var Element;

	Element = document.getElementById( Identifier );
	Element.value = TextValue;

	Element = document.getElementById( Identifier + '-Value' );
	Element.innerHTML = TextValue;
	if(gDropDowns[Identifier].IsSubmittable)
	{
		if(document.getElementById('Submit')) { document.getElementById('Submit').style.display='block'; }
	}
	else
	{
		if(document.getElementById('Submit')) { document.getElementById('Submit').style.display='none'; }
	}
	DropDownHandler( Identifier, 0, false );

	var FoundLink = false;
	var Values = gDropDowns[ Identifier ].Values;
	for( var ValuesIndex = 0; ValuesIndex < Values.length; ValuesIndex++)
	{
		if( Values[ValuesIndex][Identifier] == TextValue )
		{
			if( Values[ValuesIndex]['href'] != null  && gDropDowns[ Identifier ].ChainedDropDowns == null)
			{
				location.href = Values[ValuesIndex]['href'];
				FoundLink = true;
			}
			break;
		}
	}

	if( !FoundLink )
	{
		BuildDropDownLists( Identifier, TextValue );
		if( ValuesIndex < Values.length && gDropDowns[Identifier].FormName != null && gDropDowns[Identifier].FormName != '' && Values[ValuesIndex][Identifier+'Link'] != null)
		{
			document.getElementById(gDropDowns[Identifier].FormName).action = Values[ValuesIndex][Identifier+'Link'];
			if(Values[ValuesIndex]['Target'] == 1) { document.getElementById(gDropDowns[Identifier].FormName).target = "_blank"; }
			else { document.getElementById(gDropDowns[Identifier].FormName).target = "_self"; }
			if(gDropDowns[Identifier].IsSubmittable && !document.getElementById('Submit'))
			{
				document.getElementById(gDropDowns[Identifier].FormName).submit();
			}

		}
	}
}
