// common

function getElementsByClassName(classname,tag,node) {
	if(tag == null) {
		tag = '*';
	}
	if(node == null) {
		node = document;
	}
	var result = new Array();
	var elements = node.getElementsByTagName(tag);
	var len = elements.length;
	var pattern = new RegExp('(^|\\s)'+classname+'(\\s|$)');
	for(i=0,j=0; i<len; i++) {
		if(pattern.test(elements[i].className)) {
			result[j++] = elements[i];
		}
	}
	return result;
}

function addLoadEvent(func) {
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		var oldonload = window.onload;
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function addUnloadEvent(func) {
	if(typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		var oldonunload = window.onunload;
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
}

// styleswitcher

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;
    }
  }
  return false;
}

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 'standard';
}

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 loadStyle() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

addUnloadEvent(function() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
});

addLoadEvent(loadStyle);

loadStyle();

// other

function openwin(url,defx,defy) {
	var sizes = defy ? "width="+defx+",height="+defy+"," : "";
	var popwin = window.open(url,"popwin",sizes+"menubar=no,directories=no,location=no,toolbar=no,scrollbars=no");
	if(popwin) {
		popwin.focus(); 
		if(defy) {
			popwin.resizeTo(defx+20,defy+40);
		}
		return(false);
	} else {
		return(true);
	}
}

function initinput(id,defval) {
	addLoadEvent(function () { 
		var input = document.getElementById(id);
		if(input) {
			if(!input.value) { 
				input.value = defval; 
			}
			input.onfocus = function () {
				if(this.value == defval) { this.value = ''; } 
			};
			input.onblur = function () { 
				if(this.value == '') { this.value = defval; } 
			};
		}
	});
}

function initradios() {
	var hrefs = getElementsByClassName('radio','A');
	for(i=0; i<hrefs.length; i++) {
		if(hrefs[i].previousSibling && hrefs[i].previousSibling.tagName == 'INPUT') {
			hrefs[i].onclick = function () { this.previousSibling.checked = true; };
		}
	}
}

addLoadEvent(initradios);
