﻿// String.Format für JS
function stringFormat(str) {
    for (i = 1; i < arguments.length; i++) {
        str = str.replace('{' + (i - 1) + '}', arguments[i]);
    }
    return str;
}

// URL params auslesen
// Usage : 
$.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return $.getUrlVars()[name];
    }
});

function getDate(month, year) {
    return new Date(year, month, 1);
}

// prototype function in Array to get the index of the given element
// return value if not found -1
Array.prototype.containsAtIndex = function (element) {
    var foundIndex = -1;
    for (var i = 0; i <= this.length; i++) {
        if (this[i] === element) {
            foundIndex = i;
        }
    }
    return foundIndex;
}

// foreach for arrays
Array.prototype.foreach = function (callback) {
    for (var k = 0; k < this.length; k++) {
        callback(k, this[k]);
    }
}
// Example
//var a = ['a', 'b', 'c', 'd'];
//a.foreach(function(k, v) {
//    document.writeln(k + ' ' + v);
//});

/// Create the Namespace Manager that we'll use to
/// make creating namespaces a little easier.
if (typeof Namespace == 'undefined') var Namespace = {};
if (!Namespace.Manager) Namespace.Manager = {};

Namespace.Manager = {
    Register: function (namespace) {
        namespace = namespace.split('.');

        if (!window[namespace[0]]) window[namespace[0]] = {};

        var strFullNamespace = namespace[0];
        for (var i = 1; i < namespace.length; i++) {
            strFullNamespace += "." + namespace[i];
            eval("if(!window." + strFullNamespace + ")window." + strFullNamespace + "={};");
        }
    }
};

/*
Here's a sample usage of the above code to create and use an object that's placed inside a namespace:

// Register our Namespace
Namespace.Manager.Register("PietschSoft.Utility.Class");

// Add the Triplet class to the namespace created above
PietschSoft.Utility.Class.Triplet = function(one, two, three)
{
this.First = one;
this.Second = two;
this.Third = three;
}

// Create an instance of our Triplet class
var myTriplet = new PietschSoft.Utility.Class.Triplet("1", "2", "3");

// Read the values out of the properties of you Triplet class
alert(myTriplet.First + "\n" + myTriplet.Second + "\n" + myTriplet.Third);
*/
