/* ======================================================================================
 *	                                     B I S T R O 
 * ====================================================================================== 
 * AUTHOR       : S. Evers / J.H. van Rossum / R. Smit
 * DATE         : $Date: 8-07-10 11:43 $
 * MODIFIED	BY  : $Author: Bergr $
 * DOCUMENT     : $Workfile: Namenlijst.js $
 * COPYRIGHT    : © 2005 Bistro. All rights reserved
 * ======================================================================================
 */

/* ======================================================================================
 * Function			:	toggleLayer(idLayer, idCheckboxList)
 * Description		:	This function is used to show and hide content in DIV-elements
 * Parameter
 *	idDiv			:	The id of the layer that will be 'toggled'
 * ======================================================================================
*/
function toggleLayer(idDiv1, idDiv2)
{
	
	if (document.getElementById)
	{
		// rechtbanken
		var style2 = document.getElementById(idDiv1).style;	
		style2.display = document.getElementById('_ctl0_ContentPlaceHolder1_chklInstances_5').checked ? "block" : "";
		
		// gerechtshoven
		style2 = document.getElementById(idDiv2).style;		
		style2.display = document.getElementById('_ctl0_ContentPlaceHolder1_chklInstances_4').checked ? "block" : "";
		
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[idDiv].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[idDiv].style;
		style2.display = style2.display? "":"block";
	}
}

/* ======================================================================================
 * Function			:	CheckBeforeClick(idCheckboxList)
 * Description		:	This function is used to set a global variable when the first
						checkbox of an ASP.NET checkboxlist is checked
 * Parameter
 *  idCheckboxList	:	The id of the ASP.NET checkboxlist
 * ======================================================================================
*/

// Global variable
var beforeClickOn = false;

function CheckBeforeClick(idCheckboxList)
{
	if (document.getElementById(idCheckboxList + "_0").checked)
	{
		beforeClickOn = true;
	}
	else
	{
		beforeClickOn = false;
	}
}

/* ======================================================================================
 * Function			:	SelectCheckBoxes(idCheckboxList, nrOfCheckboxes)
 * Description		:	This function is used to select one or more checkboxes in an
 *						ASP.NET checkboxlist. Because these checkboxlists do not have
 *						onclick-events for every single checkbox, this work-around is 
 *						used.						
 * Parameter
 *  idCheckboxList	:	The id of the ASP.NET checkboxlist
 *	nrOfCheckboxes	:	The total number of checkboxes in the ASP.NET checkboxlist
 * ======================================================================================
*/
function SelectCheckBoxes(idCheckboxList, nrOfCheckboxes)
{
	var boxes = new Array();
	var counter = 0;
	
	// Count the number of checked checkboxes
	for (var i = 0; i < nrOfCheckboxes; i++)
	{
		boxes[i] = document.getElementById(idCheckboxList + "_" + i);
		
		if (boxes[i].id != idCheckboxList + "_0")
		{
			if (boxes[i].checked)
			{
				counter ++;
			}
		}
	}
	
	// If the 'select all' checkbox IS NOT selected before clicking on it AND
	// IS selected after clicking on it, THEN the rest of the checkboxes must NOT be
	// selected
	if ((!beforeClickOn) && (document.getElementById(idCheckboxList + "_0").checked))
	{
		for (var i = 1; i < nrOfCheckboxes; i++)
		{
			document.getElementById(idCheckboxList + "_" + i).checked = false;
		}
	}
	// If the 'select all' checkbox IS selected before clicking on it AND 
	// still IS selected after clicking on it AND if one or more of the other
	// checkboxes ARE selected, THEN the 'select all' checkbox must NOT be selected
	else if ((beforeClickOn) && (document.getElementById(idCheckboxList + "_0").checked) && (counter > 0))
	{
		document.getElementById(idCheckboxList + "_0").checked = false;
	}
	// If the 'select all' checkbox IS selected before clicking on it AND
	// IS NOT selected after clicking on it AND NONE of the other checkboxes
	// ARE selected, THEN the the 'select all' checkbox must be selected
	else if ((beforeClickOn) && (!document.getElementById(idCheckboxList + "_0").checked) && (counter == 0))
	{
		document.getElementById(idCheckboxList + "_0").checked = true;
	}
	else if (counter == nrOfCheckboxes - 1)
	{
		document.getElementById(idCheckboxList + "_0").checked = true;
		
		for (var i = 1; i < nrOfCheckboxes; i++)
		{
			document.getElementById(idCheckboxList + "_" + i).checked = false;
		}
	}
	else if (counter == 0)
	{
		document.getElementById(idCheckboxList + "_0").checked = true;
	}
}

