window.onload = function()
{
	prepareLinks();
	highlightNav();
	highlightTopNav();
}

function prepareLinks()
{
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++)
	{
		if (links[i].getAttribute("className"))
		{
			if (links[i].getAttribute("classname") == "external") 
			{
				if (links[i].getAttribute('title') == '')
					links[i].setAttribute("title","(opens in a new window)");
				links[i].onclick = function()
				{
					OpenNewWindow(this.getAttribute('href'));
					return false;
				}
			}
			else if (links[i].getAttribute('classname').substring(0,6) == 'popup_')
			{
				links[i].setAttribute('title','this is a popup');
				links[i].onclick = function()
				{
					popup(50,50,this.getAttribute('classname').split('_')[1],this.getAttribute('classname').split('_')[2],this.getAttribute('href'));
					return false;
				}
			}
			else
			{
	//			var linkText  = links[i].firstChild.nodeValue;
	//			links[i].setAttribute('title','this is a link to '+linkText);
			}
		}
		else if (links[i].getAttribute("class"))
		{
			if (links[i].getAttribute("class"))
			{
				if (links[i].getAttribute("class") == "external") 
				{
					if (links[i].getAttribute('title') == '')
						links[i].setAttribute("title","(opens in a new window)");
					links[i].onclick = function()
					{
						OpenNewWindow(this.getAttribute('href'));
						return false;
					}
				}
				else if (links[i].getAttribute('class').substring(0,6) == 'popup_')
				{
					links[i].setAttribute('title','this is a popup');
					links[i].onclick = function()
					{
						popup(50,50,this.getAttribute('class').split('_')[1],this.getAttribute('class').split('_')[2],this.getAttribute('href'));
						return false;
					}
				}
			}
		}
  	}
}

function popup(x,y,w,h,fn)
{
	NewWindow = window.open(fn,'descr','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=0,scrollbars=1,resizable=1,copyhistory=no,width='+w+',height='+h+', top='+y+',left='+x)
}


function OpenNewWindow(fn)
{
	NewWindow = window.open(fn)
}


function checkAnswerFields()
{
	var name = document.forms[0].name.value;
	var Title = document.forms[0].Title.value;
	var Charity = document.forms[0].Charity.value;
	var regNo = document.forms[0].regNo.value;
	var phone = document.forms[0].phone.value;
	var Email = document.forms[0].Email.value;
	var vf = true;
	var msg = '';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (filter.test(Email))
		vf = true;
	else
	{
		msg = 'Sorry it looks like you have entered an invalid email address.\n';
		vf = false;
	}
	if (name.length == 0)
	{
		msg = msg + 'You have forgotten to fill in your name.\n';
		vf = false;
	}
	if (Title.length == 0)
	{
		msg = msg + 'You have forgotten to fill in your Title.\n';
		vf = false;
	}
	if (Charity.length == 0)
	{
		msg = msg + 'You have forgotten to fill in your Charity.\n';
		vf = false;
	}
	if (regNo.length == 0)
	{
		msg = msg + 'You have forgotten to fill in your regNo.\n';
		vf = false;
	}
	if (phone.length == 0)
	{
		msg = msg + 'You have forgotten to fill in your phone.\n';
		vf = false;
	}

	if (msg != '')
		alert(msg);
	return vf;
}

function highlightNav(){
	var nav = document.getElementById('inner_nav');
	if (nav == null){
		return;
	}
	
	var elements = nav.getElementsByTagName('dd');
	var currentElement;
	var theLink;
	
	var currentPage = window.location.protocol + '//' + window.location.host + window.location.pathname;
	for (var i = 0; i < elements.length; i++){
		currentElement = elements[i];
		theLink = currentElement.childNodes[0];
		//alert (theLink.href)
		//alert (currentPage)		
		if (currentPage == theLink.href){
			if (currentElement.className != null && currentElement.className != ''){
				currentElement.className += " onpage";
			} else {
				currentElement.className += "onpage";
			}
		}
	}
}

function highlightTopNav()
{
	var nav = document.getElementById('header');
	var elements = nav.getElementsByTagName('li');
	var currentElement;
	var theLink;
	var currentPageDir = getTopUrlDir(window.location.href);
	for (var i = 0; i < elements.length; i++){
		currentElement = elements[i];
		theLink = getTopUrlDir(currentElement.childNodes[0].href);
		if (theLink == currentPageDir){
			if (currentElement.className != null && currentElement.className != ''){
				currentElement.className += " onpage";
			} else {
				currentElement.className += "onpage";
			}
		}
	}
}

function getTopUrlDir(pageUrl){
	var parts = pageUrl.split('/');
	if (null == parts || parts.length < 4){
		return '';
	}
	return parts[3];
}
