﻿// TODO: should make util namespace

function truncateNumber(num,dec){return Math.round(num*Math.pow(10, dec))/Math.pow(10, dec);}
function URLDecode(psEncodeString){var lsRegExp=/\+/g;return unescape(String(psEncodeString).replace(lsRegExp," "));}

function padleft(val, ch, num) { var re = new RegExp(".{" + num + "}$"); var pad = ""; do { pad += ch; } while (pad.length < num)return re.exec(pad + val); }
function padright(val, ch, num) { var re = new RegExp("^.{" + num + "}"); var pad = ""; do { pad += ch; } while (pad.length < num)return re.exec(val + pad); }


function evalJavascript(script) {
    if (script == undefined || script == null || script == "") return;

    if (window.execScript)  // IE    
        window.execScript(script.replace(/<!--/g,""), 'javascript'); //removes "<!--" html comment tags otherwise execScript fails
    else
        window.eval(script); //eval works in FireFox, TODO: test in chrome.
}

/** Tries to call the given function and catches any resultant errors.
 * Shows any error in dialog box.
 **/
function tryThis(f) {
    if (!f) return;
    try {
        f();
    }
    catch (e) {
        var m = "";
        switch (typeof(e)) {
            case "string":
                m = e;
                break;
            case "object":
                for (var a in e)
                    m += typeof(e[a]) + " " + a + ": " + (typeof(e[a]) == "function" ? "function" : e[a]) + "\n";
                break;
            case "number":
                m = e;
            default: m = "Unspecified error";
        }
        alert(m);
    }
}

/**
 * Returns a function that wraps the argument (which must also be a function) in tryThis.
**/
function tryThisFunc(f) {
    return function() { tryThis(f); }
}

/**
 * Use this function to register another function to be called once and only once upon page load.
 * Especially needed for AJAX controls which can be loaded several times on the page.
**/
function onLoadOnce(f) {
    Sys.Application.add_load(function() {
        // unregister this handler once it's called once
        Sys.Application.remove_load(arguments.callee);
        tryThis(f);
    });
}

/**
 * Use this function to register another function to be called once and only once upon page init.
 * Especially needed for AJAX controls which can be loaded several times on the page.
**/
function onInitOnce(f) {
    Sys.Application.add_init(function() {
        Sys.Application.remove_init(arguments.callee);
        tryThis(f);
    });
}


// "Static" helper class allowing to create deep copies of JS objects
var ObjectHandler = {
    getCloneOfObject: function(oldObject, level) {
        var tempClone;
        if (level == undefined)
            level = 0;
        if (oldObject == null)
            tempClone = null;
        else if (typeof (oldObject) == 'object' && oldObject.length != undefined) {//array
            tempClone = new Array;
            for (var i = 0; i < oldObject.length; ++i)
                tempClone[tempClone.length] = ObjectHandler.getCloneOfObject(oldObject[i], level + 1);
        }
        else if (typeof (oldObject) == 'object') {//object
            tempClone = new Object;
            for (var j in oldObject)
                tempClone[j] = ObjectHandler.getCloneOfObject(oldObject[j], level + 1);
        }
        else //plain
            tempClone = oldObject;

        return tempClone;
    }
};

function trustedSite(filePath) {
    if (filePath.search(/fakepath/) > -1) {
        alert('You are currently using Internet Explorer 8. To use Traffic Data Online, you must ' +
              'add http://www.trafficdataonline.com to the list of trusted sites in your security options, ' +
              'and then set the security level to "Medium" for trusted sites. You will need to uncheck ' +
              '"Require server verification" to add the trusted site.');
        return false;
    }
    return true;
}

var UTIL_onbeforeunload_Keys = new Array();
var UTIL_onbeforeunload_Functions = {};

// allow running multiple onbeforeunload events
// track each handler by a key so that when we re-add that handler on a postback, it only gets run once
function add_onbeforeunload(key, func) {
    // add the key if we haven't used it yet
    if (!UTIL_onbeforeunload_Functions[key])
        UTIL_onbeforeunload_Keys.push(key);

    // store the function with a key
    UTIL_onbeforeunload_Functions[key] = func;

    // set the window event
    window.onbeforeunload = UTIL_onbeforeunload_handler;
}

function UTIL_onbeforeunload_handler() {
    // run through all onbeforeunload events, with a try-catch around each
    for (var i = 0; i < UTIL_onbeforeunload_Keys.length; i++) {
        tryThis(UTIL_onbeforeunload_Functions[UTIL_onbeforeunload_Keys[i]]);
    }
}
