
/*generic js function
adds onload events to the page
param func - the function to be added
return nothing
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload !='function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
var linkHoverColor = "#CC39D4";
var linkActiveColor = "#7502B4";
var linkBasicColor = "black";

function prepareSubmenus(menuId){
	if(!document.getElementById(menuId)) return false;
	
	var menuoffset=-3;
	//get all the submenus(lists) in the parent list
	var ultags=document.getElementById(menuId).getElementsByTagName("ul");
	for (var t=0; t<ultags.length; t++){
    	/*var spanref=document.createElement("span");
		spanref.className="arrowdiv";
		spanref.innerHTML="&nbsp;&nbsp;";
		ultags[t].parentNode.getElementsByTagName("a")[0].appendChild(spanref);*/
		
		//remove the link from the list and replace it with a span
		//alert("before swap");
		swapLinkForSpan(ultags[t].parentNode.getAttribute("id"));
    	ultags[t].parentNode.onmouseover=function(){
    		this.getElementsByTagName("ul")[0].style.left=this.parentNode.offsetWidth+menuoffset+"px";
    		this.getElementsByTagName("ul")[0].style.display="block";
			var disabledLink = this.getElementsByTagName("span")[0];
    	}
    	ultags[t].parentNode.onmouseout=function(){
    		this.getElementsByTagName("ul")[0].style.display="none";
    	}
		
    }
	setMenuOnclick(menuId);
}

function setMenuOnclick(menuId){
	if(!document.getElementById(menuId)) return false;
	
	var litags=document.getElementById(menuId).getElementsByTagName("li");
	//alert(litags.length);
	var i = 0;
	for (var t=0; t<litags.length; t++){
		if(litags[t].firstChild.tagName == "A"){
			//alert(litags[t].firstChild.innerHTML);
			litags[t].onclick = function(){
				//alert(this.firstChild.innerHTML);
				var text = this.firstChild.getAttribute("href");
				window.location  = text;
				return false;
			}
			litags[t].onmouseover = function(){
				this.firstChild.style.color = linkHoverColor;
			}
			litags[t].onmouseout = function(){
				this.firstChild.style.color = linkBasicColor;
			}
			
		}
	}
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

function swapLinkForSpan(listItemId){
	//alert("swapping");
	if (!document.getElementById(listItemId)) return false;
	
	var listItem = document.getElementById(listItemId);
	
	//retreive the link from the list item
	var obsoleteLink = listItem.firstChild
	
	if (obsoleteLink.tagName == "A"){
		//get the text from the link
		
		var linkText = html_entity_decode(obsoleteLink.innerHTML);
		var txt = document.createTextNode(linkText);
		var spn = document.createElement("span");
		spn.appendChild(txt);
		
		//replace the old link with the newly created span
		listItem.replaceChild(spn, obsoleteLink);
		
		return true;
	}
	else return false;
}

function prepareActiveLink(linkId){
	if (!document.getElementById(linkId)) return false;
	
	swapLinkForSpan(linkId);
	var listItems = document.getElementById(linkId).getElementsByTagName("SPAN");
	if(listItems.length > 0){
		listItems[0].style.color = linkActiveColor;
	}
}


function addEmailDetails(tableId){
	var tbody = document.getElementById(tableId).getElementsByTagName("TBODY")[0];
	var row = document.createElement("tr");
    var td1 = document.createElement("td");
    td1.appendChild(document.createTextNode("E-mail:"));
    var td2 = document.createElement("td");
	var email = "webenquiries@aesthetiquetraining.co.uk";
	var hrefText = "mailto:" + email;
	var mailLink = document.createElement("a");
	mailLink.setAttribute("href", hrefText);
	var linkText   = document.createTextNode(email);
	mailLink.appendChild(linkText);
	
    td2.appendChild (mailLink);
    row.appendChild(td1);
    row.appendChild(td2);
    tbody.appendChild(row);
}

















