﻿///<reference path="~/Scripts/jQuery/jquery-1.3.2.min.js" />
/*
 * InPlace-Library v 1.0.0
 * A set of Utilities
 *
 * Copyright (c) 2010 QuantumIT
 * All rights reserved.
 *
 * Date: 2010-07-14
 */ 
;

/*********** window Extensions *************/
$.namespace = function(ns) {
    nsParts = ns.split(".");
    root = window;
    for (var i = 0; i < nsParts.length; i++) {
        if (typeof root[nsParts[i]] == "undefined") {
            root[nsParts[i]] = new Object();
        }
        root = root[nsParts[i]];
    }
};

window.registerNameSpace = function(ns) {
    $.namespace(ns);
};

/*********** Array Extensions *************/

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt /*, from*/) {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}

Array.prototype.exists = function(x) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == x) return true;
    }
    return false;
};

Array.prototype.filter = function(fnc) {
    var a = new Array();
    for (var i = 0; i < this.length; i++) {
        if (fnc(this[i])) {
            a.push(this[i]);
        }
    }
    return a;
};

Array.prototype.find = function(ary, element) {
    for (var i = 0; i < ary.length; i++) {
        if (ary[i] == element) {
            return i;
        }
    }
    return -1;
};

/*********** String Extensions *************/

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, "");
};

String.prototype.escape = function(string) {
    return this.replace(/('|\\)/g, '\\$1');
};

String.prototype.contains = function(t) {
    return this.indexOf(t) >= 0 ? true : false;
};

String.prototype.reverse = function() {
    var s = '';
    var i = this.length;
    while (i > 0) {
        s += this.substring(i - 1, i);
        i--;
    }
    return s;
};

String.prototype.padLeft = function(n, padChar) {
    return padChar.repeat(n - this.length) + this;
};

String.prototype.padRight = function(n, padChar) {
    //b == '15000'
    return this + padChar.repeat(n - this.length);
};

String.prototype.trim = function() {
    return (this.replace(/^\s+/, '').replace(/\s+$/, ''));
};

String.prototype.repeat = function(count) {
    var result = '';
    for (var i = 0; i < count; i++) result += this;
    return result;
};

String.prototype.format = function() {
    var str = this;
    for (var i = 0, n = arguments.length; i < n; i++) {
        var re = new RegExp('\\{' + (i) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
};

String.prototype.startsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(0, t.length));
    } else {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    }
};

String.prototype.endsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(this.length - t.length));
    } else {
        return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    }
};

// clear format from a string representation of a number
String.prototype.cleanNumber = function() {
    return parseFloat(this.replace(/[^0-9|.|-]/g, ''));
};

String.prototype.insertAt = function(loc, strChunk) {
    return (this.valueOf().substr(0, loc)) + strChunk + (this.valueOf().substr(loc))
};
