﻿
/*
    Computer Science Group, LLC
    225-766-2397 office
    225-205-0263 cell
    darrell@computersciencegroup.com
    Copyright © 2010 Computer Science Group, LLC, All rights reserved.
*/

//csgTextBox(<opFormWindow>, <opTextBoxId>, <opMaxLength>, <opCsgToolbar>, <opddFormGridTable>)
//06/10/2010
//09/04/2010 place standard event functionality for toolbar in class
function csgTextBox(opFormWindow, opTextBoxId, opMaxLength, opCsgToolbar, opddFormGridTable)
{
    //global used in properties/methods/events
    var oThis = this;
    this.formWindow = opFormWindow;
    this.textBox = this.formWindow.document.getElementById(opTextBoxId);
    this.cssClassCsgTextBoxSelected = "csgTextBoxSelected";
    this.cssClassCsgTextBox = "csgTextBox";
    this.cssClassCsgTextBoxBadValue = "csgTextBoxBadValue";
    this.cssClassCsgTextBoxReadOnly = "csgTextBoxReadOnly";
    this.cssClassCsgTextBoxReadOnlySelected = "csgTextBoxReadOnlySelected";
    //11/11/2011 take care of readonly
    this.isReadOnly = this.textBox.readOnly;
    if (this.isReadOnly) { this.textBox.className = this.cssClassCsgTextBoxReadOnly; }
    else { this.textBox.className = this.cssClassCsgTextBox; }
    if (opMaxLength) { this.textBox.maxLength = opMaxLength; }
    //11/26/2010
    //refreshes the script to prevent the error: javascript can't execute code from a freed script
    //If provided, use the drag and drop form to find the iframe with the grid table.
    if (opddFormGridTable) { this.ddFormGridTable = opddFormGridTable; }
    else { this.ddFormGridTable = false; }
    //--------------------------------------
    //09/04/2010 toolbar for events
     if (opCsgToolbar) { this.toolbar = opCsgToolbar; }
    /****************************************************************************************
    events
    ****************************************************************************************/
    //onFocus()
    //06/10/2010
    this.onFocus = function()
    {
        if (oThis.isReadOnly == true)
        {
            event.srcElement.className = oThis.cssClassCsgTextBoxReadOnlySelected;
            event.srcElement.blur();
        }
        else
        {
            event.srcElement.select();
            event.srcElement.className = oThis.cssClassCsgTextBoxSelected;
            if (oThis.formWindow.textBoxOnFocus) { oThis.formWindow.textBoxOnFocus(oThis); }
        }
    }
    this.textBox.onfocus = oThis.onFocus;
    //onBlur()
    //06/10/2010
    this.onBlur = function()
    {
        if (oThis.isReadOnly == true) { event.srcElement.className = oThis.cssClassCsgTextBoxReadOnly; }
        else
        {
            event.srcElement.className = oThis.cssClassCsgTextBox;
            if (oThis.formWindow.textBoxOnBlur) { oThis.formWindow.textBoxOnBlur(oThis); }
        }
    }
    this.textBox.onblur = oThis.onBlur;	
    //onKeyPress()
    //isNumericKeyCode(<opKeyCode>)
    //06/10/2010
    //09/04/2010 common editing toolbar features
    //notice, can be over ridden in page
    this.isNumericKeyCode = function (opKeyCode) { return ((opKeyCode >= 48) && (opKeyCode <= 57)) }
    this.onKeyPress = function ()
    {
        if (oThis.isReadOnly == true)
        {
            event.keyCode = null;
            event.srcElement.blur();
        }
        else
        {
            if (oThis.formWindow.textBoxOnKeyPress) { oThis.formWindow.textBoxOnKeyPress(oThis); }
            else
            {
                //09/04/2010 common editing toolbar features
                //notice, can be over ridden in page
                oThis.setEditState();
            }
        }
    }
    this.textBox.onkeypress  = oThis.onKeyPress;
    //onKeyUp()
    //06/10/2010
    //09/04/2010 common editing toolbar features
    //notice, can be over ridden in page
    this.onKeyUp = function ()
    {
        if (oThis.formWindow.textBoxOnKeyUp) { oThis.formWindow.textBoxOnKeyUp(oThis); }
        else
        {
            //09/04/2010 common editing toolbar features
            //notice, can be over ridden in page
            oThis.setEditState();
        }
        //11/26/2010
        if (oThis.ddFormGridTable) { oThis.gridTable = pageGetGridTable(oThis.ddFormGridTable); }
        else { oThis.gridTable = pageGetGridTable(); }
        //--------------------------------------
        if (oThis.gridTable)
        {
            if (oThis.gridTable.formDataHasChanged)
            {
                //some forms like, log in, don't have a toolbar
                if (oThis.toolbar)
                {
                    if (oThis.gridTable.formDataHasChanged(self) == true) { oThis.toolbar.setEditStateOn(); }
                    else { oThis.toolbar.setEditStateOff(); }
                }
            }
        }
    }

    this.textBox.onkeyup = oThis.onKeyUp;	
    //onChange()
    //06/10/2010
    //09/04/2010 common editing toolbar features
    //notice, can be over ridden in page
    this.onChange = function ()
    {
        if (oThis.formWindow.textBoxOnChange) { oThis.formWindow.textBoxOnChange(oThis); }
        else
        {
            //09/04/2010 common editing toolbar features
            //notice, can be over ridden in page
            oThis.setEditState();
        }
    }
    this.textBox.onchange = oThis.onChange;

    /****************************************************************************************
    methods
    ****************************************************************************************/

    //setReadOnly()
    //06/10/2010
    this.setReadOnly = function ()
    {
        oThis.isReadOnly = true;
        oThis.textBox.className = oThis.cssClassCsgTextBoxReadOnlySelected;
    }
    
    //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 = oThis.isNumericKeyCode(oKeyCode);
        //if not a number, must be a '/'
        if (!oReturn) { oReturn = (oKeyCode == 47); }
        return oReturn;
    }

    //setEditState()
    //09/04/2010
    this.setEditState = function ()
    {
        //11/24/2010
        //11/26/2010
        if (oThis.ddFormGridTable) { oThis.gridTable = pageGetGridTable(oThis.ddFormGridTable); }
        else { oThis.gridTable = pageGetGridTable(); }
        if ((oThis.gridTable) && (oThis.toolbar))
        {
            if (oThis.gridTable.formDataHasChanged(self) == true) { oThis.toolbar.setEditStateOn(); }
            else { oThis.toolbar.setEditStateOff(); }
        }
    }

    //--------------------------------------------------------------------------------------------------
    //updateCrudType()
    //10/29/2010 - see if an iframe has a txei form in the content window
    //--------------------------------------------------------------------------------------------------
    this.updateCrudType = function ()
    {
        if (window.txeiCrudType_id)
        {
            if (window.txei) { window.txei.setCrudType(window.txeiCrudType_id, "U"); }
        }
    }
}

