﻿//This function formats the text so that it has commas and a period followed by two decimal places
function formatCurrency(textbox)
{
    var name = textbox.id.substr(0, textbox.id.length - 7);
    
    if(document.getElementById(name + "RegExpValidator").style.display == "none" && document.getElementById(name + "RequiredValidator").style.display == "none")
    {
        textbox.value = textbox.value.replace(/\,|\$/g,'');
        
        var index = textbox.value.indexOf('.');
        
        if(index < 0)
        {
            textbox.value += ".00";
        }
        else if(index == textbox.value.length - 2)
        {
            textbox.value += "0";
        }
        else if(index == textbox.value.length - 1)
        {
            textbox.value += "00";
        }
        
        if(index == 0)
        {
            textbox.value = "0" + textbox.value;
        }
        else
        {
            while(textbox.value.charAt(0) == '0')
            {
                if(textbox.value.charAt(1) == '.')
                {
                    break;
                }
                textbox.value = textbox.value.substr(1);
            }
        }
        
        var position = ((textbox.value.length % 3 == 0) ? 3 : textbox.value.length % 3);
        
        var count = Math.floor((textbox.value.length - 4)/3);
        
        for(i = 0; i < count; i++)
        {
            textbox.value = textbox.value.substr(0,position) + "," + textbox.value.substr(position);
            
            position += 4;
        }
        
        return true;				            
    }
    return false;
}

//
function formatNumber(textbox)
{
    var name = textbox.id.substr(0, textbox.id.length - 7);
    
    if(document.getElementById(name + "RegExpValidator").style.display == "none" && document.getElementById(name + "RequiredValidator").style.display == "none")
    {
        var index = textbox.value.indexOf('.');
        
        if(index == textbox.value.length - 1)
        {
            textbox.value = textbox.value.substr(0, index);
        }
        else if(index >= 0 && index < textbox.value.length - 1)
        {
            var numDigits = textbox.value.length - index - 1;
            var digits = textbox.value.substr(index + 1, numDigits);
            
            if(digits - 0 == 0)
            {
                textbox.value = textbox.value.substr(0, index);
            }
            if(textbox.value == "")
            {
                textbox.value = "0";
            }
            else if(index == 0)
            {
                textbox.value = "0" + textbox.value;
            }
        }
        while(textbox.value.charAt(0) == '0')
        {
            if(textbox.value.charAt(1) == '.' || textbox.value.length == 1)
            {
                break;
            }
            textbox.value = textbox.value.substr(1);
        }
    }
    
    return true; 
}

//This function formats the text so that there is at least one digit before and after the decimal point
function formatRate(textbox)
{
    var name = textbox.id.substr(0, textbox.id.length - 7);
    
    if(document.getElementById(name + "RegExpValidator").style.display == "none" && document.getElementById(name + "RequiredValidator").style.display == "none")
    {
        var index = textbox.value.indexOf('.');
        
        if(index == textbox.value.length - 1)
        {
            textbox.value += "0";
        }
        else if(index < 0)
        {
            textbox.value += ".0";
        }
        if(index == 0)
        {
            textbox.value = "0" + textbox.value;
        }
        while(textbox.value.charAt(0) == '0')
        {
            if(textbox.value.charAt(1) == '.' || textbox.value.length == 1)
            {
                break;
            }
            textbox.value = textbox.value.substr(1);
        }
    }
    
    return true; 
}

