/* script.js */
var now = new Date()
window.name = "main"+now.getTime()

var bAllPara
var bDone = false
var sMenu
var frm
var bAdminMode = 'true'
var bAdminModePermission

function popUp(URL, name, dx, dy, iNoScrollbars) {
	if (iNoScrollbars) {
		iScrollbars = 0
	} else {
		iScrollbars = 1
	}
	sOptions = 'toolbar=0,scrollbars='+iScrollbars+',location=0,statusbar=0,menubar=0,resizable=1,width=' + dx + ',height=' + dy
	sOptions += ',left=' + ((screen.width-dx)/2) + ',top=' + ((screen.height-dy)/2) + ',screenX=' + ((screen.width-dx)/2) + ',screenY=' + ((screen.height-dy)/2) + ''

	window.open(window.location.protocol + '//' + window.location.host + URL, name, sOptions)
}

function submitToPopUp(action, dx, dy) {
	popUp('', 'popupwindow', dx, dy)
	document.docmgt.action="index.php?action="+action
	document.docmgt.submit()
}

function submitToMain(action) {
	document.docmgt.target = window.opener ? window.opener.name : ""
	document.docmgt.action = action
	document.docmgt.submit()
}

function checkRemove(type, message, item) {
	return confirm(message)
}

function addConfirmToLinks(sMessage){
	if (bDone == false) {
		for (var a=0; a<document.links.length; a++) {
			if (A = document.links[a]) {
				if (isItemInHtmlarea(A)) {
				} else {
					sOldHref = A.href
					if (A.href.search('mailto:')) {
						A.href = "javascript:doConfirm('"+sOldHref+"', '"+sMessage+"')";
					}
				}
			}
		}
		for (var a=0; a<document.forms.length; a++) {
			if (oForm = document.forms[a]) {
				if (isItemInHtmlarea(oForm)){
				} else {
					oForm.onsubmit = function(event) { return confirm(sMessage); }
				}
			}
		}
	}
	enable(document.getElementById('savebutton'))
	enable(document.getElementById('savebutton_footer'))
	bDone = true
}

function doConfirm(sOldHref, sMessage) {
	if (confirm(sMessage)) {
		window.location = sOldHref;
	}
}

function isItemInHtmlarea(oLink) {
	var elem = oLink
	while (elem.name != "htmlarea" && elem.name != "htmlarea2") {
		elem = elem.parentElement
		if (elem == null)
			break
	}
	if (elem) {
		return true
	}
}

function selOn(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selOn";
	}
}
function selOff(ctrl) {
	if (ctrl) {
		ctrl.className = "selOff";
	}
}
function selDown(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selDown";
	}
}
function selUp(ctrl) {
	if (ctrl && ctrl.disabled != true) {
		ctrl.className = "selUp";
	}
}

function disable(ctrl) {
	if (ctrl && !ctrl.disabled) {
		ctrl.style.color = "graytext";
		ctrl.onclicktemp = ctrl.onclick
		ctrl.onclick = "";
		ctrl.disabled = true;
		makeGray(ctrl, true);
	}
}
function enable(ctrl) {
	if (ctrl) {
		ctrl.style.color = "menutext";
		if (ctrl.onclicktemp && ctrl.onclick == "") {
			ctrl.onclick = ctrl.onclicktemp
		}
		ctrl.disabled = false;
		makeGray(ctrl, false);
	}
}

function makeGray(el,b) {
	var sInter;
	var sPath = "";

	sInter = (b) ? "disabled/disabled_" : "";

	var imgs = findChildren(el, "tagName", "IMG");

	for (var i=0; i<imgs.length; i++) {
		if (b) {
			if (!imgs[i].orig_src) {
				sFull = imgs[i].orig_src = imgs[i].src;
				aFull = sFull.split('/');
				sFilename = aFull[aFull.length-1];
				for (var j=0; j < (aFull.length-1); j++) {
					sPath = sPath + aFull[j]+"/";
				}
				imgs[i].src = sPath+sInter+sFilename;
			}
		} else {
			if (imgs[i].orig_src) {
				imgs[i].src = imgs[i].orig_src;
				imgs[i].orig_src = null;
			}
		}
	}
}

