/**
* The method is used to filter a select box from an other select box
* using an AJAX call to populate the second combobox. The remote script
* must accept a POST parameter named "FilterFrom", and output a JSON object
* containing an array of key-pair arrays named "elements".
*
* Sample JSON : {elements:[ [1, "Label 1"], [2, "Label 2"], [3, "Label 3"] ]}
*
* @param string p_sIdMaster  The combobox used to filter the other combobo
* @param string p_sIdTarget  The combobox to filter
* @param string p_sUrl       Url of the remote script (php, asp or other)
*                            that will output JSON to populate the combobox
* @param string p_iId_preselected The value of the element to be preselected
*                                 when the combo is populated
*
* @copyright  Cyber Génération Copyright (c) 2007
* @author     Philippe Meunier
*/
function filterSelectBox(p_sCsvMasterValues, p_sIdTarget, p_sUrl, p_sCvsPreselected, p_bKeepSelection) {

    var retrieveData = {
        method: 'post',
        parameters: {FilterFrom: p_sCsvMasterValues},
        asynchronous: false,
        onFailure: function(t) {

        },
        onSuccess: function(t) {

            var oJson = t.responseText.evalJSON();
            var oNode;
            var oTextNode;
            var arrPreselected = p_sCvsPreselected.toString().split(',').invoke('strip');
            var oTarget = $(p_sIdTarget);

            if(p_bKeepSelection) {
                var arrSelectionToKeep = $A(oTarget.options).findAll(
                    function(e) {
                        return e.selected;
                    }
                );

                arrSelectionToKeep = arrSelectionToKeep.pluck('value');

                arrPreselected = arrPreselected.concat(arrSelectionToKeep).uniq();
            }

            oTarget.immediateDescendants().invoke('remove');

            oJson.elements.each(
                function(e) {
                    oNode = document.createElement("option");
                    oTextNode = document.createTextNode(e[1]);

                    oNode.value = e[0];
                    oNode.appendChild(oTextNode);

                    if(arrPreselected.include(oNode.value)) {
                        oNode.selected = "selected";
                    }

                    oTarget.appendChild(oNode);
                }
            )
        }
    }

    new Ajax.Request(p_sUrl, retrieveData);
}

function installCheckboxCheckAll(p_sCheckAll, p_sCheckboxes_name) {
    arrCheckboxes = $$('input[name="' + p_sCheckboxes_name + '"][type="checkbox"]');
    oCheckAll = $(p_sCheckAll);

    if(oCheckAll && arrCheckboxes) {
        oCheckAll.observe('click', function() {
            arrCheckboxes.each(function(e) {

                if(oCheckAll.nodeName == 'INPUT') {
                    e.checked = oCheckAll.checked;
                } else if(oCheckAll.nodeName = 'A') {
                    e.checked = true;
                }
            });
        });

        if(oCheckAll.nodeName == 'INPUT') {
            arrCheckboxes.each(function(e1) {
                e1.observe('click', function() {
                    if(arrCheckboxes.any(function(e2){ return !e2.checked})) {
                        oCheckAll.checked = false;
                    } else {
                        oCheckAll.checked = true;
                    }
                });
            });
        }
    }
}
/**
 *
 * @param {Object} p_textbox    A reference to the input object or the ID of it (both works, thanks to prototype)
 * @param {Object} p_language   Language code in which to display the calendar
 * @param {Object} p_showTime   Allow the user to set the time using the date picker
 */
function showDHTMLSuiteCalendar(p_textbox, p_language, p_showTime) {
    var oCalendar;
    var p_textbox = $(p_textbox);
    p_language = (!p_language ? 'fr' : p_language);
    p_showTime = (typeof(p_showTime) == 'undefined' ? false : p_showTime);

    if(!window.oDhtmlSuiteCalendar) {
        window.oDhtmlSuiteCalendar = new DHTMLSuite.calendar();

        var myCalendarModel = new DHTMLSuite.calendarModel();
        myCalendarModel.setLanguageCode(p_language);

        window.oDhtmlSuiteCalendar.setCalendarModelReference(myCalendarModel);
    }

    oCalendar = window.oDhtmlSuiteCalendar;

    oCalendar.minuteDropDownInterval = 1;
    oCalendar.numberOfRowsInHourDropDown = 5;
    oCalendar.isDragable = false;
    oCalendar.displayTimeBar = p_showTime;

    oCalendar.setCalendarPositionByHTMLElement(p_textbox,0,p_textbox.offsetHeight+2);
    oCalendar.addHtmlElementReference('myDate',p_textbox);

    if(p_showTime) {
        oCalendar.setInitialDateFromInput(p_textbox,'yyyy-mm-dd hh:ii');
        oCalendar.setCallbackFunctionOnDayClick('callbackFunctionDHTMLSuiteCalendarOnDayClickDateTime');
    } else {
        oCalendar.setInitialDateFromInput(p_textbox,'yyyy-mm-dd');
        oCalendar.setCallbackFunctionOnDayClick('callbackFunctionDHTMLSuiteCalendarOnDayClickDateOnly');
    }

    if(oCalendar.isVisible()){
        oCalendar.hide();
    }else{
        oCalendar.resetViewDisplayedMonth();
        oCalendar.display();
    }

}

function callbackFunctionDHTMLSuiteCalendarOnDayClickDateOnly(inputArray) {
    var oCalendar = inputArray['calendarRef'];

    var references = oCalendar.getHtmlElementReferences(); // Get back reference to form field.
    references.myDate.value = inputArray.year + '-' + inputArray.month + '-' + inputArray.day;
    oCalendar.hide();

}

function callbackFunctionDHTMLSuiteCalendarOnDayClickDateTime(inputArray) {
    var oCalendar = inputArray['calendarRef'];

    var references = oCalendar.getHtmlElementReferences(); // Get back reference to form field.
    references.myDate.value = inputArray.year + '-' + inputArray.month + '-' + inputArray.day + ' ' + inputArray.hour + ':' + inputArray.minute;
    oCalendar.hide();

}