// JavaScript Document
function sendBoxState(idModule,moduleState){ //lancement de la fonction
	if (window.XMLHttpRequest) { //test si le navigateur est un autre que internet explorrer
		xhr = new XMLHttpRequest();	//deffinit l'object xhr
	}else if (window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");	//deffinit l'object xhr
    }
	liens = urlBase+'localInc/misc/setBoxState.php?PHPSESSID='+sessToken+'&idModule='+idModule+'&state='+moduleState;
	//alert(liens);
	//on appelle le fichier reponses.php
	xhr.open("GET", liens, true);
	xhr.send(null);
}

function toggleModule(caller,box){
	boxRef = document.getElementById(box);
	if(boxRef.className == 'moduleContentOpen'){
		caller.className = 'moduleTitleClosed';
		boxRef.className = 'moduleContentClosed';
		sendBoxState(box,'closed');
	}else{
		caller.className = 'moduleTitleOpen';
		boxRef.className = 'moduleContentOpen';
		sendBoxState(box,'open');
	}
}

function toggleLayer(layerId){
	var layerRef = document.getElementById(layerId);
	if(layerRef.style.display == 'none'){
		layerRef.style.display = 'block';
	}else{
		layerRef.style.display = 'none';
	}
}

/*
IMAGE SWAPS
*/

function swapArticleListImage(id,source){
	document.getElementById(id).src = source;	
}

function swapDetailImg(idNew,idDest){
	document.getElementById(idDest).src = document.getElementById(idNew).src.toString();
}

/*
CHAR STUFF
*/
function utf8_encode ( argString ) {
    // Encodes an ISO-8859-1 string to UTF-8  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/utf8_encode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}