// Global variable
var functionArray = new Array();

/* ======================================================================================
 * Function			:	fillFunctionList()
 * Description		:						
 * ======================================================================================
*/
function fillFunctionList()
{
	// Remember the previously selected functionname;
	var strPrevSelected = "";
	if (document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions.selectedIndex > -1)
	{
		strPrevSelected = document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions.options[document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions.selectedIndex].text;
	}
	
	// Clear array
	functionArray = new Array();
	
	if (document.forms[0].elements["_ctl0_ContentPlaceHolder1_chklInstances_0"].checked)
	{
		addArrayToArray(strHogeRaad.split(","));
		addArrayToArray(strCrvB.split(","));
		addArrayToArray(strCBB.split(","));
		addArrayToArray(strGerechtshoven.split(","));
		addArrayToArray(strRechtbanken.split(","));
	}
	else
	{
		if (document.forms[0].elements["_ctl0_ContentPlaceHolder1_chklInstances_1"].checked)
		{
			addArrayToArray(strHogeRaad.split(","));
		}
		if (document.forms[0].elements["_ctl0_ContentPlaceHolder1_chklInstances_2"].checked)
		{
			addArrayToArray(strCrvB.split(","));
		}
		if (document.forms[0].elements["_ctl0_ContentPlaceHolder1_chklInstances_3"].checked)
		{
			addArrayToArray(strCBB.split(","));
		}
		if (document.forms[0].elements["_ctl0_ContentPlaceHolder1_chklInstances_4"].checked)
		{
			addArrayToArray(strGerechtshoven.split(","));
		}
		if (document.forms[0].elements["_ctl0_ContentPlaceHolder1_chklInstances_5"].checked)
		{
			addArrayToArray(strRechtbanken.split(","));
		}
	}	

	// Sort array with functionnames
	functionArray.sort();

	// Delete all old items from selectbox
	deleteAllOption(document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions);

	// Add first item "Alle functies"
	addOption(document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions, "Alle functies", "", false);

	// Loop through array to add all items to the selectbox
	for (i = 0; i < functionArray.length; i++)
	{
		addOption(document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions, functionArray[i], functionArray[i], false);
	}
	
	// try to select the previously selected functionname
	if (strPrevSelected != "")
	{
		selectOption(document.forms[0]._ctl0_ContentPlaceHolder1_ddlFunctions, strPrevSelected);
	}
}

/* ======================================================================================
 * Function			:	addArrayToArray(arrayList)
 * Description		:							
 * Parameter
 *  arrayList		:	
 * ======================================================================================
*/
function addArrayToArray(arrayList) {


    
	var blnFound = false;
	for (j = 0; j < arrayList.length; j++)
	{
		strItem = arrayList[j];
		blnFound = false;
		
		for (i = 0; i < functionArray.length; i++)
		{
			if (functionArray[i] == strItem)
			{
				var blnFound = true;
			}	
		}
		
		if (!blnFound)
		{
		    functionArray[functionArray.length] = htmlEncode(strItem);
		}
	}
}


function htmlEncode(str) {
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
}


/* ======================================================================================
 * Function			:	addOption(obj, text, value, selected)
 * Description		:							
 * Parameter
 *  obj				:	
 *  text			:	
 *  value			:	
 *  selected		:	
 * ======================================================================================
*/
function addOption(obj, text, value, selected)
{
	if (obj != null && obj.options != null)
	{
		obj.options[obj.options.length] = new Option(text, value, false, selected);
	}
}

/* ======================================================================================
 * Function			:	deleteAllOption(obj)
 * Description		:							
 * Parameter
 *  obj				:	
 * ======================================================================================
*/
function deleteAllOption(obj)
{
	while (obj.options.length > 0)
	{
		obj.options[0] = null;
	}
}

/* ======================================================================================
 * Function			:	selectOption(obj, strText)
 * Description		:							
 * Parameter
 *  obj				:	
 *  strText			:	
 * ======================================================================================
*/
function selectOption(obj, strText)
{
	for (var i = 0; i < obj.options.length; i++)
	{
		if (obj.options[i].text == strText)
		{
			obj.selectedIndex = i;
		}
	}
}
