﻿//Linq support for Arrays
Array.prototype.Where = function(func) {
    var temp = new Array();
    for (var x = 0; x < this.length; x++) {
        if (func(this[x]))
            temp.push(this[x]);
    }
    return temp;
};

Array.prototype.PushCollection = function(collection) {
    for (var x = 0; x < collection.length; x++)
        this.push(collection[x]);
};

Array.prototype.First = function(func) {
    for (var x = 0; x < this.length; x++) {
        if (func(this[x]))
            return this[x];
    }
    return null;
}

Array.prototype.FirstIndex = function(func) {
    for (var x = 0; x < this.length; x++) {
        if (func(this[x]))
            return x;
    }
    return null;
}

Array.prototype.Any = function(func) {
    for (var x = 0; x < this.length; x++) {
        if (func(this[x]))
            return true;
    }
    return false;
}

String.prototype.replaceAll = function(searchfor, replacewith){
    var t = this;
    while(t.indexOf(searchfor)!=-1)
        t = t.replace(searchfor, replacewith);    
    return t;
}
String.prototype.format = function(){
    var t = this;
    for(var x = 0; x < arguments.length; x++)
        t = t.replaceAll("{" + x.toString() + "}", arguments[x]);
    return t;
};
// Date
Date.prototype.ParsePrefix = function(prefix) {
    // is it optional unfilled?
    if (document.getElementById(prefix + "month").value + "/" + document.getElementById(prefix + "day").value + "/" + document.getElementById(prefix + "year").value == "MM/DD/YYYY") return "MM/DD/YYYY";
    
    var x = Date.parse(document.getElementById(prefix + "month").value + "/" + document.getElementById(prefix + "day").value + "/" + document.getElementById(prefix + "year").value);
    //alert(prefix);
    //var x = Date.parse("10/30/2009");
    var d = new Date(x);
    return d.getMonth() + 1 + "/" + d.getDate() + "/" + d.getFullYear();
}