/*
 * $Id:$
 *
 * This is a javascript file used for random, misc things.
 */


/*
 * Return the query string args for this page.
 */
function currentQueryParams() {
    var qsParams = new Object();

    function qs() {
        var query = window.location.search.substring(1);
        var params = query.split('&');
        for (var i=0; i<params.length; i++) {
            var pos = params[i].indexOf('=');
            if (pos > 0) {
                var key = params[i].substring(0,pos);
                var val = unescape(params[i].substring(pos+1).replace(/[+]/g," "));
                qsParams[key] = val;
            }
        }
    } 
    qs();
    return qsParams;
}

/*
 * Floating point round. Round to a given decimal place.
 */
Math.roundf = function(value, places) {
    return Math.round(value * Math.pow(10, places)) / Math.pow(10, places);
}

Math.rrandom = function(lower, upper) {
    return Math.round(lower + (Math.random() * (upper - lower)));
}

function randomToken() {
    return Math.rrandom(1000000, 10000000).toString(36).toUpperCase();
}

function makeErrorHandler(handler) {
    return function(e) {
        if (e instanceof Error) {
            handler('Error at line ' + e.lineNumber +
                    ' in ' + e.fileName + '\n' +
                    'Message: ' + e.message);
            if (e.cause) {
                var errorStatus = e.cause.status;
                var statusText = e.cause.statusText;
                handler('Root cause: HTTP error ' + errorStatus + ' with status text of: ' +
                        statusText);
            }
        } else {
            handler(e.toString());
        }
    }
}


String.prototype.trim = function() {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


// From:  http://blogs.vertigo.com/personal/aanttila/Blog/Lists/Posts/Post.aspx?List=552eb137%2D35b0%2D44f9%2Dbee8%2D641e08159e2b&ID=6
function getWindowHeight()
{
    return document.all ? document.body.offsetHeight : window.innerHeight; 
}

function getWindowWidth() { 
    return document.all  ? document.body.offsetWidth : window.innerWidth; 
}
