var tooninactief = false;
var his = false;
var medegasten = false;
if (document.getElementById("boekingshistory")) {
	document.getElementById("boekingshistory").style.display = "none";
}
function jsReverseDate(d){
	var tmp = d.split("-");
	rd = tmp[2] + "-" + tmp[1] + "-" + tmp[0];	
	return rd;

}

// toonkalander gerelateerd
function herstelKeuze(){
	
	ele = false;
	elb = false;
	if(begindatum != ''){
		idname_begindatum = begindatum.replace(/-/g,'');
		elb = "dag_" + idname_begindatum;
	}
	
	if(einddatum != ''){
		idname_einddatum = einddatum.replace(/-/g,'');
		ele = "dag_" + idname_einddatum;	
	}
	
	if(ele){
		document.getElementById(ele).className = "calendar_available";	
	}
	
	if(elb){
		document.getElementById(elb).className = "calendar_available";	
	}
	
	
	resetKeuze();
	
}

function resetKeuze() {
	var begind = new Date();
	begind = toJSDdate(document.form.begindatum.value);
	beginnfo = begind;
	eindd = toJSDdate(document.form.einddatum.value);
	eindnfo = eindd;
	while (begind <= eindd) {
		idname = begind.getFullYear() + fillTwo(begind.getMonth()+1) + fillTwo(begind.getDate());
		if (document.getElementById('dag_' + idname)) document.getElementById('dag_' + idname).className='calendar_available';
		begind = addDays(begind,1);
	}
	document.form.begindatum.value = '';
	document.form.einddatum.value = '';
	begindatum = '';
	einddatum = '';
	document.getElementById('keuze_periode').innerHTML = '';
}


function selectDatum(pDatum, bPerDag) {
	if (document.form.einddatum.value != '') {
		resetKeuze();
	}
	
	idname = pDatum.replace(/-/g,'');

	if (begindatum == '') {
		document.getElementById('dag_' + idname).className='calendar_selected';
		begindatum = pDatum;
		document.form.begindatum.value = pDatum;
	} else {
		// Wanneer dezelfde datum wordt gekozen, reset
		if(pDatum == begindatum){
			resetKeuze();
			if (document.getElementById('dag_' + idname)){
				document.getElementById('dag_' + idname).className='calendar_available';
			}
			return;
		}
		
		// aanpassing tbv boeken per dag
		if ((begindatum >= pDatum && bPerDag != true) || (begindatum > pDatum && bPerDag == true)) {
			alert('<?=pt("Kies een einddatum die na de begindatum ligt.")?>');
		} else {
			var begind = new Date();
			begind = toJSDdate(begindatum);
			beginnfo = begind;
			eindd = toJSDdate(pDatum);
			eindnfo = eindd;
			idname = begind.getFullYear() + fillTwo(begind.getMonth()+1) + fillTwo(begind.getDate());
			
			while (begind <= eindd) {
				idname = begind.getFullYear() + fillTwo(begind.getMonth()+1) + fillTwo(begind.getDate());
			
				if (document.getElementById('dag_' + idname)){
					document.getElementById('dag_' + idname).className='calendar_selected';
				} 
				
				begind = addDays(begind,1);
			}
			
			einddatum = pDatum;
			document.form.einddatum.value = pDatum;
			beginnfo = toJSDdate(begindatum);
			
			if(bPerDag){
				document.getElementById('keuze_periode').innerHTML = beginnfo.getDate() + '-' + fillTwo(beginnfo.getMonth()+1) + '-' + beginnfo.getFullYear() + ' Tot en met ' + eindnfo.getDate() + '-' + fillTwo(eindnfo.getMonth()+1) + '-' + eindnfo.getFullYear();
			}else{
				document.getElementById('keuze_periode').innerHTML = beginnfo.getDate() + '-' + fillTwo(beginnfo.getMonth()+1) + '-' + beginnfo.getFullYear() + ' Tot ' + eindnfo.getDate() + '-' + fillTwo(eindnfo.getMonth()+1) + '-' + eindnfo.getFullYear();	
			}
			
		}
	}
}

// einde toonkalander
function checkdate(objName) {
		var datefield = objName;
		if (chkdate(objName) == false) {
			datefield.select();
			alert("<?=t('Deze datum is incorrect.');?>");
			datefield.focus();
			return false;
		}
			else {
			return true;
	   }
}

function chkdate(objName) {
	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
	   }
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   }
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
		   }
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
			err = 9;
			return false;
		}
		}else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
		//datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
		theMonth = toString(intMonth-1);
		datefield.value = intday + "-" + strMonth + "-" + strYear;
	}
	return true;
}

