// dropdown select location change
function sel(Url) {
	if (Url) {
  	window.open(Url,"_blank");
  }
}

// open a popup window
function openwin(url,w,h,s) {
  var scrl;
	if (s) { scrl="scrollbars=yes,"; w = w+16; }
	else { scrl="scrollbars=no,"; }

	if (!w) w = 640;
	if (!h) h = 480;

	fenster = window.open(url,"_blank",scrl+"resizable=yes,width="+w+",height="+h+",screenX=50,screenY=50");
	fenster.focus();
}
var active;
function fontsizeup() {
  active = getActiveStyleSheet();
  setActiveStyleSheet('large');
}

function fontsizedown() {
  active = getActiveStyleSheet();
  setActiveStyleSheet('reg');
}

function setActiveStyleSheet(title) {
  if(title == 'reg'){
	document.body.className = 'reg';
  }else{
	document.body.className = 'large';
  }
}

function getActiveStyleSheet() {
	if (document.body.className == 'large') {
		return 'large';
	} else {
		return 'reg';
	}
}

function getPreferredStyleSheet() {
  return ('reg');
}

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 null;
}

function styleSheetOnLoad() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  if (title == 'null') {
    title = getPreferredStyleSheet();
  }
  setActiveStyleSheet(title);
}

function styleSheetOnUnload() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

//hide/show lang dropdown when onmouseover/onmouseout on main navigation
function showHideLang(){
  if (document.getElementById){
    lang_container = document.getElementById('lang-container');
    if (lang_container) {
      if (lang_container.style.visibility == 'hidden'){
        lang_container.style.visibility = 'visible';
      } else {
        lang_container.style.visibility = 'hidden';
      }
    }
  }
}

// JavaScript Server Side Includes - XML Requests

function funcClientSideInclude(id, url){
	var req = false;

	// Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest){
		try {
			req = new XMLHttpRequest();
		}
		catch(err){
			req = false;
		}
	}
	else if (window.ActiveXObject) {
		// For Internet Explorer on Windows
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(err){
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(err){
				req = false;
			}
		}
	}

	var element = document.getElementById(id);
	if(!element){
		alert("ID error: " + id + " passed to clientSideInclude. You need a div or span element with this id in your page.");
		return;
	}

	if(req){
		// Synchronous request
		req.open('GET', url, false);
		req.send(null);
		element.innerHTML = req.responseText;
	}
	else {
	element.innerHTML =
	"Sorry, your browser does not support XMLHTTPRequest objects. This page requires Internet Explorer 5 or better for Windows, or Firefox for any system, or Safari. Other compatible browsers may also exist.";
	}
}

function openExternalLink(href, target) {
  var newwin = window.open('/util/exit-notice.shtml?url=' + encodeURIComponent(href) + '&target=' + encodeURIComponent(target),'openlink','width=605,height=222,screenX=50%,screenY=50%,top=50%,left=50%,resizable=yes,toolbar=no,scrollbars=no')
  newwin.focus();
}

function setupExternalLinks() {
  var links = document.getElementsByTagName('a');
  for (var i = links.length; i != 0; i--) {
    var a = links[i-1];
    if (!a.href) continue;

    if (a.href.indexOf('http') != -1 &&
            a.href.indexOf(window.location.hostname) == -1 &&
            !excludeUrl(a.href)) {
                a.onclick = function() {
                openExternalLink(this.href, this.target);
                return false;
            }

    }
  }
}

var excludeUrls = Array();
function excludeUrl(url){
  re = /www./gi;
  for(var l = 0; l < excludeUrls.length; l++){
    if(url.indexOf(excludeUrls[l]) == 0 || url.indexOf(excludeUrls[l].replace(re, "")) == 0){
      return true;
    }
  }
  return false;
}

function init() {
  styleSheetOnLoad();
  setupExternalLinks();
}

function uninit() {
  styleSheetOnUnload();
}

function initCustom() {
}

function uninitCustom() {
}

function imgChangeSrc(img_name, src){
	if(document.getElementById){
		document.getElementById(img_name).src = src;
	}
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function showHide(name, clsname_hide, clsname_show){
	if(document.getElementById){
		r = document.getElementById(name);
		if(r.style.display == 'block'){
			if(clsname_hide && clsname_show){
				t = document.getElementById(name+'-td');
				t.className = 'nopad';
			}
			r.style.display = 'none';
		}else{
			if(clsname_hide && clsname_show){
				t = document.getElementById(name+'-td');
				t.className = clsname_show;
			}
			r.style.display = 'block';
		}
	}
}
