//-------------------------------------------------------------------------------
// check if an element is empty in some way										-
//-------------------------------------------------------------------------------
function isEmpty(T) { 
	try {
		return (T === null || T === undefined || T === "" || T === NaN); 
		}
	catch (ex) {
		return true;
		}
	}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function isVisible(T) {
	if (T.className.indexOf('visible') >= 0) {
		return true;
		}
	else if (T.className.indexOf('hidden') >= 0) {
		return false;
		}
	return (T.style && T.style.display != 'none');
	}
//-------------------------------------------------------------------------------
// get an element by it's id. if it's not found then try to find the 			/
// element by it's name.														/
//-------------------------------------------------------------------------------
function getElement(id) {
	var v = null;
	if (document.getElementById) {
		v = document.getElementById(id);
		if (isEmpty(v)) {
			try {
				v = top.document.getElementById(id);
			} catch(e) {}
			}
		if (isEmpty(v) === false) {
			return v;
			}
		}
	if (document.all) {
		v = document.all[name];
		if (isEmpty(v) === false) {
			return v;
			}
		}
	if (document.layers) {
		v = document.layers[name];
		if (isEmpty(v) === false) {
			return v;
			}
		}
	var e = getElementsByClassName('DIV', id, document);
	return (e.length > 0) ? e[0] : null;
	}
//-------------------------------------------------------------------------------
// fetch all elements of a specific type, that have the given classname			/
// sample:																		/
//		var a = getElementsByClassName('div','widget');							/
// returns:																		/
//		array of elements														/
//-------------------------------------------------------------------------------
function getElementsByClassName (type,name,context) {
	var result = [];
	var name1 = "";
	var name2 = " " + name.toLowerCase() + " ";
	
	var elements = isEmpty(context) ? document.getElementsByTagName(type) : context.getElementsByTagName(type);
	var element = null;
	
	var i, n; for (i = 0, n = elements.length; i < n; i++) {

		element = elements[i];

		if (isEmpty(element.className)) {
			continue;
			}

		name1 = element.className.toLowerCase();
		if (name1.indexOf(name.toLowerCase()) < 0) {
			continue;
			}

			name1 = " " + name1 + " ";
		if (name1.indexOf(name2) < 0) {
			continue;
			}

		result.push(element);
		}
	return result;
	}
//-------------------------------------------------------------------------------
// get the inner-text of an element												/
//-------------------------------------------------------------------------------
function getText (element) {
	var t = "";
	if (isEmpty(element.innerText) === false) {
		t = element.innerText;
		}
	else {
		t = element.textContent;
		}
	if (isEmpty(t)) {
		t = "";
		}
	// strip leading and trailing spaces
	t = t.replace( /^\s+/g, "" );
	return t.replace( /\s+$/g, "" );
	}
//-------------------------------------------------------------------------------
// set focus to the control with the given id									/
//-------------------------------------------------------------------------------
function setFocus(id) {
	var e = getElement(id);
	if (!isEmpty(e)) {
		e.focus();
		}
	}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function setWaitCursor (set) {
	document.documentElement.className = set ? 'waitCursor' : '';
	}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function disableElementsByTagName(tagName, _disable) {
	var e = document.getElementsByTagName("INPUT");
	var i, n; for (i = 0, n = e.length; i < n; i++) {
		e[i].disabled = _disable;
		}
	}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function setSelectionRange (input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus(); 
		input.setSelectionRange(selectionStart, selectionEnd); 
		}
	else if (input.createTextRange) {
		var range = input.createTextRange(); 
		range.collapse(true); 
		range.moveEnd('character', selectionEnd); 
		range.moveStart('character', selectionStart); 
		range.select(); 
		}
	}
//-------------------------------------------------------------------------------
// register an event handler on an event target									/
//		element: 	the element to register the handler for.					/
//		event:		event name													/
//		what:		event handler function										/
//		capture:	if true, useCapture indicates that the user wishes to 		/
//					initiate capture. After initiating capture, all events of 	/
//					the specified type will be dispatched to the registered 	/
//					EventListener before being dispatched to any EventTargets 	/
//					beneath them in the tree. Events which are bubbling upward 	/
//					through the tree will not trigger an EventListener 			/
//					designated to use capture.									/
//-------------------------------------------------------------------------------
function handleEvent (element, event, what, capture) {
	if (isEmpty(element['on' + event])) {
		element['on' + event] = function(){};
		}
	if (element.attachEvent) {
		element.attachEvent('on' + event, what);
		}
	else if (element.addEventListener) {
		element.addEventListener(event, what, isEmpty(capture) ? false : before);
		}
	else {
		what();
		}
	}
//-------------------------------------------------------------------------------
// removal of event listeners from the event target								/
//-------------------------------------------------------------------------------
function unhandleEvent (element, event, what) {
	if (element.detachEvent) {
		element.detachEvent('on' + event, what);
		}
	else if (element.removeEventListener) {
		element.removeEventListener(event, what, false);
		}
	}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
function cancelEvent(event) {
	// cancel the click in all possible ways
	event.returnValue = false;
	event.cancelBubble = true;
	event.cancel = true;
	if (isEmpty(event.stopPropagation) === false) {
		event.stopPropagation();
		}
	if (isEmpty(event.preventDefault) === false) {
		event.preventDefault();
		}
	return false;
	}
