﻿//--------------- DEFAULT SCRIPTS ---------------//
// manage table rows alternance
$(  function()
    {
        $('table tr:even').addClass('even');
        $('table tr:odd').addClass('odd');
    }
);


//--------------- URL METHODS ---------------//
// set a parameter in the given url
// return the modified url
function UrlSetParameter(url, name, value)
{
    // if input is valid
    if((typeof(url) != 'undefined')
        && (typeof(name) != 'undefined'))
    {
        var dico = new Object();
        var keys = new Array();

        // 1 - parse the url parameters
        var urls = url.split("?");
        // if any parameter
        if(urls.length > 1)
        {
            // truncate the base url
            url = urls[0];
            // split the parameters
            urls = urls[1].split("&");
            // if more than 1
            if(urls.length > 1)
            {
                var i = 0;
                while(i < urls.length)
                {
                    // split key/value
                    var current = urls[i].split("=");

                    // push the key
                    keys.push(current[0]);
                    // store the value
                    dico[current[0]] = current[1];

                    i++;
                }
            }
            // if only 1 parameter
            else
            {
                // split key/value
                var current = urls[0].split("=");

                // push the key
                keys.push(current[0]);
                // store the value
                dico[current[0]] = current[1];
            }
        }
        
        // 2 - set the parameter
        // if value is valid
        if((typeof(value) != 'undefined')
            && (value != null)
            && (value != ''))
        {
            // if already in the dico
            if(typeof(dico[name]) != 'undefined')
                // update the value
                dico[name] = value;
            // if no already in the dico
            else
            {
                // add the key
                keys.push(name);
                // store the value
                dico[name] = value;
            }
        }
        // if parameter clearing
        else
            // remove the parameter
            dico[name] = null;

        // 3 - recompute full url
        var x = 0;
        var suffix = '?';
        while(x < keys.length)
        {
            // if valid
            if(dico[keys[x]] != null)
            {
                // append parameter
                url = url + suffix + keys[x] + '=' + dico[keys[x]];
                // update suffix
                suffix = '&';                
            }
            x++;
        }
    }
    return url;
}


