﻿

//###alpha filter key array;
//#  meant to be used for index referencing with foreign language filters; 
//#  i.e. on the German page, the user selects "A" from the filter and "A|Ä" (index[0]) is fed to the 
//#  (as of yet not implemented) RegEx object for testing.
var filter_num = new Array("0","1","2","3","4","5","6","7","8","9");
var filter_ltr = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");


//#  list of character variants by language
var filter_ltr_en = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
var filter_ltr_de = new Array("A|Ä","B","C","D","E","F","G","H","I","J","K","L","M","N","O|Ö","P","Q","R","S|ß","T","U|Ü","V","W","X","Y","Z");
var filter_ltr_fr = new Array("a|à|â|æ","b","c|ç|œ","d","e|é|è|ê|ë","f","g","h","i|î|ï","j","k","l","m","n","o|ô","p","q","r","s","t","u|û|ù|ü","v","w","x","y|ÿ","x","z");


//#  the object array of elements to be filtered
var oLists = new Array();


//#  message to be diplayed in the event there is no content
var no_item_msg = "There are currently no items for this section.";

if( location.href.indexOf("/de-1031/") != -1 )
{
	no_item_msg = "There are currently no items for this section.";
}
if( location.href.indexOf("/fr-1036/") != -1 )
{
	no_item_msg = "There are currently no items for this section.";
}


//###populates and alphanumeric filter; we work a lot with objects and properties here
//#  oLists is established, the array of objects to be filtered; e.g. hide/show
//#  sTargetId : the ID of the element we want to populate with the alpha filter
//#  sContainerNodeId : the ID of the element that contains the list of elements to be filtered
//#  sTagName : the type of tag contained in sContainerNodeId that is to be filtered (i.e. A, TD, etc)
//#  bShowNum : whether or not to display the number list
//#  bShowAlpha : whether or not to display the letter list
function PopulateAlphaSpan( sTargetId, sContainerNodeId, sTagName, bShowNum, bShowAlpha )
{
	try
	{		
		//# defining our objects and object arrays;
		//# an item in oLists is created for each sTargetId passed in to allow for potencially infinate menus!!!
		var oTarget = getObject(sTargetId);
		var oContainerNode = getObject(sContainerNodeId);
		
		//# oLists[sTargetId] is passed a collection of sTagName elements
		var index = oLists.length;
		oLists[sTargetId] = new Array(); 
		oLists[sTargetId] = getCollectionByObject( oContainerNode, sTagName );
		
		if( oLists[sTargetId].length > 0 )
		{
			FirstFilter( sTargetId, oLists[sTargetId][0].getAttribute("name").charAt(0) );
			
			/*#######################
			charAt(0) concatnation for A/SPAN test (below); I don't think this will be easy for foreign languages...
			loop through oLists[sTargetId] and concatnation [i].innerHTML.charAt(0) into a single string.
			Later, we'll use inc to as a test to see if a given letter or number exists in oContainerNode
			#######################*/
			var inc = "";
			for( i = 0; i < oLists[sTargetId].length; i++ )
			{
				inc += oLists[sTargetId][i].innerHTML.charAt(0);
			}

			var content = "";
			var txt = "";
			var tag = "";
			
			if( bShowNum )
			{//# create the numeric filter elements
				CreateFilterObjects( oTarget, filter_num, inc );
			}

			if( bShowAlpha )
			{//# create the letter filter elements
				CreateFilterObjects( oTarget, filter_ltr, inc );
			}
		
		}
		else
		{
			oTarget.innerHTML = no_item_msg;
		}
	}catch(e){ throwException( 'alpha_filter.js : PopulateAlphaSpan()', e.lineNumber, e, e.message ); }
}

function CreateFilterObjects( oTarget, oArray, sTestString )
{
	//###loop through oArray testing to see if it (the current filter alphanumeric) exists in sTestString 
	//#  (the concatnated string aon all the first characters of each element in target element)
	//#  true = A element is created in oTarget and assigned it's onclick event
	//#  false = SPAN element is created in oTarget
	for( i = 0; i < oArray.length; i++ )
	{
		var new_a = new Object();
		if(sTestString.indexOf(oArray[i]) != -1)
		{
			new_a = createChildObject( "A", "", oTarget, true );
			new_a.onclick = FilterBySelected;
		}
		else
		{
			new_a = createChildObject( "SPAN", "", oTarget, true );
		}
		new_a.innerHTML = oArray[i];
	}
}



//###first-run function when the page loads to apply the alpha filter to the list
//#  sRegExp tends to be the first available alphanumeric
//#  FilterBySelected() does the same thing but triggered onClick
function FirstFilter(sTargetId, sRegExp)
{
	
	var test_char = ( isNaN(sRegExp) )
		? sRegExp.toUpperCase() + "|" + sRegExp.toLowerCase()
		: sRegExp
	;

	var test_reg = new RegExp(test_char);
	
	return AlphaFilter( sTargetId, test_reg );
	
}
function FilterBySelected()
{
	var oSender = this;
	var oParent = oSender.parentNode;
	
	var test_char = ( isNaN(oSender.innerHTML) )
		? oSender.innerHTML.toUpperCase() + "|" + oSender.innerHTML.toLowerCase()
		: oSender.innerHTML
	;
	
	var test_reg = new RegExp(test_char);
	
	return AlphaFilter( oParent.id, test_reg );
	
}


function AlphaFilter( sListId, oRegExp )
{
	try
	{

		if(getObject("readout"))
		{
			getObject("readout").innerHTML = oLists[sListId].length;
		}
		
		for( i = 0; i < oLists[sListId].length; i++ )
		{
		
			oLists[sListId][i].style.display = ( oRegExp.test(oLists[sListId][i].getAttribute("name").charAt(0)) )
				? "block"
				: "none"
			;
		
		}
			
	}catch(e){ throwException( 'alpha_filter.js : filter()', e.lineNumber, e, e.message ); }

	return false;

}


var oTableList = new Array(  );
function IniTableList(sTargetId)
{
	try
	{
	
		oTableList = getCollectionByObject( getObject(sTargetId), "TABLE" ); 
		
		for( i = 0 ; i < oTableList.length; i++ )
		{
			
			if(getCollectionByObject( oTableList[i], "TR" ).length > 1 )
			{
				oTableList[i].style.display = "block";
				break;
			}
		
		}
			
	}catch(e){ throwException( 'alpha_filter.js : IniTableList()', e.lineNumber, e, e.message ); }
}
function FirstTable()
{
}


function ToggleSection( sFolderId )
{
	try
	{
	
		for( i = 0; i < oTableList.length; i++ )
		{
			if( oTableList[i].getAttribute("folder") == sFolderId )
			{
				oTableList[i].style.display = "block";
				
				if( getCollectionByObject( oTableList[i], "TD").length == 0 )
				{//##if there are no itemns to display in the table, we need to create the element and display the NONE message.
					var tr = new Object();
					tr = createChildObject( "TR", "", getCollectionByObject( oTableList[i], "TBODY")[0], true );
					
					var td = new Object();
					td = createChildObject( "TD", "", tr, true );
					
					td.innerHTML = no_item_msg;
				}
			}
			else
			{
				oTableList[i].style.display = "none";
			}
			
		}
	
	}catch(e){ throwException( 'alpha_filter.js : filter()', e.lineNumber, e, e.message ); }
	
	return false;	
}

