// Event attacher
// ----------------------------------------------

if (!document.attachEvent){
	document.events = [];
	document.attachEvent = function(type, reference) {
	    var evt = this.events;
	    if(!evt[type]) evt[type] = [];
	    evt[type][evt[type].length] = reference;

	    document[type] = function(e) { 
	        document.executeEvents(type, e);        
	    }
	}    

	document.executeEvents = function(type, e) {
	    for(var i in this.events[type]) {
	        this.events[type][i](e);
	    }    
	}
	window.onunload = function() { document.executeEvents('onunload'); }
	window.onload = function() { document.executeEvents('onload'); }
	window.onresize = function() { document.executeEvents('onresize'); }
}

//var OP   = (navigator.userAgent.indexOf("Opera") != -1);
//var IE   = (navigator.userAgent.indexOf("MSIE") != -1) && !OP;

var xmlhttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xmlhttp = false;
  }
}
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
  xmlhttp = new XMLHttpRequest();
}

function getElementValue(){
	
	var values = new Array();
	var metas = document.getElementsByTagName("meta");
	for (var i = metas.length; --i >= 0;) {
		var meta = metas[i];
		if (/content-type/i.test(meta.httpEquiv)) {
			r = /^text\/html; *charset=(.*)$/i.exec(meta.content);
			values['charset'] = r[1];
		} else if(/sessionlifetime/i.test(meta.httpEquiv)){
			values['sessionlifetime'] = meta.content;
		}	else if ((/keywords/i.test(meta.name)) || (/keywords/i.test(meta.id))) {
			values['keywords'] = meta.content;
		}	else if ((/description/i.test(meta.name)) || (/description/i.test(meta.id))) {
			values['description'] = meta.content;
		} else if(/page_uri/i.test(meta.httpEquiv)){
			values['page_uri'] = meta.content;
		}
	}
	var base = document.getElementsByTagName("base");
	if (base[0].href){
		values['base'] = base[0].href;
	}
	var title = document.getElementsByTagName("title")[0];
	title = title ? title.innerHTML : '';
	values['title'] = title;
	return values;
}


function popUpEditInPlace(url){
	// open popup voor edit van select
	window.open(url, 'EditInPlace', 'width=800,height=650,resizable=yes,scrollbars=yes,toolbar=no,top='+((screen.availHeight/2)-(600/2))+',left='+((screen.availWidth/2)-(620/2))+'');
}

function newItem(module){
	popUpEditInPlace(getElementValue()['base'] + 'webbeheer/modules/' + module + '/?viewmode=popup');
}

function editItem(module, id){
	popUpEditInPlace(getElementValue()['base'] + 'webbeheer/modules/' + module + '/?viewmode=popup&id=' + id);
}

function deleteItem(module, id){
	if (confirm('Weet u zeker dat u dit item wilt verwijderen?')){
	
		xmlhttp.open("GET", getElementValue()['base'] + 'webbeheer/modules/' + module + '/?viewmode=popup&id=' + id + '&delete=true', true); //method, target, async (true)
  		xmlhttp.onreadystatechange = function() {
	    	if (xmlhttp.readyState==4){	    		
	    		var answerTxt = xmlhttp.responseText;
	      		if (answerTxt != ""){
	      			getOverview();
	      		}
	    	}
	    }
	    xmlhttp.send(null);			
	}
}


function loguit(){
	xmlhttp.open("GET", getElementValue()['base'] + 'webbeheer/loguit.php', true); //method, target, async (true)
  	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState==4){	    		
	    	var answerTxt = xmlhttp.responseText;
	      	if (answerTxt != ""){
	      		getOverview();
	      	}
	    }
	}
	xmlhttp.send(null);	
}



function getOverview(){
	document.location.href = document.location.href;
	return true;
}

/*******************************************
Drie functie om EIP aan/uit te zetten
********************************************/
function initEip(){
    sCookie = readCookie('eiptoggle');
    if(sCookie != null && sCookie == 'uit'){
         switchEip('none');
         document.getElementById('eipToggleLabel').innerHTML = 'aanzetten';
    }
}

document.attachEvent("onload", initEip); // na het inladen van de pagina wordt gecontroleerd of EIP uitgezet moet worden

function toggleEip(){

    // als er een cookie is dan beteken dat dat de gebruiker EIP heeft uitgezet
    sCookie = readCookie('eiptoggle');
	
    if(sCookie != null && sCookie == 'uit'){
        sSetState = 'block'; // aanzetten
        document.getElementById('eipToggleLabel').innerHTML = 'uitzetten';
        eraseCookie('eiptoggle'); // cookie verwijderen
    } else {
        sSetState = 'none';  // uitzetten
        document.getElementById('eipToggleLabel').innerHTML = 'aanzetten';
        createCookie('eiptoggle','uit',100);  // cookie maken waarin is opgeslagen dat de EIP is uitgeschakeld
    }
    
    switchEip(sSetState);
    
}

// alle edit in place shizzle tonen/hiden 
function switchEip(sSetState){
    
    var allPageTags = new Array();  

    //Populate the array with all the page tags  
    var allPageTags = document.getElementsByTagName("span");  
    
    //Cycle through the tags using a for loop  
    for (i=0; i<allPageTags.length; i++) {  
        //Pick out the tags with our class name --> <span class="editinplace">     
        if (allPageTags[i].className=='editinplace') {  
            //Manipulate this in whatever way you want  
            allPageTags[i].style.display=sSetState;  
        }
    }
}

/*******************************************
Drie functie om cookies te gebruiken binnen javascript
********************************************/
function createCookie(name,value,days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}