/*******************************************************************************
 * COPYRIGHT NOTICE *
 * 
 * Copyright 2009-2017 ProjeQtOr - Pascal BERNARD - support@projeqtor.org
 * Contributors : Eliott Legrand (05/2018)
 * 
 * This file is part of ProjeQtOr.
 * 
 * ProjeQtOr is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * ProjeQtOr is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * ProjeQtOr. If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can get complete code of ProjeQtOr, other resource, help and information
 * about contributors at http://www.projeqtor.org
 * 
 * DO NOT REMOVE THIS NOTICE **
 ******************************************************************************/

// LEAVE SYSTEM
/* ============================================================================
 * This js file contents all implemented functions for LEAVE SYSTEM
   ============================================================================ */

// **********************
// MISCELANIOUS FUNCTIONS
// **********************

/**
 * Show Xhr error in error popup
 * php file must include '../view/leaveCalendarPopupErrorAndResult.php'
 * @param {XhrError} error
 * @param {string} msg
 * @return {void}
 */
function showXhrErrorInErrorPopup(error,msg="") {
    hideWait();
    theError = new String();
    theError = i18n("unknow");
    errorTitle= i18n("network")+" "+i18n("ERROR");
    errorTitle = errorTitle.toUpperCase();
    
    if (typeof(error.xhr)==="undefined") {
        theError = i18n("notXhrError");
    } else if(typeof(error.status)!=="undefined") {
        theError = error.status;
    } else if(typeof(error.message)!=="undefined") {
        theError = error.message;        
    }
    if (dojo.byId('resultPopup')) {
        // Set content Node of result
        contentNode = dojo.byId('resultPopup');
        dojo.removeClass(contentNode);
        dojo.addClass(contentNode, "messageERROR");
        dojo.addClass(contentNode, "closeBoxIconLeave");
        contentNode.innerHTML = "<h3>"+errorTitle+"</h3><br>"+msg+"<br>"+theError+"<br><i>"+i18n("clickIntoToClose")+"</i>";

        // Show result
        contentNode.style.display = "block";
    } else {
        console.log(errorTitle+' - '+msg+' - '+theError);
    }
}

/**
 * Show the result of a SqlElement operation in a popup
 * php file must include '../view/leaveCalendarPopupErrorAndResult.php'
 * @param {String} msg : The result of a SqlElement operation
 * @return {void}
 */
function showSqlElementResultInPopup(msg) {
    hideWait();
    // Retrieve type of message
    var type = getSqlElementOperationStatus(msg);
    
    // Retrieve useful message
    indexResult = msg.indexOf("<input");
    var theMsg = msg.substr(0,indexResult);
    
    if (dojo.byId('resultPopup')) {
        // Set content Node of result
        contentNode = dojo.byId('resultPopup');
        dojo.removeClass(contentNode);
        dojo.addClass(contentNode, "message"+type);
        dojo.addClass(contentNode, "closeBoxIconLeave");
        contentNode.innerHTML = i18n("result")+"<br><br>"+theMsg+"<br><br><i>"+i18n("clickIntoToClose")+"</i>";

        // Show result
        contentNode.style.display = "block";

        // If type = "OK" or "NO_CHANGE" => fade short time
        if (type=="OK" || type=="NO_CHANGE") {
            dojo.fadeIn({
              node : contentNode,
              duration : 1000,
              onEnd : function() {
                dojo.fadeOut({
                  node : contentNode,
                  duration : 3000
                }).play();
              }
            }).play();
        } else { // fade long time
            dojo.fadeIn({
              node : contentNode,
              duration : 7000,
              onEnd : function() {
                dojo.fadeOut({
                  node : contentNode,
                  duration : 5000
                }).play();
              }
            }).play();        
        }
    } else {
        console.log(i18n("result")+" - status = "+type+" - msg = "+theMsg);
    }    
}

// *********************
// FUNCTIONS FOR COOKIES
// *********************
/**
 * 
 * @param {String} cname : The cookie name to get the value
 * @returns {void} The cookie value
 */
function getCookieValue(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "unknow";
}
// *********************
// END FUNCTIONS FOR COOKIES
// *********************


// *********************
// FUNCTIONS FOR CSS
// *********************
/**
 * Return the opposite color of the color passed in parameter
 * @param {String} hex : The color in Hexa to opposite
 * @param {Boolean} bw : If true, color opposite is Black or White
 * @returns {String} : The oppocite color
 */
function oppositeColor(hex, bw) {
    if (typeof(hex)=="undefined") {return '#FFFFFF';}
    if (hex.indexOf('#') === 0) {
        hex = hex.slice(1);
    }
    // convert 3-digit hex to 6-digits.
    if (hex.length === 3) {
        hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
    }
    if (hex.length !== 6) {
        throw new Error('Invalid HEX color.');
    }
    var r = parseInt(hex.slice(0, 2), 16),
        g = parseInt(hex.slice(2, 4), 16),
        b = parseInt(hex.slice(4, 6), 16);
    if (bw) {
        return (r * 0.299 + g * 0.587 + b * 0.114) > 186
            ? '#000000'
            : '#FFFFFF';
    }
    // invert color components
    r = (255 - r).toString(16);
    g = (255 - g).toString(16);
    b = (255 - b).toString(16);
    // pad each with zeros and return
    return "#" + padZero(r) + padZero(g) + padZero(b);
}

/**
 * Test if selector passed in parameter exists in document
 * @param {String} selector to test existance in document
 * @returns {Boolean} : true if selector exists in document
 */
function selectorExists(selector) { 
    var theElt = document.querySelector(selector);
    if (theElt==null) { return false;} else {return true;}
}

/**
 * Create a CSS class named classStatus with background color colorStatus
 * @param {String} classStatus : The status class to create
 * @param {String} colorStatus : The background color of status class
 * @returns {Boolean} : True if status class is created
 */
function createLeaveStatusClass(classStatus, colorStatus) {
    if (typeof(colorStatus)=='undefined') { return false;}
    if (!selectorExists(classStatus)) {
        var color = oppositeColor(colorStatus, true);
        var style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = classStatus + ' { ';
        style.innerHTML += 'background-color: '+ colorStatus + ' !important; ';
        style.innerHTML += 'color: ' + color + ' !important; ';
        style.innerHTML += '}';        
        document.getElementsByTagName('head')[0].appendChild(style);
    }
}
// *********************
// END FUNCTIONS FOR CSS
// *********************

// ****************************
// FUNCTIONS FOR LEAVE CALENDAR
// ****************************
// Variables Off and Work day list to resource that is not user connected
var emplWorkDayList = new Array();
var emplOffDayList = new Array();
var emplDefaultOffDays = new Array();
var theSelectedLeave = null;

/**
 * Get the employee's id for which manage leave 
 * that is store in view/leaveCalendar.php (idEmployeeCalendar) or in model/LeaveMain.php (idEmployee)
 * @param {boolean} fromLeaveCalendar : true if from Leave Calendar - false if from LeaveMain
 * @returns {Element.value} : The id employee
 */
function getIdEmployeeInLeaveCalMain(fromLeaveCalendar=false) {
    if (!fromLeaveCalendar) {
        if (dijit.byId("idEmployee")) {
            return dijit.byId("idEmployee").value;
        } else {
            return null;
        }
    } else {
        if (typeof(document.getElementById('idEmployeeCalendar'))=="undefined") {
            return null;
        } else {
            return document.getElementById('idEmployeeCalendar').value;
        }        
    }
}

/**
 * Get the connected user's id that is store in idUserCalendar (view/leaveCalendar.php) 
 * @returns {Element.value} : The user's id
 */
function getIdUserInLeaveCalendar() {
    if (typeof(document.getElementById('idUserCalendar'))=="undefined") {
        return null;
    } else {
        return document.getElementById('idUserCalendar').value;
    }        
}

/**
 * Initialize the workDayList and OffDayList for a resource
 * @param {integer} idRes : resource's id for which initialize
 * @param {string} startDate : start date for which initialize
 * @param {string} endDate : end date for which initialize
 * @returns {void}
 */
function initOffDayOfRes(idRes,startDate="",endDate="") {
    var url = "../tool/getOffDaysOfResource.php?idRes="+idRes;
    if (startDate!=="") {
        url += "&startDate="+startDate;
    }
    if (endDate!=="") {
        url += "&endDate="+endDate;        
    }
    dojo.xhrGet({
        url : url,
        handleAs : "text",
        sync: true,
        load : function(data, args) {
            var datas = JSON.parse(data);
            var opStatus = getSqlElementOperationStatus(datas);
            if (opStatus.indexOf("TYPEOF")===-1) {
                if (opStatus==='NOT RESULT OF SQLELEMENT OPERATION') {
                    datas = setLikeResultDivMessage(null, 
                                  opStatus+'<br>'+datas, 
                                  false,
                                  "", 
                                  "InitOffDayOfRes",
                                  "INVALID"
                            );
                }
                showSqlElementResultInPopup(datas);
            } else {
                emplOffDayList[idRes] = datas['offDays'];
                emplWorkDayList[idRes] = datas['workDays'];
                emplDefaultOffDays = datas['defaultOffDays'];
            }
        },
        error: function(error){
            showXhrErrorInErrorPopup(error,'getOffDaysOfResource.php');
        }
    });        
}

/**
 * Return if the date passed in parameter is a day off for the resource passed in parameter
 * @param {Date} vDate : The date to test
 * @param {Integer} idRes : The resource's id to test - If null, use of default calendar (ie calendar definition with id=1)
 * @param {Integer} idUser : The connected user
 * @returns {Boolean} : true if day off
 */
