/*
 *   (C) Copyright Paul Hildebrand 2001 - 2008
 *
 *   $Author: paulh $ 
 *   $Revision: 211 $ 
 *   $Date: 2009-09-16 14:54:14 +0200 (Wed, 16 Sep 2009) $ 
 */
 
function addListener(obj, eType, func) {
	if (obj.addEventListener)
	 	obj.addEventListener(eType, func, false);
	else if (obj.attachEvent) 
		obj.attachEvent('on' + eType, func);
}

function resetImageSize(evt) {
	var image;
	if (evt.target) {
		image = evt.target;
	} else {
		image = evt.srcElement;
	}
	image.width = image.getAttribute('originalWidth');
	image.style.cursor = 'auto';
	image.style.borderWidth = '0px';
}

function setNotAvailableImage(evt) {
	var image;
	if (evt.target) {
		image = evt.target;
	} else {
		image = evt.srcElement;
	} 
	image.src='/pics/na.gif';
}

function correctImageSize(image) {
	var maxWidth = 580;
	var blockCheckNode = image;
	if (image.parentNode.tagName == 'A') {
		blockCheckNode = image.parentNode;
	}
	
	if (blockCheckNode.parentNode.tagName == 'BLOCKQUOTE') {
		maxWidth = 480;
	}

	if (image.width > maxWidth) {
		image.style.borderColor = 'gray';
		image.style.borderWidth = '1px';
		image.style.borderStyle = 'dotted';
		image.style.cursor = "pointer";
		image.setAttribute('originalWidth', image.width);
		image.width = maxWidth;
		
		/* Als het plaatje gebruikt wordt in een link niet onclick op zetten: */
		if (image.parentNode.tagName != 'A') {
		  addListener(image, 'click', resetImageSize);
		}
	}
}

function updateForumImages(list, setNotAvailable) {
	var inCompleteImages = new Array;
	var k = 0;
	for (var i=0; i < list.length; i++) {
		if (list[i].className == 'forumImage') {
			if (setNotAvailable) {
				addListener(list[i], 'error', setNotAvailableImage);
			}
			
		    if (list[i].complete) {
				correctImageSize(list[i]);
			} else {
				inCompleteImages[k++] = list[i];
			}
		}
	}
	return inCompleteImages;
}

function correctIncompletes() {
	inCompletes = updateForumImages(inCompletes, false);
	if (inCompletes.length == 0) {
		window.clearInterval(intervalId);
	}
}

function fixMarquee() {
	var allDivs = document.body.getElementsByTagName("div");
	for (var i=0; i < allDivs.length; i++) {
		if (allDivs[i].className == 'marquee') {
			var m = document.createElement('marquee');
			m.innerHTML = allDivs[i].innerHTML;
			allDivs[i].innerHTML = '';
			allDivs[i].appendChild(m);
		}
	}
}


function smile(text) {
	var target = document.reactieform.txt;
	if (target) {
		if (document.all && target.cursorPos) {
			var cursorPos = target.cursorPos;
			cursorPos.text = cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ? text + ' ' : text;
			target.focus();
		} else if (target.selectionStart != undefined) {
			target.focus();
			var location = target.selectionStart;
			var content = target.value.substr(0, location) + text + target.value.substr(location, target.value.length - 1);
			target.value = content;
			target.setSelectionRange(location + text.length, location + text.length)
		} else {
			target.value += text;
			target.focus();
		}
	}
}

function storeCursor(element)
{
  if ( document.all && element.createTextRange ) element.cursorPos = document.selection.createRange().duplicate();
}

