var MESSAGE_LEVEL_INFO = 0;
var MESSAGE_LEVEL_ATTENTION = 1;
var MESSAGE_LEVEL_ERROR = 2;
var MESSAGE_LEVEL_CONFIRM = 3;
var animationIndex = 1;
var callbackFunction;
var callbackCancelFunction;
var homepageTimer;
var myHomeAnimationTimerLength = 10000;
var popupScrollPosition = 0;

var sliderArray = new Array();
var sliderValueArray = new Array();
sliderValueArray[1] = 0;
sliderValueArray[2] = 63;
sliderValueArray[3] = 147;
sliderValueArray[4] = 231;
var sliderPxArray = new Array();
sliderPxArray[0] = 1;
sliderPxArray[63] = 2;
sliderPxArray[147] = 3;
sliderPxArray[231] = 4;

//21, 42, 63, 84, 105, 125
var sliderImpactValueArray = new Array();
sliderImpactValueArray[-3] = 84;
sliderImpactValueArray[-2] = 42;
sliderImpactValueArray[-1] = 63;
sliderImpactValueArray[0] = 84;
sliderImpactValueArray[1] = 105;
sliderImpactValueArray[2] = 125;
var sliderImpactPxArray = new Array();
sliderImpactPxArray[42] = -2;
sliderImpactPxArray[63] = -1;
sliderImpactPxArray[84] = 0;
sliderImpactPxArray[105] = 1;
sliderImpactPxArray[125] = 2;

var sliderTackledValueArray = new Array();
sliderTackledValueArray[0] = 9;
sliderTackledValueArray[1] = 42;
sliderTackledValueArray[2] = 76;
var sliderTackledPxArray = new Array();
sliderTackledPxArray[9] = 0;
sliderTackledPxArray[42] = 1;
sliderTackledPxArray[76] = 2;

String.prototype.br2nl = function(){
    return this.split('<br>\n').join('\n')
}
String.prototype.nl2br = function(){
    return (this + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br/>$2')
}

document.observe("dom:loaded", function() {
    displayTopAnchor();
});

Event.observe(document.onresize ? document : window, "resize", function() {
    displayTopAnchor();
});

function addslashes(str) {
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\0/g,'\\0');
    return str;
}
function stripslashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\0/g,'\0');
    str=str.replace(/\\\\/g,'\\');
    return str;
}
/*
function br2ln(s){
    return s.replace('/(<br/>)|(<br>)/','\n');
}
*/
function getScrollTop() {
    var scrollOffset;
    scrollOffset = document.viewport.getScrollOffsets();
    return scrollOffset.top;
}
function toggleSlide(divId){
	
    if ($(divId).visible()){
        Effect.BlindUp(divId);
    } else {
        Effect.BlindDown(divId);
    }
    return false;
}

function login() {
    var username = $('username').value;
    var userpassword = $('userpassword').value;
    var missingFields = 0;
    var errorMessage = '';

    $('username').className = '';
    $('userpassword').className = '';
	
    if(username=='') {
        missingFields++;
        errorMessage += '\n - '+t('Login');
        $('username').className = 'attention';
    }
    if(userpassword=='') {
        missingFields++;
        errorMessage += '\n - Password';
        $('userpassword').className = 'attention';
    }
    if(missingFields==1) {
        displayAlert('The following field is missing:' + errorMessage);
    } else if(missingFields>1){
        displayAlert('The following fields are missing:' + errorMessage);
    } else {
        new Ajax.Request('/php/login.php', {
            method: 'post',
            parameters: 'username='+encodeURIComponent(username)+'&userpassword='+encodeURIComponent(userpassword),
            onSuccess: function(transport) {
                if (transport.responseText == 1) {
                    document.location.reload(true);
                } else {
                    $('username').className = 'error';
                    $('userpassword').className = 'error';
                    displayAlert('Incorrect username or password',MESSAGE_LEVEL_ERROR);
                }
            }
        });
    }
	
}

function logout() {
    new Ajax.Request('/php/logout.php', {
        method: 'get',
        onSuccess: function() {
            document.location.reload(true);
        }
    });
}

/**
 * 
 * @param messageContent
 * @param messageLevel (0 : MESSAGE_LEVEL_INFO, 1 : MESSAGE_LEVEL_ATTENTION, 2 : MESSAGE_LEVEL_ERROR, 3 : MESSAGE_LEVEL_CONFIRM)
 * @param myCallbackFunction Callback function
 * @return
 */
function displayAlert(messageContent, messageLevel, myCallbackFunction) {
    if (messageLevel == undefined) messageLevel = MESSAGE_LEVEL_ATTENTION;
    callbackFunction = myCallbackFunction;
    if(arguments.length == 4) {
        callbackCancelFunction = arguments[3];
    } else {
        callbackCancelFunction = false;
    }

    var divId = '';
    var divContent = '';
    
    switch(messageLevel) {
        case MESSAGE_LEVEL_INFO:
            divId = 'popup-txt';
            divContent = '<div class="popupDivClass" id="popup-alert-bg"><div class="button-cancel" onclick="hidePopup()"></div><div id="'+divId+'">'+messageContent.nl2br()+'</div></div>';
            break;
        case MESSAGE_LEVEL_ATTENTION:
            divId = 'popup-txt-alert';
            divContent = '<div class="popupDivClass" id="popup-alert-bg"><div class="button-cancel" onclick="hidePopup()"></div><div id="'+divId+'">'+messageContent.nl2br()+'</div></div>';
            break;
        case MESSAGE_LEVEL_ERROR:
            divId = 'popup-txt-error';
            divContent = '<div class="popupDivClass" id="popup-alert-bg"><div class="button-cancel" onclick="hidePopup()"></div><div id="'+divId+'">'+messageContent.nl2br()+'</div></div>';
            break;
        case MESSAGE_LEVEL_CONFIRM:
            
            divId = 'popup-txt-alert';
            divContent = '<div class="popupDivClass" id="popup-alert-bg"><div class="button-cancel" onclick="hidePopup()"></div><div id="'+divId+'">'+messageContent.nl2br()+'<br /><table border="0" align="center" cellpadding="0" cellspacing="10"><tr><td width="100"><div class="button" onclick="hidePopup();';
            if(callbackCancelFunction) divContent += 'callbackCancelFunction.call();';
            divContent += '"><br />'+t('NO')+'<br /><br /></div></td><td width="100"><div class="button" onclick="hidePopup();callbackFunction.call();"><br />'+t('YES')+'<br /><br /></div></td></tr></table><br /></div></div>';
            
            break;
    }
    if(!$('blackScreen') || !$('blackScreen').visible()) {
        showBlackScreen();
        window.scrollTo(0, 0);
        Effect.BlindDown('blackScreen', {
            duration: 0.1,
            afterFinish:function (){
                $('rfsc-page').insert({
                    bottom: divContent
                });
            }
        });
    } else {
        popupScrollPosition = window.scrollY;
        window.scrollTo(0, 0);
        if($('rfsc-popup') && $('rfsc-popup').visible()) {
            $('rfsc-popup').hide();
        }
        $('rfsc-page').insert({
            bottom: divContent
        });        
    }

    return true;
}

function toggleDiv(divId){

    if ($(divId).visible()){			
        Effect.BlindUp(divId,{
            duration:0.2
        });
   
    } else {
        Effect.BlindDown(divId,{
            duration:0.2
        });
    }

}

function toggleIllustrationSection(elt){
    elt = $(elt);
    var iconDiv = elt.down('div');
    var panelDiv = elt.next('div');
    if (panelDiv.visible()){
        iconDiv.innerHTML = '&larr;';
        Effect.BlindUp(panelDiv,{
            duration:0.2
        });

    } else {
        iconDiv.innerHTML = '&darr;';
        Effect.BlindDown(panelDiv,{
            duration:0.2
        });
    }

}

