﻿
String.prototype.parseNETDate = function () {
    var milli = this.replace(/\/Date\((-?\d+)\)\//, '$1');
    var d = new Date(parseInt(milli));
    return d;
}

Number.prototype.formatDecimal = function (bwz) {
    num = this.toString().replace(/\$|\,/g, '');
    if (isNaN(num))  
        num = "0";
    // if blank when zero flag is true returns blank.
    if (bwz) {
        if (num == "0")
            return "";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

Number.prototype.formatCurrency = function (bwz) {
    num = this.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    // if blank when zero flag is true returns blank.
    if (bwz) {
        if (num == "0")
            return "";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}


Date.prototype.formatDate = function (format) {
    var date = this;
    if (!format)
        format = "MM/dd/yyyy";

    var month = date.getMonth() + 1;
    var year = date.getFullYear();

    format = format.replace("MM", month.toString().padL(2, "0"));

    if (format.indexOf("yyyy") > -1)
        format = format.replace("yyyy", year.toString());
    else if (format.indexOf("yy") > -1)
        format = format.replace("yy", year.toString().substr(2, 2));

    format = format.replace("dd", date.getDate().toString().padL(2, "0"));

    var hours = date.getHours();
    if (format.indexOf("t") > -1) {
        if (hours > 11)
            format = format.replace("t", "pm")
        else
            format = format.replace("t", "am")
    }
    if (format.indexOf("HH") > -1)
        format = format.replace("HH", hours.toString().padL(2, "0"));
    if (format.indexOf("hh") > -1) {
        if (hours > 12) hours - 12;
        if (hours == 0) hours = 12;
        format = format.replace("hh", hours.toString().padL(2, "0"));
    }
    if (format.indexOf("mm") > -1)
        format = format.replace("mm", date.getMinutes().toString().padL(2, "0"));
    if (format.indexOf("ss") > -1)
        format = format.replace("ss", date.getSeconds().toString().padL(2, "0"));
    return format;
}
// This function removes non-numeric characters
String.prototype.stripNonNumeric = function () {
    str = this;
    str += '';
    var rgx = /^\d|\.|-$/;
    var out = '';
    for (var i = 0; i < str.length; i++) {
        if (rgx.test(str.charAt(i))) {
            if (!((str.charAt(i) == '.' && out.indexOf('.') != -1) ||
             (str.charAt(i) == '-' && out.length != 0))) {
                out += str.charAt(i);
            }
        }
    }
    return out;
}
String.prototype.toFloat = function () {
    return parseFloat(this.replace(/,/g, ''));
}

Date.prototype.dateAdd = function (dd, mm, yy) {
    var date = this;
    var newyy = date.getFullYear() + yy;
    var newmm = date.getMonth() + mm;


    if (newmm > 12) newmm = newmm - 12;
    var newdd = date.getDate() + dd;
    if (newmm == 2) {
        if (((newyy / 4) * 4) == newyy) { // leap year
            if (newdd > 29) newdd = newdd - 29;
            else newdd = newdd - 28;
        }
        else newdd = newdd - 28;
    }
    else if (newmm == 4 || newmm == 6 || newmm == 9 || newmm == 11) {
        if (newdd > 30) newdd = newdd - 30;
    }
    else {
        if (newdd > 31) newdd = newdd - 31;
    }
    return new Date(newyy, newmm, newdd);
}

String.repeat = function (chr, count) {
    var str = "";
    for (var x = 0; x < count; x++) { str += chr };
    return str;
}

String.prototype.padL = function (width, pad) {
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) return this.substr(0, width);

    return (String.repeat(pad, length) + this).substr(0, width);
}

String.prototype.padR = function (width, pad) {

    if (!width || width < 1)
        return this;
    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) this.substr(0, width);

    return (this + String.repeat(pad, length)).substr(0, width);
}

function parseSAPDate(strDate) {
    var dateParts = strDate.split("-");
    var date = new Date(dateParts[0], (dateParts[1] - 1), dateParts[2]);
    return date;
}
function getQueryString(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