//textBoxValidateMinMaxValues(<opElement>)
//11/01/2010
textBoxValidateMinMaxValues = function (opElement)
{
    var iValue;
    var iMinValue;
    var iMaxValue;
    var otextBox = false;
    //get the textbox to return
    if (opElement.csgParent) { otextBox = opElement.csgParent.textBox }
    else if (opElement.textBox) { otextBox = opElement.textBox; }
    else { otextBox = false; }
    if (otextBox)
    {
        if (otextBox.value)
        {
            //----------------------------
            //validate minimum value
            if (opElement.minValue)
            {
                //if this is a datetime, isANumber will return false
                if (opElement.csgParent.isANumber(opElement.minValue.toString()) == true)
                {
                    //value entered
                    if (opElement.csgParent.textBox.value)
                    {
                        try { iValue = parseInt(opElement.csgParent.textBox.value); } catch (e) { iValue = 0; }
                    }
                    else { iValue = 0; }
                    //min value
                    try { iMinValue = parseInt(opElement.minValue); } catch (e) { iMinValue = 0; }
                    if (iValue < iMinValue)
                    {
                        otextBox.focus();
                        otextBox.className = opElement.csgParent.cssClassCsgTextBoxBadValue;
                        alert("Value is below the minimum value of " + iMinValue.toString() + ". Value will be modified to comply with business rules.");
                        otextBox.value = iMinValue.toString();
                        otextBox.className = opElement.csgParent.cssClassCsgTextBox;
                    }
                }
            }
            //----------------------------
            //validate maximum value
            if (opElement.maxValue)
            {
                //if this is a datetime, isANumber will return false
                if (opElement.csgParent.isANumber(opElement.maxValue.toString()) == true)
                {
                    //value entered
                    if (opElement.csgParent.textBox.value)
                    {
                        try { iValue = parseInt(opElement.csgParent.textBox.value); } catch (e) { iValue = 0; }
                    }
                    else { iValue = 0; }
                    //min value
                    try { iMaxValue = parseInt(opElement.maxValue); } catch (e) { iMaxValue = 0; }
                    if (iValue > iMaxValue)
                    {
                        otextBox.focus();
                        otextBox.className = opElement.csgParent.cssClassCsgTextBoxBadValue;
                        alert("Value is above the maximum value of " + iMaxValue.toString() + ". Value will be modified to comply with business rules.");
                        otextBox.value = iMaxValue.toString();
                        otextBox.className = opElement.csgParent.cssClassCsgTextBox;
                    }
                }
            }
        }
    }
    return otextBox;
}