function toggleMoreInfosDiv(){
    divId = 'TOOL-stop-on-txt';
    var visible = 1;
    if ($(divId).visible()){
        Effect.BlindUp(divId,{
            duration:0.2
        });
        $('moreInfo').show();
        visible = 0;

    } else {
        Effect.BlindDown(divId,{
            duration:0.2
        });
    }
    var myUrl = document.location.href;
    if(myUrl.indexOf('?') != -1) {
        myUrl = myUrl.substring(0,myUrl.indexOf('?'));
    }
    
    new Ajax.Request('/php/rfsc-ajax.php', {
        method: 'post',
        parameters: 'rfsc_action=saveMoreInfosStatus&visible='+visible+'&url='+encodeURIComponent(myUrl),
        onSuccess: function() {
        }
    });
}

function toggleCreateNewProjectDiv(){

    if ($('GetStarted').visible()){
        if($('GS-scale').visible()) {
            Effect.BlindUp('GS-scale',{
                duration:0.2
            });
            Effect.BlindUp('GS-process',{
                duration:0.2
            });
        } else {
            Effect.BlindUp('GetStarted',{
                duration:0.2
            });
        }

    } else {        
        $$('.GS-item').each(function(elt){
            elt.hide();
        });
        $('AddNewProject').show();
        Effect.BlindDown('GetStarted',{
            duration:0.2
        });
    }
}

function toggleGetStartedDiv(){
    if ($('GetStarted').visible()){
        if(!$('GS-scale').visible()) {
            Effect.BlindDown('GS-scale',{
                duration:0.2
            });
            Effect.BlindDown('GS-process',{
                duration:0.2
            });
        } else {
            Effect.BlindUp('GetStarted',{
                duration:0.2
            });
        }

    } else {

        $$('.GS-item').each(function(elt){
            elt.show();
        });
        Effect.BlindDown('GetStarted',{
            duration:0.2
        });
    }
}

function hideProjectDiv(){
    if ($('GetStarted').visible()){
        Effect.BlindUp('GetStarted',{
            duration:0.2
        });

    }
}

function selectProject(project_id, gotoUrl){
    
    new Ajax.Request('/php/rfsc-ajax.php', {
        parameters: 'rfsc_action=selectProject&project_id='+project_id,
        method: 'post',
        onSuccess: function() {
            if(gotoUrl) {
                document.location.href=gotoUrl;
            }
        }
    });
}

function saveProject(){
    var project_name = '';
    if($('GS-scale').visible()) {
        if($('which-scale-city-wide').checked) {
            project_name += 'city-wide';
        } else if($('which-scale-deprived-neighbourhood').checked) {
            project_name += 'deprived-neighbourhood';
        }
        if(project_name != '') {
            project_name += '/';
        }
        if($('which-process-integrated').checked) {
            project_name += 'integrated';
        } else if($('which-process-sectorial').checked) {
            project_name += 'sectorial';
        }
        if(project_name != '') {
            project_name += ' - ';
        }

    }
    project_name += $F('NewProjectName');

    new Ajax.Request('/php/rfsc-ajax.php', {
        parameters: 'rfsc_action=saveProject&project_name='+encodeURIComponent(project_name),
        method: 'post',
        onSuccess: function() {
            hideProjectDiv();
            document.location.reload();
        }
    });
}


function deleteProject(project_id){
    var projectName = $('project_name-'+project_id).innerHTML;
    displayAlert('Do you really want to delete your project : "'+projectName+'" ?', MESSAGE_LEVEL_CONFIRM, function(){
        new Ajax.Request('/php/rfsc-ajax.php', {
            parameters: 'rfsc_action=deleteProject&project_id='+project_id,
            method: 'post',
            onSuccess: function() {
                document.location.reload();
            }
        });
    });
}

function hidePopup(){
    if(document.onresize) {
        Event.stopObserving(document, "resize");
    } else {
        Event.stopObserving(window, "resize");
    }
    if($('rfsc-popup') && !$('rfsc-popup').visible() && $('popup-alert-bg') && $('popup-alert-bg').visible()) {
        window.scrollTo(0, popupScrollPosition);
        $('rfsc-popup').show();
        $('popup-alert-bg').hide();
    } else {
        Effect.BlindUp('blackScreen', {
            duration: 0.1,
            afterFinish:function (){
                $('blackScreen').remove();
                $$('.popupDivClass').each(function(elt){
                    elt.remove();
                });
            }
        });
    }

}

function showBlackScreen() {
    if($('blackScreen')) $('blackScreen').show();
    else {
        $('rfsc-page').insert({
            before: '<div id="blackScreen" style="display: none;" onclick="hidePopup()"></div>'
        });
    }
    $('blackScreen').style.width = (getTotalWidth())+'px';
    $('blackScreen').style.height = (getTotalHeight())+'px';
    Event.observe(document.onresize ? document : window, "resize", function() {
        $('blackScreen').style.width = (getTotalWidth())+'px';
        $('blackScreen').style.height = (getTotalHeight())+'px';
    });
}

function getTotalHeight() {
    // firefox is ok
    var height = document.documentElement.scrollHeight;
	
    // now IE 7 + Opera with "min window"
    if(document.documentElement.clientHeight > height ) {
        height  = document.documentElement.clientHeight;
    }
    // last for safari
    if(document.body.scrollHeight > height) {
        height = document.body.scrollHeight;
    }
    return height;
}

function getTotalWidth() {
    // firefox is ok
    var width = document.documentElement.scrollWidth;
	
    // now IE 7 + Opera with "min window"
    if(document.documentElement.clientWidth > width ) {
        width  = document.documentElement.clientWidth;
    }
    // last for safari
    if(document.body.scrollWidth > width) {
        width = document.body.scrollWidth;
    }
    return width;
}

function getParameter( parameterName ) {
    var queryString = window.location.search.substring(1);
    //alert(queryString);
    //if (queryString.length==0) {return "null";}
    var parameters = new Array();
    parameters = queryString.split('&');
    for(var i = 0; i < parameters.length; i++) {
        //alert(parameters[i]);
        //alert(parameters[i].indexOf(parameterName));
        if (parameters[i].indexOf(parameterName)>=0) {
            //alert(parameters[i]);
            var parameterValue = new Array();
            parameterValue = parameters[i].split('=');
            return parameterValue[1];
        }
    }
    return null;
}


function openQuestion(tool_id,question_id) {

    $$('#level_one_question_'+question_id+' .item-with-open-function').each(function(elt){
        $(elt).writeAttribute('onClick','closeQuestion('+tool_id+','+question_id+')');
    });
    $('level_one_question_'+question_id).next('.level_two_questions').show();

    $('level_one_question_'+question_id).className = 'T00-tool-qn1p';
    $('level_one_question_'+question_id).down('.T00-display-on').className = 'T00-display-off';

    if(tool_id==1){
        $('level_one_question_'+question_id).down('td').next(2).innerHTML = '<a href="#" onclick="selectLevelTwoQuestions('+question_id+', true);return false;">'+t('all')+'</a> - <a href="#" onclick="selectLevelTwoQuestions('+question_id+', false);return false;">'+t('Clear')+'</a>';
    }
}

