/**
 * 	GBA GROUP JAVASCRIPT
 */

/**
 * Adds an onload event
 * 
 * @param func
 * @return
 */
function addLoadEvent(func) 
{
	var oldonload = window.onload;
  	if (typeof window.onload != 'function') 
  	{
    	window.onload = func;
  	}
  	else 
  	{
    	window.onload = function() 
    	{
      		if (oldonload) 
      		{
        		oldonload();
      		}
      		func();
    	}
  	}
}

/**
 * Loop around and add onClick Events to the Main Nav
 * 
 * @return
 */
function activateLinks()
{
	// Firstly lets get the elements that have the jsMainNav class
	linksArray = $$('a.jsMainNav');
	
	// Loop around $linksArray and add a behaviour
	for (i=0; i<linksArray.length; i++)
	{
		// Loop around links and add function
		linksArray[i].onclick = function()
	    {
			followLink(this);
	        return false;
	    }
	}
	
	// Now lets get the top section links
	linksArray = $$('a.jsSectionNav');
	
	// Loop around $linksArray and add a behaviour
	for (i=0; i<linksArray.length; i++)
	{
		// Loop around links and add function
		linksArray[i].onclick = function()
	    {
			// Only open nav if it isn't already open
			if($('page-close-tab').getAttribute('rel') == 'closed')
			{
				openNav(this);
			}
	        return false;
	    }
	}
	
}

function activateRollovers()
{
	// Fistly lets get the elements that have the jsRollover class
	rolloverArray = $$('img.jsRollover');

	// Loop around rolloverArray
	for (i=0; i<rolloverArray.length; i++)
	{
		rolloverArray[i].onmouseover = function() 
		{
			// Add this function which works out rollover based on -o.ext
			var length = this.getAttribute('src').length;
		
			// Work out the new src		
			var new_src = this.getAttribute('src').substr(0, length-4) + "-over.gif" 
			
			// Set the src
			this.setAttribute('src', new_src);
			return false;
		}
			
		rolloverArray[i].onmouseout = function() 
		{
			// Add this function which works out rollover based on -o.ext
			var length = this.getAttribute('src').length;
			var src = this.getAttribute('src').substr(0, length-9) + ".gif";
			this.setAttribute('src', src);
			return false;
		}
	
	}
}

/**
 * Close the Main Navigation
 * 
 * @return
 */
function closeNav()
{
	// Close the Navigation
	Effect.BlindUp('main-nav');
	
	// Change the tab
	$('page-close-tab').setAttribute('rel', 'closed');
	$('page-close-tab').innerHTML = "<a href=\"#\" onclick=\"openNav();\"><img src=\"/images/nav/open-tab.gif\" alt=\"Open Navigation\" /></a>";
}

/**
 * Opens the main navigation
 * 
 * @return
 */
function openNav()
{
	// Get the main name
	node = $('main-nav');
	//alert(node.display);
	
	// Close the navigation
	Effect.BlindDown('main-nav');
	
	// Change the tab
	$('page-close-tab').setAttribute('rel', 'open');
	$('page-close-tab').innerHTML = "<a href=\"#\" onclick=\"closeNav();\"><img src=\"/images/nav/close-tab.gif\" alt=\"Close Navigation\" /></a>";
}

/**
 * Closes the main navigation and follows the HREF of the link
 * 
 * @param node
 * @return
 */
function followLink(node)
{
	// Firstly we need to get the URL of the page
	var href = node.readAttribute('href');

	// Close the Navigation
	Effect.BlindUp('main-nav', 
	{
		afterFinish: function() 
		{
			// Load the new page
			window.location = href;
		}
	});
	
	// Change the tab
	$('page-close-tab').innerHTML = "<a href=\"#\" onclick=\"openNav();\"><img src=\"/images/nav/open-tab.gif\" alt=\"Open Navigation\" /></a>";
}

function sendApplicationForm()
{
	$error = 0;
		
	if(document.contact.code.value == "")
	{
		$error = 1;
		$message = "Please enter the security code as shown on screen.";
	}
		
	if(document.contact.applicationForm.value == "")
	{
		$error = 1;
		$message = "Please attach a copy of your application form using the Browse Button";
	}
		
	if(document.contact.email.value == "")
	{
		$error = 1;
		$message = "Please enter your email address";
	}
	
	if(document.contact.telephone.value == "")
	{
		$error = 1;
		$message = "Please enter your telephone number";
	}
	
	if(document.contact.postcode.value == "")
	{
		$error = 1;
		$message = "Please enter your postcode";
	}
	
	if(document.contact.country.value == "")
	{
		$error = 1;
		$message = "Please enter your country";
	}
	
	if(document.contact.city.value == "")
	{
		$error = 1;
		$message = "Please enter your town / city";
	}
	
	if(document.contact.address.value == "")
	{
		$error = 1;
		$message = "Please enter your address";
	}
		
	if(document.contact.name.value == "")
	{
		$error = 1;
		$message = "Please enter your name";
	}
		
	if($error == 1)
	{
		alert($message);
		return false;
	}
	else
	{
		document.contact.submit();	
	}
}


addLoadEvent(activateLinks); 
addLoadEvent(activateRollovers); 