﻿
/*
    Computer Science Group, LLC
    225-766-2397 office
    225-205-0263 cell
    darrell@computersciencegroup.com
    Copyright © 2008 Computer Science Group, LLC, All rights reserved.
*/

//csgDropDownList(<opCsgDropDownListID>)
//04/21/2008
function csgDropDownList(opCsgDropDownListID)
{
    //global used in properties/methods/events
    var oCsgDropDownList = this;
    /****************************************************************************************
    properties
    ****************************************************************************************/
    //cannot continue if DropDownList is not found.
    this.dropDownList = document.getElementById(opCsgDropDownListID);
    if (!this.dropDownList)
    {
        alert("Drop down list " + opCsgDropDownListID + " not found.");
        return;
    }
    this.dropDownListID = opCsgDropDownListID;
    this.cssClassCsgDropDownList = "csgDropDownList";
    /****************************************************************************************
    events
    ****************************************************************************************/
    //onclick
    this.onClick = function() { if (ocsgPage.dropDownListOnClick) { ocsgPage.dropDownListOnClick(oCsgDropDownList); } }
    this.dropDownList.onclick = oCsgDropDownList.onClick;
    //ondblclick
    this.onDblClick = function() { if (ocsgPage.dropDownListOnDblClick) { ocsgPage.dropDownListOnDblClick(oCsgDropDownList); } }
    this.dropDownList.ondblclick = oCsgDropDownList.onDblClick;
    //onkeydown
    this.onKeyDown = function() { if (ocsgPage.dropDownListOnKeyDown) { ocsgPage.dropDownListOnKeyDown(oCsgDropDownList); } }
    this.dropDownList.onkeydown = oCsgDropDownList.onKeyDown;
    //onkeyup
    this.onKeyUp = function() { if (ocsgPage.dropDownListOnKeyUp) { ocsgPage.dropDownListOnKeyUp(oCsgDropDownList); } }
    this.dropDownList.onkeyup = oCsgDropDownList.onKeyUp;
    //onkeypress
    this.onKeyPress = function() { if (ocsgPage.dropDownListOnKeyPress) { ocsgPage.dropDownListOnKeyPress(oCsgDropDownList); } }
    this.dropDownList.onkeypress = oCsgDropDownList.onKeyPress;
    //onmouseout
    this.onMouseOut = function() { if (ocsgPage.dropDownListOnMouseOut) { ocsgPage.dropDownListOnMouseOut(oCsgDropDownList); } }
    this.dropDownList.onmouseout = oCsgDropDownList.onMouseOut;
    //onmouseover
    this.onMouseOver = function() { if (ocsgPage.dropDownListOnMouseOver) { ocsgPage.dropDownListOnMouseOver(oCsgDropDownList); } }
    this.dropDownList.onmouseover = oCsgDropDownList.onMouseOver;
    //onmousemove
    this.onMouseMove = function() { if (ocsgPage.dropDownListOnMouseMove) { ocsgPage.dropDownListOnMouseMov(oCsgDropDownList); } }
    this.dropDownList.onmousemove = oCsgDropDownList.onMouseMove;
    //onmousedown
    this.onMouseDown = function() { if (ocsgPage.dropDownListOnMouseDown) { ocsgPage.dropDownListOnMouseDown(oCsgDropDownList); } }
    this.dropDownList.onmousedown = oCsgDropDownList.onMouseDown;
    //onmouseup
    this.onMouseUp = function() { if (ocsgPage.dropDownListOnMouseUp) { ocsgPage.dropDownListOnMouseUp(oCsgDropDownList); } }
    this.dropDownList.onmouseup = oCsgDropDownList.onMouseUp;
    //onblur
    this.onBlur = function() { if (ocsgPage.dropDownListOnBlur) { ocsgPage.dropDownListOnBlur(oCsgDropDownList); } }
    this.dropDownList.onblur = oCsgDropDownList.onBlur;
    //onfocus
    this.onFocus = function()
    {
        ocsgPage.setCurrentElement(event.srcElement);
        if (ocsgPage.dropDownListOnFocus) { ocsgPage.dropDownListOnFocus(oCsgDropDownList); }
    }
    this.dropDownList.onfocus = oCsgDropDownList.onFocus;
    this.onChange = function()
    {
        if (ocsgPage.dropDownListOnChange) { ocsgPage.dropDownListOnChange(oCsgDropDownList); }
    }
    this.dropDownList.onchange = oCsgDropDownList.onChange;
    /****************************************************************************************
    methods
    ****************************************************************************************/
    //setValueFromGridCell(<spColumnName>, <opGrid>)
    //07/02/2008
    this.setValueFromGridCell = function(spColumnName, opGrid)
    {
        var oHidden;
        var oCell = opGrid.getCellByColumnName(opGrid.getActiveRow(), spColumnName);
        if (oCell)
        {
            oCsgDropDownList.setValue(oCell.innerText);
            //see if associated hidden field is found
            oHidden = document.getElementById(spColumnName);
            if (oHidden) { oHidden.value = oCell.innerText; }
        }        
    }
    //setValueFromGridHidden(<spColumnName>, <opGrid>)
    //07/22/2008
    this.setValueFromGridHidden = function(spColumnName, opGrid)
    {
        var oHidden;
        var oGridHidden = opGrid.getGridHiddenField(spColumnName);
        if (oGridHidden)
        {
            oCsgDropDownList.setValue(oGridHidden.value);
            //see if associated hidden field is found
            oHidden = document.getElementById(spColumnName);
            if (oHidden) { oHidden.value = oGridHidden.value; }
        }
    }
    //setValue(<opValue>)
    //04/22/2008
    this.setValue = function(opValue)
    {
        oCsgDropDownList.dropDownList.value = "";
        for (var i=0; i<oCsgDropDownList.dropDownList.children.length; i++)
        {
            if (parseInt(opValue) > -1)
            {
                if (oCsgDropDownList.dropDownList.children[i].value == opValue)
                {
                    oCsgDropDownList.dropDownList.children[i].selected = true;
                    break;
                }
                else { oCsgDropDownList.dropDownList.children[i].selected = false; }
            }
            else { oCsgDropDownList.dropDownList.children[i].selected = false; }
        }
    }
    //getValue()
    //04/22/2008
    this.getValue = function()
    {
        var oValue;
        for (var i=0; i<oCsgDropDownList.dropDownList.children.length; i++)
        {
            if (oCsgDropDownList.dropDownList.children[i].selected == true)
            {
                oValue = new Object();
                oValue.value = oCsgDropDownList.dropDownList.children[i].value;
                oValue.innerText = oCsgDropDownList.dropDownList.children[i].innerText;
                break;
            }
        }
        return oValue;
    }
    //onChangeMethod(<opGrid>, <opToolbar>)
    //12/10/2008
    this.onChangeMethod = function(opGrid, opToolbar)
    {
        var oGridDataIsChanged;
//        if (opGrid.getCellByColumnName(opGrid.getActiveRow(), oCsgDropDownList.dropDownListID))
//        {
            if (ocsgPage.gridDataIsChanged) { oGridDataIsChanged = ocsgPage.gridDataIsChanged(); }
            opGrid.enableGrid(!oGridDataIsChanged);
            opToolbar.enableEditingToolbar(oGridDataIsChanged);
//        }
    }
    //onKeyUpMethod(<opKeyCode>)
    //07/26/2008
    this.onKeyUpMethod = function(opKeyCode)
    {
        if ((opKeyCode == 27) || (opKeyCode == 46))
        {
            oCsgDropDownList.setValue(null);
            if (ocsgPage.dropDownListOnChange) { ocsgPage.dropDownListOnChange(oCsgDropDownList); }
        }
    }
}