function closeQuestion(tool_id,question_id) {

    $$('#level_one_question_'+question_id+' .item-with-open-function').each(function(elt){
        $(elt).writeAttribute('onClick','openQuestion('+tool_id+','+question_id+')');
    });
    $('level_one_question_'+question_id).next('.level_two_questions').hide();

    $('level_one_question_'+question_id).className = 'T00-tool-qn1';
    $('level_one_question_'+question_id).down('.T00-display-off').className = 'T00-display-on';

    if(tool_id==1){
        $('level_one_question_'+question_id).down('td').next(2).innerHTML = '<a href="#" onclick="selectLevelTwoQuestions('+question_id+', true);return false;">'+t('all')+'</a> - <a href="#" onclick="selectLevelTwoQuestions('+question_id+', false);return false;">'+t('Clear')+'</a>';
        var hasSelectedQuestions = false;
        $('level_one_question_'+question_id).next('.level_two_questions').select('.T02-bt-select-on').each(function(){
            hasSelectedQuestions = true;
        });

        if(hasSelectedQuestions) $('level_one_question_'+question_id).down('td').next(2).innerHTML = '<div class="T02-q1-selected"></div>';
        else $('level_one_question_'+question_id).down('td').next(2).innerHTML = '';
    }
}

function selectQuestion(question_id) {
    if(arguments.length>1) forceValue = true;
    else forceValue = false;
    if(forceValue){
        if (arguments[1]) {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-on item-with-checkbox';
        } else {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-off item-with-checkbox';
        }
    } else {
        if ($('level_two_question_checkbox_'+question_id).className == 'T02-bt-select-off item-with-checkbox') {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-on item-with-checkbox';
        } else {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-off item-with-checkbox';
        }
    }
    saveQuestions();
}

function selectQuestionWithoutSaving(question_id) {
    if(arguments.length>1) forceValue = true;
    else forceValue = false;
    if(forceValue){
        if (arguments[1]) {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-on item-with-checkbox';
        } else {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-off item-with-checkbox';
        }
    } else {
        if ($('level_two_question_checkbox_'+question_id).className == 'T02-bt-select-off item-with-checkbox') {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-on item-with-checkbox';
        } else {
            $('level_two_question_checkbox_'+question_id).className = 'T02-bt-select-off item-with-checkbox';
        }
    }
}

