/***
 * variables used for email cloacking
 */
var _at = "@";
var _mt = "mailto";
var _dot = ".";

var showTip = false;

var mandaat_timerID;
var mandaat_div;
var mandaat_top;

function mandaat_dostartscroll() {
	mandaat_timerID = window.setTimeout('mandaat_startscroll()', 0);
}

function mandaat_waitscroll() {
	mandaat_div.style.top = (mandaat_top - 1) + "px";
	mandaat_timerID = window.setTimeout("mandaat_startscroll()", 30);
}

function mandaat_stopscroll() {
	window.clearTimeout(mandaat_timerID);
}

function mandaat_startscroll() {
	if (numofmandaten > 1) {
		mandaat_div = $("mandaat_scrollcont");
		mandaat_height = $("mandaat_scrollcontainer").offsetHeight;
		mandaat_top = mandaat_div.offsetTop;
		if (mandaat_top > 0 - (numofmandaten * mandaat_height)) {
			if (mandaat_top % mandaat_height == 0) {
				mandaat_timerID = window.setTimeout("mandaat_waitscroll()", 2000);
			} else {
				mandaat_div.style.top = (mandaat_top - 1) + "px";
				mandaat_timerID = window.setTimeout("mandaat_startscroll()", 30);
			}
		} else {
			mandaat_div.style.top = "0px";
			mandaat_timerID = window.setTimeout("mandaat_startscroll()", 0)	;
		}
	}
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

Array.prototype.inArray = function(value) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == value) return true;
	}
	return false;
};

function add_line() {
	tlines++;

	tbl = $("linkstable");
	row = tbl.insertRow(tbl.rows.length);
	row.style.backgroundColor = "#ffffff";
	row.id = "row" + tlines;

	var cell = row.insertCell(0);
	cell.style.whiteSpace = "nowrap";
	cell.innerHTML = '<input type=text name="link[]" id="link' + tlines + '" maxlength="255" style="width:215px" value="http://">';

	var cell = row.insertCell(1);
	cell.style.whiteSpace = "nowrap";
	cell.innerHTML = '<input type=text name="linktekst[]"  maxlength="255" style="width:215px">';

	var cell = row.insertCell(2);
	cell.style.whiteSpace = "nowrap";
	cell.setAttribute("align", "center");
	cell.innerHTML = '<a href="" onClick="clear_line(' + tlines + ');return false"><img src="/images/bar_delete.png" width="28" height="29" border="0" align="absmiddle" title="Wissen"></a>';

	if (tbl.rows.length > 1) {
		$("linksdiv").style.display = 'inline';
	}
}

function clear_line(n) {
	link = $("link" + n).value;
	if (link.toLowerCase() != 'http://') {
		if (!confirm("Wilt u de link '" + link + "' zeker wissen?")) return false;
	}
	o = document.getElementById("row"+n);
	while(o && o.tagName != "TR") o = o.parentNode;
	if(o) o.parentNode.removeChild(o);
	tbl = $("linkstable");
	if (tbl.rows.length == 1) {
		$("linksdiv").style.display = 'none';
	}
}

function change_status(type, el) {
	ajax("/includes/status.php?type=" + escape(type) + (arguments.length > 2 && arguments[2] != '' ? "&id=" + escape(arguments[2]) : ""), "GET", false, false, function (h, t) {
		//alert(h + t);
		if (t == "OK") {
			el.src = (el.src.match('_aan') ?  el.src.replace('_aan', '_uit') : el.src.replace('_uit' , '_aan'));
		} else if (t != "NOTOK") {
			alert(t);
		}
	});
}


function do_search(obj) {
	if (trim(obj.search_value.value) == "" || trim(obj.search_value.value) == 'zoek openzones') {
		obj.search_value.focus();
		alert("Gelieve een zoekterm in te tikken.");
		return;
	}
	
	obj.submit();
}