function findChildren(el, type, value) {
	var children = el.childNodes;
	var tmp = new Array();
	if (!children || !children.length) {
		return tmp
	}
	var j=0;

	for (var i=0; i<children.length; i++) {
		if (eval("children[i]." + type + "==\"" + value + "\"")) {
			tmp[tmp.length] = children[i];
		}
		tmp = tmp.concat(findChildren(children[i], type, value));
	}
	return tmp;
}

function menuOn(ctrl) {
	if (ctrl) {
		ctrl.className = "menuOn";
	}
}
function menuOff(ctrl) {
	if (ctrl) {
		ctrl.className = "menuOff";
	}
}
function menuDown(ctrl) {
}
function menuUp(ctrl) {
}

function showMenu(oMenu, oButton) {
	var iZIndex;
	var iBorder = 1;
	iZIndex = 150

	var oBody = document.getElementById('body');

	oMenu.style.display = 'inline';
	oMenu.style.position = 'absolute'
	iPosition = getPosition(oButton);
	oMenu.style.left = iPosition[0];
	oButton.height = 20;
	oMenu.style.top = iPosition[1] + oButton.height;
	oMenu.style.zIndex = iZIndex;
	if (oMenu.id != 'filter_form') {
		oBody.appendChild(oMenu);
	}

	// disable the <select>s and other objects that are covered by the menu
	var oIframe = document.getElementById(oMenu.id + '_iFrame');
	if (!oIframe) {
		oIframe = document.createElement('iframe');
		oIframe.setAttribute("id", oMenu.id + '_iFrame');
		oIframe.style.border = 0;
		oIframe.width  = 0;
		oIframe.height = 0;
		oIframe.style.position = "absolute";
		oIframe.style.visibility = 'hidden';
		oIframe.src = '/blank.html';
		if (oBody) {
			oBody.appendChild(oIframe);
		}
	}
	if (oIframe) {
		iHeight = oMenu.clientHeight + 2*iBorder;
		iWidth  = oMenu.clientWidth  + 2*iBorder;
		oIframe.height = parseInt(iHeight);
		oIframe.width  = parseInt(iWidth);
		oIframe.style.top  = iPosition[1] + oButton.height;
		oIframe.style.left = iPosition[0]
		oIframe.style.visibility = 'visible';
		oIframe.style.zIndex  = iZIndex - 1;
	}
}

function hideMenu(oMenu) {
	if (oMenu) {
		oMenu.style.display='none'
		oIframe = document.getElementById(oMenu.id + '_iFrame');
		if (oIframe) {
			oIframe.style.visibility = 'hidden';
		}
	}
}

function toggleMenu(sDropMenu, oButton) {
	document.onclick = onclickhandler
	sMenu = sDropMenu;
	oMenu = document.getElementById(sDropMenu)
	if (oMenu.style.display=='none' || !oMenu.style.display) {
		showMenu(oMenu, oButton)
	} else {
		hideMenu(oMenu)
	}
}

function onclickhandler(event) {
	if (sMenu) {
		hideMenu(document.getElementById(sMenu));
	}
}

function getPosition(oButton) {
	var posX = 0
	var posY = 0
	if (oButton.offsetParent) {
		for (var posX = 0, posY = 0; oButton.offsetParent; oButton = oButton.offsetParent) {
			posX += oButton.offsetLeft;
			posY += oButton.offsetTop;
		}
		return [posX, posY];
	} else {
		if (!oButton.x && !oButton.y) {
			return [0,0];
		} else {
			return [oButton.x, oButton.y];
		}
	}
}

function printWindow() {
	bV = parseInt(navigator.appVersion)
	if (bV >= 4) {
		window.print()
	}
}

function selectParagraph(sId)
{
	if (getCookie('adminmode') == 'true') {
		if (document.getElementById('toolbardiv' + sId)) {
			document.getElementById('toolbardiv' + sId).style.display = 'block';
			if (getCookie('allpara') != 'true') {
				document.getElementById('pc_paragraph'+sId).className = 'pc_paragraph SelectParagraph';
			} else {
				document.getElementById('pc_paragraph'+sId).className = 'pc_paragraph';
			}
		}
	}
}

