
function setuppage() {
	stripe('pubindex', 'data_1', 'data_2');
	removeTableBorders();
	checkextlinks();
}



// Function to get rid of any images which don't have a src value
 function removeEmptyImages() {
	var allimages = document.getElementsByTagName("img");
	if (! allimages) { return; }
      for (var i = 0; i < allimages.length; i++) {
          var image_path = allimages[i].src;
		  var substring = image_path.substring((image_path.length -1),image_path.length);
			if ((image_path == '') ||(substring == '/')) {
				allimages[i].parentNode.removeChild(allimages[i]);
			}
        }
  }

// Function to get rid of any subtitle
 function removeEmptySubtitles() {
	var subtitle = document.getElementById("subtitle");
	if (! subtitle) { return; }
	   	if (subtitle.innerHTML == '') {
			subtitle.parentNode.removeChild(subtitle);
		}
  }

// Function to get rid of the border property of a table, these are generally put in by content authors
 function removeTableBorders() {
	var alltables = document.getElementsByTagName("table");
	if (! alltables) { return; }
      for (var i = 0; i < alltables.length; i++) {
          var border = alltables[i].border;
		  if (alltables[i].border != '0') {
				alltables[i].border = '0';
			}
        }
  }

// Function to stripe the table
 function stripe(classname, oddclass, evenclass) {
    
	var even = false;
	var all_tables = document.getElementsByTagName("table");
	
   
		for (var t = 0; t < all_tables.length; t++)
		{
			if (all_tables[t].className == classname) {
				var trs = all_tables[t].getElementsByTagName("tr");
				for (var i = 0; i < trs.length; i++) {
					// get all the cells in this row...
					var tds = trs[i].getElementsByTagName("td");
					// and iterate through them...
					for (var j = 0; j < tds.length; j++) {
						var mytd = tds[j];
						if (even == false ) {
							tds[j].className=oddclass;
						} else {
							tds[j].className=evenclass;
						}
					}
				even =  ! even;
				}
			}
		}
  }

// Function to check external links
function checkextlinks(){
	if (document.getElementById('maincontent')) {
		var links = document.getElementById('maincontent').getElementsByTagName('a');
	} else if (document.getElementById('maincontent2')) {
		var links = document.getElementById('maincontent2').getElementsByTagName('a');
	}

	if (links) {
		for (var i=0; i<links.length; i++) {
			var link = links[i];
			setExtlink(link);
		}
	}
}



// function to check for an external url and then add an arrow if necessary
function setExtlink(link) {
	var hrefvalue = link.getAttribute("href");
	if (hrefvalue && hrefvalue.match(/^http:/) && (!hrefvalue.match(/\.epa\.ie/))) {
		if (!hrefvalue.match(/rook/)) {
				if (!hrefvalue.match(/192\.168\.1\.31/)) {
					var domain = hrefvalue.match(/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/);
					domain = RegExp.$2;
					var icon = document.createElement("img");
					icon.src = "http://www.epa.ie/images/icon_web.gif";
					icon.setAttribute("alt","External link");
					link.parentNode.insertBefore(icon, link);
				}
		}
	}
}

// opens a new browser window for the map
function openmaplink(src) {
  newWin = window.open(src.getAttribute("href"), '', 'toolbar=no,location=no,titlebar=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width=1014,height=728,top=0,left=0', false);
}


// Style Switcher
  function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  createCookie("style", title, 365);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return 'standard';
}

window.onload = function(e) {
  var cookie = readCookie("epa_style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("epa_style", title, 365);
}

var cookie = readCookie("epa_style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);