/* Utility functions */
function addEvent(obj, evType, fn) {
  /* adds an eventListener for browsers which support it Written by Scott Andrew */
  if (obj.addEventListener) { obj.addEventListener(evType, fn, true); return true; }
  else if (obj.attachEvent) { var r = obj.attachEvent("on"+evType, fn); return r; }
  else { return false; }
}
function element(id) {
  var e = false;
  if (document.getElementById) { e = document.getElementById(id); }
  else if (document.all) { e = document.all(id); }
  return e;
}
// login
function toPassword(objOldInput) {
	var objNewElement = document.createElement('input');
	objNewElement.setAttribute('type', 'password');
    objNewElement.setAttribute('name', objOldInput.name);
	objOldInput.parentNode.replaceChild(objNewElement, objOldInput);
	toPassword.el = objNewElement;
	setTimeout('toPassword.el.focus()',100);
	return true;
}
// menu.js
function hasULchildren(linode) {
	var uls = linode.getElementsByTagName('ul')[0];
	if(uls) return true;
	return false;
}
function efMenu(menuid, menutype) {
	var menudiv = element(menuid);
	var listitems = menudiv.getElementsByTagName('li');
	if(listitems){
		for(var i=0; i<listitems.length; i++){
			var listitem = listitems[i];
			if(hasULchildren(listitem)) {
				listitem.onmouseover = function (e) {
					if (this.firstChild.nodeName.toUpperCase() == 'A') {
						if(this.firstChild.className.indexOf('active') != -1) { this.firstChild.className = 'active mouse'; }
						else { this.firstChild.className = 'mouse'; }
					}
					for (var i=0; i < this.childNodes.length; i++) {
						var child = this.childNodes[i];
						if (child.nodeName.toUpperCase() == 'UL') {child.style.visibility = 'visible'; }
					}
					return false;
				};
				// onmouseout function
				listitem.onmouseout = function (e) {
					for (var i=0; i < this.childNodes.length; i++) {
						var child = this.childNodes[i];
						if (child.nodeName.toUpperCase() == 'UL') {	child.style.visibility = 'hidden';}
					}
					if (this.firstChild.nodeName.toUpperCase() == 'A') {
						if(this.firstChild.className.indexOf('active') != -1) { this.firstChild.className = 'active'; }
						else { this.firstChild.className = ''; }
					}
					return false;
				};
			}
		}
	}
	return void(0);
}