function unselectParagraph(sId)
{
	var oT = document.getElementById('toolbardiv'+sId);
	if (oT && (getCookie('allpara') != 'true' || getCookie('adminmode') != 'true')
	) {
		oT.style.display = 'none';
		document.getElementById('pc_paragraph'+sId).className = 'UnselectParagraph';
	}
}

function toggleAllPara()
{
	var bAllPara
	var sC = document.getElementById('allpara').className;
	if (getCookie('allpara') == 'true') {
		setAllPara('false')
		sC = 'selOff'
	} else {
		setAllPara('true')
		sC = 'selDown'
	}
}

function setAllPara(bAllPara)
{
	if (bAllPara != 'false') {
		bAllPara = 'true';
	}

	if (bAllPara == 'true') {
		setCookie('allpara', 'true', 7);
		setParaIds(aParaIds);
	} else {
		setCookie('allpara', 'false', 7);
		//removeCookie('allpara');
		setParaIds(aParaIds);
	}
}

function setParaIds(aParaIds)
{
	bShow = isShowAll()
	for (var i = 0; i < aParaIds.length; i++) {
		if (bShow == true && getCookie('adminmode') == 'true') {
			selectParagraph(aParaIds[i])
		} else {
			unselectParagraph(aParaIds[i])
		}
	}
}

function isShowAll()
{
	var oA = document.getElementById('allpara');
	if (getCookie('allpara') == 'true' && getCookie('adminmode') == 'true') {
		bShow = true;
		if (oA) {
			oA.className = 'selDown'
		}
	} else {
		bShow = false;
		if (oA) {
			oA.className = 'selOff'
		}
	}
	return bShow
}

function setAdminMode(bAdminMode)
{
	//var bAdminMode;
	if (bAdminMode != 'false') {
		bAdminMode = 'true';
	}
	if (bAdminMode == 'true') {
		setCookie('adminmode', 'true', 7);
	} else {
		setCookie('adminmode', 'false', 7);
	}
	oMenu = document.getElementById('toptoolbar')
	if (oMenu) {
		if (bAdminMode == 'true') {
			oMenu.style.display = 'block'
		} else {
			oMenu.style.display = 'none'
		}
	}
	oAddParagraph = document.getElementById('addparagraphtoolbar')
	if (oAddParagraph) {
		if (bAdminMode == 'true') {
			oAddParagraph.style.display = 'block'
		} else {
			oAddParagraph.style.display = 'none'
		}
	}
	setParaIds(aParaIds);
}

function toggleAdminMode()
{
	if (getCookie('adminmode') == 'true') {
		setAdminMode('false')
	} else {
		setAdminMode('true')
	}
}

// Cookie handling
function setCookie( sName, sValue, nDays ) {
	var expires = "";
	if ( nDays ) {
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}
	document.cookie = sName + "=" + sValue + expires + "; path=/;";
}

function getCookie(sName) {
	var re  = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	if (res != null) {
		return res[3];
	}
	else {
		removeCookie(sName)
		return null;
	}
}

function removeCookie( name ) {
	setCookie( name, "", -1 );
}


// Cookie Form
// <body onload="cookieForms('open', 'yourform')" onunload="cookieForms('save', 'yourform')">

var expDays = 100;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');
function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);
function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}

// cookieForms saves form content of a page.

