﻿//This function assumes all values passed are of the numeric type. It validates
//a number (prVal) to make sure it is greater than or equal to a low value (prL)
//and that is less than or equal to a high value (prH).  This returns true if
//condition is met and false otherwise.
function IsRange(pvVal, pvL, pvH) {
    return (pvVal >= pvL && pvVal <= pvH);
}

//This function validates if a string (prVal) is all numerics. It returns true if the
//condition is met and false otherwise.
function IsNumFormat(pvVal) {
    var lRGX = /^[\d]+$/;
    return lRGX.test(pvVal);
}

//This function validates if a string (prVal) is all numerics. It returns true if the
//condition is met and false otherwise.
function IsFloatFormat(pvVal) {
    var lRGX = /^[\d]+(\.\d+)?$/;
    return lRGX.test(pvVal);
}

//This function validates if a string (prVal) is a float format after all commas have been stripped
//from the input. It returns true if the condition is met and false otherwise.
function IsNumFormatLoose(pvVal) {
    var lVal = pvVal.replace(/,/g, "");
    return IsFloatFormat(lVal);
}

//This function validates if a string (prVal) is a float format with no manipulation to the input.
//It returns true if the condition is met and false otherwise.
function IsNumFormatStrict(pvVal) {
    var lRGX = /^[\d]+(,\d{3})*(\.\d+)?$/;
    return lRGX.test(pvVal);
}

//This function validates if a string (prVal) is a valid 12 hour time format.  It
//returns true if the condition is met and false otherwise.
function IsTimeFormat(pvVal) {
    var lRGX = /^(1[0-2]|[1-9]):([0-5]?[0-9]) (AM|PM)$/;
    return lRGX.test(pvVal);
}

//This fuction validates if a string (prVal) "looks" like a valid date format (MM/DD/YYYY).
//It returns true if the condition is met and false otherwise.
function IsDateFormat(pvVal) {
    var lRGX = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    return lRGX.test(pvVal);
}

//This function pads the left side of a string (prVal) with a value specified (prPad)
//until it reaches the total length specified (prLen).
function PadLeft(pvVal, pvLen, pvPad) {
    while (pvVal.length < pvLen)
    {
        pvVal = pvPad + pvVal;
    }
    return pvVal;
}

//This function pads the right side of a string (prVal) with a value specified (prPad)
//until it reaches the total length specified (prLen).
function PadRight(pvVal, pvLen, pvPad) {
    while (pvVal.length < pvLen)
    {
        pvVal = pvVal + pvPad;
    }
    return pvVal;
}

//This function trims all empty space from the left side of a string (prVal).
function LeftTrim(pvVal) {
    return pvVal.replace(/^\s+/, "");
}

//This function trims all empty space from the right side of a string (prVal).
function RightTrim(pvVal) {
    return pvVal.replace(/\s+$/, "");
}

//This function trims all empty space from the left and right side of a string (prVal).
function FullTrim(pvVal) {
    return RightTrim(LeftTrim(pvVal));
}

//This function validates a string (pvInt) to be of the string type and returns
//the value (pvInt) if it is a valid numeric and return a default (pvDef) if it
//is not a valid numeric type.
function GetInteger(pvInt, pvDef) {
    var lInt = pvInt.replace(/\,/g, ""); /*Remove all commas for easier manipulation*/
    return (IsNumFormat(lInt)) ? parseInt(lInt, 10) : parseInt(pvDef, 10);
}

//This function validates a string (pvFloat) to be of the string type and returns
//the value (pvFloat) if it is a valid numeric and return a default (pvDef) if it
//is not a valid numeric type.
function GetFloat(pvFloat, pvDef) {
    var lFloat = pvFloat.replace(/\,/g, ""); /*Remove all commas for easier manipulation*/
    return (IsFloatFormat(lFloat)) ? parseFloat(lFloat) : parseFloat(pvDef, 10);
}

//This function rounds a numeric to a spcified number of decimal points (pvPrecision)
//and returns a string of the format ###.###.  A string values is returned as trailing
//zeros [0] after the decimal point a truncated with numeric types.
function RoundFloat(pvFloat, pvPrecision) {
    var lWorkingFloat = String(pvFloat).replace(/\,/g, ""); /*Remove all commas for easier formatting*/
    if (lWorkingFloat.length == 0) { return ""; } /*Return a blank string as there is nothing passed*/

    if (lWorkingFloat == "0") { return PadRight("0.", pvPrecision + 2, "0"); }
    var lFloat = String(Math.round(parseFloat(lWorkingFloat) * Math.pow(10, pvPrecision)));
    var lDecimal = lFloat.length - pvPrecision;
    return lFloat.substr(0, lDecimal) + "." + lFloat.substr(lDecimal, pvPrecision);
}

//This function takes a string of digits and decimal and properly inserts commas to make viewing
//numbers over 1000K easier.
function FormatNumeric(pvNumber) {
    var lWhole = (pvNumber.indexOf(".") < 0) ? pvNumber : pvNumber.substr(0, pvNumber.indexOf("."));
    lWhole = lWhole.replace(/,/g, ""); /*Remove any client entered commas for easier formatting*/
    var lDecimal = (pvNumber.indexOf(".") < 0) ? "" : pvNumber.substring(pvNumber.indexOf(".") + 1);

    var lWholeFormatted = lWhole.substring(0, ((lWhole.length % 3) == 0) ? 3 : lWhole.length % 3);
    if (lWhole.length > 3)
    {/*Add all remaining commas*/
        var lFrom = lWholeFormatted.length;
        while (lFrom < lWhole.length)
        {
            lWholeFormatted += "," + lWhole.substr(lFrom, 3);
            lFrom += 3;
        }
    }

    /*Return back the formatting whole portion and any decimal portion*/
    return lWholeFormatted + ((lDecimal.length > 0) ? "." + lDecimal : "");
}

//This function formats a the string passed explictly to this function using a
//variable number of optional parameters that are read by checking the arguments
//object available to ALL javascript functions.
function StringFormat(pvText) {
    for (var x = 1; x < arguments.length; x++)
    {
        pvText = pvText.replace("{" + (x - 1) + "}", arguments[x]);
    }
    return pvText;
}

//This function opens the window passed.
function OpenUrl(pvUrl) {
    var lWindow = window.open(pvUrl, "", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0");
    lWindow.focus();
}

//This function is primarily used for debugging javascript.
//This function outputs text to a debug object on a page
function JavaScriptDebug(prText, prAppend) {
    var lJavaScriptDebug = $("#JavaScriptDebug");

    if (lJavaScriptDebug.length == 0)
    {
        /*The debug container does not exist*/
        $("body").prepend("<div id=\"JavaScriptDebug\" style=\"width:450px;border:solid 1px #FF0000;float:right;\"></div>");
        lJavaScriptDebug = $("#JavaScriptDebug");
    }

    /*Append to existing HTML or define new HTML*/
    var curhtml = $("#JavaScriptDebug").html();
    if (prAppend) { lJavaScriptDebug.html(curhtml + prText); }
    else { lJavaScriptDebug.html(prText); }
}
