﻿String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }


Date.prototype.toAjaxDateString = function() {
return this.getFullYear().padZero(4) + "-" + (this.getMonth() + 1).padZero(2) + "-" + this.getDate().padZero(2) + "T00:00:00";
};

Number.prototype.padZero = function(count) {
    var numZeropad = this + '';
    while (numZeropad.length < count) {
        numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

Number.prototype.toFileSize = function() {
    if (this / 1099511627776 > 1)
        return Math.round(this / 1099511627776, 1) + " Tb";
    else if (this / 1073741824 > 1)
        return Math.round(this / 1073741824, 1) + " Gb";
    else if (this / 1048576 > 1)
        return Math.round(this / 1048576, 1) + " Mb";
    else if (this / 1024 > 1)
        return Math.round(this / 1024, 1) + " Kb";
    else if (this > 1)
        return Math.round(this, 1) + " byte";
    else
        return this;
}

Number.prototype.formatMoney = function(c, d, t) {
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); 
};


Number.prototype.CmToInch = function() {
    return this*0.393700787;
};

Number.prototype.InchToCm = function() {
    return this*2.54;
};

Number.prototype.InchToFoot = function() {
    return this/12;
};

Number.prototype.FootToInch = function() {
    return 12/this;
};

Number.prototype.CmToMeter = function() {
    return this/100;
};

Number.prototype.MeterToCm = function() {
    return 100/this;
};

Number.prototype.RoundTo = function(DecimalPoints) {
    return Math.round(this * Math.pow(10, DecimalPoints)) / Math.pow(10, DecimalPoints);
};

String.prototype.toFileSize = function() {
    try {
        var num = parseInt(this);
        if (num != NaN) return num.toFileSize(); else return this;
    }
    catch (ex) { return this; }
}



// For convenience...
Date.prototype.format_ddmmyyyy = function() {
    return this.getDate() + '/' + (this.getMonth()+1) + '/' + this.getFullYear();
};

function Translate(key) {
    var value = translation[key];
    return (value == undefined) ? "$" + key : value;
}


function CalculateProportionByWidth(w, ImageWidth, ImageHeight) {
    if (Constants.MetricSystem) {
        var dpi = ImageWidth / (w / 2.54);
        return Math.round((ImageHeight / dpi) * 2.54);
    }
    else {
        var dpi = ImageWidth / w;
        return Math.round(ImageHeight / dpi);
    }
}

function CalculateProportionByHeight(h, ImageWidth, ImageHeight) {
    if (Constants.MetricSystem) {
        var dpi = ImageHeight / (h / 2.54);
        return Math.round((ImageWidth / dpi) * 2.54);
    }
    else {
        var dpi = ImageHeight / h;
        return Math.round(ImageWidth / dpi);
    }
}


function CalculateMinimumMaximumPrintSize(ImageWidth,ImageHeight) {
    var MMPS = { TooSmall: false, TooBig: false };
    var Rotated = false;
    if (ImageWidth > ImageHeight) {
        var tIW = ImageWidth;
        ImageWidth = ImageHeight;
        ImageHeight = tIW;
        Rotated = true;
    }

    MMPS.MinimumPrintWidth = Math.round((ImageWidth / Constants.MaximumDPI) * ((Constants.MetricSystem) ? 2.54 : 1));
    MMPS.MaximumPrintWidth = Math.round((ImageWidth / Constants.MinimumDPI) * ((Constants.MetricSystem) ? 2.54 : 1));
    MMPS.MinimumPrintHeight = CalculateProportionByWidth(MMPS.MinimumPrintWidth, ImageWidth, ImageHeight);
    MMPS.MaximumPrintHeight = CalculateProportionByWidth(MMPS.MaximumPrintWidth, ImageWidth, ImageHeight);

    while (MMPS.MinimumPrintWidth < Constants.MinimumWidth || CalculateProportionByWidth(MMPS.MinimumPrintWidth, ImageWidth, ImageHeight) < Constants.MinimumHeight) {
        MMPS.MinimumPrintWidth++;
        MMPS.MinimumPrintHeight = CalculateProportionByWidth(MMPS.MinimumPrintWidth, ImageWidth, ImageHeight);
    }

    while (MMPS.MaximumPrintWidth > Constants.MaximumWidth || CalculateProportionByWidth(MMPS.MaximumPrintWidth, ImageWidth, ImageHeight) > Constants.MaximumHeight) {
        MMPS.MaximumPrintWidth--;
        MMPS.MaximumPrintHeight = CalculateProportionByWidth(MMPS.MaximumPrintWidth, ImageWidth, ImageHeight);
    }

    if (MMPS.MinimumPrintWidth < Constants.MinimumWidth || MMPS.MinimumPrintHeight < Constants.MinimumHeight || MMPS.MaximumPrintWidth < Constants.MinimumWidth || MMPS.MaximumPrintHeight < Constants.MinimumHeight) {
        MMPS.TooSmall = true;
    }

    if (MMPS.MaximumPrintWidth > Constants.MaximumWidth || MMPS.MaximumPrintHeight > Constants.MaximumHeight || MMPS.MinimumPrintWidth > Constants.MaximumWidth || MMPS.MinimumPrintHeight > Constants.MaximumHeight) {
        MMPS.TooBig = true;
    }


    if (Rotated) {
        var TMPMIN = MMPS.MinimumPrintWidth;
        MMPS.MinimumPrintWidth = MMPS.MinimumPrintHeight;
        MMPS.MinimumPrintHeight = TMPMIN;
        
        var TMPMAX = MMPS.MaximumPrintWidth;
        MMPS.MaximumPrintWidth = MMPS.MaximumPrintHeight;
        MMPS.MaximumPrintHeight = TMPMAX;        
    }
    return MMPS;
    
  


}