function show_login(max) {
	el = $("div_pw_bg");
	el2 = $("div_pw")
	if (el.offsetHeight >= max) return;
	el.style.height = (el.offsetHeight + 2) + "px";
	el2.style.height = (el2.offsetHeight + 2) + "px";
	setTimeout("show_login(" + max + ")", 10);
}

function hide_login(max) {
	el = $("div_pw_bg");
	el2 = $("div_pw")
	if (el.offsetHeight <= max) {
		el.style.visibility = "hidden";
		el2.style.visibility = "hidden";
		return;
	}
	el.style.height = (el.offsetHeight - 2) + "px";
	el2.style.height = (el2.offsetHeight - 2) + "px";
	setTimeout("hide_login(" + max + ")", 10);
}

function display_login() {
	el1 = $("div_pw_bg");
	el1.style.zIndex = 10000000;
	el2 = $("div_pw");
	el2.style.zIndex = 10000001;
	if (el1.style.visibility == "hidden") {
		el1.style.height = "2px";
		el1.style.visibility = "visible";
		el2.style.height = "2px";
		el2.style.visibility = "visible";
		setTimeout("show_login(122)", 10);
	} else {
		setTimeout("hide_login(2)", 10);
	}
}

function update_pw_field(fld, what, classname, width, fld_name) {
	fld_content = ""
	fld_id = fld.id;
	if (what == "focus") {
		if (fld.type == "text") {
			fld_content = '<input type="password" id="' + fld_id + '" name="' + fld_name + '" class="' + classname + '" style="width:' + width + 'px; height:17px; border:none; background-color:transparent;" onBlur="update_pw_field(this, \'blur\', \'' + classname + '\', ' + width + ', \'' + fld_name + '\');">';
		}
	} else if (what == "blur") {
		if (trim(fld.value) == "") {
			fld_content = '<input type="text" id="' + fld_id + '" name="' + fld_name + '" value="paswoord" class="' + classname + '" style="width:' + width + 'px; height:17px; border:none; background-color:transparent;" onFocus="update_pw_field(this, \'focus\', \'' + classname + '\', ' + width + ', \'' + fld_name + '\');">';
		}
	}
	if (fld_content != "") {
		$("div_" + fld_id).innerHTML = fld_content;
		if (what == "focus") window.setTimeout(function() { $(fld.id).focus(); }, 100);
	}
}

function check_login(frm) {
	with (frm) {
		if (trim(email.value) == "" || trim(email.value) == "e-mailadres" || trim(password.value) == "" || trim(password.value) == "paswoord") {
			alert("Gelieve zowel e-mailadres als paswoord in te vullen.");
			if (trim(email.value) == "" || trim(email.value) == "e-mailadres") {
				email.focus();
			} else {
				password.focus();
			}
			return;
		}
		submit();
	}
}

function check_registreer(frm) {
	with (frm) {
		if (trim(voornaam.value) == "" || trim(voornaam.value) == "voornaam" || trim(naam.value) == "" || trim(naam.value) == "naam" || trim(lidnummer.value) == "" || trim(lidnummer.value) == "lidnummer" || trim(lidnummer.value) == "0") {
			alert("Gelieve zowel voornaam, naam als lidnummer in te vullen.");
			if (trim(voornaam.value) == "" || trim(voornaam.value) == "voornaam") {
				voornaam.focus();
			} else if (trim(naam.value) == "" || trim(naam.value) == "naam") {
				naam.focus();
			} else {
				lidnummer.focus();
			}
			return;
		}
		submit();
	}
}

