﻿function ValidateDate(txt)
{
    // Shortcut key - enter a "T" for today's date
    if (txt.value.toLowerCase() == 't')
    {
        var d = new Date();
        txt.value = (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
    }
    
    if (txt.value == '')
        return;

    if (!isDate(txt.value))
    {
        alert('Invalid date entered.\n\nPlease enter a date with one of the following formats:\nmm/dd/yyyy, mm-dd-yyyy, mm.dd.yyyy, mmddyyyy');
        txt.focus();
        return;
    }
    
    txt.value = FormatDate(txt.value);
}

function ValidateCurrency(txt)
{
    if (!isCurrency(txt.value, 2))
    {
        alert('Please provide a valid dollar amount with no more than 2 places after the decimal.');
        txt.focus();
        return;
    }
    
    txt.value = FormatNum(txt.value, 2, '$');
}

function ValidatePercent(o, iDecimals)
{ 
    if (!isPercent(o.value, iDecimals))
    { 
        o.focus(); 
        alert("Please provide a valid percent with no more than " + iDecimals + " places after the decimal."); 
        return; 
    } 
    o.value = FormatNum(o.value, iDecimals, "%"); 
} 

function ValidateNumber(txt, iDecimals, bSupressFormatting, sPermittedSign)
{ 
    // sPermittedSign:
    //      P = positive only
	//      N = negative only
	//      B = positive and negative
	
    if (!isNumber(txt.value, iDecimals))
    {
        alert('Please provide a valid number with no more than ' + iDecimals + ' places after the decimal.');
        txt.focus();
        return;
    }
    
    var inputStr = txt.value;
    
    // Strip out $ and ,
    inputStr = inputStr.replace(/\$/g, ''); 
    inputStr = inputStr.replace(/,/g, ''); 

	if (parseInt(inputStr) > 2147483647)
	{
		alert('Please provide a valid number that is less than 2,147,483,647.');
		txt.focus();
		return;
	}

	if (parseInt(inputStr) > 0 && sPermittedSign == 'N')
	{
		alert('Please provide a number less than zero.');
		txt.focus();
		return;
	}
	
	if (parseInt(inputStr) < 0 && sPermittedSign == 'P')
	{
		alert('Please provide a number greater than zero.');
		txt.focus();
		return;
	}
	
	if (bSupressFormatting)
		return;
		
    txt.value = FormatNum(txt.value, iDecimals, ''); 
}

function isDate(testDate)
{
    if (testDate.length == 8 && parseInt(testDate) != 'NaN')
        return true;

    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;

    if (!testDate.match(RegExPattern))
        return false;
    
    return true;
}

function isCurrency(inputVal, iDecimals) 
{
    inputVal = inputVal.replace(/\$/g, '');
    inputVal = inputVal.replace(/-/g, '');
        
    return isNumber(inputVal, iDecimals); 
}

function isNumber(inputVal, iDecimals) 
{
    oneDecimal = false;
    var charsAfterDecimal = 0;
    inputStr = inputVal.toString();
    for (var i = 0; i < inputStr.length; i++) 
    { 
        var oneChar = inputStr.charAt(i);
    
        if (i == 0 && oneChar == '-')
            continue;
            
        if (oneDecimal) 
            charsAfterDecimal++;

        if (oneChar == '.' && !oneDecimal) 
        {
            oneDecimal = true; 
            continue; 
        } 
            
        if (oneDecimal && charsAfterDecimal > iDecimals)
            return false;
            
        if (oneChar == ',')
            continue;
            
        if (oneChar < '0' || oneChar > '9')
            return false;
    } 
    return true;
} 

function FormatDate(sDate)
{
    if (sDate.length == 0)
        return '';
        
    var input = sDate;
    
    // Replace other delimiters with '/'
    input = input.replace(/-/g, '/');
    input = input.replace(/\./g, '/');
    
    var delim1 = input.indexOf('/');
    var delim2 = input.lastIndexOf('/');
    
    var mm, dd, yyyy;
    
    if (delim1 != -1)
    {
        // there are delimiters; extract component values 
        mm = parseInt(input.substring(0, delim1), 10);
        dd = parseInt(input.substring(delim1 + 1, delim2), 10);
        yyyy = parseInt(input.substring(delim2 + 1, input.length), 10);
    }
    else 
    {
        // there are no delimiters; extract component values 
        mm = parseInt(input.substring(0, 2), 10);
        dd = parseInt(input.substring(2, 4), 10);
        yyyy = parseInt(input.substring(4, input.length), 10);
    }
    
    // validate year, allowing for checks between year ranges 
    // passed as parameters from other validation functions 
    if (yyyy < 100) 
    { 
        // entered value is two digits, which we allow for 1950-2049 
        if (yyyy >= 50)
            yyyy += 1900;
        else
            yyyy += 2000;
    }
    
    return mm + '/' + dd + '/' + yyyy;
}

function FormatNum(sNum, iDec, sType)
{
    // sType = $, %, '' 
    if (typeof sNum == 'number')
        sNum = sNum.toString(); 
    
    var sTemp = ''; 
    var sNumWhole = ''; 
    var sNumFrac = ''; 
    var iDecLoc = 0; 
    var counter = 0; 
    var bNegative = false; 
    var regExp = ''; 
    
    // remove commas 
    regExp = /,/g;
    sNum = sNum.replace(regExp, ''); 
    
    // remove $ 
    regExp = /\$/g;
    sNum = sNum.replace(regExp, '');
    
    // remove % 
    regExp = /%/g;
    sNum = sNum.replace(regExp, '');
    
    // remove - 
    if (sNum.indexOf('-') != -1)
        bNegative = true;
    
    regExp = /-/g;
    sNum = sNum.replace(regExp, ''); 
            
    iDecLoc = sNum.indexOf('.');
    
    if (iDecLoc == -1)
    {
        sNumFrac = ''; 
        sNumWhole = sNum; 
    } 
    else
    {
        sNumWhole = sNum.substr(0, iDecLoc); 
        sNumFrac = sNum.slice(iDecLoc + 1); 
    } 

    while (sNumWhole.length > 0 && sNumWhole.charAt(0) == '0')
        sNumWhole = sNumWhole.substr(1, sNumWhole.length - 1); 
                    
    if (sNumWhole.length == 0)
        sNumWhole = '0'; 

    for (i = sNumWhole.length - 1; i > -1; i--)
    { 
        if (counter > 0 && counter % 3 == 0) 
            sTemp = ',' + sTemp;
                    
        sTemp = sNumWhole.charAt(i) + sTemp;
        counter++; 
    } 
    
    while (sNumFrac.length < iDec)
        sNumFrac = sNumFrac + '0';
            
    if (sType == '$')
        sTemp = '$' + sTemp;
    
    if (bNegative)
        sTemp = '-' + sTemp; 
    
    // remove - 
    if (iDec != 0)
        sTemp = sTemp + '.' + sNumFrac; 
            
    if (sType == '%')
        sTemp = sTemp + '%'; 
            
    return sTemp; 
} 

function isPercent(inputVal, iDecimals) 
{
    oneDecimal = false 
    var charsAfterDecimal = 0; 
    inputStr = inputVal.toString();
    for (var i = 0; i < inputStr.length; i++) 
    {
            var oneChar = inputStr.charAt(i) 
            if (i == 0 && oneChar == '-') { 
                continue 
            } 
            if (oneChar == '%'){ 
                    continue 
            } 
            
            if (oneChar == '.' && !oneDecimal) { 
                    oneDecimal = true 
                    continue 
            } 
            
            if (oneDecimal) 
                    charsAfterDecimal++; 
            
            if (charsAfterDecimal > iDecimals){ 
                    return false 
            } 
            
            if (oneChar == ','){ 
                    continue 
            } 
            
            if (oneChar < '0' || oneChar > '9') { 
                    return false 
            } 
    } 
    return true 
} 