var count_popups = 0;

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function destroyOverlay(obj,id) {

	selects = document.getElementsByTagName("select");
        for (var i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	document.body.scroll='';

	if (id == undefined) {

		if (obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id == 'infopage') {
			var overlayBoxContainer = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	
		}
		else if (obj.parentNode.id == 'registerRight') {
			var overlayBoxContainer = obj.parentNode.parentNode.parentNode.parentNode.id;
	
		}
		else if (obj.parentNode.id == 'registerContainer') {
			var overlayBoxContainer = obj.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	
		}
		else if (obj.parentNode.parentNode.id == 'greetingcard') {
			var overlayBoxContainer = obj.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	
		}
		else {
			var overlayBoxContainer = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	
		}
	
	}
	else {
		var overlayBoxContainer = id;
	}
	shadowBorderRemoveReference(document.getElementById(overlayBoxContainer).childNodes[0].id);

	var body = document.getElementsByTagName('body')[0];
	var container = document.getElementById(overlayBoxContainer);

	//destroyOverlayChildren(container);

	container.innerHTML = '';
	container.parentNode.removeChild(container);
	container = null;
	
	//body.removeChild(container);
	//container = null;

	/**
	 * Popup is gesloten, dus 1 van de teller afhalen
	 */
	count_popups--;

	/**
	 * Alleen verwijderen als er nog maar 1 popup is.
	 */
	if (count_popups < 1) {
		var overlay = document.getElementById('overlayBox');
		body.removeChild(overlay);
		overlay = null;
	}
	//body.removeChild(container);
	//body.removeChild(overlay);
	//document.getElementById('overlayBox').style.display = 'none';

}

function destroyOverlayChildren(container) {

//alert(container.childNodes.length);
	for(i=container.childNodes.length-1;i>-1;i--) {

		if (container.childNodes[i].id != '') {
			alert(container.childNodes[i].id);
		}
		destroyOverlayChildren(container.childNodes[0]);
		container.childNodes[0].innerHTML = '';
		container.removeChild(container.childNodes[0]);
		//while(container.hasChildNodes()) {
		//	container.innerHTML = '';
        //	container.removeChild(container.firstChild);
		//}
	}
	return true;

}

function closePopup(obj,id) {
 
	destroyOverlay(obj,id);

}

function viewAgenda(id,obj) {

		var objClass = obj.parentNode.className;
		if (objClass.indexOf('done') == -1) {
			obj.parentNode.className = 'visited done';
		}

        new popup('viewagenda','&id='+id,'');
        
        var save = new ajaxRequest('/agendaitem.php','id='+id);
		save.method = 'GET';
		save.container = '';
		save.process();

}

function viewAgendaMessage(id,obj) {

		var objClass = obj.parentNode.className;
		if (objClass.indexOf('done') == -1) {
			obj.parentNode.className = 'visited done';
		}

        new popup('viewagendaitem','&id='+id,'');

}

function viewWebForm(obj) {

		var objClass = obj.parentNode.className;

        new popup('contactForm');

}

function addAgenda() {
	if( checklogin() ) {
        new popup('addagenda','&d='+calendarDate,'');
	}
}

function addAgendaItem() {

        new popup('addagendaitem','&d='+calendarDate,'');

}

function calculator() {

        new popup('calculator','','');

}

function timer(){
	if( checklogin() ) {
		new popup('timer','','');
	}
}

function sneeuwhoogten() {

        new popup('sneeuwhoogten','','');

}

function addVerjaardagskalender(month) {

        if (month == undefined) var month = '';
        new popup('addbirthday','&month='+month,'','9999');

}

function viewVerjaardagskalender(id,type) {
        if (id == undefined) var id = '';
        if (type == undefined) var type = '';
        new popup('addbirthday','&id='+id+'&type='+type,'','9999');

}

function reminderVerjaardagskalender(id) {
        if (id == undefined) var id = '';
        new popup('reminderbirthday','&ids='+id,'','9999');

}

function showSayingOfTheDay() {

        new popup('sayingoftheday','','','9999');

}

function manager(page) {
	if( checklogin() ) {
		page = (page === undefined || page > 3) ? '' : page;
	
	    var id = new popup('manager'+page,'','');
	    var container = document.getElementById(id.containerContent);
	    container.parentNode.style.width = '750px';
	    container.parentNode.style.left = '40%'; 
	}
}

/*	function viewInfoPage(cat_name, art_id)
	@param cat_name :: category name for this item (cannot be null)
	@param art_id :: the article id for this item, if null it will generate a overview
*/
function viewInfoPage(cat_name , art_id) {
	if( ( cat_name != 'adresboek' && cat_name != 'klantenprofiel' ) || checklogin() ) {
		var cat_nameCorrect = (cat_name == undefined ? '-' : cat_name);
		var art_idCorrect =(art_id == undefined ? '-' : art_id);
		var idForInfoPage = cat_nameCorrect+"|"+art_idCorrect;
        new popup('infopage','&id='+idForInfoPage,'');
	}
}

popups = new Array();
function popup(page,cmd,function_to_call,zindex) {

	this.page				= (page == undefined ? '' : page);
	this.cmd				= (cmd == undefined ? '' : cmd);
	this.function_to_call	= (function_to_call == undefined ? '' : function_to_call);
	this.container          = 'overlayBox';
	this.containerContent   = 'overlayBoxContent'; // default naam
	this.zindex             = (zindex == undefined ? '' : zindex);
	var popup = this.getContent();
	popups.push(this);

	return popup;

}

popup.prototype.getContent = function() {

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var body = document.getElementsByTagName('body')[0];

	if (this.page == 'addagenda') {
		var overlayBoxContainerId = this.page;
	}
	else if (this.page == 'viewagenda') {
		var overlayBoxContainerId = this.page;
	}
	else {
		var overlayBoxContainerId = this.page+'_'+this.cmd;
		var overlayBoxContainerId = this.page+'_'+count_popups;
	}
	this.containerContent = overlayBoxContainerId+'_content';

	if (document.getElementById('overlayBox') === null) {
		var newOverlayBox = document.createElement('div');
		newOverlayBox.setAttribute('id','overlayBox');
	}

	var newContainer = document.createElement('div');
	newContainer.setAttribute('id',overlayBoxContainerId);
	newContainer.setAttribute('class','overlayBoxContainer popup');
	newContainer.className = 'overlayBoxContainer popup';

	var newContainerContent = document.createElement('div');
	newContainerContent.setAttribute('id',this.containerContent);

	var newContainerBottom = document.createElement('div');
	newContainerBottom.setAttribute('id','overlayBoxBottom');

	newContainer.appendChild(newContainerContent);
	newContainer.appendChild(newContainerBottom);

	if (document.getElementById('overlayBox') === null) {
		body.insertBefore(newContainer,body.firstChild);
		body.insertBefore(newOverlayBox,body.firstChild);
	}
	else {
		body.insertBefore(newContainer,document.getElementById('container'));
	}

	objOverlay = document.getElementById('overlayBox');
	objContainer = document.getElementById(overlayBoxContainerId);
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'none';
	objContainer.style.display = 'block';

	if (this.page == 'greetingcard') {
		objContainer.style.top = '300px';
		objContainer.style.left = '42%';
	}

    if (this.zindex != '') {
        objContainer.style.zIndex = this.zindex;
    }

	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {

		selects[i].style.visibility = "hidden";
	}


	newContainer = null;
	newContainerContent = null;
	newContainerBottom = null;

	/**
	 * Nieuwe popup aangemaakt, dus 1 optellen
	 */
	count_popups++;

	//showLayer(this.container);
	this.function_to_call = 'shadowBorder()';
	this.process();

	return overlayBoxContainerId;

//	new Draggable( overlayBoxContainerId, { starteffect: null, endeffect: null, zindex: (9000+count_popups) } );
}

popup.prototype.process = function() {

	var content = new ajaxRequest('/'+this.page+'.php','cmd='+this.cmd);
	content.container = this.containerContent;
	content.form = 'userform';
	content.function_to_call = this.function_to_call;
	content.process();

}




function logout() {

        new logoutBox();

}

logoutBoxs = new Array();
function logoutBox() {

	this.container          = 'overlayBox';
	this.containerContent   = 'overlayBoxContent';
	this.getContent();
	logoutBoxs.push(this);

}

logoutBox.prototype.getContent = function() {

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var body = document.getElementsByTagName('body')[0];

	var newOverlayBox = document.createElement('div');
	newOverlayBox.setAttribute('id','overlayBox');

	var newContainer = document.createElement('div');
	newContainer.setAttribute('id','overlayBoxContainer');

	var newContainerContent = document.createElement('div');
	newContainerContent.setAttribute('id','overlayBoxContent');

	var newContainerBottom = document.createElement('div');
	newContainerBottom.setAttribute('id','overlayBoxBottom');

	newContainer.appendChild(newContainerContent);
	newContainer.appendChild(newContainerBottom);
	body.insertBefore(newContainer,body.firstChild);
	body.insertBefore(newOverlayBox,body.firstChild);



	objOverlay = document.getElementById('overlayBox');
	objContainer = document.getElementById('overlayBoxContainer');

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	objContainer.style.display = 'block';

	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {

		selects[i].style.visibility = "hidden";
	}

	newOverlayBox = null;
	newContainer = null;
	newContainerContent = null;
	newContainerBottom = null;

	showLayer(this.container);
	this.process();

}

logoutBox.prototype.process = function() {

	var content = new ajaxRequest('/login.php','cmd=logout');
	content.container = this.containerContent;
	content.form = 'registerform';
	content.function_to_call = 'logoutProcess()';
	content.process();

}



function openRegister() {

        new registerBox();

}

registerBoxs = new Array();
function registerBox(page) {

	if (page == undefined) this.page = 'register.php' 
	else this.page			= page;
	this.container          = 'overlayBox';
	this.containerContent   = 'overlayBoxContent';
	this.getContent();
	registerBoxs.push(this);

}

registerBox.prototype.getContent = function() {

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var body = document.getElementsByTagName('body')[0];

	var newOverlayBox = document.createElement('div');
	newOverlayBox.setAttribute('id','overlayBox');

	var newContainer = document.createElement('div');
	newContainer.setAttribute('id','overlayBoxContainer');
	newContainer.setAttribute('class','register');
	newContainer.className = 'register';

	var newContainerContent = document.createElement('div');
	newContainerContent.setAttribute('id','overlayBoxContent');

	var newContainerBottom = document.createElement('div');
	newContainerBottom.setAttribute('id','overlayBoxBottom');

	newContainer.appendChild(newContainerContent);
	newContainer.appendChild(newContainerBottom);
	body.insertBefore(newContainer,body.firstChild);
	body.insertBefore(newOverlayBox,body.firstChild);



	objOverlay = document.getElementById('overlayBox');
	objContainer = document.getElementById('overlayBoxContainer');
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	objContainer.style.display = 'block';

	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {

		selects[i].style.visibility = "hidden";
	}

	newOverlayBox = null;
	newContainer = null;
	newContainerContent = null;
	newContainerBottom = null;

	count_popups++;

	showLayer(this.container);
	this.process();

}

registerBox.prototype.process = function() {

	var content = new ajaxRequest('/'+this.page,'');
	content.container = this.containerContent;
	content.form = 'registerform';
	content.process();

}

function closeRegister() {

	var obj = document.getElementById('icons');
	destroyOverlay(obj);

}


function profielgegevens() {
	if( checklogin() ) {
		new registerBox('profiel.php');
	}
}

function kantoorplanner() {
	if( checklogin() ) {
		new fullscreen('','kantoorplanner.php');
	}
}

function ketenberichten() {
	new popup('ketenberichten');
}

function actieberichten() {
	new popup('actieberichten');
}

function evenementen() {
	new fullscreen('','evenementen.php');
}

function iframe(url,height,sso) {
	if( url.indexOf('learning') > -1 ) {
		var needlogin = true;
	}
	else {
		var needlogin = false;
	}
	
	if( ( needlogin && checklogin() ) || !needlogin ) {
		if( height == undefined ) var height = 500;
		if( sso == undefined ) var sso = 0;
		
		new fullscreen('','iframe.php?url='+url+'&height='+height+'&sso='+sso);
	}
}

function cockpit() {
	new fullscreen('','cockpit.php');
}

function adresboek(page,id) {

        if (page == 'undefined') var page = 'klanten';
        if (id == 'undefined') var id = '';
		new fullscreen(id,'adresboek_'+page+'.php');

}

function klantenprofiel(page) {

		if (document.getElementById('overlayFullscreen') !== null) {

			var overlayFullscreen = document.getElementById('overlayFullscreen');
			overlayFullscreen.innerHTML = '';
			overlayFullscreen.parentNode.removeChild(overlayFullscreen);

		}

        if (page == 'undefined') var page = 'zaken';
		new fullscreen('','klantenprofiel_'+page+'.php');

}

function verjaardagskalender(month) {
	if( checklogin() ) {
        if (document.getElementById('overlayFullscreen') != null) {
			closePopup('','overlayFullscreen');
        }
        if (month == undefined) var month = '';
		new fullscreen('&month='+month,'verjaardagskalender.php');
	}
}

function openFullscreen(id) {

        new fullscreen(id);

}

function vakantiechecklist() {

        new fullscreen('','vakantiechecklist.php');

}

function kalender0809() {

        new fullscreen('','kalender0809.php');

}

fullscreens = new Array();
function fullscreen(id,page) {

	this.page				= (page == undefined ? 'pagefullscreen.php' : page);
	this.id					= (id == undefined ? '' : id);
	this.container          = 'overlayBox';
	this.containerContent   = 'overlayFullscreen'; // default naam
	this.getContent();
	fullscreens.push(this);

}

fullscreen.prototype.getContent = function() {

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var body = document.getElementsByTagName('body')[0];

	if (document.getElementById('overlayBox') === null) {
		var newOverlayBox = document.createElement('div');
		newOverlayBox.setAttribute('id','overlayBox');
	}

	var newContainer = document.createElement('div');
	newContainer.setAttribute('id',this.containerContent);
	newContainer.setAttribute('class','overlayFullscreen');
	newContainer.className = 'overlayFullscreen';

	if (document.getElementById('overlayBox') === null) {
		body.insertBefore(newContainer,body.firstChild);
		body.insertBefore(newOverlayBox,body.firstChild);
	}
	else {
		body.insertBefore(newContainer,document.getElementById('container'));
	}

	objOverlay = document.getElementById('overlayBox');
	objContainer = document.getElementById(this.containerContent);
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'none';
	objContainer.style.display = 'block';

	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {

		selects[i].style.visibility = "hidden";
	}

	newOverlayBox = null;
	newContainer = null;

	/**
	 * Nieuwe popup aangemaakt, dus 1 optellen
	 */
	count_popups++;

	this.function_to_call = 'shadowBorder()';
	this.process();
//	new Draggable( overlayBoxContainerId, { starteffect: null, endeffect: null, zindex: (9000+count_popups) } );
}

fullscreen.prototype.process = function() {

	var content = new ajaxRequest('/'+this.page,'id='+this.id);
	content.container = this.containerContent;
	//content.form = 'userform';
	//content.function_to_call = this.function_to_call;
	content.process();

}

function resizingDiv( sDiv ) {
	oDiv = document.getElementById(sDiv);
	oDiv.resize = function( w, h ) {
		oDiv.style.height = h + 'px';
		oDiv.style.width = w + 'px';
	}
	return oDiv;
}

function resizeRegisterFormStep2(){
	resizingDiv('registerContainer').resize('700','800');
	resizingDiv('preRegisterContainer').resize('700','800');
	//resizingDiv('registerform').resize('700','1100');
	resizingDiv('overlayBoxContent').resize('700','800');
	resizingDiv('overlayBoxContainer').resize('700','1200');
	resizingDiv('registerContainer').resize('600','800');
}

function resizeRegisterFormStep3(){
	resizingDiv('registerRight').resize('680','400');
	resizingDiv('overlayBoxContent').resize('680','400');
	resizingDiv('overlayBoxContainer').resize('680','400');
}

function resizeProfileForm(){
	resizingDiv('registerRight').resize('680','700');
	resizingDiv('overlayBoxContent').resize('680','700');
	resizingDiv('overlayBoxContainer').resize('680','700');
}

function resizeProfileFormStep2(){
	resizingDiv('registerContainer').resize('700','900');
	resizingDiv('preRegisterContainer').resize('700','900');
	//resizingDiv('registerform').resize('700','1100');
	resizingDiv('overlayBoxContent').resize('700','900');
	resizingDiv('overlayBoxContainer').resize('700','1100');
	resizingDiv('registerContainer').resize('600','900');
}

/*	function createWenskaarten()
*/
function createGreetingCard() {
	if( checklogin() ) {
		//new popup('infopage','&id='+idForInfoPage,'');
		new popup('greetingcard');
	}
}

function createContactForm() {
	//new popup('infopage','&id='+idForInfoPage,'');
	new popup('contactForm');
}

/**
 * 
 */
function generatePreview( textArea ) {
	$('previewGreetingCardText').innerHTML = textArea.value;
}

/**
 * generatePreview through Ajax
 */

function generatePreviewAjax( selectTemplate ) {
	var doAjax = function () {
		var request = new ajaxRequest('/greetingcard.php','');
		request.container = $('previewGreetingCard');
		request.form = 'greetingCardForm';
		request.process();
	}
	doAjax();
}

function generatePreviewAjax( selectTemplate, text ) {
	var type = document.getElementById('type').value;
	var text = text.replace('\n','<br>');
	$('greetingcardpreviewframe').src = "ajax/greetingcardpreview.php?id=" + selectTemplate + '&text=' + text +'&type=' + type;
}

function doSearch() {

	var obj = document.getElementById('search');
	if (obj == undefined) var q = '';
	else var q = obj.value;

	new popup('search','&q='+q);

}

function printVerjaardagsKalender(){
	$('container').style.display = 'none';
	print( document.getElementById('verjaardagskalender').innerHTML );
	$('container').style.display = 'block';
}

function printKantoorplanner(){
	$('container').style.display = 'none';
	print( document.getElementById('kantoorplanner').innerHTML );
	$('container').style.display = 'block';
}

function printAdresboek(){
	$('container').style.display = 'none';
	print( document.getElementById('adresboek').innerHTML );
	$('container').style.display = 'block';
}

function printKlantenprofiel(){
	$('container').style.display = 'none';
	print( document.getElementById('adresboek').innerHTML );
	$('container').style.display = 'block';
}

function printVakantiechecklist(){
	var openprint = window.open('print-vakantiechecklist.php');
	openprint.document.close();
	openprint.focus();
	
	//$('container').style.display = 'none';
	//print( document.getElementById('vakantiechecklist').innerHTML );
	//$('container').style.display = 'block';
}

function kantoorkalendarMonthYear( sMonth, sYear ){
	if( sMonth && sYear ){
		new Ajax.Updater('overlayFullscreen', 'ajax/kantoorplanner.php?month='+sMonth+'&year='+sYear);
	}else{
		new Ajax.Updater('overlayFullscreen', 'ajax/kantoorplanner.php' );
	}
}

function zooversearch() {

        new popup('zooversearch','','');

}