﻿function CheckDate(sInput) {
    var bSuccess = true;
    var i = 0;
    var iCharCode = 0;

    sInput = sInput.replace(" ", "");

    for (i = 0; i < sInput.length; i++) {
        iCharCode = sInput.charCodeAt(i);

        if ((i == 2) || (i == 5)) {
            if (iCharCode != 47) {
                if (iCharCode == 45) {
                    sInput = sInput.substring(0, i) + "/" + sInput.substring(i + 1);
                }
                else {
                    bSuccess = false;
                }
            }
        }
        else {
            if ((iCharCode < 48) || (iCharCode > 57)) {
                bSuccess = false;
            }
        }
    }

    if ((bSuccess) && (sInput.length == 8)) {
        sInput = sInput.substring(0, 6) + "20" + sInput.substring(6);
    }

    return sInput;
}

function CheckDecimal(sInput) {
    var bSuccess = true;
    var i = 0;
    var iCharCode = 0;

    sInput = sInput.replace(" ", "");
    sInput = sInput.replace(",", "");

    for (i = 0; i < sInput.length; i++) {
        iCharCode = sInput.charCodeAt(i);

        if (((iCharCode < 48) || (iCharCode > 58)) && (iCharCode != 46) && (iCharCode != 45)) {
            bSuccess = false;
        }
    }

    if (bSuccess) {
        if (sInput.substring(0, 1) == ".") {
            sInput = "0" + sInput.toString();
        }
    }

    return sInput;
}

function CheckInteger(sInput) {
    var bSuccess = true;
    var i = 0;
    var iCharCode = 0;

    sInput = sInput.replace(" ", "");
    sInput = sInput.replace(",", "");

    for (i = 0; i < sInput.length; i++) {
        iCharCode = sInput.charCodeAt(i);

        if (((iCharCode < 48) || (iCharCode > 58)) && (iCharCode != 45)) {
            bSuccess = false;
        }
    }

    return sInput;
}

function ValidateDate(sElementName, sFieldDisplayName) {
    var bSuccess = true;
    var i = 0;
    var iCharCode = 0;
    var sInput

    sInput = CheckDate(document.getElementById(sElementName).value);

    if (sInput != document.getElementById(sElementName).value) {
        document.getElementById(sElementName).value = sInput;
    }

    for (i = 0; i < sInput.length; i++) {
        iCharCode = sInput.charCodeAt(i);

        if ((i == 2) || (i == 5)) {
            if (iCharCode != 47) {
                bSuccess = false;
            }
        }
        else {
            if ((iCharCode < 48) || (iCharCode > 57)) {
                bSuccess = false;
            }
        }
    }

    if (!bSuccess) {
        if (sFieldDisplayName != "Silent") {
            alert("Error, the value in field '" + sFieldDisplayName + "' is invalid!  Please enter a date in the format dd/mm/yyyy.");
        }
    }

    return bSuccess;
}

function ValidateDecimal(sElementName, sFieldDisplayName) {
    var bSuccess = true;
    var i = 0;
    var iCharCode = 0;
    var sInput;

    sInput = CheckDecimal(document.getElementById(sElementName).value);

    if (sInput != document.getElementById(sElementName).value) {
        document.getElementById(sElementName).value = sInput;
    }

    for (i = 0; i < sInput.length; i++) {
        iCharCode = sInput.charCodeAt(i);

        if (((iCharCode < 48) || (iCharCode > 58)) && (iCharCode != 46) && (iCharCode != 45)) {
            bSuccess = false;
        }
    }

    if (!bSuccess) {
        alert("Error, the value in field '" + sFieldDisplayName + "' is invalid!  Please enter a number.");
    }

    return bSuccess;
}

function ValidateInteger(sElementName, sFieldDisplayName) {
    var bSuccess = true;
    var i = 0;
    var iCharCode = 0;
    var sInput

    sInput = CheckInteger(document.getElementById(sElementName).value);

    if (sInput != document.getElementById(sElementName).value) {
        document.getElementById(sElementName).value = sInput;
    }

    for (i = 0; i < sInput.length; i++) {
        iCharCode = sInput.charCodeAt(i);

        if (((iCharCode < 48) || (iCharCode > 58)) && (iCharCode != 45)) {
            bSuccess = false;
        }
    }

    if (!bSuccess) {
        alert("Error, the value in field '" + sFieldDisplayName + "' is invalid!  Please enter a whole number.");
    }

    return bSuccess;
}

function ValidateMustSelect(sElementName, sFieldDisplayName) {
    var bSuccess = true;

    if (document.getElementById(sElementName).value == "") {
        bSuccess = false;
    }

    if (!bSuccess) {
        alert("Error, you must select a " + sFieldDisplayName + "!");
    }

    return bSuccess;
}

function ValidateNotNull(sElementName, sFieldDisplayName) {
    var bSuccess = true;

    if (document.getElementById(sElementName).value == "") {
        bSuccess = false;
    }

    if (!bSuccess) {
        alert("Error, the field '" + sFieldDisplayName + "' cannot be empty!");
    }

    return bSuccess;
}