/** Highlighting search results */
	function getHLColor(index) {
		switch(index) {
			case 0 : return 'yellow';
			case 1 : return 'pink';
			case 2 : return 'lightgreen';
			case 3 : return 'lime';
			case 4 : return 'fuchsia';
			default : return 'lightblue';
		}
	}

	function getMatchList(str, hl) {
		var tmpStr = str.toLowerCase();
		var list = new Array();
		var iter = 0;
		for (var h = 0; h < hl.length; h++) {
			var beginOffset = tmpStr.indexOf(hl[h]);
			if (beginOffset >= 0) {
				var offSet = 0;
				while (beginOffset >= 0) {
					var match = new Array(3);
					match[0] = beginOffset; 
					match[1] = beginOffset + hl[h].length; // endOffset.
					match[2] = getHLColor(h);
					beginOffset = tmpStr.indexOf(hl[h], match[1]);
					list[iter++] = match;
				}
			}
		}
		// sort the list by beginOffset.
		list.sort(function(a,b){return a[0] - b[0]});
		
		// remove duplicates with offsets within another offset;
		if (list.length > 1) {
			for (var i = list.length - 1; i >= 1 ; i--) {
				if (list[i][0] < list[i-1][1]) {
					list.splice(i,1);
				}
			}
		}
		
		return list;
	}

	function findTextNodes(el, hl, nodeStack) {
		var nodes = el.childNodes;
		for (var i = 0; i < el.childNodes.length; i++) {
			if (nodes[i].nodeType == 3) {
				if (nodes[i].data != undefined && nodes[i].data != '') {
					nodeStack.push(nodes[i]);
				}
			} else if (nodes[i].nodeType == 1) {
				// ObjectNode.
				findTextNodes(nodes[i], hl, nodeStack);
			}
		}
	}
	
	function replaceTextInChildren(hl, nodeStack) {
		while (nodeStack.length != 0) {
			var curNode = nodeStack.pop();
			var list = getMatchList(curNode.data, hl);
			var offSet = 0;
			if (list.length > 0 ) {
				var newNodes = new Array();
				var ni = 0;
				for (var n = 0; n < list.length; n++) {
					newNodes[ni++] = document.createTextNode(curNode.data.substring(offSet, list[n][0]));
		
					var uNode = document.createElement('span');
					uNode.className= 'highlight';
					uNode.style.backgroundColor = list[n][2]; 
					uNode.appendChild(document.createTextNode(curNode.data.substring(list[n][0], list[n][1])));
					newNodes[ni++] = uNode;
					
					offSet = list[n][1];
				}
				newNodes[ni++] = document.createTextNode(curNode.data.substring(offSet));

			
				for (var nn = 0; nn < newNodes.length; nn++) {
					curNode.parentNode.insertBefore(newNodes[nn], curNode);
				}
				curNode.parentNode.removeChild(curNode);
			}
		}
	}

	function doHighLight(hl) {
		var divElements = document.getElementsByTagName('div');
		var nodeStack = new Array();
		var i = 0;
		for (i = 0; i < divElements.length; i++) {
			if (divElements[i].className == 'normal') {
				findTextNodes(divElements[i], hl, nodeStack);
			}
		}
		if (nodeStack.length > 0) {
			replaceTextInChildren(hl, nodeStack);	
		}
	}
	

/** Search functions: */ 	
function storeSearchOptions() {
	var ws = document.getElementById('whatSelect');
	var so = new Array();
	for (var i =0; i < ws.options.length; i++) {
		so[i] = new Array(ws.options[i].value, ws.options[i].text);
	}
	return so;
}

function updateSearchOptions(searchOptions) {
	if (document.getElementById('showTypeMessage') != undefined) {
		
		var ws = document.getElementById('whatSelect');
		var selected = ws.options[ws.selectedIndex].value;
		var showMessage = document.getElementById('showTypeMessage').checked;
		
		for(var i = ws.options.length-1; i >= 0; i--) {
			ws.remove(i);
		}
	
		var si = 0;	
		for (var i = 0; i < searchOptions.length; i++) {
			if (!showMessage || (showMessage && (searchOptions[i][0] == 'content' || searchOptions[i][0] == 'username'))) { 
				var option = document.createElement('option');
				option.text = searchOptions[i][1];
				option.value = searchOptions[i][0];
				if (document.all) {
					ws.add(option);
				} else {
				 	ws.appendChild(option);
				}
				if (option.value == selected) {
					ws.selectedIndex = si;
				}
				si++;
			}
		}
	}
}

/* Datum controle */
function checkDate(day,month,year) {
	// Standaard zetten we de return waarde op true
	retval=true;
	if(isNaN(day)||isNaN(month)||isNaN(year)) {
		retval=false;
	} else if(day <=0||month <=0||year <=0) {
		retval=false;
	} else if(month==2) {
		// Februari heeft maximaal 29 dagen
		if(day>29) { 
			retval=false;
		} else if(day==29) {
			// Controleren of het een schrikkeljaar is
			if(!((year % 4)==0||(year % 100)==0||(year % 400)==0)) {
				retval=false;
			}
		}
	} else if(day>30) {
		// April,Juni,september en november hebben maar 30 dagen
		if(month==4||month==6||month==9||month==11) {
			retval=false;
		}
	}
	return retval;
}