// use the following code to call it:
//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
function cookieForms() {
	var mode = cookieForms.arguments[0];

	for (f=1; f<cookieForms.arguments.length; f++) {
		formName = cookieForms.arguments[f];

		if (mode == 'open') {
			cookieValue = GetCookie('saved_'+formName);
			if (cookieValue != null) {
				var cookieArray = cookieValue.split('#cf#');

				if (cookieArray.length == document[formName].elements.length) {
					for (i=0; i<document[formName].elements.length; i++) {

						if (cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
						else if ((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
						else if ((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
						else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
					}
				}
			}
		}

		if (mode == 'save') {
			cookieValue = '';
			for (i=0; i<document[formName].elements.length; i++) {
				fieldType = document[formName].elements[i].type;

				if (fieldType == 'password') { passValue = ''; }
				else if (fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
				else if (fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
				else if (fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
				else { passValue = document[formName].elements[i].value; }

				cookieValue = cookieValue + passValue + '#cf#';
			}
			cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter

			SetCookie('saved_'+formName, cookieValue, exp);
		}
	}
}

var nbsp = 160;
var node_text = 3;
var emptyString = /^\s*$/
var glb_vfld;

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

function DisplayMessage(fld, msgtype, message)
{
	var dispmessage;
	if (emptyString.test(message))
		dispmessage = String.fromCharCode(nbsp);
	else
		dispmessage = message;
	var elem = document.getElementById(fld);
	if (elem) {
		if (elem.firstChild) {
			elem.firstChild.nodeValue = dispmessage;
		}

		elem.className = msgtype;
	}
}

var sErrorMandatory = " ! Verplicht"
var sMandatory = " Verplicht"

function FormValidate(oForm, sErrorMsg)
{
	if (typeof sErrorMsg == "undefined") {
    	sErrorMsg = "Er zijn verplichte velden niet ingevuld. Vul een waarde in.";
	}
	var iError = 0;

	if (!document.getElementById) {
		return true;
	}
	for (var i = (oForm.elements.length-1); i >= 0; i--) {
		sName = oForm.elements[i].name;
		sValidationId = sName + '_validation';
		if (document.getElementById(sValidationId)) {
			if (document.getElementById(sValidationId).value == 'mandatory') {
				sRequiredId = 'required_' + oForm.elements[i].name;
				if (emptyString.test(oForm.elements[i].value)) {
					DisplayMessage(sRequiredId, "inline_error", sErrorMandatory);
					oForm.elements[i].focus();
					iError = iError + 1;
				} else {
					DisplayMessage(sRequiredId, "required", sMandatory);
				}
			}
		}
	}

	if (iError > 0) {
		alert(sErrorMsg);
		return false;
	} else {
		return true;
	}
}

function popzoekerUp(URL, name, dx, dy, iNoScrollbars) {
	if (iNoScrollbars) {
		iScrollbars = 0
	} else {
		iScrollbars = 1
	}
	sOptions = 'toolbar=0,scrollbars='+iScrollbars+',location=0,statusbar=0,menubar=0,resizable=1,width=' + dx + ',height=' + dy
	sOptions += ',left=' + ((screen.width-dx)/2) + ',top=' + ((screen.height-dy)/2) + ',screenX=' + ((screen.width-dx)/2) + ',screenY=' + ((screen.height-dy)/2) + ''
	popupwindow = window.open(URL, name, sOptions)
	return popupwindow
}

/* image swap in page */
function swapimg(image,width,height) {
	document.images.image.src = '/page/images/thumb/w' + width + 'h' + height + '_' + image
	if (image == '') {
		document.getElementById('showhide').className='swapimage_hide'
	}else{
		document.getElementById('showhide').className='swapimage_show'
	}
}

function hasimage(img){
	var image = img.toLowerCase()
	if ((image.lastIndexOf('.jpg') == -1) &&
		(image.lastIndexOf('.gif') == -1) &&
		(image.lastIndexOf('.png') == -1)) {
		return false
	}
	return true
}

/* Scripts for datadirect */
function setFormValueX(sField, sValue)
{
	if (oField = eval('document.mailerform.'+sField)) {
		oField.value = sValue
	}
}
function setFormValue(sField, sValue)
{
	if (sField == 'Aanhef') {
		if (sValue == 'dhr') {
			document.mailerform.Aanhef[1].checked = true
		} else {
			document.mailerform.Aanhef[0].checked = true
		}
	} else if (oField = eval('document.mailerform.'+sField)) {
		oField.value = sValue
	}
}

function limitTextarea(field, maxlimit)
{
	if (maxlimit != 0) {
		if (field.value.length > maxlimit) {
			field.value = field.value.substring(0, maxlimit);
		}
	}
}
