/*---------- SCRIPTCONTROLLER ----------*/

function MVBScriptController() {
    this.m_pageLoadFunctions = Array();

    this.AddPageLoad = function(pageLoadFunction) {
        this.m_pageLoadFunctions.push(pageLoadFunction);
    }

    this.pageLoaded = function() {
        //the executed function inherits the scope of this function, so here we use a wierd(azertyuiop) name that hopefully never will be used in the executed functions.
        for (azertyuiop = 0; azertyuiop < this.m_pageLoadFunctions.length; azertyuiop++) {
            this.m_pageLoadFunctions[azertyuiop]();
        }
    }
}

var ScriptController = new MVBScriptController();

/*---------- PAGELOAD ----------*/
//Only one pageLoad() function is automatically executed.
//Therefore all modules can use ScriptController.AddPageLoad(FunctionPtr) instead of pageLoad().
function pageLoad() {
    ScriptController.pageLoaded();
}




/*---------- Cookies ----------*/
function CreateCookie(name, value, days) {
    var expires;
    var date = new Date();
    if (days) {
        date.setTime(date.getTime() + (days * 86400000)); //86400000 = milliseconds per day
    } else {
        date.setTime(date.getTime() + 2592000000); // defaults to 30 days
    }
    expires = "; expires=" + date.toGMTString();

    document.cookie = name + "=" + value + expires + "; path=/";
}

function GetCookie(name) {
    var _ret = new Array();
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) {
            _ret.fromKeyValueString(c.substring(nameEQ.length, c.length));
        }
    }
    return _ret;
}

function EraseCookie(name) {
    CreateCookie(name, "", -1);
}


/*---------- prototype Extensions ----------*/

if (!Array.prototype.contains) {
    Array.prototype.contains = function(obj) {
        var len = this.length;
        for (var i = 0; i < len; i++) {
            if (this[i] === obj) { return true; }
        }
        return false;
    };
}

if (!Array.prototype.toKeyValueString) {
    Array.prototype.toKeyValueString = function() {
        var _ret = "";
        for (var key in this) {
            if (!(typeof (this[key]) == 'function')) {
                _ret += key + "=" + this[key] + "&";
            }
        }
        return _ret;
    }
}

if (!Array.prototype.fromKeyValueString) {
    Array.prototype.fromKeyValueString = function(input) {
        this.clear();
        var _split = input.split('&');
        for (var i = 0; i < _split.length; i++) {
            if (_split[i] != '') {
                var _kv = _split[i].split('=');
                if (_kv.length != 2) { throw new Error(input + " is not a valid keyvalue string") }
                this[_kv[0]] = _kv[1];
            }
        }
    }
}

if (!Array.prototype.clear) {
    Array.prototype.clear = function() {
        for (var key in this) {
            if (!(typeof (this[key]) == 'function')) {
                delete (this[key]);
            }
        }
    }
}

if (!Date.prototype.addMinutes) {
    Date.prototype.addMinutes = function(minutes) {
        return new Date(this.getTime() + minutes * 60000);
    }
}

if (!Date.prototype.addSeconds) {
    Date.prototype.addSeconds = function(seconds) {
        return new Date(this.getTime() + seconds * 1000);
    }
}

if (!String.prototype.endsWith) {
    String.prototype.endsWith = function(obj) {
        return (this.match(str + "$") == str)
    }
}
