/* jshint ignore:start */
// Code here will be ignored by JSHint.
/**
 * Created by c-lajeunf on 3/10/2017.
 */
var params = getAllUrlParams();
var maxRTP = 0;
var minRTP = 0;

for (key in params) {

    validaSpanFormat();

    //console.log(key +  " : " + params[key] );

    //*** min max RTP handle seperatly
    if (key == "maxRTP") {
        maxRTP = params[key]
    } else if (key == "minRTP") {
        minRTP = params[key]
    }

    var span = document.getElementById(key);
    if (span != null) {
        span.innerHTML = decodeURIComponent(params[key]);
        if (key == "minRTP" && document.getElementById("RTP") != null) {
            document.getElementById("RTP").innerHTML = decodeURIComponent(params[key]);
        }
    }

}

//*** handle min max RTP
var mutipleSpan = document.getElementById("multipleRTP");
var singleSpan = document.getElementById("singleRTP");
if (maxRTP == minRTP) {
    mutipleSpan.style.display = "none";
    singleSpan.style.display = "";
} else {
    mutipleSpan.style.display = "";
    singleSpan.style.display = "none";
}


function validaSpanFormat() {
    //**** validate Min Max RTP Span.
    var minRTP = document.getElementById("minRTP");
    var maxRTP = document.getElementById("maxRTP");
    var RTP = document.getElementById("RTP");

    if (minRTP != null && maxRTP != null && RTP != null) { //** if all exist
        if (minRTP.parentNode.id != "multipleRTP" || maxRTP.parentNode.id != "multipleRTP" || RTP.parentNode.id != "singleRTP") {
            throw new Error('Invalide Min Max RTP span.  format should be : <span id="multipleRTP"><span id="minRTP">value</span> to <span id="maxRTP">value</span> </span><span id="singleRTP"><span id="RTP">value</span></span>')
        }
    } else if (minRTP != null || maxRTP != null || RTP != null) { //** if not all exist = invalid
        throw new Error('Invalide Min Max RTP span.  format should be : <span id="multipleRTP"><span id="minRTP">value</span> to <span id="maxRTP">value</span> </span><span id="singleRTP"><span id="RTP">value</span></span>')
    }

}

function getAllUrlParams(url) {

    // get query string from url (optional) or window
    var queryString = url ? url.split('?')[1] : window.location.search.slice(1);

    // we'll store the parameters here
    var obj = {};

    // if query string exists
    if (queryString) {

        // stuff after # is not part of query string, so get rid of it
        queryString = queryString.split('#')[0];

        // split our query string into its component parts
        var arr = queryString.split('&');

        for (var i = 0; i < arr.length; i++) {
            // separate the keys and the values
            var a = arr[i].split('=');

            // in case params look like: list[]=thing1&list[]=thing2
            var paramNum = undefined;
            var paramName = a[0].replace(/\[\d*\]/, function(v) {
                paramNum = v.slice(1, -1);
                return '';
            });

            // set parameter value (use 'true' if empty)
            var paramValue = typeof(a[1]) === 'undefined' ? true : a[1];

            // (optional) keep case consistent
            //paramName = paramName.toLowerCase();
            //paramValue = paramValue.toLowerCase();

            // if parameter name already exists
            if (obj[paramName]) {
                // convert value to array (if still string)
                if (typeof obj[paramName] === 'string') {
                    obj[paramName] = [obj[paramName]];
                }
                // if no array index number specified...
                if (typeof paramNum === 'undefined') {
                    // put the value on the end of the array
                    obj[paramName].push(paramValue);
                }
                // if array index number specified...
                else {
                    // put the value at that index number
                    obj[paramName][paramNum] = paramValue;
                }
            }
            // if param name doesn't exist yet, set it
            else {
                obj[paramName] = paramValue;
            }
        }
    }

    return obj;
}

/* jshint ignore:end */