﻿
//this function will validate the date
function isValidDate(dateNode) {
    //debugger;
    //parse the incoming value
    var timestamp = Date.parse(dateNode)
    //check the node
    if (isNaN(timestamp) != false) {
        return true;
    }
    else {
        return false;
    }
}

//this function will validate the tithe/donation amount
function IsValidAmount(amountNode) {
    //check for invaid chars
    if (HasInvalidChars(amountNode.value, ".")) {
        //flag the backgroundcolor
        amountNode.style.backgroundColor = "Yellow";
    }
    //check for alphabetic chars
    else if (HasAlphaChars(amountNode.value)) {
        //flag the backgroundcolor
        amountNode.style.backgroundColor = "Yellow";
    }
    else {
    }
}

//this function will check for illegal characters
function HasInvalidChars(val, exceptions) {
    //local variables
    var illegalChars = "~`!@#$%^&*()-_=+{[}]|\:;\"'<,>.?/";

    //check to see if exceptions are passed
    if (exceptions != "") {
        //cycle through the invalid chars and remove according to the passed exceptions
        for (var exceptionIndex = 0; exceptionIndex < exceptions.length; exceptionIndex++) {
            //remove the char
            illegalChars = illegalChars.replace(exceptions.substr(exceptionIndex, 1), "");
        }
    }

    //cycle through incoming string and check for illegal chars
    for (var char = 0; char < val.length; val++) {
        //check the string char
        if (illegalChars.indexOf(val.substr(char, 1)) != -1) {
            //return true
            return true;
        }
    }

    //return false
    return false;
}

//this function will check for alphabetic chars
function HasAlphaChars(val) {
    //local variables
    var alphaChars = "abcdefghijklmnopqrtstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    //cycle through incoming string and check for illegal chars
    for (var char = 0; char < val.length; val++) {
        //check the string char
        if (alphaChars.indexOf(val.substr(char, 1)) != -1) {
            //return true
            return true;
        }
    }
}

//this function will check for numeric chars
function HasNumericChars(val) {
    //local variables
    var numericChars = "1234567890";

    //cycle through incoming string and check for illegal chars
    for (var char = 0; char < val.length; val++) {
        //check the string char
        if (numericChars.indexOf(val.substr(char, 1)) != -1) {
            //return true
            return true;
        }
    }
}

