﻿
/*
    Computer Science Group, LLC
    225-766-2397 office
    225-205-0263 cell
    darrell@computersciencegroup.com
    Copyright © 2008 Computer Science Group, LLC, All rights reserved.
*/

//csgTextBoxDecimal(<opCsgTextBoxID>, <ipPrecision>, <ipScale>)
//09/06/2008
//<ipPrecision> - total length, not including decimal point.
//<ipScale> - number of digits after the decimal point.
function csgTextBoxDecimal(opCsgTextBoxID, ipPrecision, ipScale)
{
    //cannot continue if textbox is not found.
    if (!document.getElementById(opCsgTextBoxID))
    {
        alert("TextBox " + opCsgTextBoxID + " not found.");
        return;
    }
    /****************************************************************************************
    //Inherits from csgTextBox
    ****************************************************************************************/
    this.parent = new csgTextBox(opCsgTextBoxID);
    var oCsgTextBoxDecimal = this;
    /****************************************************************************************
    properties
    ****************************************************************************************/
    this.precision = parseInt(ipPrecision);
    this.scale = parseInt(ipScale);
    //allow for decimal
    this.parent.textBox.maxLength = this.precision + 1;
    /****************************************************************************************
    events
    ****************************************************************************************/
    //onkeypress - replace onkeypress event from csgTextBox
    this.onKeyPress = function()
    {
        var iIndex;
        var sScale;
        //only one decimal
        if (event.keyCode == 46)
        {
            if (event.srcElement.value.indexOf(".") > 0)
            {
                //if the texbox is selected, clear it
                if (document.selection.createRange().text == event.srcElement.value) { event.srcElement.value = ""; }
                else { event.keyCode  = null; }
            }
        }
        else
        {
            //must enter a number
            if (oCsgTextBoxDecimal.parent.isNumericKeyCode(event.keyCode) == false) { event.keyCode  = null; }
            //if the texbox is selected, clear it
            if (document.selection.createRange().text == event.srcElement.value) { event.srcElement.value = ""; }
            //only so many decimals
            if (event.srcElement.value)
            {
                iIndex = event.srcElement.value.indexOf(".");
                if (iIndex > 0)
                {
                    sScale = event.srcElement.value.substr(iIndex + 1);
                    if (sScale.length >= oCsgTextBoxDecimal.scale) { event.keyCode  = null; }
                }
            }
        }
        if (ocsgPage.textBoxOnKeyPress) { ocsgPage.textBoxOnKeyPress(oCsgTextBoxDecimal.parent); }
    }
    this.parent.textBox.onkeypress = oCsgTextBoxDecimal.onKeyPress;	
    //onblur - replace onblur event from csgTextBox
    this.onBlur = function()
    {
        event.srcElement.className = oCsgTextBoxDecimal.parent.cssClassCsgTextBox;
        var oValue = oCsgTextBoxDecimal.formatTheNumber(oCsgTextBoxDecimal.parent.textBox.value);
        if (oValue)
        {
            if (oCsgTextBoxDecimal.parent.isANumber(oValue) == false) { event.srcElement.className = oCsgTextBoxDecimal.parent.cssClassCsgTextBoxBadValue; }
        }
        event.srcElement.value = oValue;
        if (ocsgPage.textBoxOnBlur) { ocsgPage.textBoxOnBlur(oCsgTextBoxDecimal.parent); }
    }
    this.parent.textBox.onblur = oCsgTextBoxDecimal.onBlur;	
    /****************************************************************************************
    methods
    ****************************************************************************************/
    //formatTheNumber(<opValue>)
    //09/07/2008
    this.formatTheNumber = function(opValue)
    {
        var oValue;
        var sTemp;
        var sValue = "";
        var sDecimal = "";
        var sScale = "";
        var iIndex;
        if (opValue)
        {
            oValue = opValue;
            if (oCsgTextBoxDecimal.parent.isANumber(oValue) == true)
            {
                sValue = oCsgTextBoxDecimal.parent.roundedNumber(oValue, oCsgTextBoxDecimal.scale).toString();
            }
            iIndex = sValue.indexOf(".");
            if (iIndex < 0)
            {
                //no decimal
                sDecimal = sValue;
                sScale = "0";
            }
            else
            {
                //got a decimal
                sDecimal = sValue.substr(0, iIndex);
                sScale = sValue.substr(iIndex + 1);
            }
            sDecimal = (sDecimal == "" ? "0" : sDecimal)
            sScale = (sScale == "" ? "0" : sScale)
            sTemp = sDecimal + "." + sScale;
            while (true)
            {
                if ((sTemp.length <= oCsgTextBoxDecimal.parent.textBox.maxLength) && (sScale.length <= oCsgTextBoxDecimal.scale))
                {
                    sValue = sDecimal + "." + sScale;
                }
                else { break; }
                sTemp = sDecimal + "." + sScale;
                sScale = sScale + "0";
            }
        }
        return sValue;
    }
}