function check_bevestig(frm) {
	with (frm) {
		if (trim(password.value) == "" || trim(password.value) == "paswoord" || trim(password_herhaal.value) == "" || trim(password_herhaal.value) == "paswoord" || trim(email.value) == "" || trim(email.value) == "e-mail") {
			alert("Gelieve zowel tweemaal uw paswoord als uw e-mail in te vullen. Opgelet, het paswoord 'paswoord' is niet toegestaan.");
			if (trim(password.value) == "" || trim(password.value) == "paswoord") {
				password.focus();
			} else if (trim(password_herhaal.value) == "" || trim(password_herhaal.value) == "paswoord") {
				password_herhaal.focus();
			} else {
				email.focus();
			}
			return;
		} else if (trim(password.value).length < 4) {
			alert ("Gelieve een paswoord van minimaal 4 karakters te kiezen.");
			password.value = "";
			password_herhaal.value = "";
			password_herhaal.focus();
			password.focus();
			password.focus();
			return;
		} else if (trim(password.value).match(/[^a-z]/i)) {
			alert ("Gelieve enkel letters te gebruiken in uw paswoord.");
			password.value = "";
			password_herhaal.value = "";
			password_herhaal.focus();
			password.focus();
			password.focus();
			return;
		} else if (trim(password.value) != trim(password_herhaal.value)) {
			alert("Gelieve tweemaal hetzelfde paswoord in te vullen");
			password.value = "";
			password_herhaal.value = "";
			password_herhaal.focus();
			password.focus();
			password.focus();
			return;
		}
		submit();
	}
}

function check_password(frm) {
	with(frm) {
		if (trim(email.value) == "" || trim(email.value) == "e-mailadres") {
			alert("Gelieve uw e-mailadres in te vullen.");
			email.focus();
			return false;
		}
		submit();
	}
}

function check_email(fld) {
	if (trim(fld.value) == "") {
		fld.value = trim(fld.value);
		return;
	}
	ajax("/includes/check_email.php?email=" + escape(trim(fld.value)), "GET", false, false, function(h, t) {
		if (t == "invalidemail") {
			alert("Gelieve een geldig e-mailadres in te vullen.");
			fld.focus();
			fld.focus();
		}
	});
}

function add_friend(uid) {
	ajax("/includes/add_friend.php?uid=" + escape(uid) , "GET", false, false, function(h, t) {
		//alert(t);
		e = t.split("!!");
		if (e[0] == "OK") {
			alert(e[1] + " werd aan uw vrienden toegevoegd.");
		} else if (e[0] == "duplicate") {
			alert(e[1] + " is reeds een van uw vrienden.");
		} else if (e[0] == "NOTOK") {
			alert(e[1] + " kon niet aan uw vrienden toegevoegd worden.  Gelieve later opnieuw te proberen.");
		}
	});
}

function remove_friend(uid) {
	ajax("/includes/add_friend.php?del=" + escape(uid), "GET", false, false, function(h, t) {
		//alert(t);
		e = t.split("!!");
		if (e[0] == "OK") {
			o = $("friend_" + uid);
			os = $("friendsep_" + uid);
			p = o.parentNode;
			p.removeChild(o);
			p.removeChild(os);
			if (p.rows.length == 0) $("table_friends").parentNode.removeChild($("table_friends"));
			alert(e[1] + " werd uit uw vriendenlijst verwijderd.");
		} else {
			alert(e[1] + " kon niet uit uw vriendenlijst verwijderd worden.  Gelieve later opnieuw te proberen.");
		}
    });
}

function show_more_fotos(uid, page) {
	ajax("/includes/update_fotos.php?uid=" + escape(uid) + "&page=" + escape(page), "GET", false, false, function(h, t) {
		//alert(t);
		if (t != "NOTOK") {
			$("div_fotos").innerHTML = t;
			myLightbox.updateImageList();
		}
	});
}

function webthumbnail(url, el) {
	ajax("/includes/getwebthumbnail.php?url=" + url, "GET", false, false, function(h, t) {
		if (t != "false") {
			if (showTip) Tip('<img src=\'' + t + '\' width=\'200\' height=\'150\' border=\'0\'>');
			el.onmouseover = function() { Tip('<img src=\'' + t + '\' width=\'200\' height=\'150\' border=\'0\'>'); };
		} else {
			if (showTip) Tip('<img src=\'/images/websitethumb.gif\' width=\'200\' height=\'150\' border=\'0\'>');
			el.onmouseover = function() { Tip('<img src=\'/images/websitethumb.gif\' width=\'200\' height=\'150\' border=\'0\'>'); };
		}
		el.onmouseout = function() { UnTip(); };
	});
}