function isOffDayOfResource(vDate, idRes=null, idUser=null) {
    var cWorkDayList;
    var cOffDayList;
    
    if (idRes==null) {
        // Use datas of the default calendar
        cWorkDayList = getCookieValue("workDayList");
        cOffDayList = getCookieValue("offDayList");
        if (cWorkDayList!="unknow") {
            workDayList = cWorkDayList;
        } else {
            cWorkDayList = workDayList;
        }
        if (cOffDayList!="unknow") {
            offDayList = cOffDayList;
        } else {
            cOffDayList = offDayList;
        }
    } else {
        if (idUser == idRes) {
            // Use datas of the user calendar
            cWorkDayList = getCookieValue("uWorkDayList");
            cOffDayList = getCookieValue("uOffDayList");        
            if (cWorkDayList!="unknow") {
                uWorkDayList = cWorkDayList;
            } else {
                cWorkDayList = uWorkDayList;
            }
            if (cOffDayList!="unknow") {
                uOffDayList = cOffDayList;
            } else {
                cOffDayList = uOffDayList;
            }
        } else {
            // Take a leave for an another ressource (not for him self)
            if (typeof(emplWorkDayList[idRes])=="undefined") {
                cWorkDayList="";
                cOffDayList="";
            } else {
                cWorkDayList = emplWorkDayList[idRes];
                cOffDayList = emplOffDayList[idRes];
            }
        }
    }
    trueFalse=false;
    if (idUser == idRes) {
        trueFalse = ( defaultOffDays.indexOf(vDate.getDay()) != -1 );
    } else {
        for (var key in emplDefaultOffDays) {
            if (key == vDate.getDay() && trueFalse==false) {
                trueFalse=true;
                break;
            }
        }
    }
    
    if ( trueFalse ) {
        var day=(vDate.getFullYear()*10000)+((vDate.getMonth()+1)*100)+vDate.getDate();
        if (cWorkDayList.lastIndexOf('#'+day+'#')>=0) {
            return false; 
        } else {
            return true;
        }
    } else {
        var day=(vDate.getFullYear()*10000)+((vDate.getMonth()+1)*100)+vDate.getDate();
        if (cOffDayList.lastIndexOf('#'+day+'#')>=0) {
            return true; 
        } else {
            return false;
        }
    }
}

/**
 * Calculate the nomber of open days between to dates
 * @param {Date} paramStartDate
 * @param {Date} paramEndDate
 * @param {Integer} idRes The employee's id for which search open days
 * @param {Integer} idUser The connected user's id
 * @returns {Number} : Number of open days between to dates
 */
function openDayDiffDates(paramStartDate, paramEndDate, idRes=null, idUser=null) {
    var currentDate = new Date();
    if (!isDate(paramStartDate))
      return '';
    if (!isDate(paramEndDate))
      return '';
    currentDate.setFullYear(paramStartDate.getFullYear(), paramStartDate.getMonth(), paramStartDate.getDate());
    currentDate.setHours(0,0,0,0);
    var endDate = paramEndDate;
    if (paramEndDate < paramStartDate) {
      return 0;
    }
    var duration = 0;

    while (currentDate <= endDate) {
      if (!isOffDayOfResource(currentDate, idRes, idUser)) {
        duration++;
      }
      currentDate = addDaysToDate(currentDate, 1);
    }
    return duration;
}


/**
 * to calculate the nbDays between two leaves
 * @param {string} idStartDate
 * @param {string} idEndDate
 * @param {string} idStartAM
 * @param {string} idStartPM
 * @param {string} idEndAM
 * @param {string} idEndPM
 * @param {string} idNbDays
 * @param {integer} idRes
 * @param {integer} idUser
 * @returns void
 */
function calculateHalfDaysForLeave(idStartDate, idEndDate, idStartAM, idStartPM, idEndAM, idEndPM, idNbDays, idRes, idUser){
    var startDate = dijit.byId(idStartDate).get("value");
    var endDate = dijit.byId(idEndDate).get("value");
    
    if (idRes != idUser) {
        initOffDayOfRes(idRes, transformDateToSqlDate(startDate), transformDateToSqlDate(endDate));
    }
    
    var startDate = dijit.byId(idStartDate).get("value");
    var endDate = dijit.byId(idEndDate).get("value");
    var nbOpenDays=openDayDiffDates(startDate,endDate, idRes, idUser);
    //if the two dates are set and the dates are the same and the number of days is superior to 0
    if(startDate && endDate && startDate.getTime() === endDate.getTime() && nbOpenDays > 0){
        //if two cases are ticked
        if((dijit.byId(idStartAM).checked && dijit.byId(idEndPM).checked) || 
           (dijit.byId(idStartAM).checked && dijit.byId(idEndAM).checked) || 
           (dijit.byId(idStartPM).checked && dijit.byId(idEndPM).checked) ||
           (dijit.byId(idStartPM).checked && dijit.byId(idEndAM).checked)) {
           
            if(dijit.byId(idStartAM).checked && dijit.byId(idEndPM).checked){
                dijit.byId(idNbDays).set("value",nbOpenDays);
            }else if(dijit.byId(idStartAM).checked && dijit.byId(idEndAM).checked){
                dijit.byId(idNbDays).set("value",(nbOpenDays-0.5));
            }else if(dijit.byId(idStartPM).checked && dijit.byId(idEndPM).checked){
                dijit.byId(idNbDays).set("value",(nbOpenDays-0.5));
            }else if(dijit.byId(idStartPM).checked && dijit.byId(idEndAM).checked){
                dijit.byId(idNbDays).set("value",nbOpenDays-1);
            }
        }else{
            dijit.byId(idNbDays).set("value",nbOpenDays);
        }
    }
    //else if the two dates are different
    else if(startDate && endDate && startDate.getTime() !== endDate.getTime() && nbOpenDays > 0){
        if((dijit.byId(idStartAM).checked && dijit.byId(idEndPM).checked) || 
           (dijit.byId(idStartAM).checked && dijit.byId(idEndAM).checked) || 
           (dijit.byId(idStartPM).checked && dijit.byId(idEndPM).checked) || 
           (dijit.byId(idStartPM).checked && dijit.byId(idEndAM).checked)) {
            
            if(dijit.byId(idStartAM).checked && dijit.byId(idEndPM).checked){
                dijit.byId(idNbDays).set("value",nbOpenDays);
            }else if(dijit.byId(idStartAM).checked && dijit.byId(idEndAM).checked){
                if(!isOffDayOfResource(endDate, idRes, idUser)){
                    dijit.byId(idNbDays).set("value",(nbOpenDays-0.5));
                }else{
                    dijit.byId(idNbDays).set("value",(nbOpenDays));
                }
            }else if(dijit.byId(idStartPM).checked && dijit.byId(idEndPM).checked){
                if(!isOffDayOfResource(startDate, idRes, idUser)){
                    dijit.byId(idNbDays).set("value",(nbOpenDays-0.5));
                }else{
                    dijit.byId(idNbDays).set("value",(nbOpenDays));
                }
            }else if(dijit.byId(idStartPM).checked && dijit.byId(idEndAM).checked){
                if((!isOffDayOfResource(startDate, idRes, idUser)) && (!isOffDayOfResource(endDate, idRes, idUser))){
                    dijit.byId(idNbDays).set("value",(nbOpenDays-1));
                }else if((!isOffDayOfResource(startDate, idRes, idUser)) && isOffDayOfResource(endDate, idRes, idUser)){
                    dijit.byId(idNbDays).set("value",(nbOpenDays-0.5));
                }else if(isOffDayOfResource(startDate, idRes, idUser) && (!isOffDayOfResource(endDate, idRes, idUser))){
                    dijit.byId(idNbDays).set("value",(nbOpenDays-0.5));
                }else{
                    dijit.byId(idNbDays).set("value",(nbOpenDays));
                }
            }
        }else{
            dijit.byId(idNbDays).set("value",nbOpenDays);
        }
    }else{
        dijit.byId(idNbDays).set("value",0);
    }
    
}

function calculateNbRemainingDays(from="", idRes=null, idUser=null, nbDays=null, idStatus=null, idLeaveType=null) {
    if (from === "" || idRes === null || idUser === null) {return "";}
    var nbRemainingDays="";
    var theLeft = null;
    var theNbDays = null;
    var theLeaveNbDays = null;
    var idLeave=null;
    var idStatusNew=null;
    var idStatusOld=null;
    var idLeaveTypeNew=null;
    var idLeaveTypeOld=null;
    
    if (from === "fromLeaveMain") {
        nbRemainingDays="idNbDaysRemaining";
        if (idStatus === null || idLeaveType === null) { 
            dijit.byId(nbRemainingDays).set("value","");
            dojo.byId(nbRemainingDays).style.display="none";
            return;            
        }
        if (employeeLeft!=idRes) {
            getLeftByLeaveType(idRes);
        }
        left = leftArray[dijit.byId('idLeaveType').value];
        theNbDays = Number(dijit.byId('nbDays').value);
        theLeaveNbDays = nbDays;
        idLeave=dijit.byId('id').value;
        idStatusNew=dijit.byId('idStatus').value;
        idStatusOld=idStatus;
        idLeaveTypeNew=dijit.byId('idLeaveType').value;
        idLeaveTypeOld=idLeaveType;
    } else if (from === "fromLeaveCalendar") {
        nbRemainingDays = 'popupNbRemainingDays';
        left = leftArray[dijit.byId('popupLeaveType').value];
        theNbDays = Number(dijit.byId('popupNbDays').value);
        theLeaveNbDays = Number(theSelectedLeave.nbDays);
        idLeave = dijit.byId('popupLeaveId').value
        idStatusNew = dijit.byId('popupStatus').value;
        idStatusOld = theSelectedLeave.idStatus;
        idLeaveTypeNew = dijit.byId('popupLeaveType').value;
        idLeaveTypeOld = theSelectedLeave.idLeaveType;        
    } else {
        return;
    }

    // left is null => No limit
    if (left==null) {
        dijit.byId(nbRemainingDays).set("value","");
        dojo.byId(nbRemainingDays).style.display="none";
        return;
    }

    if (left!=null) {theLeft = Number(left);}
    
    dojo.byId(nbRemainingDays).style.display="block";

    // New Leave
    // ---------
    if (idLeave<0 || idLeave=="") {
        // status is cancelled
        if (idStatusNew == 9) {
            dijit.byId(nbRemainingDays).set("value",theLeft);
        } else {
            dijit.byId(nbRemainingDays).set("value",theLeft-theNbDays);
        }
        return;
    }

    // Existing Leave
    // --------------
    // status was not cancelled and become cancelled
    if (idStatusOld != 9 && idStatusNew == 9) {
        // Same LeaveType
        if (idLeaveTypeOld == idLeaveTypeNew) {                    
            dijit.byId(nbRemainingDays).set("value",theLeft+theLeaveNbDays);
        } else {
            dijit.byId(nbRemainingDays).set("value",theLeft);                    
        }
        return;
    }
    // status is cancelled but become not cancelled
    if (idStatusOld == 9 && idStatusNew != 9) {
        dijit.byId(nbRemainingDays).set("value",theLeft-theNbDays);
        return;            
    }

    // status was not cancelled and don't become cancelled
    if (idLeaveTypeOld == idLeaveTypeNew) {
        // Type don't change
        dijit.byId(nbRemainingDays).set("value",theLeft-theNbDays+theLeaveNbDays);            
    } else {
        // Type change
        dijit.byId(nbRemainingDays).set("value",theLeft-theNbDays);            
    }    
}

