// UTILITY FUNCTIONS

function printStackTrace(maxLen) {
    if (undefined == maxLen) maxLen = 10;
    var callstack = [];
    var isCallstackPopulated = false;
    try {
        i.dont.exist += 0; // doesn't exist- that's the point
    } catch (e) {
        if (e.stack) { // Firefox
            var lines = e.stack.split("\n");
            for ( var i = 0, len = lines.length; i < len; i++) {
                if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                    callstack.push(lines[i]);
                }
            }
            // Remove call to printStackTrace()
            callstack.shift();
            isCallstackPopulated = true;
        } else
            if (window.opera && e.message) { // Opera
                var lines = e.message.split("\n");
                for ( var i = 0, len = lines.length; i < len; i++) {
                    if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
                        var entry = lines[i];
                        // Append next line also since it has the file info
                        if (lines[i + 1]) {
                            entry += " at " + lines[i + 1];
                            i++;
                        }
                        callstack.push(entry);
                    }
                }
                // Remove call to printStackTrace()
                callstack.shift();
                isCallstackPopulated = true;
            }
    }
    if (!isCallstackPopulated) { // IE and Safari
        var currentFunction = arguments.callee.caller;
        while (currentFunction) {
            var fn = currentFunction.toString();
            var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous";
            callstack.push(fname);
            currentFunction = currentFunction.caller;
        }
    }
    //output(callstack);
    return callstack.join("\n");
}

function output(arr) {
    var win = popUp('stacktrace', "<html><head><title>STACKTRACE</title></head><body><h1>Stacktrace</h1>" + arr.join("nn") + "</body></html>");
}


function popUp(id, data) {
    var win = window.open('', id, 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=300');
    if (undefined != data) {
        win.document.open("text/html", "replace");
        win.document.write(escapeHTML(data));
        win.document.close();
    }
    return win;
}

function escapeHTML(text) {
    return text.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
}

function trim(str, chars) {
    if (null == str || undefined == str) str = ''
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function clearCache(cb){
    var req = {
        type : "GET"
        ,url : base_path + "/frontpage/page/clearcache"
        ,data : ""
        ,success : function(return_data,msg) {
            if(cb)cb(true,return_data,msg);
        }
        ,error : (function(request, msg, error) {
            if(cb)cb(false,return_data,msg,error);
        })

    };
    jQuery.ajax(req);
    return false;
}


function setUpClearCacheLinks(){
    jQuery('.clear_cache').click(function(el) {
        var link=jQuery(this);
        if(clearCache) {
            clearCache(
                function(res){
                    link.text(res?'Cache slettet..':'Feil ved sletting av cache!');
                    link.addClass(res?'ok':'fail');
                }
            );
            return false;
        }
        return true;
    });
}


// FIXME Maybe replace this with the jquery equivalent?
function isArray(obj) {
    return obj.constructor == Array;
}



