/*

Author: Mark McDonnell [ mark [at] storm-media [dot] co [dot] uk ]

Application Date: 2006.02.27 / Monday.

Issues: N/A

*/

// Global Variables

	var d = document;
	
// DOM Functions

	function fnCreateObj(obj)
	{
		return d.createElement(obj);
	}
	// eDIV = fnCreateObj('div');
	
	function fnCreateStr(str)
	{
		return d.createTextNode(str);
	}
	// eTxtNode = fnCreateStr('Hello World!');
	
	function fnAppendChild(pNode,cNode)
	{
		pNode.appendChild(cNode);
	}
	// fnAppendChild(eUL,eLI);
	
	function fnSetAttribute(obj,attr,val)
	{
		obj.setAttribute(attr,val);
	}
	// fnSetAttribute(eDIV,'id','myDiv');
	
	function fnGetElement(objID)
	{
		return d.getElementById(objID);
	}
	// fnGetElement('id');	

	function fnInsertAfter(newElement, targetElement)
	{
		var parent = targetElement.parentNode;
		if(parent.lastChild == targetElement)
		{
			parent.appendChild(newElement);
		}
		else
		{
			parent.insertBefore(newElement, targetElement.nextSibling);
		}
	}
	
// Popup Window Function

	var Popup = {
		open: function(domain, w, h, toggle)
		{
			this.options = {
			url: domain,
			width: w,
			height: h,
			scrollbars: toggle
			}
			
			window.open(this.options.url, '', 'resizable=yes, width=' + this.options.width + ',height=' + this.options.height + ',scrollbars=' + this.options.scrollbars);
		}
	}	
	// document.onclick = function() { Popup.open('http://www.storm-media.co.uk/', 780, 440, 'yes'); }

// Event Function

function fnAddEvent(obj, evType, fn, useCapture)
{
	
	if (obj.addEventListener)
	// Gecko browsers (Mozilla / Firefox)
	{
		obj.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (obj.attachEvent)
	// Internet Explorer 5+
	{
		var r = obj.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{
		// This sets the event handler for NS4 & IE5/Mac.
		elm['on' + evType] = fn;
	}

}

// Event Triggers

	fnAddEvent(window, 'load', fnSetSideNav);
	fnAddEvent(window, 'load', fnSetFooterTarget);
	fnAddEvent(window, 'load', fnColourTableRows)

// Triggered Functions

	function fnSetSideNav()
	{
		/*
			This function first hides all the sub nav links in the side navigation.
			Then adds an onClick event to each link that has a sub list.
		*/
		
		if(!document.getElementById('id-SubNavigation')) // If the required element(s) does not exist, then return the function.			
		{
			return;
		}
		else // else hide the navigation using JavaScripts CSS-DOM, this way the menu is still accessible without JavaScript.
		{
			var storeSubNav = d.getElementById('id-SubNavigation');
			var storeSubLists = storeSubNav.getElementsByTagName('ul');
		}
		
		for(i=0; i<storeSubLists.length; i++)
		{
			storeSubLists[i].style.display = 'none'; // Hide all lists that are found within an li element.
			
			//alert(storeSubLists[i].getAttribute('id'));
			
			eval("storeSubLists[i].parentNode.onclick = function(){ fnToggleSubNavLists(" + i + ",storeSubLists["+ i +"].getAttribute('id')); fnToggleLinkStyle(this, 'on');}") // Add event handlers to each top link that has a sub nav list.
			
			/*
				The anonymous function was not evaluating the variable 'i' correctly.
				Once the anonymous function was called, the 'i' variable equalled the final loop number?
				
				To solve this issue I used the 'eval()' method to run the mouse event
				and escaped the string when it reached the 'i' variable inside the anonymous function
				so its value was kept the same as the loops current iteration.
			*/
		}
	}
	
	function fnToggleSubNavLists(argNumber, argElement)
	{
		var storeSubNav = d.getElementById('id-SubNavigation');
		var storeSubLists = storeSubNav.getElementsByTagName('ul');
		
		for(i=0; i<storeSubLists.length; i++)
		{
			storeSubLists[i].style.display = 'none'; // Hide all lists that are found within an li element.
		}
		
		d.getElementById(argElement).style.display = "block"; // Show the selected side navigation list
	}
	
	function fnToggleLinkStyle(element, state)
	{
		var storeSubNav = d.getElementById('id-SubNavigation');
		var storeSubLists = storeSubNav.getElementsByTagName('ul');
		
		for(i=0; i<storeSubLists.length; i++)
		{
			// Reset the link's styles.
			storeSubLists[i].parentNode.firstChild.className = 'cls-JSMenuClass';
		}
		
		// Set the current top link to keep its 'over' state.
		element.firstChild.className = 'cls-JSMenuClassOver';
	}
	
	function fnSetFooterTarget()
	{
		if(fnGetElement('id-Footer') != null)
		{
			var storeFooterLink = fnGetElement('id-Footer'); // If the element does exist then create a variable to contain it.
			var storeLinks = storeFooterLink.getElementsByTagName('a'); // store all anchor elements.
			
			for(i=0; i<storeLinks.length; i++)
			{
				storeLinks[i].onclick = function() { Popup.open(this, 800, 460, 'yes'); return false; } // open the url in a popup window and use 'return false' to prevent the html link from firing.
			}
		}
	}
	
	function fnColourTableRows()
	{
		if(fnGetElement('idTable') != null)
		{
			var storeTABLE = document.getElementById('idTable');
			var storeTR = storeTABLE.getElementsByTagName('TR');
			var state = 'even';
			
			for(i=1; i<storeTR.length; i++)
			{
				if(state == 'odd')
				{
					storeTR[i].style.backgroundColor = "#d9e2ef";
					state = 'even';
				}
				else if(state == 'even')
				{
					storeTR[i].style.backgroundColor = "#c9d7e7";
					state = 'odd';
				}
			}		
		}
	}
	
// Misc Functions

	function fnCheckAllFormBoxes(FormID, FormChecked)
	{
		var storeForms = document.getElementById(FormID);
		var storeInputs = storeForms.getElementsByTagName('input');
		
		for(i=0; i<storeInputs.length; i++)
		{
			if(FormChecked == 'checked' && storeInputs[i].getAttribute('type') == 'checkbox')
			{
				storeInputs[i].setAttribute('checked',FormChecked);
			}
			else if(FormChecked == 'uncheck' && storeInputs[i].getAttribute('type') == 'checkbox')
			{
				storeInputs[i].removeAttribute('checked');
				// The button in the form is a 'reset' button so IE doesn't need this current function
				// to remove the 'checked' attribute. Mozilla on the other hand ignores the 'reset'
				// button and instead uses the DOM to remove any 'checked' attributes.
			}
		}
	}

	function submitForm(){
		var i
		for(i=0; i<document.Prosearch.fSearchType.length; i++){
			if(document.Prosearch.fSearchType[i].checked)
				document.Prosearch.action = document.Prosearch.fSearchType[i].value;
		}
	
		document.Prosearch.submit()
		
	}