//-------------------------------------------------------------------------------
// apply skinning to the specified element										/
//		type = 'skin_border'													/
//		id = element id															/
//-------------------------------------------------------------------------------
function applySkin(type,id) {
	var border = [
		'topleft', 'topright', 'bottomleft', 'bottomright', 
		'left', 'top', 'right', 'bottom', 
		'background'
		];
	var target = getElement(id);
	if (isEmpty(target) === false) {
		var i, n; for (i = 0, n = border.length; i < n; i++) {
			var skin = document.createElement("DIV");
				skin.className = 'skin_' + type;
				skin.id = border[i];
			if (target.hasChildNodes()) {
				target.insertBefore(skin, target.childNodes[0]);
				}
			else {
				target.appendChild(skin);
				}
			skin.style.zIndex = -1;
			}
		}
	}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function encode_utf8(rohtext) {
    rohtext = rohtext.replace(/\r\n/g,"\n");
    var utftext = "";
    for (var n = 0; n < rohtext.length; n++) {
        var c=rohtext.charCodeAt(n);
        if (c < 128) {
            utftext += String.fromCharCode(c);
			}
        else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c>>6)|192);
            utftext += String.fromCharCode((c&63)|128);
			}
        else {
            utftext += String.fromCharCode((c>>12)|224);
            utftext += String.fromCharCode(((c>>6)&63)|128);
            utftext += String.fromCharCode((c&63)|128);
			}
        }
    return utftext;
	}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
function decode_utf8(utftext) {
    var plaintext = ""; 
	var i = 0; 
	var c = 0;
	var c1 = 0;
	var c2 = 0;
    while (i < utftext.length) {
        c = utftext.charCodeAt(i);
        if (c < 128) {
            plaintext += String.fromCharCode(c);
            i++;
			}
        else if ((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            plaintext += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
			}
        else {
            c2 = utftext.charCodeAt(i + 1); 
			c3 = utftext.charCodeAt(i + 2);
            plaintext += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
			}
        }
    return plaintext;
	}

//-------------------------------------------------------------------------------
// Performs a yellow fade background animation on the specified element
//-------------------------------------------------------------------------------
var _cancelPulseElt;
function yellowFade(el, doPulse, interval) {
	var b = 155;
	
	if (!interval) interval = 40;
	
	el.style.backgroundColor = 'rgb(255,255,'+ b +')';
	(function f() {
		if (el && el != _cancelPulseElt) {
			el.style.backgroundColor = 'rgb(255,255,'+ (b+=4) +')';
			if (b < 255)
				setTimeout(f, interval);
			else if (doPulse)
				yellowFadeBack(el, doPulse, interval);
		}
	});
	try {
    	setTimeout(f, interval * 25);
	} catch(e) {}
}
//-------------------------------------------------------------------------------
// Performs a reverse yellow fade background animation on the specified element
//-------------------------------------------------------------------------------
function yellowFadeBack(el, doPulse, interval) {
	var b = 255;

	if (!interval) interval = 40;
	
	el.style.backgroundColor = 'rgb(255,255,'+ b +')';
	(function g() {
		if (el && el != _cancelPulseElt) {
			el.style.backgroundColor = 'rgb(255,255,'+ (b-=4) +')';
			if (b > 155)
				setTimeout(g, interval);
			else if (doPulse)
				yellowFade(el, doPulse, interval);
		}
	});
	try {
	    setTimeout(g, interval * 25);
    } catch(e) {}
}
//-------------------------------------------------------------------------------
// Displays a pulsating background (to and from yellow) on the specified element 
//-------------------------------------------------------------------------------
function pulse(el) {
	if (el) {
		yellowFade(el, true, 20);
	}
}
//-------------------------------------------------------------------------------
// Cancels the pulsating background of the specified element 
//-------------------------------------------------------------------------------
function cancelPulse(el) {
	_cancelPulseElt = el;
}

//--------------------------------------------------------------------------------------------------
// generic busy indicator
// when busy, also place a large transparent 'mask' over the entire page
// to prevent clicks while something is still loading/processing
//--------------------------------------------------------------------------------------------------
function setBusy(what) {
    var busyIndicator = getElement("busyIndicator");
    var busyMask = getElement("busyMask");

    if (isEmpty(busyIndicator.counter)) {
        busyIndicator.counter = 0;
    }

    switch (what) {
        case true:
            busyIndicator.counter++;
            break;
        case false:
            busyIndicator.counter--;
            break;
    }

    if (busyIndicator.counter < 0) {
        busyIndicator.counter = 0;
    }

    busyMask.style.display = (busyIndicator.counter > 0 ? "block" : "none");
    busyIndicator.style.display = (busyIndicator.counter > 0 ? "block" : "none");
    document.body.style.cursor = (busyIndicator.counter > 0 ? "wait" : "default");

    if (busyIndicator.counter > 0) {
        busyMask.disabled = "disabled";
    } else {
        busyMask.disabled = "";
       }

      }

    function SetSelection(me, x) {
	
		var e = document.getElementsByTagName("INPUT");
		var i, n;
		for (i = 0, n = e.length; i < n; i++) {
			var type = e[i].attributes["type"].value;
			if (type == "radio") {
				e[i].checked = false;
				}
		   }
		me.checked = true;

		var hi = getElement('hidSelection');
		hi.value = x;	
	    }

	function CustomSubmit() {
		var hi = getElement('hidSelection');
		if ((hi.value == "") || (hi.value == null)) {
			alert('Je bent vergeten een datum te selecteren.\r\nGeef aan welke datum je de training wilt volgen om je inschrijving compleet te maken.');
			return false;
			}
	    }