﻿
/*
    Computer Science Group, LLC
    225-766-2397 office
    225-205-0263 cell
    darrell@computersciencegroup.com
    Copyright © 2008 Computer Science Group, LLC, All rights reserved.
*/

//csgTextBox(<opCsgTextBoxID>)
//08/19/2008
function csgTextBox(opCsgTextBoxID)
{
    //global used in properties/methods/events
    var oCsgTextBox = this;
    /****************************************************************************************
    properties
    ****************************************************************************************/
    //cannot continue if textbox is not found.
    this.textBox = document.getElementById(opCsgTextBoxID);
    if (!this.textBox)
    {
        alert("TextBox " + opCsgTextBoxID + " not found.");
        return;
    }
    this.textBoxID = opCsgTextBoxID;
    this.isNumeric = false;
    this.cssClassCsgTextBoxSelected = "csgTextBoxSelected";
    this.cssClassCsgTextBox = "csgTextBox";
    this.cssClassCsgTextBoxBadValue = "csgTextBoxBadValue";
    oCsgTextBox.className = oCsgTextBox.cssClassCsgTextBox;
    /****************************************************************************************
    events
    ****************************************************************************************/
    //onclick
    this.onClick = function() { if (ocsgPage.textBoxOnClick) { ocsgPage.textBoxOnClick(oCsgTextBox); } }
    this.textBox.onclick = oCsgTextBox.onClick;
    //ondblclick
    this.onDblClick = function() { if (ocsgPage.textBoxOnDblClick) { ocsgPage.textBoxOnDblClick(oCsgTextBox); } }
    this.textBox.ondblclick = oCsgTextBox.onDblClick;
    //onkeydown
    this.onKeyDown = function() { if (ocsgPage.textBoxOnKeyDown) { ocsgPage.textBoxOnKeyDown(oCsgTextBox); } }
    this.textBox.onkeydown = oCsgTextBox.onKeyDown;
    //onkeyup
    this.onKeyUp = function() { if (ocsgPage.textBoxOnKeyUp) { ocsgPage.textBoxOnKeyUp(oCsgTextBox); } }
    this.textBox.onkeyup = oCsgTextBox.onKeyUp;
    //onkeypress
    this.onKeyPress = function()
    {
        if (oCsgTextBox.isNumeric == true) { if (oCsgTextBox.isNumericKeyCode(event.keyCode ) == false) { event.keyCode  = null; } }
        if (ocsgPage.textBoxOnKeyPress) { ocsgPage.textBoxOnKeyPress(oCsgTextBox); }
    }
    this.textBox.onkeypress = oCsgTextBox.onKeyPress;	
    //onmouseout
    this.onMouseOut = function() { if (ocsgPage.textBoxOnMouseOut) { ocsgPage.textBoxOnMouseOut(oCsgTextBox); } }
    this.textBox.onmouseout = oCsgTextBox.onMouseOut;	
    //onmouseover
    this.onMouseOver = function() { if (ocsgPage.textBoxOnMouseOver) { ocsgPage.textBoxOnMouseOver(oCsgTextBox); } }
    this.textBox.onmouseover = oCsgTextBox.onMouseOver;	
    //onmousemove
    this.onMouseMove = function() { if (ocsgPage.textBoxOnMouseMove) { ocsgPage.textBoxOnMouseMove(oCsgTextBox); } }
    this.textBox.onmousemove = oCsgTextBox.onMouseMove;	
    //onmousedown
    this.onMouseDown = function() { if (ocsgPage.textBoxOnMouseDown) { ocsgPage.textBoxOnMouseDown(oCsgTextBox); } }
    this.textBox.onmousedown = oCsgTextBox.onMouseDown;	
    //onmouseup
    this.onMouseUp = function() { if (ocsgPage.textBoxOnMouseUp) { ocsgPage.textBoxOnMouseUp(oCsgTextBox); } }
    this.textBox.onmouseup = oCsgTextBox.onMouseUp;	
    //onblur
    this.onBlur = function()
    {
        event.srcElement.className = oCsgTextBox.cssClassCsgTextBox;
        if (ocsgPage.textBoxOnBlur) { ocsgPage.textBoxOnBlur(oCsgTextBox); }
    }
    this.textBox.onblur = oCsgTextBox.onBlur;	
    //onfocus
    this.onFocus = function()
    {
        event.srcElement.select();
        event.srcElement.className = oCsgTextBox.cssClassCsgTextBoxSelected;
        ocsgPage.setCurrentElement(event.srcElement);
        if (ocsgPage.textBoxOnFocus) { ocsgPage.textBoxOnFocus(oCsgTextBox); }
    }
    this.textBox.onfocus = oCsgTextBox.onFocus;
    //onchange
    this.onChange = function() { if (ocsgPage.textBoxOnChange) { ocsgPage.textBoxOnChange(oCsgTextBox); } }
    this.textBox.onchange = oCsgTextBox.onChange;
    /****************************************************************************************
    methods
    ****************************************************************************************/
    //setValueFromGridCell(<spColumnName>, <opGrid>)
    //09/18/2008
    this.setValueFromGridCell = function(spColumnName, opGrid)
    {
        var oCell = opGrid.getCellByColumnName(opGrid.getActiveRow(), spColumnName);
        if (oCell) { oCsgTextBox.setValue(oCell.innerText); }
        else { oCsgTextBox.setValue("")}
        oCsgTextBox.textBox.className = oCsgTextBox.cssClassCsgTextBox;
    }
    //setValueFromGridHidden(<spColumnName>, <opGrid>)
    //09/18/2008
    this.setValueFromGridHidden = function(spColumnName, opGrid)
    {
        var oGridHidden = opGrid.getGridHiddenField(spColumnName);
        if (oGridHidden) { oCsgTextBox.setValue(oGridHidden.value); }
        else { oCsgTextBox.setValue("")}
        oCsgTextBox.textBox.className = oCsgTextBox.cssClassCsgTextBox;
    }
    //setValue(<opValue>)
    //04/21/2008
    this.setValue = function(opValue)
    {
        if (typeof(opValue) == "undefined") { oCsgTextBox.textBox.value = ""; }
        else { oCsgTextBox.textBox.value = ocsgPage.processString(opValue); }
    }
    //onKeyUpMethod(<opGrid>, <opToolbar>)
    //07/26/2008
    this.onKeyUpMethod = function(opGrid, opToolbar)
    {
        var oChanged = ocsgPage.gridDataIsChanged();
        opToolbar.enableEditingToolbar(oChanged);
        opGrid.enableNavigationToolbar(!oChanged);

    }
    //isNumericKeyCode(<oKeyCode>)
    //07/03/2008
    //46-57 is 0-9 on keyboard
    this.isNumericKeyCode = function(oKeyCode)
    {
        var oReturn = ((oKeyCode >= 48) && (oKeyCode <= 57));
        return oReturn;
    }
    //isANumber(<opValue>)
    //08/13/2008
    this.isANumber = function (opValue) { return (!isNaN(opValue)); }    
       
    //roundedNumber(<opValue>, <opScale>)
    //09/07/2008
    this.roundedNumber = function(opValue, opScale)
    {
        var iRounded;
        var iR;
        var sTemp;
        if (opScale == 1) { iR = 1; }
        else
        {
            sTemp = "10";
            for (var i=1; i<opScale; i++) { sTemp = sTemp + "0"; }
            iR = parseInt(sTemp);
        }
        iRounded = Math.round(opValue * iR) / iR;
        return iRounded;
    }
    //isDateCode(<oKeyCode>)
    //09/17/2008
    //must be number or '/'
    this.isDateCode = function(oKeyCode)
    {
        var oReturn = oCsgTextBox.isNumericKeyCode(oKeyCode);
        //if not a number, must be a '/'
        if (!oReturn) { oReturn = (oKeyCode == 47); }
        return oReturn;
    }
}