﻿
// Formats a decimal value that is retunred by a JSON-Service into a nice formatted string
// Input: 10500.7589
// Output: 10.500,76
function FormatDecimal(value) {
    var help = value.toFixed(2).replace(".", ",").toString();
    var formattedValue = "";
    var counter = 1;

    for (var i = help.length - 4; i >= 0; i--) {

        formattedValue = help.charAt(i) + formattedValue;

        if (counter++ % 3 == 0 && i > 0) {
            formattedValue = "." + formattedValue;
        }
    }

    formattedValue += help.substr(help.length - 3, 3);

    return formattedValue;
};

// Correct decimal values, so that they are recognized on the webservice
function CorrectDecimalValue(value) {

    if (value == "") {
        return "0";
    }

    value = value.toString().replace(/,/gi, ".");

    var correctValue = "";
    var validChars = "0123456789.";
    var lastPointIndex = value.lastIndexOf(".");

    for (var i = 0; i < value.length; i++) {

        var char = value.charAt(i);

        if (validChars.indexOf(char) >= 0) {
            if (char == "." && i == lastPointIndex) {
                correctValue += char;
            }
            else if (char != ".") {
                correctValue += char;
            }
        }
    }

    if (correctValue.lastIndexOf(".") == correctValue.length - 1) {
        correctValue += "0";
    }

    return correctValue;
};

// Correct integer values, so that they are recognized on the webservice
function CorrectIntegerValue(value) {

    if (value == "") {
        return "0";
    }

    var correctValue = "";
    var validChars = "0123456789";

    for (var i = 0; i < value.length; i++) {

        var char = value.charAt(i);

        if (validChars.indexOf(char) >= 0) {
                correctValue += char;
        }
    }

    return correctValue;
};
