/*
	<file>
		show_hide_content.js
	</file>
	<description>
		Javascript code to show and hide content contained within divs.
	</description>
	<dependencies>
	</dependencies>
	<usage>
	</usage>
*/

/*
	<name>
		getElement()
	</name>
	<arguments>
		<argument>
			<name>
				elementName
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the the element from the page.
			</usage>
		</argument>
	</arguments>
	<usage>
		<input type="button" onClick="javascript:hideDiv('blah');">
		<div id="blah">This is some content to be hidden when button is clicked.</div>
	</usage>
*/
function getElement(elementName)
{
	// For IE5 and NS6 or higher, use this code
	if(document.getElementById)
		return document.getElementById(elementName);
			
	// For NS4 or lower, use this code
	else
		if(document.layers)
			return document.layers[elementName];
			
	// For IE4 or lower, use this code
		else
			return document.all[elementName];
}

/*
	<name>
		hideDiv()
	</name>
	<arguments>
		<argument>
			<name>
				divName
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the div element from the page.
			</usage>
		</argument>
	</arguments>
	<usage>
		<input type="button" onClick="javascript:hideDiv('blah');">
		<div id="blah">This is some content to be hidden when button is clicked.</div>
	</usage>
*/
function hideDiv(divName)
{
	// Finds the element by name and sets its display to none
	getElement(divName).style.display = "none";
}

/*
	<name>
		showDiv()
	</name>
	<arguments>
		<argument>
			<name>
				divName
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the div element from the page.
			</usage>
		</argument>
	</arguments>
	<usage>
		<input type="button" onClick="javascript:showDiv('blah');">
		<div id="blah" style="display: none">This is some content to be shown when button is clicked.</div>
	</usage>
*/
function showDiv(divName)
{
	// Finds the element by name and sets its display to block which will show it as normal
	getElement(divName).style.display = "block";
}

/*
	<name>
		toggleDiv()
	</name>
	<arguments>
		<argument>
			<name>
				divName
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the div element from the page.
			</usage>
		</argument>
	</arguments>
	<usage>
		<input type="button" onClick="javascript:toggleDiv('blah');">
		<div id="blah">This is some content to be shown or hidden (toggled) when button is clicked.</div>
	</usage>
*/
function toggleDiv(divName)
{
	// Retrieve the div element
	var element = getElement(divName);
	
	// If the element doesn't exist, try to display it anyways
	if(element == null)
		showDiv(divName);
	// The else statement meaning this element exists
	else
	{
		// If the display for this element is set to none or is blank, show it
		if(element.style.display == "none" || element.style.display == "")
			showDiv(divName);
		// If the display for this element is set to block or anything else, hide it
		else
			hideDiv(divName);
	}
}

/*
	<name>
		toggleDivIfOtherIs()
	</name>
	<arguments>
		<argument>
			<name>
				divName
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the div element from the page.
			</usage>
		</argument>
		<argument>
			<name>
				otherDivName
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the second div element from the page.
			</usage>
		</argument>
		<argument>
			<name>
				condition
			</name>
			<type>
				string
			</type>
			<usage>
				Used in retrieving the display style on the 2nd div to test as a condition
				for toggling the first div.
			</usage>
		</argument>
	</arguments>
	<usage>
		<input type="button" onClick="javascript:toggleDiv('blah');">
		<div id="blah">This is some content to be shown or hidden (toggled) when button is clicked.</div>
	</usage>
*/
function toggleDivIfOtherIs(divName, otherDivName, condition)
{
	// Retrieve the "other" div element
	var otherElement = getElement(otherDivName);

	// If the element doesn't exist, try to display it anyways
	if(otherElement == null)
		showDiv(divName);
	// The else statement meaning this elemtn exists
	else
	{
		// Check that the display for this element is set to the condition
		if(otherElement.style.display == condition || ((condition == "none") && otherElement.style.display == ""))
		{
			// If the condition is met, toggle the div
			toggleDiv(divName);
		}
	}
}

function purgeDivsById(divIdSubstring, thisDiv)
{
	var element = null;
	var count = 1;

	while((element = getElement(divIdSubstring.replace("<<d>>", count))))
	{
		if(count != thisDiv)
			hideDiv(element.id);

		if(divIdSubstring.toLowerCase().match("sub_"))
		{
			element_id = divIdSubstring.toLowerCase().replace("sub_", "").replace("<<d>>", count);
			//alert("Count: " + count + "\nthisDiv: " + thisDiv);
			if(count != thisDiv)
			{
				if((element = getElement(element_id + "_left")))
					hideDiv(element.id);

				if((element = getElement(element_id + "_down")))
					hideDiv(element.id);
			}
		}

		count++;
	}

	// Cheat fix
}