function selectLevelTwoQuestions(question_id, doSelect) {

    if(doSelect){
        $('level_one_question_'+question_id).next('.level_two_questions').select('.T02-bt-select-off').each(function(elt){
            elt.className = 'T02-bt-select-on item-with-checkbox';
        });
    } else {
        $('level_one_question_'+question_id).next('.level_two_questions').select('.T02-bt-select-on').each(function(elt){
            elt.className = 'T02-bt-select-off item-with-checkbox';
        });
    }
    saveQuestions();
}
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen){
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

function EcrireCookie(nom, valeur){
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

function saveQuestions(){
    var request = 'rfsc_action=saveQuestions';
    $$('.T02-bt-select-on').each(function(elt){
        request += '&level_two_questions[]='+elt.id;
    });
    new Ajax.Request('/php/tool-develop-your-sustainable-strategy-1.php', {
        parameters: request,
        method: 'post',
        onSuccess: function(transport) {
            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool1-qn2Array', transport.responseText, myDate);
            }
        }
    });
}

function editComment(question_id){
    if($('level_two_question_checkbox_'+question_id).className == 'T02-bt-select-off'){
        selectQuestion(question_id, true);
    }
    var defaultComment = t('here your comment');
    var myElement = $('level_two_question_'+question_id).down('.T00-txt10');
    var myComment = myElement.down('a').innerHTML;
    if (myComment=='Add comment') myComment = defaultComment;

    $(myElement).insert({
        before : '<div class="T02-q2-comment"><div class="T02-ic-comment"></div><textarea id="question_comment_'+question_id+'" name="T01-comment" id="T01-comment" onfocus="onFocusComment(\'question_comment_'+question_id+'\')" onblur="onBlurComment(\'question_comment_'+question_id+'\')">'+myComment.br2nl()+'</textarea><div class="T00-txt10" style=" padding-top:5px;"><a href="#" onclick="cancelComment('+question_id+');return false;">'+t('cancel')+'</a> <a href="#" onclick="saveComment('+question_id+');return false;">&rarr; '+t('Save')+'</a></div></div>'
    });
    $(myElement).remove();
}

function editImpactComment(question_id){

    var defaultComment = t('here your comment');
    var myElement = $('level_one_question_'+question_id).down('.T00-txt10');
    var myComment = myElement.down('a').innerHTML;
    if (myComment==t('Add comment about the level of impact')) myComment = defaultComment;

    $(myElement).insert({
        before : '<div class="T02-q2-comment"><div class="T02-ic-comment"></div><textarea id="question_comment_'+question_id+'" name="T01-comment" id="T01-comment" onfocus="onFocusComment(\'question_comment_'+question_id+'\')" onblur="onBlurComment(\'question_comment_'+question_id+'\')">'+myComment.br2nl()+'</textarea><div class="T00-txt10" style=" padding-top:5px;"><a href="#" onclick="cancelImpactComment('+question_id+');return false;">'+t('cancel')+'</a> <a href="#" onclick="saveImpactComment('+question_id+');return false;">&rarr; '+t('Save')+'</a></div></div>'
    });
    $(myElement).remove();
}

function saveImpactComment(question_id){
    var myCommentValue = '';

    if($F('question_comment_'+question_id)!=t('here your comment')) myCommentValue = $F('question_comment_'+question_id);


    new Ajax.Request('/php/tool-check-the-integrated-approach-of-your-strategy-3.php', {
        parameters: 'rfsc_action=saveComment&question_id='+question_id+'&question_comment='+encodeURIComponent(myCommentValue),
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('level_one_question_'+question_id).down('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = t('Add comment about the level of impact');
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editImpactComment('+question_id+');return false;">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}

function cancelImpactComment(question_id){
    new Ajax.Request('/php/tool-check-the-integrated-approach-of-your-strategy-3.php', {
        parameters: 'rfsc_action=getComment&question_id='+question_id,
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('level_one_question_'+question_id).down('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = t('Add comment about the level of impact');
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editImpactComment('+question_id+');return false;">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}


function editPriorityComment(question_id){

    var defaultComment = t('here your comment');
    var myElement = $('level_one_question_'+question_id).down('.T00-txt10');
    var myComment = myElement.down('a').innerHTML;
    if (myComment==t('Add your comments. Explain your choice')) myComment = defaultComment;

    $(myElement).insert({
        before : '<div class="T02-q2-comment"><div class="T02-ic-comment"></div><textarea id="question_comment_'+question_id+'" name="T01-comment" id="T01-comment" onfocus="onFocusComment(\'question_comment_'+question_id+'\')" onblur="onBlurComment(\'question_comment_'+question_id+'\')">'+myComment.br2nl()+'</textarea><div class="T00-txt10" style=" padding-top:5px;"><a href="#" onclick="cancelPriorityComment('+question_id+');return false;">'+t('cancel')+'</a> <a href="#" onclick="savePriorityComment('+question_id+');return false;">&rarr; '+t('Save')+'</a></div></div>'
    });
    $(myElement).remove();
}

function savePriorityComment(question_id){
    var myCommentValue = '';

    if($F('question_comment_'+question_id)!=t('here your comment')) myCommentValue = $F('question_comment_'+question_id);
    
    new Ajax.Request('/php/tool-check-the-integrated-approach-of-your-strategy-1.php', {
        parameters: 'rfsc_action=saveComment&question_id='+question_id+'&question_comment='+encodeURIComponent(myCommentValue),
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('level_one_question_'+question_id).down('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = t('Add your comments. Explain your choice');
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editPriorityComment('+question_id+');return false;">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}

function cancelPriorityComment(question_id){
    new Ajax.Request('/php/tool-check-the-integrated-approach-of-your-strategy-1.php', {
        parameters: 'rfsc_action=getComment&question_id='+question_id,
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('level_one_question_'+question_id).down('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = t('Add your comments. Explain your choice');
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editPriorityComment('+question_id+');return false;">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}


function editDeprivedComment(question_id){

    var defaultComment = t('here your comment');
    var myElement = $('level_two_question_'+question_id).down('.T00-txt10');
    var myComment = myElement.down('a').innerHTML;
    if (myComment=='Add comment') myComment = defaultComment;

    $(myElement).insert({
        before : '<div class="T02-q2-comment"><div class="T02-ic-comment"></div><textarea id="question_comment_'+question_id+'" name="T01-comment" id="T01-comment" onfocus="onFocusComment(\'question_comment_'+question_id+'\')" onblur="onBlurComment(\'question_comment_'+question_id+'\')">'+myComment.br2nl()+'</textarea><div class="T00-txt10" style=" padding-top:5px;"><a href="#" onclick="cancelDeprivedComment('+question_id+');return false;">'+t('cancel')+'</a> <a href="#" onclick="saveDeprivedComment('+question_id+');return false;">&rarr; '+t('Save')+'</a></div></div>'
    });
    $(myElement).remove();
}

function saveDeprivedComment(question_id){
    var myCommentValue = '';

    if($F('question_comment_'+question_id)!=t('here your comment')) myCommentValue = $F('question_comment_'+question_id);


    new Ajax.Request('/php/tool-focus-on-deprived-neighbourhoods-1.php', {
        parameters: 'rfsc_action=saveComment&question_id='+question_id+'&question_comment='+encodeURIComponent(myCommentValue),
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('level_two_question_'+question_id).down('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = 'Add comment';
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editDeprivedComment('+question_id+');return false;">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}

function cancelDeprivedComment(question_id){
    new Ajax.Request('/php/tool-focus-on-deprived-neighbourhoods-1.php', {
        parameters: 'rfsc_action=getComment&question_id='+question_id,
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('level_two_question_'+question_id).down('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = 'Add comment';
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editDeprivedComment('+question_id+');return false;">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}

function onFocusComment(comment_id){
    if($F(comment_id)==t('here your comment')){
        $(comment_id).value = '';
    }
}

function onBlurComment(comment_id){
    if($F(comment_id)==''){
        $(comment_id).value = t('here your comment');
    }
}

function saveComment(question_id){

    if ($('level_two_question_checkbox_'+question_id).className == 'T02-bt-select-off item-with-checkbox') {
        selectQuestion(question_id);
    }

    var myCommentValue = '';
    if($('level_two_question_checkbox_'+question_id).className == 'T02-bt-select-off'){
        selectQuestion(question_id, true);
    }
    if($F('question_comment_'+question_id)!=t('here your comment')) myCommentValue = $F('question_comment_'+question_id);

    
    new Ajax.Request('/php/tool-develop-your-sustainable-strategy-1.php', {
        parameters: 'rfsc_action=saveComment&question_id='+question_id+'&question_comment='+encodeURIComponent(myCommentValue),
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('question_comment_'+question_id).up('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = 'Add comment';
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editComment('+question_id+')">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}


function cancelComment(question_id){
    new Ajax.Request('/php/tool-develop-your-sustainable-strategy-1.php', {
        parameters: 'rfsc_action=getComment&question_id='+question_id,
        method: 'post',
        onSuccess: function(transport) {
            var myComment = '';
            var myElement = $('question_comment_'+question_id).up('.T02-q2-comment');
            if(transport.responseText!='') myComment = transport.responseText;
            else  myComment = 'Add comment';
            $(myElement).insert({
                before : '<div class="T00-txt10"><a href="#" onclick="editComment('+question_id+')">'+myComment+'</a></div>'
            });
            $(myElement).remove();
        }
    });
}

function saveCommitment(question_id, sliderValue){
    var myCookie = LireCookie('tool1-qn2Array');
    new Ajax.Request('/php/tool-develop-your-sustainable-strategy-2.php', {
        parameters: 'rfsc_action=saveCommitment&question_id='+question_id+'&question_commitment='+sliderPxArray[sliderValue]+'&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {
            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool1-qn2Array', transport.responseText, myDate);
            }
            $('question_slide_'+question_id).up('div').className = 'slideTrack T02b-slider-bg'+(sliderPxArray[sliderValue]);
        }
    });
}

function saveImportance(question_id, sliderValue){
    var myCookie = LireCookie('tool2-qn1Array');
    new Ajax.Request('/php/tool-check-the-integrated-approach-of-your-strategy-1.php', {
        parameters: 'rfsc_action=saveImportance&question_id='+question_id+'&question_importance='+sliderPxArray[sliderValue]+'&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {
            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool2-qn1Array', transport.responseText, myDate);
            }
            $('question_slide_'+question_id).up('div').className = 'slideTrack T03-slider-bg'+(sliderPxArray[sliderValue]);
        }
    });
}

function saveImpact(question_id, sliderValue){
    var myCookie = LireCookie('tool2-qn1ImpactArray');
    new Ajax.Request('/php/tool-check-the-integrated-approach-of-your-strategy-3.php', {
        parameters: 'rfsc_action=saveImpact&question_id='+question_id+'&question_impact='+sliderImpactPxArray[sliderValue]+'&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {
            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool2-qn1ImpactArray', transport.responseText, myDate);
            }
            $('question_slide_'+question_id).up('div').className = 'slideTrack T03a-slider-bg'+(sliderImpactPxArray[sliderValue]);
        }
    });
}

function saveTackled(question_id, sliderValue){
    var myCookie = LireCookie('tool3-qn2Array');
    new Ajax.Request('/php/tool-focus-on-deprived-neighbourhoods-1.php', {
        parameters: 'rfsc_action=saveTackled&question_id='+question_id+'&question_tackled='+sliderTackledPxArray[sliderValue]+'&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {

            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool3-qn2Array', transport.responseText, myDate);
            }

            $('question_slide_'+question_id).up('div').className = 'slideTrack T05a-slider-bg'+(3-sliderTackledPxArray[sliderValue]);
        }
    });
}

function checkQuestion(question_id){
    var checked = ($('question_id_'+question_id).innerHTML != '<div class="T01-td-ticked"></div>');
    new Ajax.Request('/php/characterize-your-city.php', {
        parameters: 'rfsc_action=checkQuestion&question_id='+question_id+'&checked='+checked,
        method: 'post',
        onSuccess: function(transport) {
            if(checked) {
                $('question_id_'+question_id).innerHTML = '<div class="T01-td-ticked"></div>';
            } else {
                $('question_id_'+question_id).innerHTML = '';
            }
            
        }
    });
}

function showInterdependency(question_id){
    new Ajax.Request('/php/interdependencies.php', {
        parameters: 'question_id='+question_id,
        method: 'post',
        onSuccess: function(transport) {

            showBlackScreen();
            Effect.Appear('blackScreen', {
                duration: 0.1,
                afterFinish:function (){
                    $('rfsc-page').insert({
                        bottom: transport.responseText
                    });
                    $('rfsc-popup').style.top=(getScrollTop()+50) + 'px';
                }
            });
        }
    });
}

function showIllustration(){

    var argumentsArray = showIllustration.arguments;
    var illustrations_array = 'illustrations_array[]='+argumentsArray[0];
    if(argumentsArray.length>1){
        for(i=1 ; i<argumentsArray.length ; i++) {
            illustrations_array+= '&illustrations_array[]='+argumentsArray[i];
        }
    }

    new Ajax.Request('/php/illustrations.php', {
        parameters: illustrations_array,
        method: 'post',
        onSuccess: function(transport) {

            if($('rfsc-popup')) {
                $('rfsc-popup').remove();
                $('rfsc-page').insert({
                    bottom: transport.responseText
                });
                $('rfsc-popup').style.top=(getScrollTop()+50) + 'px';
            } else {

                showBlackScreen();
                Effect.Appear('blackScreen', {
                    duration: 0.1,
                    afterFinish:function (){
                        $('rfsc-page').insert({
                            bottom: transport.responseText
                        });
                        $('rfsc-popup').style.top=(getScrollTop()+50) + 'px';
                    }
                });
            }
        }
    });
}

function generateHomeAnimation() {
    
    if($('HP-slider-ic-playpause').className == 'HP-slider-pause'){
        homepageTimer = setTimeout('generateHomeAnimation()',myHomeAnimationTimerLength);
    }

    if(animationIndex<9){
        animationIndex++;
    } else {
        animationIndex = 1;
    }
    var myHomeAnimationElement = $$('.HP-slider-anim').first();
    myHomeAnimationElement.insert({
        before: '<div class="HP-slider-anim"><img src="/images/HP/IMG-HP-'+animationIndex+'.jpg" width="900" height="273" /></div>'
    });

    Effect.SlideUp(myHomeAnimationElement,{
        afterFinish:function (){
            myHomeAnimationElement.remove();
        }
    });

    $$('.HP-slider-ic-on').each(function (elt){
        elt.className = 'HP-slider-ic';
    });
    $('HP-slider-ic-'+animationIndex).className = 'HP-slider-ic-on';

}

function playPauseHomeAnimation(){
    if($('HP-slider-ic-playpause').className == 'HP-slider-play'){
        $('HP-slider-ic-playpause').className = 'HP-slider-pause';
        generateHomeAnimation();
        $('HP-slider-ic-'+animationIndex).className = 'HP-slider-ic-on';

    } else {
        $('HP-slider-ic-playpause').className = 'HP-slider-play';
        clearTimeout(homepageTimer);
    }

}

function setHomeAnimationIndex(index){
    clearTimeout(homepageTimer);
    animationIndex = index-1;
    generateHomeAnimation();
}

function onFocusCYTComment(comment_id){
    if($(comment_id).down('textarea').value==t('here your comment'))
        $(comment_id).down('textarea').value='';
}

function onBlurCYTComment(comment_id){
    if($(comment_id).down('textarea').value=='')
        $(comment_id).down('textarea').value= t('here your comment');
}

function saveCYTComment(comment_id){
    var myTextArea = $(comment_id).down('textarea');
    if($F(myTextArea) == t('here your comment')) $(myTextArea).value = '';
    new Ajax.Request('/php/characterize-your-city.php', {
        parameters: 'rfsc_action=saveComment&comment_id='+comment_id+'&comment_content='+encodeURIComponent($F(myTextArea)),
        method: 'post',
        onSuccess: function(transport) {
            $(comment_id).innerHTML = transport.responseText;
        }
    });
}

function cancelCYTComment(comment_id){
    new Ajax.Request('/php/characterize-your-city.php', {
        parameters: 'rfsc_action=getComment&comment_id='+comment_id,
        method: 'post',
        onSuccess: function(transport) {
            $(comment_id).innerHTML = transport.responseText;
        }
    });
}

function goTo(url){
    
    var isOk = true;
    var introMsg = '';
    var msg = '';
    var index = 0;
    
    switch(url){

        case '/tools/develop-your-sustainable-strategy-3':
            index = 0;
            $$('.T00-slider-tool').each(function(elt){
                if(elt.getStyle('left')=='0px') {
                    isOk = false;
                    index++;
                    msg += ' - ' + $(elt).up('td').previous('td').down('.question_title_class').innerHTML + '\n';
                    elt.up('td').previous(1).className = 'error-bg';
                    elt.up('td').previous().className = 'error-bg';
                }
            });
            if(!isOk){
                if(index>1){
                    introMsg = t('You forgot to set levels of commitment on these actions')+' :\n\n';
                } else {
                    introMsg = t('You forgot to set levels of commitment on this action')+' :\n\n';
                }
                displayAlert(introMsg+msg, MESSAGE_LEVEL_ERROR);
            } else {
                document.location.href=url
            }
            break;

        case '/tools/check-the-integrated-approach-of-your-strategy-2':
            index = 0;
            $$('.T00-slider-tool').each(function(elt){
                if(elt.getStyle('left')=='0px') {
                    isOk = false;
                    index++;
                    //msg += ' - ' + $(elt).up('td').previous('.question_title_class').innerHTML + '\n';
                    elt.up('td').previous(1).className = 'attention-bg';
                    elt.up('td').previous().className = 'attention-bg';
                }
            });
            if(!isOk){
                if(index>1){
                    introMsg = '<br/>'+t('Are you sure the objectives highlighted ar not priorities for your territory.');
                } else {
                    introMsg = '<br/>'+t('Are you sure the objective highlighted is not priority for your territory.');
                }
                displayAlert(introMsg+msg, MESSAGE_LEVEL_CONFIRM, function(){
                    document.location.href=url;
                });
            } else {
                document.location.href=url
            }
            break;
        default:
            document.location.href=url;
            break;
    }
    
}

function toggleQn1(eltId){
    var isVisible = ($('pilier_ic_'+eltId).className != 'T00-display-on');
    if (isVisible){
        $$('#pilier_'+eltId+' .qn1').each(function(elt){
            elt.hide();
            elt.down('td').down('div').className='T00-display-on';
        });
        $$('.pilier_qn2_'+eltId).each(function(elt){
            elt.hide();
        });
        $$('.pilier-'+eltId).each(function(elt){
            elt.hide();
        });
        $('pilier_ic_'+eltId).className = 'T00-display-on';
    } else {
        $$('#pilier_'+eltId+' .qn1').each(function(elt){
            elt.show();
        });
        $('pilier_ic_'+eltId).className = 'T00-display-off';
    }
}

function toggleQn2(eltId){
    var isVisible = ($('qn1_ic_'+eltId).className != 'T00-display-on');
    if (isVisible){
        $$('.qn2_'+eltId).each(function(elt){
            elt.hide();
        });
        $$('.keyindicators_'+eltId).each(function(elt){
            elt.hide();
        });
        $('qn1_ic_'+eltId).className = 'T00-display-on';
    } else {
        $$('.qn2_'+eltId).each(function(elt){
            elt.show();
        });
        $$('.keyindicators_'+eltId).each(function(elt){
            elt.show();
        });
        $('qn1_ic_'+eltId).className = 'T00-display-off';
    }
}

function togglePack(eltId){
    var isVisible = ($('qn2_ic_'+eltId).className != 'T00-display-on');
    if (isVisible){
        $$('#qn2_'+eltId+' .T00-tool-qn2-indic').each(function(elt){
            elt.hide();
        });
        $('qn2_ic_'+eltId).className = 'T00-display-on';
    } else {
        $$('#qn2_'+eltId+' .T00-tool-qn2-indic').each(function(elt){
            elt.show();
        });
        $('qn2_ic_'+eltId).className = 'T00-display-off';
    }
}

function addIndicator(indicator_id, qn1_id, qn2_id){

    var myCookie = LireCookie('tool4-indicatorsSecondaryArray');
    var elt = $('indicator-line-'+indicator_id+'-'+qn2_id);

    new Ajax.Request('/php/tool-monitor-progress-2.php', {
        parameters: 'rfsc_action=addIndicator&indicator_id='+indicator_id+'&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {
            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool4-indicatorsSecondaryArray', transport.responseText, myDate);
            }

            var myFirstElement = elt.down('div.T04-select-bt-off');
            var mySecondElement = elt.down('td.T04-select-off').next('td.T04-select-off');

            myFirstElement.className = 'T04-select-bt-on T04-select-bt-on-'+qn1_id;
            myFirstElement.innerHTML = t('added');
            myFirstElement.removeAttribute('onclick');

            mySecondElement.className = 'T04-select-on';
            mySecondElement.innerHTML = '<div class="T04-bt-cancel" onclick="removeIndicator('+indicator_id+','+qn1_id+' , '+qn2_id+')"></div><div class="T01-td-ticked"></div>';

            var monitoringCounter = 0;
            $$('.T04-select-bt-on-'+qn1_id).each(function(){
                monitoringCounter++;
            });
            if(monitoringCounter>0) {
                var myTickedElement = $('qn1_'+qn1_id).down('td').next(3);
                myTickedElement.className = 'T04-select-on';
                myTickedElement.innerHTML = '<div class="T01-td-ticked"></div>';
            }
            updateIndicatorCounter();

        }
    });

}

function addKeyIndicator(indicator_id, qn1_id){

    var myCookie = LireCookie('tool4-indicatorsArray');
    var elt = $('key-indicator-line-'+indicator_id+'-'+qn1_id);

    new Ajax.Request('/php/tool-monitor-progress-1.php', {
        parameters: 'rfsc_action=addIndicator&indicator_id='+indicator_id+'&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {

            if(transport.responseText != '') {
                var myDate = new Date();
                myDate.setHours(myDate.getHours() + 6);
                EcrireCookie('tool4-indicatorsArray', transport.responseText, myDate);
            }

            var myFirstElement = elt.down('div.T04-select-bt-off');
            var mySecondElement = elt.down('td.T04-select-off').next('td.T04-select-off');

            myFirstElement.className = 'T04-select-bt-on T04-select-bt-on-'+qn1_id;
            myFirstElement.innerHTML = t('added');
            myFirstElement.removeAttribute('onclick');

            mySecondElement.className = 'T04-select-on';
            mySecondElement.innerHTML = '<div class="T04-bt-cancel" onclick="removeKeyIndicator('+indicator_id+','+qn1_id+')"></div><div class="T01-td-ticked"></div>';

            var monitoringCounter = 0;
            $$('.T04-select-bt-on-'+qn1_id).each(function(){
                monitoringCounter++;
            });
            
            if(monitoringCounter>0) {
                var myTickedElement = $('qn1_'+qn1_id).down('td').next(3);
                myTickedElement.className = 'T04-select-on';
                myTickedElement.innerHTML = '<div class="T01-td-ticked"></div>';
            }

            
            updateKeyIndicatorCounter();

        }
    });

}


function removeIndicator(){

    var myCookie = LireCookie('tool4-indicatorsSecondaryArray');
    var argumentsArray = removeIndicator.arguments;
    var elt;
    var isOk = true;
    
    if(argumentsArray.length==1){
        isOk = confirm('Do you really want to remove this indicator ?');
        indicator_id = argumentsArray[0];
        elt = $('indicator-line-'+indicator_id);
    } else if(argumentsArray.length==3) {
        indicator_id = argumentsArray[0];
        qn1_id = argumentsArray[1];
        qn2_id = argumentsArray[2];
        elt = $('indicator-line-'+indicator_id+'-'+qn2_id);
    }    
    if(isOk){
        new Ajax.Request('/php/tool-monitor-progress-2.php', {
            parameters: 'rfsc_action=removeIndicator&indicator_id='+indicator_id+'&cookie='+myCookie,
            method: 'post',
            onSuccess: function(transport) {

                if(transport.responseText != '') {
                    var myDate = new Date();
                    myDate.setHours(myDate.getHours() + 6);
                    EcrireCookie('tool4-indicatorsSecondaryArray', transport.responseText, myDate);
                }

                if(argumentsArray.length==1){
                    var myTable = elt.up('table');
                    elt.remove();
                    if(myTable.select('tr').length==0) {
                        myTable.up('.T02b-q2-selected').remove();
                    }

                } else if(argumentsArray.length==3) {

                    var myFirstElement = elt.down('div.T04-select-bt-on');
                    var mySecondElement = elt.down('td.T04-select-on');

                    myFirstElement.className = 'T04-select-bt-off';
                    myFirstElement.innerHTML = t('add');
                    myFirstElement.writeAttribute('onclick','addIndicator('+indicator_id+','+qn1_id+','+qn2_id+')');

                    mySecondElement.className = 'T04-select-off';
                    mySecondElement.innerHTML = '';

                    var monitoringCounter = 0;
                    $$('.T04-select-bt-on-'+qn1_id).each(function(){
                        monitoringCounter++;
                    });
                    if(monitoringCounter==0) {
                        var myTickedElement = $('qn1_'+qn1_id).down('td').next(3);
                        myTickedElement.className = 'T04-select-off';
                        myTickedElement.innerHTML = '';
                    }

                    updateIndicatorCounter();

                }
            }
        });
    }
}

function removeKeyIndicator(){
    var myCookie = LireCookie('tool4-indicatorsArray');
    var argumentsArray = removeKeyIndicator.arguments;
    var elt;
    var isOk = true;
    
    if(argumentsArray.length==1){
        isOk = confirm('Do you really want to remove this indicator ?');
        indicator_id = argumentsArray[0];
        elt = $('key-indicator-line-'+indicator_id);
    } else if(argumentsArray.length==2) {
        indicator_id = argumentsArray[0];
        qn1_id = argumentsArray[1];
        elt = $('key-indicator-line-'+indicator_id+'-'+qn1_id);
    }
    if(isOk){
        new Ajax.Request('/php/tool-monitor-progress-1.php', {
            parameters: 'rfsc_action=removeIndicator&indicator_id='+indicator_id+'&cookie='+myCookie,
            method: 'post',
            onSuccess: function(transport) {

                if(transport.responseText != '') {
                    var myDate = new Date();
                    myDate.setHours(myDate.getHours() + 6);
                    EcrireCookie('tool4-indicatorsArray', transport.responseText, myDate);
                }

                if(argumentsArray.length==1){
                    var myTable = elt.up('table');
                    elt.remove();
                    if(myTable.select('tr').length==0) {
                        myTable.up('.T02b-q2-selected').remove();
                    }

                } else if(argumentsArray.length==2) {
                    var myFirstElement = elt.down('div.T04-select-bt-on');
                    var mySecondElement = elt.down('td.T04-select-on');

                    myFirstElement.className = 'T04-select-bt-off';
                    myFirstElement.innerHTML = t('add');
                    myFirstElement.writeAttribute('onclick','addKeyIndicator('+indicator_id+','+qn1_id+')');

                    mySecondElement.className = 'T04-select-off';
                    mySecondElement.innerHTML = '';

                    var monitoringCounter = 0;
                    $$('.T04-select-bt-on-'+qn1_id).each(function(){
                        monitoringCounter++;
                    });

                    if(monitoringCounter==0) {
                        var myTickedElement = $('qn1_'+qn1_id).down('td').next(3);
                        myTickedElement.className = 'T04-select-off';
                        myTickedElement.innerHTML = '';
                    }

                    updateKeyIndicatorCounter();

                }
            }
        });
    }
}

function updateIndicatorCounter(){

    var myCookie = LireCookie('tool4-indicatorsSecondaryArray');

    new Ajax.Request('/php/tool-monitor-progress-2.php', {
        parameters: 'rfsc_action=updateIndicatorCounter&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {
            var data = eval('(' + transport.responseText + ')');
            for (var i in data) {
                if (data[i]>0) {
                    $('pilierCounter_'+i).innerHTML = '<div class="T04-select-txt" align="center">('+data[i]+')</div><div class="T01-td-ticked"></div>';
                    $('pilierCounter_'+i).className = 'T04-select-on';
                }
                else {
                    $('pilierCounter_'+i).innerHTML = '';
                    $('pilierCounter_'+i).className = 'T04-select-off';
                }
            }
        }
    });
}

function updateKeyIndicatorCounter(){

    var myCookie = LireCookie('tool4-indicatorsArray');

    new Ajax.Request('/php/tool-monitor-progress-1.php', {
        parameters: 'rfsc_action=updateIndicatorCounter&cookie='+myCookie,
        method: 'post',
        onSuccess: function(transport) {
            var data = eval('(' + transport.responseText + ')');
            for (var i in data) {
                if (data[i]>0) {
                    $('pilierCounter_'+i).innerHTML = '<div class="T04-select-txt" align="center">('+data[i]+')</div><div class="T01-td-ticked"></div>';
                    $('pilierCounter_'+i).className = 'T04-select-on';
                }
                else {
                    $('pilierCounter_'+i).innerHTML = '';
                    $('pilierCounter_'+i).className = 'T04-select-off';
                }
            }
        }
    });
}

function toggleAllItems(div){
    if(div.className == 'T00-display-all-on') {
        $$('.T00-tool-qn1').each(function (elt){
            var myOnClickAttribute = elt.down('.item-with-open-function').readAttribute('onclick');
            eval(myOnClickAttribute);
        });
        div.className = 'T00-display-all-off';
    } else {
        $$('.T00-tool-qn1p').each(function (elt){
            var myOnClickAttribute = elt.down('.item-with-open-function').readAttribute('onclick');
            eval(myOnClickAttribute);
        });
        div.className = 'T00-display-all-on';
    }

}

function toggleAllItemsDeprivedTwo(div){
    if(div.className == 'T00-display-all-on') {
        $$('.T02b-q2').each(function (elt){
            var myOnClickAttribute = elt.down('.item-with-open-function').readAttribute('onclick');
            eval(myOnClickAttribute);
        });
        div.className = 'T00-display-all-off';
    } else {
        $$('.T02b-q2-selected').each(function (elt){
            var myOnClickAttribute = elt.down('.item-with-open-function').readAttribute('onclick');
            eval(myOnClickAttribute);
        });
        div.className = 'T00-display-all-on';
    }

}

function openTackledItems(divId){
    var myContainer = $(divId);
    var myContainerId = divId;
    myContainer.className = 'T02b-q2-selected';
    myContainer.down('.T00-display-on').className = 'T00-display-off';

    $$('#'+myContainerId+' .item-with-open-function').each(function(elt){
        elt.writeAttribute('onClick','closeTackledItems(\''+divId+'\')');
    });

    $$('#'+myContainerId+' .item-open-close').each(function(elt){
        elt.show();
    });
}

function closeTackledItems(divId){
    var myContainer = $(divId);
    var myContainerId = divId;
    myContainer.className = 'T02b-q2';
    myContainer.down('.T00-display-off').className = 'T00-display-on';

    $$('#'+myContainerId+' .item-with-open-function').each(function(elt){
        elt.writeAttribute('onClick','openTackledItems(\''+divId+'\')');
    });

    $$('#'+myContainerId+' .item-open-close').each(function(elt){
        elt.hide();
    });
}

function toggleAllMonitoringItems(div){
    if(div.className == 'T00-display-all-on') {
        $$('.item-with-open-function').each(function(elt){

            if(elt.down('.T00-display-on')) {
                var myJs = elt.readAttribute('onClick');
                eval(myJs);
            }

        });
        div.className = 'T00-display-all-off';
    } else {
        $$('.T02b-q2-selected').each(function(elt){
            if(elt.down('.item-with-open-function').down('.T00-display-off')) {
                var myJs = elt.down('.item-with-open-function').readAttribute('onClick');
                eval(myJs);                
            }

        });
        div.className = 'T00-display-all-on';
        $$('.T00-tool-qn2-indic').each(function(elt){
            elt.hide();
        });
    }
}

function checkAllItems(){
    $$('.item-with-checkbox').each(function(elt){
        elt.className = 'T02-bt-select-on item-with-checkbox';
    });
    $$('.T00-tool-qn1 .tick-or-links').each(function(elt){
        elt.innerHTML = '<div class="T02-q1-selected"></div>';
    });
    saveQuestions();
}

function uncheckAllItems(){
    $$('.item-with-checkbox').each(function(elt){
        elt.className = 'T02-bt-select-off item-with-checkbox';
    });
    $$('.T00-tool-qn1 .tick-or-links').each(function(elt){
        elt.innerHTML = '';
    });
    saveQuestions();
}

function resetAllSliders(){
    var myPosition = 0;
    if(arguments.length>0) myPosition = arguments[0];
    for (var key in sliderArray){
        try {
            sliderArray[key].setValue(myPosition);
        } catch (err){}
    }
}

function resetMonotoringSelection(){
    $$('.T04-bt-cancel').each(function(elt){
        eval(elt.readAttribute('onClick'));
    });
}

function displayTopAnchor(){
    var root= document.compatMode=='BackCompat'? document.body : document.documentElement;
    var isVerticalScrollbar= root.scrollHeight>root.clientHeight;
    //var isHorizontalScrollbar= root.scrollWidth>root.clientWidth;
    if (isVerticalScrollbar){
        $('goTopAnchor').show();
    } else {
        $('goTopAnchor').hide();
    }
}

function removeAllIndicators(){
    if(confirm('Do you really want to remove all Indicators ?')){

        new Ajax.Request('/php/tool-monitor-progress-1.php', {
            parameters: 'rfsc_action=removeAllIndicators',
            method: 'post',
            onSuccess: function(transport) {

                if(transport.responseText != '') {
                    var myDate = new Date();
                    myDate.setHours(myDate.getHours() + 6);
                    EcrireCookie('tool4-indicatorsArray', transport.responseText, myDate);
                }

                $$('.T02b-q2-selected').each(function(elt){
                    elt.remove();
                });
            }
        });
    }
}

function enablePageComment(){
    $('page-comment-textarea').observe('click',function(){
        if($F('page-comment-textarea') == t('AddYourComments')) {
            $('page-comment-textarea').value = '';
        }
    });
    $('page-comment-textarea').observe('blur',function(){
        if($F('page-comment-textarea') == '') {
            $('page-comment-textarea').value = t('AddYourComments');
        }
    });
    
    $('page-comment-clear').observe('click',function(){
        $('page-comment-textarea').value = t('AddYourComments');
    });
    $('page-comment-clear').writeAttribute('onclick','return false');
    
    $('page-comment-save').observe('click',function(){
        var myComment = '';
        if($F('page-comment-textarea') != t('AddYourComments')){
            myComment = $F('page-comment-textarea');
        }
        new Ajax.Request('/php/rfsc-ajax.php', {
            parameters: 'rfsc_action=savePageComment&comment='+encodeURIComponent(myComment)+'&url='+encodeURIComponent(document.location.href),
            method: 'post',
            onSuccess: function() {
            }
        });
    });
    $('page-comment-save').writeAttribute('onclick','return false');
}

function saveCityChallenges(elt){
    var url = '/php/share-1.php';
    var currentUrl = document.location.href;
    var isOk = true;
    if(currentUrl.indexOf('2') != -1) {
        url = '/php/share-2.php';
        var index = 0;
        $$('input[type="checkbox"]').each(function (e){
            if(e.checked) index++;
        });
        if (index>3) {
            elt.checked = false;
            isOk = false;
        }
    }
    if(isOk) {
        new Ajax.Request(url, {
            parameters: 'rfsc_action=saveChallenges&status='+elt.checked+'&challenge_label='+encodeURIComponent(elt.value),
            method: 'post',
            onSuccess: function() {
            }
        });
    }
}

function clearCityChallenges() {
    var url = '/php/share-1.php';
    var currentUrl = document.location.href;
    if(currentUrl.indexOf('2') != -1) {
        url = '/php/share-2.php';
    }

    new Ajax.Request(url, {
        parameters: 'rfsc_action=clearChallenges',
        method: 'post',
        onSuccess: function() {
            $$('input[type="checkbox"]').each(function (elt){
                elt.checked = false;
            });
        }
    });

}

function deleteIllustration(iid) {
    displayAlert('Do you really want to delete this illustration ?', MESSAGE_LEVEL_CONFIRM, function(){
        new Ajax.Request('/php/illustrations-add.php', {
            parameters: 'rfsc_action=deleteIllustration&iid='+iid,
            method: 'post',
            onSuccess: function() {
                document.location.href = '/networks/illustrations-1';
            }
        });
    });
}

function updateQuestionComboFromPilier(elt, table_id, question_id) {
    var myElement = $(elt);
    var theme_id = $(elt).value;
    if(theme_id!=''){
        new Ajax.Request('/php/illustrations-add.php', {
            parameters: 'rfsc_action=updateQuestionComboFromPilier&theme_id='+theme_id,
            method: 'post',
            onSuccess: function(transport) {
                myElement.up('td').next('td').innerHTML = transport.responseText;
                if(table_id != -1) {
                    $(table_id).down('td').next('td').down('select').value = question_id;
                }
            }
        });
    } else {
        myElement.up('td').next('td').innerHTML = '<select name="interdependency-qn1[]" style="width: 150px;" disabled="disabled"><option value="">&rarr; choose Objective &larr;</option></select>';
    }
}

function updateSearchQuestionComboFromPilier() {
    var argumentsArray = updateSearchQuestionComboFromPilier.arguments;
    var question_id = '';
    if(argumentsArray.length>0) {
        question_id = argumentsArray[0];
    }
    var theme_id = $F('pilier-selection');
    if(theme_id!=''){
        new Ajax.Request('/php/illustrations-search.php', {
            parameters: 'rfsc_action=updateQuestionComboFromPilier&theme_id='+theme_id+'&question_id='+question_id,
            method: 'post',
            onSuccess: function(transport) {
                $('qn1-selection').update(transport.responseText);
                $('qn1-selection').disabled = false;
            }
        });
    } else {
        $('qn1-selection').update('<option>→ choose item ←</option>');
        $('qn1-selection').disabled = true;
    }
}

function illustrationOnFocus(elt){
    var commentValue = $(elt).value;
    if(commentValue == t('Add your comment')) {
        $(elt).value = '';
    }
}

function illustrationOnBlur(elt) {
    var commentValue = $(elt).value;
    if(commentValue == '') {
        $(elt).value = t('Add your comment');
    }
}

function addIllustrationQn1Line(elt) {
    var myTable = elt.up('div').previous('table');
    myTable.insert({
        after:'<table width="100%" border="0" cellspacing="0" cellpadding="0">'+myTable.innerHTML+'</table>'
        });
    myTable = elt.up('div').previous('table');
    var myFirstCombo = myTable.down('td').down('select');
    myFirstCombo.value = '';
    var mySecondCombo = myTable.down('td').next('td');
    mySecondCombo.innerHTML = '<select name="interdependency-qn1[]" style="width: 150px;" disabled="disabled"><option value="">&rarr; choose Objective &larr;</option></select>';
}

function changeLang(lang){
    new Ajax.Request('/php/rfsc-ajax.php', {
        parameters: 'rfsc_action=changeLang&site_lang='+lang,
        method: 'post',
        onSuccess: function() {
            document.location.reload();
        }
    });
}

function sendIllustration(illustration_id, illustration_title) {
    displayAlert('Do you really want send this Illustration to the moderator ?', MESSAGE_LEVEL_CONFIRM, function(){
        new Ajax.Request('/php/illustrations-add.php', {
            parameters: 'rfsc_action=send&illustration_id='+illustration_id+'&illustration_title='+encodeURIComponent(illustration_title),
            method: 'post',
            onSuccess: function() {
                document.location.reload();
            }
        });
    });
}

function searchIllustration() {
    $('illustration-search-form').submit();
}

function masqueradeUser(){
    new Ajax.Request('/php/rfsc-ajax.php', {
        parameters: 'rfsc_action=masqueradeUser&user_id='+$F('userMasqueradeCombo'),
        method: 'post',
        onSuccess: function() {
            document.location.reload();
        }
    });
}

function t(id) {
    var output = '';
    if(globalTranslationArray[id.toLowerCase()]==undefined) {
        new Ajax.Request('/php/rfsc-ajax.php', {
            parameters: 'rfsc_action=createNewTranslation&id='+encodeURIComponent(id.toLowerCase()),
            method: 'post',
            onSuccess: function() {                
            }
        });
        output = id;
    } else {
        output = globalTranslationArray[id.toLowerCase()];
    }
    return output;
}

function downloadResults(project_id, url, session_id) {
    selectProject(project_id, url+'?print');
}

function monitoringFilter() {
    document.location.href = '/tools/monitor-progress-3?f='+$F('monitoringFilterCombo');
}

function showAddYourOwnIndicator(question_id){
    var indicator_id = 0;
    if(arguments.length>1) indicator_id = arguments[1];
    new Ajax.Request('/php/popup-add-indicator.php', {
        parameters: 'question_id='+question_id+'&indicator_id='+indicator_id,
        method: 'get',
        onSuccess: function(transport) {

            showBlackScreen();
            Effect.Appear('blackScreen', {
                duration: 0.1,
                afterFinish:function (){
                    $('rfsc-page').insert({
                        bottom: transport.responseText
                    });
                    $('rfsc-popup').style.top=(getScrollTop()+50) + 'px';
                }
            });
        }
    });
}

function onChangeIndicatorShareStatus() {
    if($('share-with-users').checked) {
        $('share-documents-box').show();
        $$('#share-documents-box input').each(function(elt){
            elt.disabled = false;
        });
    } else {
        $('share-documents-box').hide();
        $$('#share-documents-box input').each(function(elt){
            elt.disabled = true;
        });
    }
}

function saveOwnIndicator(){
    var isOk = true;
    var msg = '';
    
    if($F('indicator_title') == '') {
        isOk = false;
        msg = t('The field name is mandatory');
    }
    if(!isOk) {
        alert(msg);
    } else {
        $('own-indicator-form').submit();
    }
}

function deleteOwnIndicator(indicator_id) {
    displayAlert(t('Do you really want to delete your indicator ?'), MESSAGE_LEVEL_CONFIRM, function(){
        new Ajax.Request('/php/popup-add-indicator.php', {
            parameters: 'rfsc_action=deleteIndicator&indicator_id='+indicator_id,
            method: 'post',
            onSuccess: function() {
                document.location.reload();
            }
        });
    });
}

function addFile(elt) {
    elt.insert({
        'before':'<div><input type="file" name="share-indicator[]" /> <a href="#" onclick="this.up(\'div\').remove();return false">x</a></div>'
    });
}

function onPillarChange(){
    var getParameters = '';
    if($F('monitoringToolsSearchPillar') != '') {
        getParameters = '?p='+$F('monitoringToolsSearchPillar');
    }
    document.location.href = '/tools/monitor-progress-2'+getParameters;
}

function onQn1Change() {
    var getParameters = '';
    if($F('monitoringToolsSearchQN1') != '') {
        getParameters = '&qn1='+$F('monitoringToolsSearchQN1');
    }
    document.location.href = '/tools/monitor-progress-2?p='+$F('monitoringToolsSearchPillar')+getParameters;
}

function onQn2Change(){
    var getParameters = '';
    if($F('monitoringToolsSearchQN2') != '') {
        getParameters = '&qn2='+$F('monitoringToolsSearchQN2');
    }
    document.location.href = '/tools/monitor-progress-2?p='+$F('monitoringToolsSearchPillar')+'&qn1='+$F('monitoringToolsSearchQN1')+getParameters;
}
