// JavaScript Document
addOnload(updatesubmenu);
addOnload(equaliseparagraphs);
addOnload(toggleQuestion);
function addOnload(newFunction) {
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) {
				oldOnload();
			}
			newFunction();
		}
	}
	else {
		window.onload = newFunction;
	} 
}

function updatesubmenu()
{
	fullURL =  window.location.href;
	var list  = document.getElementById("sub-nav");
	var listItems = list.getElementsByTagName("a");
	for(var j=0;j<=listItems.length-1; j++) 
	{
		if((listItems[j].href) == (fullURL))
		{
		 listItems[j].className = "selected"; // select the 'a' and write a class of selected on it
		}
	}
}

function equaliseparagraphs() // grab the paragraphs inside of location and equalise the heights
{
	var elements =	getElementsByClass("location-address");
	for(var j=0;j<=elements.length-1; j=j+2) 
	{
		var firstpara = elements[j].getElementsByTagName('p')[0];
		var secondpara = elements[j+1].getElementsByTagName('p')[0];
		var large_p = Math.max(firstpara.offsetHeight,secondpara.offsetHeight);
		
		firstpara.style.height = large_p+'px';
		secondpara.style.height = large_p+'px';
		
	}
	
}


function toggleQuestion()
{
	if(document.getElementById('question1'))
	{
		
		var allp = getElementsByClass('question');
		var alla = getElementsByClass('answer');
		var showAll = document.getElementById('ShowAll');
		
		showAll.onclick = function ()
		{
			if(alla[0].style.display=='none')
			{
				for(var k=0; k<alla.length; k++)
				{
		 		alla[k].style.display = '';
				}
			showAll.innerHTML = 'Hide All Answers';
			return false;
			}
			else 
			{
				for(var h=0; h<alla.length;h++)
				{
		 		alla[h].style.display = 'none';
				}
			showAll.innerHTML = 'Show All Answers';
			return false;
			}
			
		}
		
		for(var j=0; j<alla.length; j++)
		{
		 	alla[j].style.display = 'none';
		}
		
		for(var i=0; i<allp.length; i++)
		{
		 	allp[i].onclick = toggleanswer;
		}
		
		
	}
	function toggleanswer()
	{
			var id = (this.id).substring(8);
			var question = document.getElementById('answer'+id);
			if(question.style.display=='none')
			{
				question.style.display='block';
				return false;
			}
			else 
			{
				question.style.display='none';
				return false;
			}
	}	
}



function getElementsByClass (theClass) //helper class
{
  var elementArray = [];

  if (document.all)
  {
    elementArray = document.all;
  }
  else
  {
    elementArray = document.getElementsByTagName("*");
  }

  var matchedArray = [];
  var pattern = new RegExp("(^| )" + theClass + "( |$)");

  for (var i = 0; i < elementArray.length; i++)
  {
    if (pattern.test(elementArray[i].className))
    {
      matchedArray[matchedArray.length] = elementArray[i];
    }
  }

  return matchedArray;
};




