function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
	if (req.readyState == 4) { // only if req shows "complete" and status is OK
		if (req.status == 200) { // only if "OK"
			response	= req.responseXML.documentElement;
			divId		= response.getElementsByTagName('divId')[0].firstChild.data;
			result		= response.getElementsByTagName('result')[0].firstChild.data;
			if (divId && result)
				receiveXMLrequest(divId,result);
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function sendXMLrequest(name,querystring) {
	loadXMLDoc('http://dev.utrechtslandschap/vrijwilligers.php?'+name+'='+querystring);
}

function receiveXMLrequest(divId,result) {
	var div = document.getElementById(divId);
	if (div)
		div.innerHTML = result;
	if (divId == 'activiteit') {
		if (document.getElementById('tocht'))
			document.getElementById('tocht').innerHTML = '...';
		if (document.getElementById('data'))
			document.getElementById('data').innerHTML = '';
	}
}

function totalAmount(el) {
	var i, rv, pd = [];
	var selectedAmount, totalAmount;

	//array of selectboxes
	if (document.getElementById('aantal_beschermers'))
		pd['volw_beschermers'] 		= document.getElementById('aantal_beschermers');
	if (document.getElementById('aantal_volwassenen'))
		pd['volw_geenbeschermers'] 	= document.getElementById('aantal_volwassenen');

	if (document.getElementById('aantalkinderen_beschermers'))
		pd['kind_beschermers'] 		= document.getElementById('aantalkinderen_beschermers');
	if (document.getElementById('aantalkinderen_geenbeschermers'))
		pd['kind_geenbeschermers'] 	= document.getElementById('aantalkinderen_geenbeschermers');
	if (document.getElementById('aantalkinderen_tm5_beschermers'))
		pd['kind_tm5_beschermers'] = document.getElementById('aantalkinderen_tm5_beschermers');
	if (document.getElementById('aantalkinderen_tm5_geenbeschermers'))
		pd['kind_tm5_geenbeschermers'] = document.getElementById('aantalkinderen_tm5_geenbeschermers');
	if (document.getElementById('aantalkinderen_tm12_beschermers'))
		pd['kind_tm12_beschermers'] = document.getElementById('aantalkinderen_tm12_beschermers');
	if (document.getElementById('aantalkinderen_tm12_geenbeschermers'))
		pd['kind_tm12_geenbeschermers'] = document.getElementById('aantalkinderen_tm12_geenbeschermers');
	
	//get the totalAmount (should be the same in every selectbox, so just pick the first one)
	totalAmount = pd['volw_geenbeschermers'].options.length-1;
	var maxKinderen = document.forms[0].maxkinderen.value;
	var selectedAmountKind = 0;
	
	//get the selectedAmount of all selectboxes together
	for (keycode in pd) {
		if (keycode.substr(0,4) == 'volw') {
			if (!selectedAmount)
				selectedAmount = 0;
			selectedAmount += parseInt(pd[keycode].value);
		}
		if (keycode.substr(0,4) == 'kind') {
			if (!selectedAmountKind)
				selectedAmountKind = 0;
			selectedAmountKind += parseInt(pd[keycode].value);
		}
	}
	if (document.forms[0].kindervaart.value == "true" && selectedAmountKind > maxKinderen) {
		alert("Er zijn slechts " + maxKinderen + " plaatsen voor kinderen beschikbaar!");
		el.options[0].selected = true;
	}
	
	//check if selectedAmount does NOT exceed the totalAmount
	if ((totalAmount - selectedAmount - selectedAmountKind) < 0) {
		alert("Er zijn slechts " + totalAmount + " plaatsen beschikbaar!");
		el.options[0].selected = true;
	}

}

//methode om video's aan het DOM toe te voegen
function setVideo(divId,videoPath) {
	var myObject = document.createElement('object'); 
	obj = document.getElementById(divId);
	obj.appendChild(myObject); 
	myObject.width = "205"; 
	myObject.classid= "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6";  
	myObject.URL = videoPath;
	myObject.uiMode = "mini";
}

//print functie (haalt bepaalde TD's uit de maintable en gooit deze in een popup)
function printPage(aantal,tdeen,tdtwee) {
	if (document.getElementById(tdeen))
		var print1 = document.getElementById(tdeen).innerHTML;
	if (document.getElementById(tdtwee))
		var print2 = document.getElementById(tdtwee).innerHTML;
	
	if (aantal ==  1)
	  window.printgedeelte = '<table><tr><td>'+print1+'</td></tr></table>';
	else if (aantal == 2)
	  window.printgedeelte = '<table><tr><td>'+print1+'</td><td>'+print2+'</td></tr></table>';
	
	var w = window.open('/print.html');
}

function toon_afbeelding_activiteiten(afbeelding) {
	afbeelding.src='/images/na_activiteiten.gif'
}

//popup voor modules binnen admin
top.window.popupActiviteiten = function () {
	popupModules('activiteiten');
}
top.window.popupReserveringen = function () {
	popupModules('reserveringsmodule/index',820,700, true, true);
}
top.window.popupProducten = function () {
	popupModules('onlineshop');
}
top.window.popupRedirect = function () {
	popupModules('redirect/index');
}

function popupModules(modulenaam,width,height, resize, sb) {
	var filename="/admin/"+modulenaam+".php";
	var fileid="Activiteiten";
	var width=width?width:"650";
	var height=height?height:"580";
	var scrollbars=sb==true?"yes":"no";
	var resizable=resize==true?"yes":"no";
	var menubar="no";
	var toolbar="no";
	var status="no";
	var location="no";
	var popupscherm=0;
	var args = "height=" + height + ", width=" + width + ", scrollbars=" + scrollbars + ", resizable=" + resizable +  ", menubar=" + menubar + ", toolbar=" +toolbar + ", status=" + status + " ,location=" + location;
	popupscherm = top.window.open(filename, fileid, args);
}

//Popup voor Foto van de week
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var popupscherm=0;

function popup_afbeelding(filename, fileid, width, height, scrollbars, resizable, menubar, toolbar, status, location) {
	if (popupscherm && ! popupscherm.closed)	popupscherm.close();
	if (!filename || filename=="")				filename="http://www.utrechtslandschap.nl";
	if (!fileid || fileid=="")					fileid="fotovandeweek";
	if (!width || width=="")					width="50";
	if (!height || height=="")					height="50";
	if (!scrollbars || scrollbars=="")			scrollbars="no";
	if (!resizable || resizable=="")			resizable="no";
	if (!menubar || menubar=="")				menubar="no";
	if (!toolbar || toolbar=="")				toolbar="no";
	if (!status || status=="")					status="no";
	if (!location || location=="")				location="no";
	popupscherm=0;
	var args = "height=" + height + ", width=" + width + ", scrollbars=" + scrollbars + ", resizable=" + resizable +  ", menubar=" + menubar + ", toolbar=" +toolbar + ", status=" + status + " ,location=" + location;
	popupscherm = open(filename, fileid, args);
	if (!InternetExplorer) 						popupscherm.focus();
}

//Functies voor online shop
function popup(url) {
	var filename="/"+url;
	var fileid="1";
	var width="500";
	var height="480";
	var scrollbars="no";
	var resizable="no";
	var menubar="no";
	var toolbar="no";
	var status="no";
	var location="no";
	var popupscherm=0;
	var args = "height=" + height + ", width=" + width + ", scrollbars=" + scrollbars + ", resizable=" + resizable +  ", menubar=" + menubar + ", toolbar=" +toolbar + ", status=" + status + " ,location=" + location;
	popupscherm = top.window.open(filename, fileid, args);
}
function bestelProduct(id) {
	if (document.winkelmand.prodid) {
		document.winkelmand.actie.value = 'producttoevoegen';
		document.winkelmand.prodid.value = id;
		document.winkelmand.submit();
	}
}
function verwijderProduct(id) {
	if (document.winkelmand.prodid) {
		document.winkelmand.actie.value = 'productverwijderen';
		document.winkelmand.prodid.value = id;
		document.winkelmand.submit();
	}
}
function bestellingBevestigen() {
	if (document.winkelmand.actie) {
		document.winkelmand.actie.value = 'bestellingbevestigen';
		document.winkelmand.submit();
	}
}

//koppel de juiste classes bij mouseover uitklapmenu
sfHover = function() {
	if (document.getElementById("nav")) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//wissel tabbladen van nieuwsviewer voorpagina
function switchTab(nr,actie) {
	if (actie == 'uit')
		switchTab.busy = false;
	else {
		for(i=0; i<4; i++) {
			if (obj=document.getElementById('nieuwsKop'+i))
				obj.className = 'nieuwsKop';
			if (obj=document.getElementById('nieuwsAfb'+i))
				obj.className = 'nieuwsAfbeelding';
			if (obj=document.getElementById('nieuwsSubKop'+i))
				obj.className = 'nieuwsSubKop';
		}
		if (actie == 'in' || actie == 'auto') {
			if (obj=document.getElementById('nieuwsKop'+nr))
				obj.className = 'nieuwsKopActief';
			if (obj=document.getElementById('nieuwsAfb'+nr))
				obj.className = 'nieuwsAfbeeldingActief';
			if (obj=document.getElementById('nieuwsSubKop'+nr))
				obj.className = 'nieuwsSubKopActief';
			if (actie == 'in')
				switchTab.busy = true;
			else
				switchTab.busy = false;
		}
	}
}

var time = 3;
var start = 1;
switchTab.busy = false;

function setTimer() {
	time = time - 1;
	setTimeout("setTimer()", 1000);
	if (time == 0) {
		if (switchTab.busy == false)
			switchTab(start,'auto');
		if (start == 2)
			start = 0;
		else
			start = start + 1;
		time = 3;
		
	}
}