function schakelElement(id){
	// zet style.display id aan wanneer uit en uit wanneer aan.
	
	if(document.getElementById(id)){
		el = document.getElementById(id);
		
		if(el.style.display == "none"){
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}	

}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
		}
		else {
			if ((intYear % 4) == 0) { return true; }
		}
		return false;
	}
function doDateCheck(from, to){
	b1 = us2nldate(from.value);
	e1 = us2nldate(to.value);
	tmp = b1.split("-");
	var bd1 = new Date(tmp[2], tmp[1], tmp[0]);
	tmp = e1.split("-");
	var ed1 = new Date(tmp[2], tmp[1], tmp[0]);

	//var dagen = Math.ceil(ed.getTime() - bd.getTime()) / (one_day);
	if((Math.ceil(bd1.getTime())) < (Math.ceil(ed1.getTime()))){
	//if (bd <= ed) {
		return true;
	}
	else {
		if (from.value == "" || to.value == "") {
			alert("Begin en eind datum moeten beiden worden ingevoerd.");
			return false;
		}
		else {
			alert("De eind datum moet na de begin datum zijn.");
			return false;
		}
	}
}

function nl2usdate(d){
		// maakt mm-dd-jjjj van dd-mm-jjjj
		tmp = d.split("-");
		return tmp[1]+"-"+tmp[0]+"-"+tmp[2];
	}
	
function us2nldate(d){
	// maakt dd-mm-jjjj van mm-dd-jjjj
	tmp = d.split("-");
	return tmp[0]+"-"+tmp[1]+"-"+tmp[2];
}
function nulWeg(v){
	if(v.substr(0,1) == '0'){
		return v.substr(1,1);
	}
	return v;
}


function showHistory(){
	if(his){
		his = false;
		document.getElementById("boekingshistory").style.display = "none";
	}else{
		his = true;
		document.getElementById("boekingshistory").style.display = "";
	}
}
function showOpmerkingen(){
	if(his){
		his = false;
		document.getElementById("boekingsopmerkingen").style.display = "none";
	}else{
		his = true;
		document.getElementById("boekingsopmerkingen").style.display = "";
	}
}

function gegevensOpschonen(gebruiker){
	if(confirm('Deze actie verwijdert alle boekingen en aanverwante gegevens (met uitzondering van de basisgegevens) voor gebruiker '+gebruiker+'. Deze actie kan niet ongedaan worden gemaakt. Weet u zeker dat u wilt doorgaan?')){
		parent.location.href = 'affiliate_gebruikers_bewerk.php?gebruiker='+gebruiker+'&actie=opschonen';	
	}
}
function zetDatum(sd,ed){
	//alert(sd+ed);
	// Stelt start/einddatum in voor een boeking via planbord
	// data strippen
	tmp = sd.split("-");
	sjaar = parseInt(stripZ(tmp[0]));
	smaand = parseInt(stripZ(tmp[1]));
	sdag = parseInt(stripZ(tmp[2]));
	tmp = ed.split("-");
	ejaar = parseInt(tmp[0]);
	emaand = parseInt(stripZ(tmp[1]));
	edag = parseInt(stripZ(tmp[2]))+1;
	// form datum velden instellen
	setSelect("begindatum_dag",sdag);
	setSelect("einddatum_dag",edag);
	setSelect("begindatum_maand",(smaand));
	setSelect("einddatum_maand",(emaand));
}
function stripZ(val){
	// stript eventuele voorloop 0 van dag/maand
	if(val.substr(0,1) == "0"){
		return val.substr(1);
	}else{
		return val;
	}
}
function setSelect(el,val){
	// Selecteerd value van select el
	ele = document.getElementById(el);
	len = ele.length;
	for(t=0;t<len;t++){
		if(ele.options[t].value == val){
			ele.options[t].selected = true;
		}
	}
}
function zetLocatie(loc){
	// selecteert de locatie
	el = document.getElementById('locatie_tabel');
	len = el.length;
	for(t=0;t<len;t++){
		if(el.options[t].text == loc){
			el.options[t].selected = true;
		} 
	}
}
function toonInactieveGebruikers()
{
	if(tooninactief){
		tooninactief = false;
		document.getElementById("inactieve_gebruikers").style.display = "none";
	}else{
		tooninactief = true;
		document.getElementById("inactieve_gebruikers").style.display = "block";
	}

}     
function toonMedegasten(){
	// laat de medegasten zien
	el = document.getElementById("medegasten_tabel");
	if(medegasten){
		el.style.display = "none";
		medegasten = false;
	}else{
		el.style.display = "block";
		medegasten = true;
	}
	
}   

