// This is a cross browser event handler
function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}



// Opens a link in a popup (or new window) when class = popup

function doPopups() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if (links[i].className.match("popup")) {
			links[i].className = links[i].className + " newWinStyle";
			if (links[i].title == "") {
				links[i].title = "(new window)";
			} else {
				links[i].title = links[i].title + " (new window)";	
			}
			links[i].onclick = function(e) {
				if(!e)e=window.event;
				if(e.shiftKey || e.ctrlKey || e.altKey) return;
				window.open(this.href,"ATICCC",",,scrollbars=no,toolbar=no,status=no,width=500px,height=500px");
				return false;
			}
		}
	}
}

// Runs all the listed functions on the loading of the window

window.onload=function(){
	doPopups();
}

// 
function getElementsByClass(element, classname) {
  var messages=new Array();
  var inc=0;
  var alltags=document.all? document.all : document.getElementsByTagName(element);
  for (i=0; i<alltags.length; i++){
    if (alltags[i].className==classname)
      messages[inc++]=alltags[i];
  }
  return messages;
}

// Moves an element to a position before/after hangerId element
function moveElement(elementId, hangerParentId, hangerId) {
  if (!document.getElementById) return false;
  
  var element = document.getElementById(elementId);
  var hanger = document.getElementById(hangerId);
  var hangerParent = document.getElementById(hangerParentId);
  
  if (hangerParent) {
    hangerParent.insertBefore(element, hanger);
  } else {
    hanger.appendChild(element);
  }
}


//
function moveRelatedLinkCategory(categoryId, hangerParentId, hangerId) {
  if (!document.getElementById) return false;
  
  if (document.getElementById(categoryId)) {
    moveElement(categoryId, hangerParentId, hangerId);
    
    // Check if related links is now empty and hide the H3
    var relatedLinkDivs = getElementsByClass("div", "rlCategory");
    var shouldHide = true;
    for (i=0; i<relatedLinkDivs.length; i++) {
      if (relatedLinkDivs[i].id != categoryId) shouldHide = false;
    }
    if (shouldHide) {
      var parentN = document.getElementById("relatedLinks");
      parentN.parentNode.removeChild(parentN);
     }
  }
}


// Deals with the expanding and collapsing of FAQs

// This does the initial hiding of the answers
function hideanswers() {
	var links = document.getElementsByTagName("div");
  	for (var i=0; i < links.length; i++) {
    if (links[i].className.match("lAnswer")) {
  		links[i].style.display = 'none';
    }
  }
}

// This controls what happens when a click occures
function linkclicked(e) {
	if (window.event) {
		var whichlink = window.event.srcElement;
	}
	else 
	{
	var whichlink = e.target;
	}
	 while (whichlink.nodeName.toLowerCase() != 'a' &&
      whichlink.nodeName.toLowerCase() != 'body')
    whichlink = whichlink.parentNode;
	var findanswer = whichlink.parentNode.getElementsByTagName("div");
  for (var i=0; i < findanswer.length; i++) {
    if (findanswer[i].className.match("lAnswer"))  {
			if (findanswer[i].style.display.match("none"))  {
			findanswer[i].style.display = 'block';
		}
		else  {
			findanswer[i].style.display = 'none';
		}
		}
	}
	if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.stopPropagation && e.preventDefault) {
      e.stopPropagation();
      e.preventDefault();
    }
  }
// This listens for the click using the addEvent function
function linkclicklistener(e) {
var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("lQuestion")) {
      	addEvent(links[i], 'click', linkclicked, false);
	}
  }
}

// Get a specific cookie
function getCookie(name)
{
	var cookies = document.cookie;
	if (cookies.indexOf(name) != -1)
	{
		var startpos = cookies.indexOf(name)+name.length+1;
		var endpos = cookies.indexOf(";",startpos)-1;
		if (endpos == -2) endpos = cookies.length;
		return unescape(cookies.substring(startpos,endpos));
	}
	else
	{
		return false; // the cookie couldn't be found! it was never set before, or it expired.
	}
}

function detectLowVision() {
	var cookieValue = getCookie("Headscape");
	if (cookieValue != "UAType=lowvision" && cookieValue != "UAType=lowvisio") {
			moveRelatedLinkCategory("rlcnextstep", "navigationContainer", "sNavigation");
			moveRelatedLinkCategory("rlcrelatedservices", "", "sNavigation");
	}
}


// Runs all the listed functions on the loading of the window
window.onload=function(){
 doPopups();
}