﻿//Validate positive/negative decimals
function numbersonly(myfield, e, negative) {
    var key;
    var keychar;
    if (window.event) {
        key = window.event.keyCode;
    } else if (e) {
        key = e.which;
    } else {
        return true;
    }
    keychar = String.fromCharCode(key);

    var keyStr = "0123456789.";
    if (negative == true) { keyStr += "-" };

    if (iscontrolkey(key)) {
        return true;
    }
    // numbers or decimal
    else if (((keyStr).indexOf(keychar) > -1)) {
        var $this = $(myfield);
        var str = keychar + $this.val();
        var countDecimal = str.split('.').length;
        var countMinus = str.split('-').length;

        if (countDecimal > 2 || countMinus > 2) {
            return (false);
        }
        return true;
    } else {
        return false;
    }
}

//Validate integers only
function integersonly(myfield, e) { 
    var key;
    var keychar;
    if (window.event) key = window.event.keyCode;
    else if (e) key = e.which;
    else return true;
    keychar = String.fromCharCode(key);
    if (iscontrolkey(key)) return true;
    // numbers or decimal
    else if ((("0123456789").indexOf(keychar) > -1)) return true;
    else return false;
}

function iscontrolkey(key) {
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27) ||
			(key == 63232) || (key == 63233) || (key == 63234) || (key == 63235) || (key == 63272)) return true;
}

//Validate Alpha-Numeric
function alphaNumericOnly(myfield, e) {
    var key;
    var keychar;
    if (window.event) key = window.event.keyCode;
    else if (e) key = e.which;
    else return true;
    keychar = String.fromCharCode(key);
    if (iscontrolkey(key)) return true;
    // numbers or decimal
    else if ((("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").indexOf(keychar.toUpperCase()) > -1)) return true;
    else return false;
}

//Test if value is integer
function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

//Round numbers for currency
function currencyRound(amount) {
    var i = parseFloat(amount);
    i = Math.round(i * 100) / 100 //Added line for number rounding

    var d = 2;
    var s;
    if (isNaN(i)) i = 0.0;
    s = String(i);
    if (s.indexOf('.') < 0) {
        s += '.00';
    }
    while (s.indexOf('.') >= (s.length - d)) {
        s += '0';
    }
    return s;
}

//Trim text field
function addTrimFunctionality(fieldID,charLimit,charLimitFieldID) {
    $(fieldID).keyup(function () {
        var charInserted = $(fieldID).val().length;
        var charLeft = (charLimit * 1) - (charInserted * 1);
        if ((charInserted * 1) >= (charLimit * 1)) {
            var fieldText = $(fieldID).val();
            fieldText = fieldText.substring(0, (charLimit * 1));
            $(fieldID).val(fieldText);
            $(charLimitFieldID).html("0");
        } else {
            $(charLimitFieldID).html(charLeft);
        }
    });

}

//IS EMAIL VALID
function checkEmail(email) {
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    } else {
        return true
    }
}

//TRIM WHITESPACES
function trimWhitespaces(input) {
    return input.replace(/^\s+|\s+$/g, "")
}

//Comma Format Large Numbers
function commaFormatNumber(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }    
    return x1 + x2;
}
