/****************************************************************************************//**
                                                                                           * \file javascript.js                                                                       *
                                                                                           * \brief The javascript for the BMLTPlugin class.                                           *
    
    This file is part of the BMLT Common Satellite Base Class Project. The project GitHub
    page is available here: https://github.com/MAGSHARE/BMLT-Common-CMS-Plugin-Class
    
    This file is part of the Basic Meeting List Toolbox (BMLT).
    
    Find out more at: https://bmlt.app
    
    BMLT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    BMLT 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 General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this code.  If not, see <http://www.gnu.org/licenses/>.
                                                                                           ********************************************************************************************/

/****************************************************************************************//**
                                                                                           *                                       AJAX HANDLER                                        *
                                                                                           ********************************************************************************************/

/****************************************************************************************//**
                                                                                           *   \brief A simple, generic AJAX request function.                                         *
                                                                                           *                                                                                           *
                                                                                           *   \returns a new XMLHTTPRequest object.                                                   *
                                                                                           ********************************************************************************************/
    
function BMLTPlugin_AjaxRequest(   url,        ///< The URI to be called
    callback,   ///< The success callback
    method,     ///< The method ('get' or 'post')
    extra_data  ///< If supplied, extra data to be delivered to the callback.
) {
    /************************************************************************************//**
                                                                                           *   \brief Create a generic XMLHTTPObject.                                              *
                                                                                           *                                                                                       *
                                                                                           *   This will account for the various flavors imposed by different browsers.            *
                                                                                           *                                                                                       *
                                                                                           *   \returns a new XMLHTTPRequest object.                                               *
                                                                                           ****************************************************************************************/
    
    function createXMLHTTPObject()
    {
        var XMLHttpArray = [
            function () {
                return new XMLHttpRequest()},
            function () {
                return new ActiveXObject("Msxml2.XMLHTTP")},
            function () {
                return new ActiveXObject("Msxml2.XMLHTTP")},
            function () {
                return new ActiveXObject("Microsoft.XMLHTTP")}
            ];
            
        var xmlhttp = false;
        
        for ( var i=0; i < XMLHttpArray.length; i++ )
            {
            try
                {
                xmlhttp = XMLHttpArray[i]();
            }
            catch(e)
                {
                continue;
            };
            break;
        };
        
        return xmlhttp;
    };
    
    var req = createXMLHTTPObject();
    req.finalCallback = callback;
    var sVars = null;
    method = method.toString().toUpperCase();
    var drupal_kludge = '';
    
    // Split the URL up, if this is a POST.
    if (method == "POST" ) {
        var rmatch = /^([^\?]*)\?(.*)$/.exec(url);
        url = rmatch[1];
        sVars = rmatch[2];
        // This horrible, horrible kludge, is because Drupal insists on having its q parameter in the GET list only.
        var rmatch_kludge = /(q=admin\/settings\/bmlt)&?(.*)/.exec(rmatch[2]);
        if (rmatch_kludge && rmatch_kludge[1] ) {
            url += '?'+rmatch_kludge[1];
            sVars = rmatch_kludge[2];
        };
    };
    if (extra_data ) {
        req.extra_data = extra_data;
    };
    req.open(method, url, true);
    if (method == "POST" ) {
        req.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    };
    req.onreadystatechange = function ( ) {
        if (req.readyState != 4 ) { return;
        }
        if(req.status != 200 ) { return;
        }
        callback(req, req.extra_data);
        req = null;
    };
    req.send(sVars);
    
    return req;
};

/****************************************************************************************//**
                                                                                           *   \brief Call to initiate a simple popup search.                                          *
                                                                                           ********************************************************************************************/
function BMLTPlugin_simple_div_filler( in_uri,     ///< The URI to call.
    in_header   ///< The text for the header.
) {
    if (!in_uri ) {
        var option_list=document.getElementById('meeting_search_select').options;
        document.getElementById('simple_search_container').innerHTML='';
        document.getElementById('meeting_search_select').selectedIndex=0;
        option_list[option_list.length-1].disabled=true;
    }
    else
        {
        document.getElementById('simple_search_container').innerHTML='<div class="BMLTPlugin_simple_throbber_container_div"><div class="BMLTPlugin_simple_throbber_div"><img class="bmlt_simple_throbber_img" alt="throbber" src="'+c_g_BMLTPlugin_images+'Throbber.gif" /></div></div>';
        BMLTPlugin_AjaxRequest(in_uri, BMLTPlugin_simple_div_filler_callback, 'get', in_header);
    };
};

/****************************************************************************************//**
                                                                                           *   \brief AJAX callback for the simple popup search.                                       *
                                                                                           ********************************************************************************************/
function BMLTPlugin_simple_div_filler_callback( in_req ///< The HTTPRequest object for this call.
) {
    document.getElementById('simple_search_container').innerHTML='<h2 class="bmlt_simple_header">'+in_req.extra_data+'</h2>'+in_req.responseText;
};

/********************************************************************************************
 * ###################################### THIRD-PARTY CODE ###################################*
 ********************************************************************************************/
/**
sprintf() for JavaScript 0.6

Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of sprintf() for JavaScript nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Changelog:
2007.04.03 - 0.1:
 - initial release
2007.09.11 - 0.2:
 - feature: added argument swapping
2007.09.17 - 0.3:
 - bug fix: no longer throws exception on empty paramenters (Hans Pufal)
2007.10.21 - 0.4:
 - unit test and patch (David Baird)
2010.05.09 - 0.5:
 - bug fix: 0 is now preceeded with a + sign
 - bug fix: the sign was not at the right position on padded results (Kamal Abdali)
 - switched from GPL to BSD license
2010.05.22 - 0.6:
 - reverted to 0.4 and fixed the bug regarding the sign of the number 0
 Note:
 Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)
 who warned me about a bug in 0.5, I discovered that the last update was
 a regress. I appologize for that.
 **/

function str_repeat(i, m)
{
    for (var o = []; m > 0; o[--m] = i);
    return o.join('');
};

function sprintf()
{
    var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = '';
    while (f) {
        if (m = /^[^\x25]+/.exec(f)) {
            o.push(m[0]);
        }
        else if (m = /^\x25{2}/.exec(f)) {
            o.push('%');
        }
        else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
            if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
                throw('Too few arguments.');
            };
            if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
                throw('Expecting number but found ' + typeof(a));
            };
            switch (m[7]) {
            case 'b': a = a.toString(2); break;
            case 'c': a = String.fromCharCode(a); break;
            case 'd': a = parseInt(a,10); break;
            case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
            case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
            case 'o': a = a.toString(8); break;
            case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
            case 'u': a = Math.abs(a); break;
            case 'x': a = a.toString(16); break;
            case 'X': a = a.toString(16).toUpperCase(); break;
            };
            a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a);
            c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
            x = m[5] - String(a).length - s.length;
            p = m[5] ? str_repeat(c, x) : '';
            o.push(s + (m[4] ? a + p : p + a));
        }
        else {
            throw('Huh ?!');
        };
        f = f.substring(m[0].length);
    };
    return o.join('');
};