function toonVerwijzer(){
	// toont de verwijzer
	el = document.getElementById("verwijzer_tabel");
	
	if(verwijzer){
		el.style.display = "none";
		verwijzer = false;
	}else{
		el.style.display = "block";
		verwijzer = true;
	}
}
  
 function meerInfoOud(sMsg, sDesc,foto,sButTxt){

	var oContainer = parent.document.getElementById('msgPopup');
	removeChilds(oContainer);
	oMsgDiv = parent.document.createElement('div');
	oTable = parent.document.createElement("table");
	oTbody = parent.document.createElement("tbody");
	oTable.id = "popup_table";
	oRowHead = parent.document.createElement("tr");
	oRowFoto = parent.document.createElement("tr");
	oRowInfo = parent.document.createElement("tr");
	oRowBut = parent.document.createElement("tr");
	td = parent.document.createElement("td");

	sTmp = '<strong class="strong_popup">' + sMsg + '</strong>';
	td.innerHTML = sTmp;
	td.id="popup_table_head"
	oRowHead.appendChild(td);
	// Als foto, foto toevoegen
	if(foto != -1){
		td = parent.document.createElement("td");
		td.id = "popup_foto";
		i = parent.document.createElement("img");
		i.src = foto;
		td.appendChild(i);
		oRowFoto.appendChild(td);
	}
	td = parent.document.createElement("td");
	//if (sDesc != '') sTmp += '<br /><br />' + sDesc;
	if (sDesc != '') sTmp += '<tr><td>>' + sDesc;
	sTmp += '</td></tr>';
	td.innerHTML = sDesc;
	oRowInfo.appendChild(td);
		//oMsgDiv.innerHTML = sTmp;

	oMsgDiv.style.position="fixed";
	oMsgDiv.style.visibility = "hidden";
	oMsgDiv.style.display = "block";
	oMsgDiv.className = "popup_raampje";
	
	//Drag.init(oMsgDiv);
	
	oBut = parent.document.createElement('button');
	oBut.innerHTML=sButTxt;
	oBut.className = "button";
	oBut.onclick = function () { 
		//oMsgDiv.parentNode.removeChild(oMsgDiv);
		removeChilds(parent.document.getElementById("msgPopup")); 
	};
	td = parent.document.createElement("td");
	td.id="popup_table_foot";
	td.appendChild(oBut);
	oRowBut.appendChild(td);
	oTbody.appendChild(oRowHead);
	oTbody.appendChild(oRowFoto);
	oTbody.appendChild(oRowInfo);
	oTbody.appendChild(oRowBut);
	oTable.appendChild(oTbody);
	oMsgDiv.appendChild(oTable)
	//oMsgDiv.appendChild(oBut);
	
	//parent.document.getElementById("msgPopup").appendChild(oMsgDiv);

	var iPopHeight = oMsgDiv.offsetHeight;
	var iPopWidth = oMsgDiv.offsetWidth;
	//var screenW = parent.document.documentElement.clientWidth;
	var screenW = getWindowWidth();
	//var screenH = parent.document.documentElement.clientHeight;
	var screenH = getWindowHeight();
	var browserversion="1";
	browsername=navigator.appName;

	// IE6 hack
	if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};
	if ((browserversion = "6") && (browsername.indexOf("Microsoft")!=-1)){
		var sHori = parent.document.documentElement.scrollTop;
		var sVert = parent.document.documentElement.scrollLeft;
		iPosLeft = ((screenW - iPopWidth)/2)+sVert;
		iPosTop = ((screenH - iPopHeight)/2)+sHori;
		oMsgDiv.style.left = iPosLeft;
		oMsgDiv.style.top = iPosTop;
		oMsgDiv.style.position = 'absolute';
	}
	// all the normal browsers
	else{
		iPosLeft = (screenW - iPopWidth)/2;
		iPosTop = (screenH - iPopHeight)/2;	
		oMsgDiv.style.left = iPosLeft+'px';
		oMsgDiv.style.top = iPosTop+'px';
	}
	if(oContainer.name && oContainer.name == "klantsite"){ // Raampje moet anders worden gepositioneerd omdat de pagina niet in een frame wordt geladen
		iPosTop  = iPosTop - 200;
		iPosLeft = iPosLeft - 200;
		oMsgDiv.style.left = iPosLeft+'px';
		oMsgDiv.style.top = iPosTop+'px';
	}
	else{
		iPosTop  = iPosTop - 100;
		iPosLeft = iPosLeft - 100;
		oMsgDiv.style.left = iPosLeft+'px';
		oMsgDiv.style.top = iPosTop+'px';
	}
	oContainer.appendChild(oMsgDiv);
	oMsgDiv.style.visibility = "";
	}
 
 function meerInfo(sMsg, sDesc,foto,sButTxt){

		var oContainer = document.getElementById('msgPopup');
		removeChilds(oContainer);
		oMsgDiv = document.createElement('div');
		oTable = document.createElement("table");
		oTbody = document.createElement("tbody");
		oTable.id = "popup_table";
		oRowHead = document.createElement("tr");
		oRowFoto = document.createElement("tr");
		oRowInfo = document.createElement("tr");
		oRowBut = document.createElement("tr");
		td = document.createElement("td");

		sTmp = '<strong class="strong_popup">' + sMsg + '</strong>';
		td.innerHTML = sTmp;
		td.id="popup_table_head"
		oRowHead.appendChild(td);
		// Als foto, foto toevoegen
		if(foto != -1){
			td = document.createElement("td");
			td.id = "popup_foto";
			i = document.createElement("img");
			i.src = foto;
			td.appendChild(i);
			oRowFoto.appendChild(td);
		}
		td = document.createElement("td");
		//if (sDesc != '') sTmp += '<br /><br />' + sDesc;
		if (sDesc != '') sTmp += '<tr><td>>' + sDesc;
		sTmp += '</td></tr>';
		td.innerHTML = sDesc;
		oRowInfo.appendChild(td);
			//oMsgDiv.innerHTML = sTmp;

		oMsgDiv.style.position="fixed";
		oMsgDiv.style.visibility = "hidden";
		oMsgDiv.style.display = "block";
		oMsgDiv.className = "popup_raampje";
		
		//Drag.init(oMsgDiv);
		
		oBut = document.createElement('button');
		oBut.innerHTML=sButTxt;
		oBut.className = "button";
		oBut.onclick = function () { 
			//oMsgDiv.parentNode.removeChild(oMsgDiv);
			removeChilds(document.getElementById("msgPopup")); 
		};
		td = document.createElement("td");
		td.id="popup_table_foot";
		td.appendChild(oBut);
		oRowBut.appendChild(td);
		oTbody.appendChild(oRowHead);
		oTbody.appendChild(oRowFoto);
		oTbody.appendChild(oRowInfo);
		oTbody.appendChild(oRowBut);
		oTable.appendChild(oTbody);
		oMsgDiv.appendChild(oTable)
		//oMsgDiv.appendChild(oBut);
		
		//parent.document.getElementById("msgPopup").appendChild(oMsgDiv);

		var iPopHeight = oMsgDiv.offsetHeight;
		var iPopWidth = oMsgDiv.offsetWidth;
		//var screenW = parent.document.documentElement.clientWidth;
		var screenW = getWindowWidth();
		//var screenH = parent.document.documentElement.clientHeight;
		var screenH = getWindowHeight();
		var browserversion="1";
		browsername=navigator.appName;

		// IE6 hack
		if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};
		if ((browserversion = "6") && (browsername.indexOf("Microsoft")!=-1)){
			var sHori = document.documentElement.scrollTop;
			var sVert = document.documentElement.scrollLeft;
			iPosLeft = ((screenW - iPopWidth)/2)+sVert;
			iPosTop = ((screenH - iPopHeight)/2)+sHori;
			oMsgDiv.style.left = iPosLeft;
			oMsgDiv.style.top = iPosTop;
			oMsgDiv.style.position = 'absolute';
		}
		// all the normal browsers
		else{
			iPosLeft = (screenW - iPopWidth)/2;
			iPosTop = (screenH - iPopHeight)/2;	
			oMsgDiv.style.left = iPosLeft+'px';
			oMsgDiv.style.top = iPosTop+'px';
		}
		if(oContainer.name && oContainer.name == "klantsite"){ // Raampje moet anders worden gepositioneerd omdat de pagina niet in een frame wordt geladen
			iPosTop  = iPosTop - 200;
			oPosLeft = iPosLeft - 200;
			oMsgDiv.style.left = iPosLeft+'px';
			oMsgDiv.style.top = iPosTop+'px';
		}
		oContainer.appendChild(oMsgDiv);
		oMsgDiv.style.visibility = "";
		}
		
	
function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function removeChilds(cell){
	if ( cell.hasChildNodes() ){
		while ( cell.childNodes.length >= 1 ){
			cell.removeChild( cell.firstChild );       
		} 
	}
}
function isdefined( variable){
	// is var defined?
    return (typeof(window[variable]) == "undefined")?  false: true;
}