// login
function element(id) {
	var e;
	if (document.getElementById) {
		e = document.getElementById(id);
	} else if (document.all) {
		e = document.all[id];
	} else {
		e = false; 
	}
	return e; 
}
function toPassword(objOldInput) {
	var objNewElement = document.createElement('input');
	objNewElement.setAttribute('type', 'password');
    objNewElement.setAttribute('name', objOldInput.name);
	objNewElement.setAttribute('id', objOldInput.id);
	objOldInput.parentNode.replaceChild(objNewElement, objOldInput);
	toPassword.el = objNewElement;
	setTimeout('toPassword.el.focus()',100);
	return true;
}
var venster = '';
function openWindow(url, name, w, h, menubar, scroll){
	var centerX, centerY, windowLeft, windowTop;
	if(document.getElementById) {
		centerX = parseInt(screen.width / 2);
		centerY = parseInt(screen.height / 2);
	} else {
		centerX = parseInt(screen.availWidth / 2);
		centerY = parseInt(screen.availHeight / 2);
	}
	windowLeft = centerX - parseInt(w/2);
	windowTop = centerY - parseInt(h/2);
	var parameters  = 'toolbar=0,location=0,resize=1,menubar=' + menubar;
	parameters += ',width=' + w + ',height=' + h + ',top=' + windowTop + ',left=' + windowLeft;
	if(scroll != null) parameters += ',scrollbars=1';
	venster = window.open(url, name, parameters);
	if(!venster.opener) venster.opener = self;
	//venster.focus();
}
var step = 1;
var timer;
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function getWindowXY() {
  var windowX = 0, windowY = 0;
  if (self.innerHeight) {
	// all except Explorer
	windowX = self.innerWidth;
	windowY = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
	// Explorer 6 Strict Mode
	windowX = document.documentElement.clientWidth;
	windowY = document.documentElement.clientHeight;
  } else if (document.body) {
	// other Explorers 
	windowX = document.body.clientWidth;
	windowY = document.body.clientHeight;
  }
  return [ windowX, windowY ];
}
function getBodyXY() {
  var x,y;
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) { // all but Explorer Mac
	x = document.body.scrollWidth;
	y = document.body.scrollHeight;
  } else {
  	// Explorer Mac would also work in Explorer 6 Strict, Mozilla and Safari
	x = document.body.offsetWidth;
	y = document.body.offsetHeight;
  }
  return [x,y];
}
function getLength() {
  var length = 0;
  var elem = element('holder');
  if(elem){
	 length = elem.style.width;
	 length = parseInt(length.replace('px', ''));
  }
  return length;
}
function stop() {
	 clearTimeout(timer);
}
function toAllLeft() {
  var coord = getScrollXY();
  var x = coord[0];
  if (x > 0) {
    window.scrollBy(-50, 0);
    timer = setTimeout("toAllLeft()", 10);
  }
}
function toAllRight() {
  var coord = getScrollXY();
  var width = getWindowXY();
  var extra = getLength();
    
  var distance = extra - width[0];
  var scrolled = coord[0];
	 
  if (scrolled < distance) {
    window.scrollBy(50, 0);
    timer = setTimeout("toAllRight()", 10);
  }
}
function stepLeft(state) {
  var coord = getScrollXY();
  var width = getWindowXY();
    
  var distance = coord[0] + width[0];
  var scrolled = coord[0];
  
  if (state == 0) {
  	if (scrolled > 0) {
  	  step = Math.floor((scrolled / width[0]));
  	  step = (step * width[0]); 
  	} else {
  	  step = 0;
  	}
  }	
  if (scrolled > 0) {
  	if (distance > step) {
	  window.scrollBy(-25, 0);
	  timer = setTimeout("stepLeft(1)", 10);
    }
  }
}
function stepRight(state) {
  var coord = getScrollXY();
  var width = getWindowXY();
  var pagelength = getLength();
 
  var scrolled = coord[0];
  var distance = pagelength - width[0];
    
  if (state == 0) {
  	if (scrolled > 0) {
  	  step = Math.ceil((distance / width[0]));
  	  step = (step * width[0]);
  	} else {
  	  step = width[0];
  	}
  }	
  if (distance >= width[0] && scrolled < (pagelength-width[0])) {
	  if (scrolled < step) {
		window.scrollBy(25, 0);
		timer = setTimeout("stepRight(1)", 10);
	  }
  }	
}
function scrollTo() {
  var el, elWidth, width = getWindowXY();
  var coord = getScrollXY();
  var scrolled = coord[0];
  el = document.getElementById('navBox');
  if(el) {
  	elWidth = el.style.width;
  	var marginLeft = elWidth.replace('px', '') / 2;
	var posLeft =  width[0]/2 - marginLeft;
	if (scrolled > 0) { posLeft = posLeft + scrolled; }
	el.style.left = posLeft + 'px';
  }
}
function submitFTP(id) {
	var pwd = '', usrnm = '';
	var formId = element(id);
	if(formId) {
		for (var i=0 ; i < formId.elements.length; i++) {
			var typ = formId.elements[i].type
           	var val = formId.elements[i].value ;
           	var nm = formId.elements[i].id ;
           	if (nm == 'password' && typ == 'password') {
				pwd = val;
			}
           	if (nm == 'username' && typ == 'text') {
				usrnm = val;
			}         
		}
		if((pwd != '' && usrnm != '') && (pwd != 'uw wachtwoord' && usrnm != 'gebruikersnaam')){
			var strFtp = 'ftp://'+ usrnm +':'+ pwd +'@ftp.bdu.nl';
			openWindow(strFtp, 'ftp', 800, 600, 1, 1);
			formId.submit();
		} else {
			formId.submit();
		}
	} 
}

window.onload = function() { scrollTo(); }
window.onscroll = function() { scrollTo(); }
window.onresize = function() { scrollTo(); }