/* ****************************************************************
 * for the changes of the spe StartAM in model/LeaveMain.php 
 * @param {inter} idUser : The connected user's id
 * @returns {undefined}
 */
function changesStartAM(idUser=null,nbDays=null,idStatus=null,idLeaveType=null) {
    var idRes = getIdEmployeeInLeaveCalMain();
    var input = dijit.byId("startAM");
    if(input.checked){
        dijit.byId("startPM").set("checked",false);
    }else{
        dijit.byId("startPM").set("checked",true);
    }
    calculateHalfDaysForLeave('startDate', 'endDate', 'startAM', 'startPM', 'endAM', 'endPM', 'nbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveMain", idRes, idUser,nbDays,idStatus,idLeaveType);
    formChanged();
}

/* ****************************************************************
 * for the changes of the spe StartPM in model/LeaveMain.php 
 * @param {inter} idUser : The connected user's id
 * @returns {undefined}
 */
function changesStartPM(idUser=null,nbDays=null,idStatus=null,idLeaveType=null) {
    var idRes = getIdEmployeeInLeaveCalMain();
    var input = dijit.byId("startPM");
    if(input.checked){
        dijit.byId("startAM").set("checked",false);
    }else{
        dijit.byId("startAM").set("checked",true);
    }
    calculateHalfDaysForLeave('startDate', 'endDate', 'startAM', 'startPM', 'endAM', 'endPM', 'nbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveMain", idRes, idUser,nbDays,idStatus,idLeaveType);
    formChanged();    
    
}

/* ****************************************************************
 * for the changes of the spe EndAM in model/LeaveMain.php 
 * @param {inter} idUser : The connected user's id
 * @returns {undefined}
 */
function changesEndAM(idUser=null,nbDays=null,idStatus=null,idLeaveType=null) {
    var idRes = getIdEmployeeInLeaveCalMain();
    var input = dijit.byId("endAM");
    if(input.checked){
        dijit.byId("endPM").set("checked",false);
    }else{
        dijit.byId("endPM").set("checked",true);
    }
    calculateHalfDaysForLeave('startDate', 'endDate', 'startAM', 'startPM', 'endAM', 'endPM', 'nbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveMain", idRes, idUser,nbDays,idStatus,idLeaveType);
    formChanged();    
    
}

/* ****************************************************************
 * for the changes of the spe EndPM in model/LeaveMain.php 
 * @param {inter} idUser : The connected user's id
 * @returns {undefined}
 */
function changesEndPM(idUser=null,nbDays=null,idStatus=null,idLeaveType=null) {
    var idRes = getIdEmployeeInLeaveCalMain();
    var input = dijit.byId("endPM");
    if(input.checked){
        dijit.byId("endAM").set("checked",false);
    }else{
        dijit.byId("endAM").set("checked",true);
    }
    calculateHalfDaysForLeave('startDate', 'endDate', 'startAM', 'startPM', 'endAM', 'endPM', 'nbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveMain", idRes, idUser,nbDays=null,idStatus=null,idLeaveType=null);
    formChanged();      
}
//end of the functions for model/leaveMain.php


//start calendar, variables for the initialization of the calendar
var leavesCalendarData = [];
var leavesTypesArray = [];
var statusArray = [];
var leftArray = [];
var employeeLeft = null;
var idEvent=0;
var daysOffCalendarData = [];

/**
 * fill the array daysOffCalendarData with the days off of the current user's calendar
 * @param {String} startDate
 * @param {String} endDate
 * @returns void
 */
function fillDaysOffCalendarDataArray(startDate,endDate){
    var idRes = null;
    var idUser = null;
    var idDayOff = 0;
    var theDate = new Date(startDate);
    theDate.setHours(0,0,0,0);
    var theEndDate = new Date(endDate);
    theEndDate.setHours(23,59,59,0);
    daysOffCalendarData = [];
    
    // The resource for which search off day
    idRes = getIdEmployeeInLeaveCalMain(true);
    // The user
    idUser = getIdUserInLeaveCalendar();
    
    if (idRes != idUser) {        
        initOffDayOfRes(idRes,transformDateToSqlDate(startDate),transformDateToSqlDate(endDate));
    }
    while (theDate<=theEndDate) {
        if (isOffDayOfResource(theDate, idRes, idUser)) {
            var theStartTime = new Date(theDate);
            theStartTime.setHours(0,0,0,0);
            var theEndTime = new Date(theDate);
            theEndTime.setHours(23,59,59,0);
            var dayOff={
                id: 'do'+idDayOff,
                calendar: "OFF",
                startTime: theStartTime,
                endTime: theEndTime,
                cssStyle: 'calendarDayOff'
            };
            daysOffCalendarData.push(dayOff);
            idDayOff++;
        }
        theDate = addDaysToDate(theDate, 1);
    }
}

/**
 * fill the array leavesCalendarData with the leaves already registered for the current user
 * @param {String} startDate
 * @param {String} endDate
 * @returns void
 */
function fillLeavesCalendarDataArray(startDate,endDate){
    //getLeaves returns an array containing three sub-arrays, one containing all of the existing leaves of the user between startDate and endDate, the second containing all the leaveTypes, the third all the existing status
    var idRes = getIdEmployeeInLeaveCalMain(true);
    leavesCalendarData = [];
    idEvent=0;
    dojo.xhrGet({
        url : "../tool/getLeaves.php?startDate="+startDate+"&endDate="+endDate+"&idRes="+idRes,
        handleAs : "text",
        sync: true,
        load : function(data, args) {
            var leaves=JSON.parse(data);
            if (isSqlElementOperationStatus(leaves)) {
               showSqlElementResultInPopup(leaves);
               return;
            }
            
            leavesTypesArray = leaves["leaveTypes"];
            statusArray = leaves["status"];
            leftArray = leaves["left"];
            employeeLeft = idRes;
            
            if(leaves['leaves']!=="empty"){                                
                for(var it in leaves['leaves']){
                    //to get the name of the leaveType of the current leave
                    var lvT="";
                    var lvtColor="";
                    for(var i in leaves["leaveTypes"]){
                        if( leaves["leaveTypes"][i]["id"] === leaves['leaves'][it]["idLeaveType"]){
                            lvT=leaves["leaveTypes"][i]["name"];
                            lvtColor=leaves["leaveTypes"][i]["color"];
                            break;
                        }
                    }
                    for(var i in leaves["status"]){
                        if( leaves["status"][i]["id"] === leaves['leaves'][it]["idStatus"]){
                            statusColor=leaves["status"][i]["color"];
                            break;
                        }
                    }
                    var nbDays = parseFloat(leaves['leaves'][it]["nbDays"]);
                    if(lvtColor==null){
                        lvtColor='#FFFFFF';
                    }
                    var textColor = oppositeColor(lvtColor, true);
                    var leaveType = '<span class="leaveType" style="background-color:'+lvtColor+'; color:'+textColor+';">&nbsp;'+lvT+'&nbsp;</span>';
                    var summary = leaveType + "<br>" + nbDays + ' ' + (nbDays<=1?i18n('day'):i18n('days'));      

                    var startDate = new Date(leaves['leaves'][it]["startDate"]);
                    var endDate = new Date(leaves['leaves'][it]["endDate"]);
                   
                    //to set the hours of the leave
                    if(leaves['leaves'][it]["startAMPM"] === "AM"){ 
                        startDate.setHours(9,0,0,0);
                    }else{
                        startDate.setHours(14,0,0,0);
                    }
                    if(leaves['leaves'][it]["endAMPM"] === "AM"){ 
                        endDate.setHours(12,0,0,0);
                    }else{
                        endDate.setHours(18,0,0,0);
                    }
                    
                    var event={
                        id: parseInt(it),
                        summary: summary,
                        startTime: startDate,
                        endTime: endDate,
                        startAMPM: leaves['leaves'][it]["startAMPM"],
                        endAMPM: leaves['leaves'][it]["endAMPM"],
                        idLeave: leaves['leaves'][it]["id"],
                        idCreator: leaves['leaves'][it]["idUser"],
                        idValidator: leaves['leaves'][it]["idResource"],
                        idRequester: leaves['leaves'][it]["idEmployee"],
                        idLeaveType: leaves['leaves'][it]["idLeaveType"],
                        status: leaves['leaves'][it]["idStatus"],
                        color: statusColor,
                        reason: leaves['leaves'][it]["comment"],
                        nbDays: leaves['leaves'][it]["nbDays"],
                        cssStyle: 'leaveStatus leaveStatus_' + leaves['leaves'][it]["idStatus"]
                    };
                    leavesCalendarData.push(event);
                }
                idEvent=leavesCalendarData.length;
            }
        },
        error: function(error){
            showXhrErrorInErrorPopup(error, 'getLeaves');
        }
    });
}


/**
 * return the date to the format month/year in the form of a string 
 * @param {type} date an object date
 * @returns {String}
 */
function formatMonthYearDate(date) {
    var monthNames = [i18n("January"), 
                      i18n("February"),
                      i18n("March"),
                      i18n("April"),
                      i18n("May"),
                      i18n("June"),
                      i18n("July"),
                      i18n("August"),
                      i18n("September"),
                      i18n("October"),
                      i18n("November"),
                      i18n("December")];
    var monthIndex = date.getMonth(), year = date.getFullYear();
    return monthNames[monthIndex] + ' ' + year;
}

/** 
 * contains the function to create the calendar in view/leaveCalendar.php at the div #calendarNode
 * @returns void
 */
function leaveCalendarDisplay(){ 
    require(["dojo/ready", 
             "dojox/calendar/projeqtorCalendar", 
             "dojo/dom-construct", 
             "dojo/store/Observable", 
             "dojo/store/Memory", 
             "dojo/dom-style", 
             "dijit/registry"
//             "dojo/domReady!"
            ],
        function(ready, projeqtorCalendar, domConstruct, Observable, Memory, domStyle, registry){
            ready(function(){
            
            //creation of the new calendar    
                calendar = new projeqtorCalendar({
                    date: new Date(),
                    dateInterval: "month",
                    // Item css style in function of it's status
                    cssClassFunc: function(item){
                        if (typeof(item.color)!=="undefined" && typeof(item.status)!=="undefined") {
                            createLeaveStatusClass('.leaveStatus_'+item.status, item.color);
                        }
                        return item.cssStyle;
                    },
                    style: "position:relative;width:100%;height:100%"
                }, "calendarNode");                                
            //a boolean to differenciate the actions of the buttons of the popup (between the creation or the modification)
                var creatBool =false;
                
            //function to fill the store of the calendar
                var fillCalendarStore = function(){
                    var cY = calendar.date.getFullYear();
                    var cM = calendar.date.getMonth();
                    //startDate and endDate are string to respect the format required for the request
                    var startDate = cM===0 ? ((cY-1)+"1223") : (cY + "" + ((cM) <10 ? "0"+(cM) : (cM) ) + "21");
                    var endDate = ( (cM+2) <13 ? (cY + "" + ((cM + 2) <10 ? "0"+(cM+2) : (cM+2) ) + "07"): ((cY+1) + "0107") );
                    fillLeavesCalendarDataArray(startDate,endDate);
                    calendar.set("store", new Observable(new Memory({data: leavesCalendarData})));                    
                };//end fillCalendarStore
                
            //to fill the store with the leaves of the current month at the creation of the calendar
                fillCalendarStore();
            
            // function to fill the decorationStor of the calendar (contents days off)
                var fillCalendarDecorationStore = function() {
                    var cY = calendar.date.getFullYear();
                    var cM = calendar.date.getMonth()+1;
                    var theCalendarDate = new Date(cY+'-'+(cM<10?'0':'')+cM+'-01');
                    var startDate = new Date(theCalendarDate);
                    var days = -24;
                    startDate.setDate(days);                    
                    var endDate = new Date(theCalendarDate);
                    days = 70;
                    endDate.setDate(days);
                    fillDaysOffCalendarDataArray(startDate,endDate);
                    calendar.set("decorationStore", new Observable(new Memory({data: daysOffCalendarData})));                                        
                }
                fillCalendarDecorationStore();
                
            //if there is no leaveTypes, there is no calendar
                if(leavesTypesArray.length===0){
                    document.getElementById("calendarNode").innerHTML = "<p>"+i18n("noLeaveTypeNoLeaveCalendar")+"</p>";
                    return;
                }
            
            
            //to remove the row header column of the calendar
                domStyle.set(calendar.matrixView.columnHeader,'left','0px');
                domStyle.set(calendar.matrixView.columnHeader,'border-left','1px solid #B5BCC7');
                domStyle.set(calendar.matrixView.grid,'left','0px');
                domStyle.set(calendar.matrixView.grid,'border-left','1px solid #B5BCC7');
                domStyle.set(calendar.matrixView.itemContainer,'left','0px');
                domStyle.set(calendar.matrixView.buttonContainer,'position','relative');
                domConstruct.destroy(calendar.matrixView.rowHeader);
                domConstruct.destroy(calendar.matrixView.yearColumnHeader);
            
            //to display the current month and year of the calendar in the header
                var calendarMonthYear = "<div id=\"calendarMonthYear\" style=\"position:absolute;\"><table><tbody><tr><td><span>"+formatMonthYearDate(calendar.date)+"</span></td></tr></tbody></table></div>";
                domConstruct.place(calendarMonthYear, calendar.matrixView.buttonContainer, "first");
            
            
            //a function to refresh the date displayed in the header
                var refreshMonthYearInHeader = function(){
                    var calendarMonthYearContent = "<table><tbody><tr><td><span>"+formatMonthYearDate(calendar.date)+"</span></td></tr></tbody></table>";
                    document.getElementById("calendarMonthYear").innerHTML = calendarMonthYearContent;
                };
            
            //to display the next month and the current year in the header when clicking the nextButton of the calendar
                require(["dojo/on"], function(on){
                    on(calendar.nextButton, "click", function(e){
                        refreshMonthYearInHeader();
                        widgetSelectDate.set("value", calendar.date);
                    });
                });
                
            //to display the previous month and the current year in the header
                require(["dojo/on"], function(on){
                    on(calendar.previousButton, "click", function(e){
                        refreshMonthYearInHeader();
                        widgetSelectDate.set("value", calendar.date);
                    });
                });
                  
            //two arrays to make the link between the names and ids of the leaveType and the status 
                var lengthLvTArray = leavesTypesArray.length;
                var popupTypeSelectOptions = [];
                for(var i = 0; i< lengthLvTArray; i++){
                    popupTypeSelectOptions.push({value:leavesTypesArray[i].id, label:leavesTypesArray[i].name, selected:false, disabled:false}); 
                }
                
                var lengthStatusArray = statusArray.length;
                var popupStatusSelectOptions = [];
                var linkIdNameStatus = [];
                for(var i = 0; i< lengthStatusArray; i++){
                    popupStatusSelectOptions.push({value:statusArray[i].id, label:statusArray[i].name, selected:false, disabled:false}); 
                    linkIdNameStatus[statusArray[i].id]= statusArray[i].name; 
                }

            //function to disable the buttons of the popup
                  var disablePopupButtons = function(){
                        validateButtonCalendarPopup.set("disabled", true);
                        deleteButtonCalendarPopup.set("disabled", true);
                        cancelButtonCalendarPopup.set("disabled", true);
                        //the closeButtonNode is a <span>, it can't be disabled
                        dojo.setStyle(dijit.byId('leavePopup').closeButtonNode,{"display": "none", "visibility": "hidden"});
                  };
                  
                  
            //to show the popup after creating an item or dblClicking/resizing/moving it to modify it
                var showModifPopup = function(idLeave, leaveType, status, startDate, endDate, startAMPM, endAMPM, nbDays, reason, create=false){
                    cancelButtonCalendarPopup.set("disabled", false);
                    dojo.setStyle(dijit.byId('leavePopup').closeButtonNode,{"display": "", "visibility": ""});
                    // On new leave, status can't be changed
                    if (dijit.byId('popupLeaveId').value < 1  ) {
                        dijit.byId('popupStatus').set("disabled", true);
                    } else {
                        // If, it's manager, status can change
                        if (document.getElementById('isManagerCalendar').value == "1") {
                            dijit.byId('popupStatus').set("disabled", false);
                        } else {
                            dijit.byId('popupStatus').set("disabled", true);
                        }
                    }
                    if(status==="1"){
                        dijit.byId('popupLeaveType').set("disabled", false);
                        dijit.byId('popupStartDate').set("disabled", false);
                        dijit.byId('popupEndDate').set("disabled", false);
                        dijit.byId("popupStartAM").set("disabled", false);
                        dijit.byId("popupStartPM").set("disabled", false);
                        dijit.byId("popupEndAM").set("disabled", false);
                        dijit.byId("popupEndPM").set("disabled", false);
                        dijit.byId('popupReason').set("disabled", false);                        
                        validateButtonCalendarPopup.set("disabled", false);
                        deleteButtonCalendarPopup.set("disabled",false);
                    }else{
                        dijit.byId('popupLeaveType').set("disabled", true);
                        dijit.byId('popupStartDate').set("disabled", true);
                        dijit.byId('popupEndDate').set("disabled", true);
                        dijit.byId("popupStartAM").set("disabled", true);
                        dijit.byId("popupStartPM").set("disabled", true);
                        dijit.byId("popupEndAM").set("disabled", true);
                        dijit.byId("popupEndPM").set("disabled", true);
                        dijit.byId('popupReason').set("disabled", true);
                        // If, it's manager, status can change
                        if (document.getElementById('isManagerCalendar').value === "1") {
                            validateButtonCalendarPopup.set("disabled", false);
                        } else {
                            validateButtonCalendarPopup.set("disabled", true);
                        }
                        deleteButtonCalendarPopup.set("disabled", true);
                    }
                    if(creatBool){
                        deleteButtonCalendarPopup.set("disabled", true);
                    }
                    
                //to set the values of the fields of the popup before opening it
                    registry.byId('popupLeaveId').set("value", idLeave);
                    registry.byId('popupLeaveType').set("options", popupTypeSelectOptions);
                    dijit.byId('popupLeaveType').attr('value', leaveType);
                    
                    registry.byId('popupStatus').set("options", popupStatusSelectOptions);
                    dijit.byId('popupStatus').attr('value', status);
//                    registry.byId('popupStatus').set("value", status);                    
//                    registry.byId('popupStatus').set("displayedValue", linkIdNameStatus[status]);
                    
                    registry.byId('popupStartDate').set("value", startDate);
                    registry.byId('popupEndDate').set("value", endDate);
                    
                    if(startAMPM==="AM"){
                        dijit.byId("popupStartAM").set("checked",true);
                        dijit.byId("popupStartPM").set("checked",false);
                    }else{
                        dijit.byId("popupStartAM").set("checked",false);
                        dijit.byId("popupStartPM").set("checked",true);
                    }
                    if(endAMPM==="AM"){                     
                        dijit.byId("popupEndAM").set("checked",true);
                        dijit.byId("popupEndPM").set("checked",false);
                    }else{
                        dijit.byId("popupEndAM").set("checked",false);
                        dijit.byId("popupEndPM").set("checked",true);
                    }                    
                    
                    registry.byId('popupNbDays').set("value", nbDays);
                    //popupNbRemainingDays
                    registry.byId('popupReason').set("value", reason);            
                    dijit.byId('leavePopup').show();
                };//end of showModifPopup
            
            
              
            //create new item on grid dblClick
                calendar.on("gridDoubleClick", function(e){
                    creatBool = true;
                    var start, end;               
                    var cal = calendar.dateModule;                                
                    start = calendar.floorToDay(e.date);
                    end = calendar.floorToDay(e.date);
                    
                    //can't create a leave if it's a dayOff
                    for(let dayOff of daysOffCalendarData){
                        if(dayOff.startTime.getTime()===start.getTime()){
                            return;
                        }
                    }
                    start.setHours(9,0,0,0);
                    end.setHours(18,0,0,0);                    
                    
                    var item = {
                        id: idEvent,
                        summary: "",
                        startTime: start,
                        endTime: end,
                        status: statusArray[0].id,
                        startAMPM: "AM",
                        endAMPM: "PM",
                        idLeave: null,
                        idLeaveType: popupTypeSelectOptions[0].value,
                        reason: "",
                        nbDays: 1.0
                    };
                    calendar.store.add(item);
                    
                    theSelectedLeave = item;
                    showModifPopup(item.idLeave, item.idLeaveType, item.status, start, end, item.startAMPM, item.endAMPM, item.nbDays, item.reason, true);
                    
                    idEvent++;
                });
            //end of the creation of a new item
                
            //when double clicking an item, a popup appear to modify it
                calendar.on('itemDoubleClick',function(e){
                    creatBool = false;
                    theSelectedLeave = e.item;                    
                    showModifPopup(e.item.idLeave, e.item.idLeaveType, e.item.status, e.item.startTime, e.item.endTime, e.item.startAMPM, e.item.endAMPM, e.item.nbDays, e.item.reason);
                });
            
            //a listener to trigger the popup when an item was edited
                calendar.on("itemEditEnd", function(e){
                    creatBool = false;
                    if(e.item._item.status !== "1"){//if the leave was validated, cancel the resize/deplacement
                        e.preventDefault();
                        e.item.startTime=e.item._item.startTime;
                        e.item.endTime=e.item._item.endTime;                        
                    }else{//calculate the new number of days of the leave
                        var idRes = getIdEmployeeInLeaveCalMain(true);
                        var idUser = getIdUserInLeaveCalendar();
                        var nbOpenDays = openDayDiffDates(e.item.startTime,e.item.endTime, idRes, idUser);
                        if(e.item._item.startAMPM==="AM" && e.item._item.endAMPM==="PM"){
                            var newNbDays = nbOpenDays;
                        }else if(e.item._item.startAMPM==="AM" && e.item._item.endAMPM==="AM"){
                            var newNbDays = nbOpenDays - 0.5;
                        }else if(e.item._item.startAMPM==="PM" && e.item._item.endAMPM==="PM"){
                            var newNbDays = nbOpenDays - 0.5;
                        }else{
                            var newNbDays = nbOpenDays - 1;
                        }
                        if(newNbDays<0){
                            newNbDays=0;
                        }
                        if(newNbDays<=0){
                            e.preventDefault();
                            e.item.startTime=e.item._item.startTime;
                            e.item.endTime=e.item._item.endTime;
                            return;
                        }                        
                        calendar.items[e.item.id]._item.nbDays = newNbDays;
                    }
                    //note: e.item.startTime is given to showModifPopup and not e.item._item.startTime, as the e.item._item contains the previous state of the object
                    theSelectedLeave = e.item._item;                    
                    showModifPopup(e.item._item.idLeave, e.item._item.idLeaveType, e.item._item.status, e.item.startTime, e.item.endTime, e.item._item.startAMPM, e.item._item.endAMPM, newNbDays, e.item._item.reason);
                });
                                
            //to remove the possibility to close the popup by pressing the escape key
                dijit.byId("leavePopup")._onKey = function(){};
            
            //to refresh the content of the summary after the save/delete of a leaveType or the change of employee (refreh all the content of the tab summary) 
                var refreshEmployeeCustomLeavesEarned=function(){
                    var idEmployee=document.getElementById('idEmployeeCalendar').value;
                    //xhrGet, return an array with every lines to be put in the div summaryLeaveEarnedOfEmployee
                    //args=idEmployee
                    //return lvType:(idLeaveType/color),lvTypeOfEmpContractType:(periodDuration),lvEarned:(startDate/endDate/quantity/leftQuantity), 
                    dojo.xhrGet({
                        url : "../tool/getEmployeeCustomLeavesEarned.php?idEmployee="+idEmployee,
                        handleAs : "text",
                        sync: true,
                        load : function(data, args) {
                            var lines = JSON.parse(data);
                            if (isSqlElementOperationStatus(lines)) {
                                showSqlElementResultInPopup(lines);
                                return;
                            }
                            var customContent = '<b>'+i18n("extraActualLeaveEarned")+'</b>';
                            customContent += '<table style="width:96%; margin-left:2%; text-align:center; border: solid 1pt;">\n\
                                                  <tr style="border: solid 1pt; height: 20px;">\n\
                                                  <th style="text-align:center;"><b>'+ i18n('leaveType')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('colName')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('quantity')+'</b></th></tr>';
                            var len =lines.length;
                            var find = false;
                            for(var i=0;i<len;i++){
                               var lenCustom = lines[i].custom.length;
                               if (lenCustom!==0) {
                                    find=true;
                                    customContent += '<tr style="border: solid 1pt; height: 20px;">';
                                    customContent += '<td style="background-color:'+lines[i].lvTColor+';color:'+lines[i].lvTOppositeColor+';">'+lines[i].lvTName+'</td>';                                   
                                    var first = true;
                                    for(var j=0;j<lenCustom;j++) {
                                        if (first===false) {
                                            customContent += '<tr style="border: solid 1pt; height: 20px;">';
                                            customContent += '<td style="text-align:right;" colspan="2">'+lines[i].custom[j].name+'</td>';
                                        } else {
                                            customContent += '<td>'+lines[i].custom[j].name+'</td>';
                                        }
                                        customContent += '<td>'+lines[i].custom[j].quantity+'</td>';
                                        if (first===false) {
                                            customContent += '</tr>';
                                        }
                                        first=false;
                                    }
                                    customContent += '</tr>';
                               } 
                            }
                            customContent += '</table>';
                            if (find) {
                                document.getElementById("customLeaveEarnedOfEmployee").innerHTML=customContent;
                            } else {
                                document.getElementById("customLeaveEarnedOfEmployee").innerHTML="";
                            }    
                        },
                        error: function(error){
                            showXhrErrorInErrorPopup(error, 'getEmployeeCustomLeavesEarned');
                        }
                    });
                };

                //to refresh the content of the summary after the save/delete of a leaveType or the change of employee (refreh all the content of the tab summary) 
                var refreshEmployeeLeavesSummary=function(){
                    var idEmployee=document.getElementById('idEmployeeCalendar').value;
                    //xhrGet, return an array with every lines to be put in the div summaryLeaveEarnedOfEmployee
                    //args=idEmployee
                    //return lvType:(idLeaveType/color),lvTypeOfEmpContractType:(periodDuration),lvEarned:(startDate/endDate/quantity/leftQuantity), 
                    dojo.xhrGet({
                        url : "../tool/getEmployeeLeaveEarnedSummary.php?idEmployee="+idEmployee,
                        handleAs : "text",
                        sync: true,
                        load : function(data, args) {
                            var lines = JSON.parse(data);
                            if (isSqlElementOperationStatus(lines)) {
                                showSqlElementResultInPopup(lines);
                                return;
                            }
                            var summaryContent = '<b>'+i18n("synthesisOfLeaveEarned")+'</b>';
                            summaryContent += '<table style="width:96%; margin-left:2%; text-align:center; border: solid 1pt;">\n\
                                                  <tr style="border: solid 1pt; height: 20px;">\n\
                                                  <th style="text-align:center;"><b>'+ i18n('leaveType')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('periodDuration')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('leavePeriod')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('quantity')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('taken')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('colLeft')+'</b></th>\n\
                                                  <th style="text-align:center;"><b>'+ i18n('earnedPeriodPlusOne')+'</b></th></tr>';
                            var len =lines.length;
                            for(var i=0;i<len;i++){
                               summaryContent += '<tr style="border: solid 1pt; height: 20px;">';
                               summaryContent += '<td style="background-color:'+lines[i].lvTColor+';color:'+lines[i].lvTOppositeColor+';">'+lines[i].lvTName+'</td>';
                               summaryContent += '<td>'+lines[i].periodDuration+'</td>';
                               summaryContent += '<td>'+lines[i].startDateEndDate+'</td>';
                               summaryContent += '<td>'+lines[i].quantity+'</td>';
                               summaryContent += '<td>'+lines[i].taken+'</td>';
                               summaryContent += '<td>'+lines[i].left+'</td>';
                               summaryContent += '<td>'+lines[i].earnedPeriodPlusOne+'</td>';
                               summaryContent += '</tr>';
                               
                            }
                            summaryContent += '</table>';
                            document.getElementById("summaryLeaveEarnedOfEmployee").innerHTML=summaryContent;
                        },
                        error: function(error){
                            showXhrErrorInErrorPopup(error, 'getEmployeeLeaveEarnedSummary');
                        }
                    });
                };
                        
            //the listeners for the actions of the buttons of the popup
                //the function for the action of the validate button
                var validPopupAction = function(){
                    showWait();
                    dijit.byId('leavePopup').hide();
                    var idEmpl = getIdEmployeeInLeaveCalMain(true);
                    
                    //the url for the xhrGet request to save the leave
                    var url = "../tool/saveLeaveOfCalendar.php?\n\
                            idEmployee="+idEmpl+"&\n\
                            idLeaveType="+dijit.byId("popupLeaveType").value+"&\n\
                            idLeaveStatus="+dijit.byId("popupStatus").value+"&\n\
                            &startDate="+dijit.byId("popupStartDate").value+"&\n\
                            endDate="+dijit.byId("popupEndDate").value+"&nbDays="+dijit.byId("popupNbDays").value+"&comment="+dijit.byId("popupReason").value;
                            if(dijit.byId("popupStartAM").checked){
                                url +="&startAMPM=AM";  
                            }else{
                                url +="&startAMPM=PM";  
                            }
                            if(dijit.byId("popupEndAM").checked){
                                url +="&endAMPM=AM";  
                            }else{
                                url +="&endAMPM=PM";  
                            }
                    if(creatBool === true){
                        url = url + "&create=true";
                    }else{//if it's a modification, give the id of the leave to update it
                       url = url + "&create=false&idLeave="+dijit.byId("popupLeaveId").value;
                    }
                    //the request to save the leave, is synchronous so the new leave is displayed after the call to fillCalendarStore()
                    dojo.xhrGet({
                        url : url,
                        handleAs : "text",
                        sync: true,
                        load : function(data, args) {
                            var res=JSON.parse(data);
                            showSqlElementResultInPopup(res);
                            refreshEmployeeLeavesSummary();
                        },
                        error: function(error){
                            showXhrErrorInErrorPopup(error, 'saveLeaveOfCalendar');
                        }
                    });
                    
                    fillCalendarStore();
                };
                               
                var deletePopupAction = function(){
                    showWait();
                    dijit.byId('leavePopup').hide();
                    //if the object is already saved on the database delete it and recorded
                    if(creatBool === false){
                       dojo.xhrGet({
                            url : "../tool/deleteLeaveOfCalendar.php?idLeave="+dijit.byId("popupLeaveId").value,
                            handleAs : "text",
                            sync: true,
                            load : function(data, args) {
                                var res=JSON.parse(data);
                                showSqlElementResultInPopup(res);
                                refreshEmployeeLeavesSummary();
                            },
                            error: function(error){
                                showXhrErrorInErrorPopup(error, 'deleteLeaveOfCalendar');
                            }
                        });                       
                        
                    } else {
                        hideWait();
                    }
                    
                    fillCalendarStore();
                };
                               
                var cancelPopupAction = function(){
                    dijit.byId('leavePopup').hide();
                    fillCalendarStore();
                };
                               
            //a listener for the validate button of the popup, WIP
                validateButtonCalendarPopup.on("click", function(e){
                    disablePopupButtons();
                    validPopupAction();                   
                });

            //a listener for the delete button of the popup, WIP
                deleteButtonCalendarPopup.on("click", function(e){
                    disablePopupButtons();
                    deletePopupAction();                    
                });

            //a listener for the cancel button of the popup, WIP
                cancelButtonCalendarPopup.on("click", function(e){
                    disablePopupButtons();
                    cancelPopupAction();                    
                });
                
            //a listener to replace the action of the button close of the popup by the action of cancel
                require(["dojo/on"], function(on){
                    on(dijit.byId('leavePopup').closeButtonNode, "click", function(e){
                        disablePopupButtons();
                        cancelPopupAction();
                    });
                });
                
            //a listener for the button to refresh the calendar
                refreshCalendarButton.on("click", function(e){
                    calendar.set('date', (new Date()));
                    widgetSelectDate.set("value", new Date());
                    refreshMonthYearInHeader();
                    fillCalendarStore();
                    fillCalendarDecorationStore();

                });
            
            // a listener for the select leaveEmployeeSelect to refresh the calendar this leave of selected employee
                if (dojo.byId('leaveEmployeeSelect')) {
                    leaveEmployeeSelect.on("change", function(e){
                        calendar.set('date', (new Date()));
                        widgetSelectDate.set("value", new Date());
                        document.getElementById('idEmployeeCalendar').value = e;
                        refreshMonthYearInHeader();
                        fillCalendarStore();
                        fillCalendarDecorationStore();
                        refreshEmployeeCustomLeavesEarned();
                        refreshEmployeeLeavesSummary();
                    });
                }
            
            //a DateTextBox to change the month and/or year of the calendar
                widgetSelectDate.on("change", function(e){
                    calendar.set("date", e);
                    refreshMonthYearInHeader();
                    fillCalendarStore();
                    fillCalendarDecorationStore();
                });
                
                widgetSelectDate.set("value", new Date());
            
            //a tooltip to show more infos about a leave when passing the mouse over it
                calendar.on("itemRollOver", function (e) {
                    if(e.item.reason){
                        var reason = e.item.reason;
                    }else{
                        var reason = "none";
                    }
                    var toolTipContent = "<p>Reason:</p>"+reason+"<br/>"+"<p>Duration:</p>"+e.item.nbDays+"<br/>"+"<p>Status:</p>"+linkIdNameStatus[e.item.status];
                    tooltipNode = e.renderer.domNode;     
                    dijit.showTooltip(toolTipContent,  tooltipNode, ["below"]); 

                 }); 

                calendar.on("itemRollOut", function (e) { 
                    dijit.hideTooltip(tooltipNode); 
                }); 

                calendar.on("itemEditBegin", function (e) { 
                    dijit.hideTooltip(tooltipNode); 
                });            
            });
        }
    );
}//end displayCalendar

//for the changes of the checkboxes of the popup in view/LeaveCalendar.php
/**
 * define the action of the checkbox StartAM of the popup, if it has been ticked, untick the checkbox StartPM and inversely
 * @returns void
 */
function changesPopupStartAM() {
    var idRes = getIdEmployeeInLeaveCalMain(true);
    var idUser = getIdUserInLeaveCalendar();
    var input = dijit.byId("popupStartAM");
    if(input.checked){
        dijit.byId("popupStartPM").set("checked",false);
    }else{
        dijit.byId("popupStartPM").set("checked",true);
    }
    calculateHalfDaysForLeave('popupStartDate', 'popupEndDate', 'popupStartAM', 'popupStartPM', 'popupEndAM', 'popupEndPM', 'popupNbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveCalendar", idRes, idUser);
}

function changesPopupStartPM() {
    var idRes = getIdEmployeeInLeaveCalMain(true);
    var idUser = getIdUserInLeaveCalendar();
    var input = dijit.byId("popupStartPM");
    if(input.checked){
        dijit.byId("popupStartAM").set("checked",false);
    }else{
        dijit.byId("popupStartAM").set("checked",true);
    }    
    calculateHalfDaysForLeave('popupStartDate', 'popupEndDate', 'popupStartAM', 'popupStartPM', 'popupEndAM', 'popupEndPM', 'popupNbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveCalendar", idRes, idUser);
}

function changesPopupEndAM() {
    var idRes = getIdEmployeeInLeaveCalMain(true);
    var idUser = getIdUserInLeaveCalendar();
    var input = dijit.byId("popupEndAM");
    if(input.checked){
        dijit.byId("popupEndPM").set("checked",false);
    }else{
        dijit.byId("popupEndPM").set("checked",true);
    }
    calculateHalfDaysForLeave('popupStartDate', 'popupEndDate', 'popupStartAM', 'popupStartPM', 'popupEndAM', 'popupEndPM', 'popupNbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveCalendar", idRes, idUser);
}

function changesPopupEndPM() {
    var idRes = getIdEmployeeInLeaveCalMain(true);
    var idUser = getIdUserInLeaveCalendar();
   var input = dijit.byId("popupEndPM");
    if(input.checked){
        dijit.byId("popupEndAM").set("checked",false);
    }else{
        dijit.byId("popupEndAM").set("checked",true);
    }
    calculateHalfDaysForLeave('popupStartDate', 'popupEndDate', 'popupStartAM', 'popupStartPM', 'popupEndAM', 'popupEndPM', 'popupNbDays', idRes, idUser);
    calculateNbRemainingDays("fromLeaveCalendar", idRes, idUser);
}

//end fn for the popup
// ********************************
// END FUNCTIONS FOR LEAVE CALENDAR
// ********************************

// ***********************************
//FUNCTIONS FOR EmploymentContractType
// ***********************************

/**
 * to add a new lvTypeOfEmpContractType, Called in model/EmploymentContractTypeMain.php
 * 
 * @param {int} idEmploymentContractType
 * @returns {undefined}
 */
function addLvTypeOfEmpContractType(idEmploymentContractType){
    var params="&idEmploymentContractType="+idEmploymentContractType;
    params +="&addMode=true&editMode=false";
    
    loadDialog('dialogLvTypeOfEmpContractType',null,true,params,true);
}

/**
 * to save a lvTypeOfEmploymentContractType, called in tool/dynamicDialogLvTypeOfEmpContractType.php
 * 
 * @returns void
 */
function saveLvTypeOfEmpContractType() {
  var formVar=dijit.byId('lvTypeOfContractTypeForm');
  if (formVar.validate()) {
    loadContent("../tool/saveLvTypeOfEmpContractType.php", "resultDiv",
        "lvTypeOfContractTypeForm", true, 'leaveTypeOfEmploymentContractType');
    dijit.byId('dialogLvTypeOfEmpContractType').hide();
  } else {
    showAlert(i18n("alertInvalidForm"));
  }
}


/**
 * defines the action of the button to edit a lvTypeOfEmploymentContractType, Called in model/EmploymentContractTypeMain.php
 * 
 * @param {int} idLvTypeOfContractType
 * @param {int} idEmploymentContractType
 * @param {int} idLeaveType
 * @param {int} startMonthPeriod between 1 and 12
 * @param {int} startDayPeriod (not used as this attribute is set to 1 in tool/dynamicDialogLvTypeOfEmpContractType.php)
 * @param {int} periodDuration 
 * @param {float} quantity
 * @param {int} earnedPeriod
 * @param {int} validityDuration
 * @param {int("bool")} isJustifiable
 * @returns void
 */
function editLvTypeOfEmpContractType (idLvTypeOfContractType,
                                      idEmploymentContractType,
                                      idLeaveType,
                                      startMonthPeriod,
                                      startDayPeriod,
                                      periodDuration,
                                      nbDaysAfterNowLeaveDemandIsAllowed,
                                      nbDaysBeforeNowLeaveDemandIsAllowed,
                                      quantity,
                                      earnedPeriod,
                                      isIntegerQuotity,
                                      validityDuration,
                                      isJustifiable,
                                      isAnticipated){
    var params = "&idLvTypeOfContractType="+idLvTypeOfContractType;
    params+="&idEmploymentContractType="+idEmploymentContractType;
    params+="&idLeaveType="+idLeaveType;
    params+="&startMonthPeriod="+startMonthPeriod;
    params+="&startDayPeriod="+startDayPeriod;
    params+="&periodDuration="+periodDuration;
    params+="&nbDaysBeforeNowLeaveDemandIsAllowed="+nbDaysBeforeNowLeaveDemandIsAllowed;
    params+="&nbDaysAfterNowLeaveDemandIsAllowed="+nbDaysAfterNowLeaveDemandIsAllowed;    
    params+="&quantity="+quantity;
    params+="&earnedPeriod="+earnedPeriod;
    params+="&isIntegerQuotity="+isIntegerQuotity;
    params+="&validityDuration="+validityDuration;
    params+="&isJustifiable="+isJustifiable;
    params+="&isAnticipated="+isAnticipated;
    params +="&addMode=false&editMode=true";
    loadDialog('dialogLvTypeOfEmpContractType',null,true,params,true);
}

/**
 * Used in employmentContractTypeMain, defines the actions of deletion of a lvTypeOfEmpContractType
 * 
 * @param {int} id of the LvTypeOfEmpContractType to delete
 * @returns void 
 */
function removeLvTypeOfEmpContractType(id){
    if (checkFormChangeInProgress()) {
    showAlert(i18n('alertOngoingChange'));
    return;
  }
  actionOK=function() {
    loadContent("../tool/removeLvTypeOfEmpContractType.php?idLvTypeOfContractType="+id, "resultDiv",
        null, true, 'leaveTypeOfEmploymentContractType');
  };
  msg=i18n('confirmDeleteLvTypeOfContractType', new Array(id));
  showConfirm(msg, actionOK);
}

/**
 * to add a new CustomEarnedRulesOfEmpContractType, Called in model/EmploymentContractTypeMain.php
 * 
 * @param {int} idEmploymentContractType
 * @returns {undefined}
 */
function addCustomEarnedRulesOfEmpContractType(idEmploymentContractType){
    var params="&idEmploymentContractType="+idEmploymentContractType;
    params +="&addMode=true&editMode=false";
    
    loadDialog('dialogCustomEarnedRulesOfEmpContractType',null,true,params,true);
}

/**
 * defines the action of the button to edit a CustomEarnedRulesOfEmpContractType, Called in model/EmploymentContractTypeMain.php
 * 
 * @param {int} idCustomEarnedRules
 * @param {int} idEmploymentContractType
 * @param {int} idLeaveType
 * @param {float} quantity
 * @param {string} name
 * @param {string} rule
 * @param {string} whereClause
 * @returns void
 */
function editCustomEarnedRulesOfEmpContractType (idCustomEarnedRules,
                                                 idEmploymentContractType,
                                                 idLeaveType,
                                                 quantity,
                                                 name,
                                                 rule,
                                                 whereClause){
    var params = "&idCustomEarnedRules="+idCustomEarnedRules;
    params+="&idEmploymentContractType="+idEmploymentContractType;
    params+="&idLeaveType="+idLeaveType;
    params+="&quantity="+quantity;
    params+="&name="+name;
    params+="&rule="+rule;
    params+="&whereClause="+whereClause;
    params +="&addMode=false&editMode=true";
    loadDialog('dialogCustomEarnedRulesOfEmpContractType',null,true,params,true);
}

/**
 * Used in employmentContractTypeMain, defines the actions of deletion of a CustomEarnedRulesOfEmpContractType
 * 
 * @param {int} idCustomEarnedRulesOfEmpContractType of the CustomEarnedRulesOfEmpContractType to delete
 * @returns void 
 */
function removeCustomEarnedRulesOfEmpContractType(idCustomEarnedRulesOfEmpContractType){
    if (checkFormChangeInProgress()) {
    showAlert(i18n('alertOngoingChange'));
    return;
  }
  actionOK=function() {
    loadContent("../tool/removeCustomEarnedRulesOfEmpContractType.php?idCustomEarnedRulesOfEmpContractType="+idCustomEarnedRulesOfEmpContractType, "resultDiv",
        null, true, 'customEarnedRulesOfEmploymentContractType');
  };
  msg=i18n('confirmDeleteCustomEarnedRulesOfEmpContractType', new Array(id));
  showConfirm(msg, actionOK);
}

/**
 * to save a CustomEarnedRulesOfEmpContractType, called in tool/dialogCustomEarnedRulesOfEmpContractType.php
 * 
 * @returns void
 */
function saveCustomEarnedRulesOfEmpContractType() {
  var formVar=dijit.byId("customEarnedRulesOfEmpContractTypeForm");
  if (formVar.validate()) {
    loadContent("../tool/saveCustomEarnedRulesOfEmpContractType.php", "resultDiv",
        "customEarnedRulesOfEmpContractTypeForm", true, "customEarnedRulesOfEmploymentContractType");
    dijit.byId('dialogCustomEarnedRulesOfEmpContractType').hide();
  } else {
    showAlert(i18n("alertInvalidForm"));
  }
}
// ***************************************
//END FUNCTIONS FOR EmploymentContractType
// ***************************************

// ***************************************
// BEGIN FUNCTIONS FOR EmployeesManaged
// ***************************************
/**
 * to add a new EmployeesManaged, Called in model/EmployesManagerMain.php
 * 
 * @param {int} idEmployeeManager
 * @returns {undefined}
 */
function addEmployeesManaged(idEmployeeManager){
    var params="&idEmployeeManager="+idEmployeeManager;
    params +="&addMode=true&editMode=false";
    
    loadDialog('dialogEmployeesManaged',null,true,params,true);
}

/**
 * defines the action of the button to edit a EmployeesManaged, Called in model/EmployesManagerMain.php
 * 
 * @param {int} idEmployeesManaged
 * @param {int} idEmployeeManager
 * @param {int} idEmployee
 * @param {float} startDate
 * @param {string} endDate
 * @param {string} idle
 * @returns void
 */
function editEmployeesManaged (idEmployeesManaged,
                               idEmployeeManager,
                               idEmployee,
                               startDate,
                               endDate,
                               idle){
    var params = "&id="+idEmployeesManaged;
    params+="&idEmployeeManager="+idEmployeeManager;
    params+="&idEmployee="+idEmployee;
    params+="&startDate="+startDate;
    params+="&endDate="+endDate;
    params+="&idle="+idle;
    params +="&addMode=false&editMode=true";
    loadDialog('dialogEmployeesManaged',null,true,params,true);
}

/**
 * Used in model/EmployesManagerMain.php, defines the actions of deletion of a EmployeesManaged
 * 
 * @param {int} id of the EmployeesManaged to delete
 * @returns void 
 */
function removeEmployeesManaged(id){
    if (checkFormChangeInProgress()) {
    showAlert(i18n('alertOngoingChange'));
    return;
  }
  actionOK=function() {
    loadContent("../tool/removeEmployeesManaged.php?id="+id, "resultDiv",
        null, true, 'employeesManaged');
  };
  msg=i18n('confirmDeleteEmployeesManaged', new Array(id));
  showConfirm(msg, actionOK);
}

/**
 * to save a EmployeesManaged, called in tool/dialogEmployeesManaged.php
 * 
 * @returns void
 */
function saveEmployeesManaged() {
  var formVar=dijit.byId("employeesManagedForm");
  if (formVar.validate()) {
    loadContent("../tool/saveEmployeesManaged.php", "resultDiv",
        "employeesManagedForm", true, "employeesManaged");
    dijit.byId('dialogEmployeesManaged').hide();
  } else {
    showAlert(i18n("alertInvalidForm"));
  }
}
// ***************************************
// END FUNCTIONS FOR EmployeesManaged
// ***************************************

// ***************************************
// OTHERS FUNCTIONS
// ***************************************
/**
 * Return true if the class and id passed in parameters are leave System object (class and id) dedicated to the leave system
 * ie : is the project dedicated to the leave system.
  * @return boolean
 */
function isLeaveMngConditionsKO() {
    var theLeaveClasses = new Array(
        'Workflow', 
        'Project', 
        'Activity', 
        'EmploymentContract',
        'EmploymentContractType'
    );
    // If no id in screen => No leave conditions to manage
    if (!dojo.byId("id")) {return false;}    
    var id = dojo.byId("id").value;
    // If no objectClassName in screen => No leave conditions to manage
    if(!dojo.byId("objectClassName")) {return false;}
    var objClass = dojo.byId("objectClassName").value;
    
    // It's not a class that has conditions for leave
    if (theLeaveClasses.indexOf(objClass)<0) {return false;}
    
    // Can't delete or copy an EmploymentContract. Delete is done with deleted resource
    if (objClass == 'EmploymentContract') {return true;}
    
    // Can't delete or copy an EmploymentContractType that is the default type (isDefault=1)
    if (objClass == 'EmploymentContractType' && 
        dojo.byId('isDefault') && 
        dojo.byId('isDefault').checked  == true) 
    {return true;}
    
    // Workflow and isLeaveMngWorkfow = 1 => Depending on Leave System    
    if (objClass == 'Workflow' && 
        dojo.byId('isLeaveMngWorkflow') && 
        dojo.byId('isLeaveMngWorkflow').checked == true)
    {return true;}
    
    // Project and isLeaveMngProject = 1 => Depending on Leave System
    if (objClass == 'Project' && 
        dojo.byId('isLeaveMngProject') && 
        dojo.byId('isLeaveMngProject').checked==true)
    {return true;}
    
    // Activity and its Project is the leave management project => Depending on Leave System
    if (objClass == 'Activity' && typeof(document.getElementById('isLeaveMngActivity'))!='undefined') {
        if (document.getElementById('isLeaveMngActivity').value == 1) { return true;}
    } 
    
    // Else return false
    return false;
}


/**
 * used in dynamicDialogCustomEarnedRulesOfEmpContractType.php
 * @returns void
 */
function addOpInTextBoxForCustomEarnedRules() {
    var textBox = 'ruleCustomEarnedRuleTextArea';
    var selectOp = 'ruleCustomEarnedRuleHelpOperators';
    var selectedOp = dijit.byId(selectOp).getValue();

    oldText = dijit.byId(textBox).getValue();
    element = document.getElementById(textBox);
    var val = element.value;
    cursPos = val.slice(0, element.selectionStart).length;

    textToAdd=selectedOp+' ';
    newText = oldText.substr(0, cursPos) + textToAdd + oldText.substr(cursPos);        
    dijit.byId(textBox).setValue(newText);
}
function addOpInTextBoxWhereClauseForCustomEarnedRules() {
    var textBox = 'whereClauseCustomEarnedRuleTextArea';
    var selectOp = 'ruleCustomEarnedRuleHelpOperators';
    var selectedOp = dijit.byId(selectOp).getValue();

    oldText = dijit.byId(textBox).getValue();
    element = document.getElementById(textBox);
    var val = element.value;
    cursPos = val.slice(0, element.selectionStart).length;

    textToAdd=selectedOp+' ';
    newText = oldText.substr(0, cursPos) + textToAdd + oldText.substr(cursPos);        
    dijit.byId(textBox).setValue(newText);
}

/**
 * used in dynamicDialogCustomEarnedRulesOfEmpContractType.php
 * @returns void
 */
function addFieldInTextBoxForCustomEarnedRules() {
    var textBox = 'ruleCustomEarnedRuleTextArea';
    var selectItems = 'ruleCustomEarnedRuleHelpListItems';
    var selectedItem = dijit.byId(selectItems).getValue();
    var selectFields = 'ruleCustomEarnedRuleHelpListFields';
    var selectedField = dijit.byId(selectFields).getValue();

    oldText = dijit.byId(textBox).getValue();
    element = document.getElementById(textBox);
    var val = element.value;
    cursPos = val.slice(0, element.selectionStart).length;

    textToAdd='${'+selectedItem+'.'+selectedField+'} ';
    newText = oldText.substr(0, cursPos) + textToAdd + oldText.substr(cursPos);        
    dijit.byId(textBox).setValue(newText);
}

function addFieldInTextBoxWhereClauseForCustomEarnedRules() {
    var textBox = 'whereClauseCustomEarnedRuleTextArea';
    var selectItems = 'ruleCustomEarnedRuleHelpListItems';
    var selectedItem = dijit.byId(selectItems).getValue();
    var selectFields = 'ruleCustomEarnedRuleHelpListFields';
    var selectedField = dijit.byId(selectFields).getValue();

    oldText = dijit.byId(textBox).getValue();
    element = document.getElementById(textBox);
    var val = element.value;
    cursPos = val.slice(0, element.selectionStart).length;

    textToAdd='${'+selectedItem+'.'+selectedField+'} ';
    newText = oldText.substr(0, cursPos) + textToAdd + oldText.substr(cursPos);        
    dijit.byId(textBox).setValue(newText);
}

/**
 * used in dynamicDialogCustomEarnedRulesOfEmpContractType.php
 * @param {string} table
 * @returns void
 */
function refreshListFieldsInDialogCustomEarnedRules(table) {
    url='../tool/getListFieldsForRulableForEmpContractType.php?table='+ table;

    var selectTarget = 'ruleCustomEarnedRuleHelpListFields';
    dijit.byId(selectTarget).removeOption(dijit.byId(selectTarget).getOptions());
    dijit.byId(selectTarget).set('value','');
    dojo.xhrGet({
        url : url,
        handleAs : "text",
        load : function(data) {
            if(data){
                var obj = JSON.parse(data);
                for ( var key in obj) {
                    var o = dojo.create("option", {label: obj[key], value: key});     
                    dijit.byId(selectTarget).addOption(o); 
                }
            }
        }
    });
}

function checkLeavesEarned(userId) {
    if (typeof(userId)=='undefined' || userId<0) {
        console.log("No check for Leaves Earned - Unknow UserId - " + userId);
    }

    var currentDate = new Date();
    
    var url='../tool/checkLeavesEarned.php?userId='+ userId;
    dojo.xhrGet({
        url : url,
        handleAs : "text",
        load : function(data) {
            if(data){
                var result = JSON.parse(data);
                if (result!='OK') {
                    console.log('Error on checking Leaves Earned at ' + currentDate +' - result = '+result);
                } else {
                    console.log("Leave Earned checked at " + currentDate + ' for user id ' + userId);                        
                }
            }
        },
        error: function(error){
            console.log("checkLeavesEarned Error at " + currentDate);
            console.log(error);
        }
    });
    
    setTimeout('checkLeavesEarned('+userId+');',3600*24*1000);
}

function loadContentDashboardEmployeeManager() {
    var yearParam = "?year="+dijit.byId("yearSelect").value;
    var monthParam = "&month="+dijit.byId("monthSelect").value;
    var employeeParam = (dijit.byId("employeeSelect").value>0?"&idEmployee="+dijit.byId("employeeSelect").value:"");
    var typeParam = (dijit.byId("leaveTypeSelect").value>0?"&idLeaveType="+dijit.byId("leaveTypeSelect").value:"");
    var statusParam = (dijit.byId("leaveStatusSelect").value>0?"&idStatus="+dijit.byId("leaveStatusSelect").value:"");
    loadContent('dashboardEmployeeManager.php'+yearParam+monthParam+employeeParam+typeParam+statusParam, 'centerDiv');        
}

function nextPrevYearDashboardEmployeeManager(plusMinus, startYear, endYear) {
    var year = parseInt(dijit.byId("yearSelect").value)+plusMinus;
    if (year<startYear || year>endYear) { return; }
    dijit.byId("yearSelect").attr('value',year);
}

function nextPrevMonthDashboardEmployeeManager(plusMinus) {
    var month = parseInt(dijit.byId("monthSelect").value)+plusMinus;
    if (month<1 || month>12) { return; }
    dijit.byId("monthSelect").attr('value',month);    
}

function validOrCancelLeave(idLeave=0,motif="",context="CAL") {
    if (idLeave<1) {return;}
    dojo.setStyle(dijit.byId('leaveValidCancelPopup').closeButtonNode,{"display": "", "visibility": ""});
    dijit.byId('popupLeaveId').set("value", idLeave);
    dijit.byId('popupReason').set("value", motif);
    
    if (context!="CAL") {
        dijit.byId('popupLeaveStatus').set("disabled", true);
        if (context=="VAL") {
            dijit.byId("leaveValidCancelPopup").attr('title',i18n('validLeave'));            
            dijit.byId("popupLeaveStatus").attr('value',12);            
        } else {
            dijit.byId("leaveValidCancelPopup").attr('title',i18n('cancelLeave'));
            dijit.byId("popupLeaveStatus").attr('value',9);
        }
    } else {
        dijit.byId("leaveValidCancelPopup").attr('title',i18n('validOrCancelLeave'));            
        dijit.byId('popupLeaveStatus').set("disabled", false);
    }

    dijit.byId('leaveValidCancelPopup').show();
}

function saveValidOrCancelLeaveStatus() {
    showWait();
    dijit.byId('leaveValidCancelPopup').hide();

    //the url for the xhrGet request to save ValidOrCancel leave's status
    var url  = "../tool/saveValidOrCancelStatusLeave.php";
    url += "?idLeave="+dijit.byId("popupLeaveId").value;
    url += "&idLeaveStatus="+dijit.byId("popupLeaveStatus").value;
    url += "&comment="+dijit.byId("popupReason").value;

    dojo.xhrGet({
        url : url,
        handleAs : "text",
        sync: true,
        load : function(data, args) {
            var res=JSON.parse(data);
            if (getSqlElementOperationStatus(res)=="OK") {
                loadContentDashboardEmployeeManager();
            } else {
                showSqlElementResultInPopup(res);
            }    
        },
        error: function(error){
            showXhrErrorInErrorPopup(error, 'saveValidOrCancelStatusLeave');
        }
    });
    
}

function getLeftByLeaveType(idEmployee=null) {
    if (idEmployee === null) {return;}

    dojo.xhrGet({
        url : "../tool/getLeftQuantityByLeaveTypeForAnEmployee?idEmployee="+idEmployee,
        handleAs : "text",
        sync: true,
        load : function(data, args) {
            datas=JSON.parse(data);
            if (isSqlElementOperationStatus(datas)) {
               showSqlElementResultInPopup(datas);
               return;
            }            
            leftArray = datas['left'];
            employeeLeft = datas['idEmployee'];
        },
        error: function(error){
            showXhrErrorInErrorPopup(error, 'getLeftQuantityByLeaveTypeForAnEmployee');
        }
    });
    
}

function exportLeaveCalendarOfDashboardEmployeeManager() {
    var year = dijit.byId("yearSelect").value;
    var month = dijit.byId("monthSelect").value;
    var idStatus = dijit.byId("leaveStatusSelect").value;
    var idLeaveType = dijit.byId("leaveTypeSelect").value;
    var idEmployee = dijit.byId("employeeSelect").value;

    var params = "year="+year;
    params = params + "&month="+month;
    params = params + "&idStatus="+idStatus;
    params = params + "&idLeaveType="+idLeaveType;
    params = params + "&idEmployee="+idEmployee;
    
    window.onbeforeunload = function() {
        return;
    };
    window.location.replace("../tool/exportLeaveCalendarOfDashboardEmployeeManager?"+params);
    
}
// LEAVE SYSTEM
