var gValidColor = "white";
var gErrorColor = "#ffdddd";
var gValid;
var gAlert;

function MarkElement(elem, valid, msg) {
	if (valid) {
		if (elem.type != "checkbox")
	 		elem.style.backgroundColor=gValidColor;
	} else {
		elem.focus();
		if (elem.type != "checkbox")
			elem.style.backgroundColor=gErrorColor;
		gValid = false;
		if (msg) gAlert = msg;
	}
	return valid;
}

function UnmarkElement(elem) {
	if ((elem.type == "text" || elem.tagName == "TEXTAREA") && !elem.value) return;
	if (elem.type == "checkbox") return;
	elem.style.backgroundColor=gValidColor;
}

var gspecialCharacterString = "' \" < > = & ? % | ;";
function checkSpecial(x) {
	var filter = /'|"|<|>|=|&|\?|%|\||;/;
	return !filter.test(x);
}

function checkAllTextFields(form) {
	var i, a;
	for (i=0; (a = form.getElementsByTagName("INPUT")[i]); i++) {
		if (a.type == "text" && a.value && !checkSpecial(a.value)) {
			return a;
		}
	}
	for (i=0; (a = form.getElementsByTagName("TEXTAREA")[i]); i++) {
		if (a.value && !checkSpecial(a.value)) {
			return a;
		}
	}
	return null;
}

function stripAllTextFields(form) {
	var i, a;
	for (i=0; (a = form.getElementsByTagName("INPUT")[i]); i++) {
		a.value = a.value.replace(/^\s+/,'')
		a.value = a.value.replace(/\s+$/,'')
	}
	for (i=0; (a = form.getElementsByTagName("TEXTAREA")[i]); i++) {
		a.value = a.value.replace(/^\s+/,'')
		a.value = a.value.replace(/\s+$/,'')
	}
	return null;
}

function checkMail(x)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(x);
}

function checkWebsite(x)
{
	return true;
}

function getCookie(cookieName)
{
	var search = cookieName + "=";
	if (document.cookie.length > 0) { // if there are any cookies
		var offset = document.cookie.indexOf(search);
		if (offset != -1) { // if cookie exists
			offset += search.length;          // set index of beginning of value
			var end = document.cookie.indexOf(";", offset);          // set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
}

function setCookie(cookieName,cookieValue,cookieExp)//expires in days
{
	if (cookieExp) {
		var expire = new Date();
		expire.setTime(expire.getTime() + 3600000*24*cookieExp);
		cookieExp = "expires="+expire.toGMTString();
	}
	//PS: overige cookies blijven ongewijzigd, wijzigt eventueel bestaand cookie
	document.cookie = cookieName+"="+escape(cookieValue)+";path=/;"+cookieExp;
}

function setSize(size) 
{
    setCookie("fontsize", size, 1000);
    location.reload(location.href);
}

function getParameter(parameterName)
{
	var search = parameterName + "=";
	var offset = location.href.indexOf(search);
	if (offset != -1) { // if parameter exists
		offset += search.length; // set index of beginning of value
		var end = location.href.indexOf("&", offset); // set index of end of value
		if (end == -1)
			end = location.href.length;
		return unescape(location.href.substring(offset, end));
	}
}

function ShowCheckedOther(id) {
	var display = "none";
	var elem = document.getElementById(id+"othercb");
	if (elem.checked) {
		display = "inline";
	}
	document.getElementById("opt "+id+"other").style.display = display;
}

function ShowSelectOther(id) {
	var display = "none";
	var elem = document.getElementById(id);
	if (elem.options[elem.length-1].selected) {
		display = "inline";
	}
	document.getElementById("opt "+id+"other").style.display = display;
}

function InitSelects(formid)
{
	var form = document.getElementById(formid);
	var i, elem;
	for (i=0; (elem = form.getElementsByTagName("SELECT")[i]); i++) {
		var toselect = document.getElementById("selected"+elem.id);
		if (toselect && toselect.value) {
			for (j=0; j < elem.length; j++) {
				if (toselect.value == (elem.options[j].value || elem.options[j].text)) {
					elem.options[j].selected = true;
				}
			}
		}
	}
}

function InitCheckboxes(formid)
{
	var form = document.getElementById(formid);
	var i, elem;
	for (i=0; (elem = form.getElementsByTagName("INPUT")[i]); i++) {
		if (elem.type == "checkbox" && document.getElementById("selected"+elem.name)) {
			var tocheck = document.getElementById("selected"+elem.name);
			if (tocheck && tocheck.value) {
				if (tocheck.value.indexOf(elem.value) != -1) {
					elem.checked = true;
				}
			}
		}
	}
}

function InitRadios(formid)
{
	var form = document.getElementById(formid);
	var i, elem;
	for (i=0; (elem = form.getElementsByTagName("INPUT")[i]); i++) {
		if (elem.type == "radio" && document.getElementById("selected"+elem.name)) {
			var tocheck = document.getElementById("selected"+elem.name);
			if (tocheck && tocheck.value) {
				if (tocheck.value == elem.value) {
					elem.checked = true;
				}
			}
		}
	}
}

function imagePopup(url, w, h) {
	w += 40;
	h += 40;
	var win = window.open(url,"imagePopup","scrollbars,status,resizable,width="+w+",height="+h);
	win.focus();
}

function moveToTop() {
    var totop = document.getElementById("totop");
    if ((document.body.scrollHeight > document.body.clientHeight) && (document.body.scrollTop > 100)) {
        totop.style.display = "block";
        totop.style.top = document.body.scrollTop+document.body.clientHeight-35+"px";
    } else {
        totop.style.display = "none";
    }
    setTimeout("moveToTop()",50);
}

function jumpToTop() {
    location.replace(location.href.split("#")[0]+"#top");
}

function DigitsOnly(e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else {
		if (e) {
			keycode = e.which;
		} else {
			return true;
		}
	}
	if (keycode > 47 && keycode <= 58) {
	   return true;
	}
	return false;
}

function openHelp(jumpto)
{
    var url = "help.cgi";
    if (jumpto) url += "#"+jumpto;
    var w = window.open(url,"xcmshelp");
    w.focus();
}

function openModalWindow(href,opts)
{
	var agt = navigator.userAgent.toLowerCase();
	var is_gecko = (agt.indexOf('gecko') != -1);
  if (opts) {
    opts = ","+opts;
  } else {
  	opts = '';
  }
  var result;
  if (is_gecko) {
	  var w = window.open(href, "", "height=540,width=700,left=50,top=50,status=0,menubar=0,location=0,toolbar=0"+opts);
		w.focus();
	} else {
  	result = window.showModalDialog(href, "", "dialogHeight:540px;dialogWidth:700px;center:yes;edge:sunken;resizable:yes;status:no");
	}
	return result;
}	

//window.status="support.js loaded";