function today() {
	var now = new Date();
	var day = now.getDate();
	var month = now.getMonth() + 1;
	var year = now.getFullYear();
	
	return (day < 10 ? "0" : "") + day + "-" + (month < 10 ? "0" : "") + month + "-" + year;
}

function chkDate(fld) {
	fld.value = fld.value.trim();
	if (fld.value.length > 0) {
		err = false;
		fld.value = fld.value.replace(/\//g, "-");
		fld.value = fld.value.replace(/\./g, "-");
		tmp = fld.value.split("-");

		if (tmp.length != 3) {
			err = true;
		} else {
			year = tmp[2]*1;
			month = tmp[1]*1 - 1;
			day = tmp[0]*1;

			var test = new Date(year, month, day);
			if (year != test.getFullYear() || month != test.getMonth() || day != test.getDate()) err = true;
		}
		if (err) {
			alert("U voerde een ongeldige datum in.  Correct datumformaat is: 'dd-mm-jjjj'. (vb. vandaag: " + today() + ")");
			window.setTimeout(function() { fld.focus();fld.select(); }, 100);
		} else {
			fld.value = (day < 10 ? "0" : "") + day + "-" + (month + 1 < 10 ? "0" : "") + (month + 1) + "-" + year;
		}
		return (!err);
	} else {
		return true;
	}
}

function check_email_add(frm, nlid, uid) {
		el = frm.email;
		if (trim(el.value) == "" || trim(el.value) == "e-mailadres") {
			alert("Gelieve een e-mailadres in te vullen.");
			el.focus();
			return;
		}
		ajax("/includes/emailcheckadd.php?nlid=" +  escape(nlid) + "&email=" + escape(el.value) + "&uid=" + escape(uid), "GET", false, false, function(h, t) {
			if (t == "false") {
					alert("Gelieve een geldig e-mailadres in te vullen.");
					el.value = "";
					el.focus();
			} else {
				alert("Uw e-mailadres werd toegevoegd aan de nieuwsbrief.");				
			}
		});
}

/***
 * Get real xy coordinates of an object
 */

function getRealLeft(el){
	xPos = $(el).offsetLeft;
	tempEl = $(el).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}

function getRealTop(el){
	yPos = $(el).offsetTop;
	tempEl = $(el).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function getpos(isID){
	trueX = getRealLeft(isID);
	trueY = getRealTop(isID);
	return Array(trueX, trueY);
}

/***
 * MouseWheel functions
 */

var ie = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
function mwheel(e) {
	e = e || window.event;
	if ($("divPopup") && $("divPopup").style.display == "block") {
		if (e.preventDefault) e.preventDefault();
		e.returnValue = false;
	}
	if (parent.$("divPopup") && parent.$('divPopup').style.display == "block") {
		if (ie) targ = e.srcElement; 
		else targ = e.target;
		
		whichobject = "";
		while (targ.parentNode) {
			if (targ.id == "editDiv") {
				whichobject = "editDiv";
				break;
			}
			targ = targ.parentNode;
		}
		
		if (whichobject == "editDiv") { 
			if (e.wheelDelta) d = -e.wheelDelta / 120;
			else d = e.detail / 3;
			window.scrollBy(0, d * (navigator.appVersion.indexOf("Safari") != -1 ? 12 : 16));
		}
		if (e.preventDefault) e.preventDefault();
		e.returnValue = false;
	}
}

if (window.addEventListener) window.addEventListener("DOMMouseScroll", mwheel, false);
window.onmousewheel = document.onmousewheel = mwheel;
