       
    //Dropdown to replace ordinary selctboxes
    //made to improve search posiblities
    function DropDown( valueObject, DataArray, Instance ) 
    {
        this.Valueobject = document.getElementById ( valueObject );
        this.InstanceName = Instance;
        this.Data = DataArray;
        this.IdField = 2;
        this.DataSetLength = 2; // how many fields represents a record
        this.ValueField = 0;    // Value field in list
        this.TextField = 1;     // Label field in list
        this.ClassName = '';    // Classname
        this.SearchableA = 0;   // Searcable fields, limit at this point is 2 fields
        this.SearchableB = 1;   // Searcable fields, limit at this point is 2 fields
        this.Value = ""         // Initializing value
        this.Container;         //Container where the control is situated
        this.SearchField;       // Searchfield where searchphrase is entered.
        this.Button;            // button to activate the entire list.
        this.ResultObject;      // Listbox to present the result.
        this.UseButton;

        this.Debug = true;     // enter debug mode
        this.DebugContainer;    // container where debug i written to
 
        this.ResultCollection = [];  // Array of found results
        this.nonefound = 'None found';        
        
        this.Init = function()
        {
            if ( !this.Valueobject ) return; 
            this.Container = this.Valueobject.parentNode;
            this.CreateElements();
            this.SetInitValue();
        }   
        
        this.SetInitValue = function ()
        {
            this.SearchInID ( this.Valueobject.value );
            var SelectedValue = this.ResultCollection [ this.TextField  ];
            this.SearchField.value = ( SelectedValue ) ? SelectedValue : '';
        }
        
        
        this.ReturnEditableClassName = function()
        {
            return (this.Valueobject.readOnly) ? ' DropdownLocked' : ' editable';
        }
        
        this.CreateElements = function()
        {
            this.Container.style.position = 'relative';
            
           
            this.SearchField = document.createElement('input');
            this.SearchField.type = "text";
            this.SearchField.id = "input_" + this.InstanceName;
            this.SearchField.title = "input_" + this.InstanceName;
            this.SearchField.className = 'IntelliSelector' + this.ReturnEditableClassName() + ( ( this.ClassName.length > 0 ) ? ' ' + this.ClassName : '')+ ( ( this.Valueobject.parentNode.className.length > 0 ) ? ' ' + this.Valueobject.parentNode.className : '');
            this.SearchField.onkeyup = new Function( this.InstanceName + ".SearchKeyUp( this.value )");
            this.SearchField.onblur = new Function(  this.InstanceName + ".SearchBlur( this.value )");
            this.SearchField.disabled = this.Valueobject.readOnly;
            this.Container.appendChild( this.SearchField );

            this.ResultObject = document.createElement('select');
            this.ResultObject.size = 15;
            this.ResultObject.id = 'DropdownResult_' + this.InstanceName;
            this.ResultObject.className = 'IntelliSelector' + ( ( this.ClassName ) ? ' ' + this.ClassName : '');
            this.ResultObject.style.position = 'absolute';
            this.ResultObject.style.left = 0 + 'px';
            this.ResultObject.style.top = 24 + 'px';
            this.ResultObject.style.display = 'none';
            this.ResultObject.onkeyup = new Function( this.InstanceName + ".AssignValueOnEnter (  )");            
            this.ResultObject.onblur = new Function( this.InstanceName + ".ResultBlur ( )");            
            this.ResultObject.onclick = new Function( this.InstanceName + ".AssignValue ( )");
            this.Container.appendChild( this.ResultObject );        
            
            if (this.UseButton)
            {
                if (!this.Valueobject.readOnly)
                {
                    this.Button = document.createElement('img');
                    this.Button.src = 'downloads/dropdown/list.gif';
                    this.Button.style.position = 'absolute';
                    this.Button.style.cursor = 'pointer';
                    this.Button.onclick =  new Function( Instance + ".PerformSearch ( '*' )");
                    this.Button.style.left = this.SearchField.offsetWidth - 18 + 'px';
                    this.Button.style.top = 3 + 'px';
                    this.Container.appendChild( this.Button );
                }
            }
            if ( this.Debug )
            {
                this.DebugContainer = document.createElement('div');
                this.DebugContainer.style.position =  'absolute';
                this.DebugContainer.style.left = 500 + 'px';
                this.DebugContainer.style.height = 300 + 'px';
                this.DebugContainer.style.width = 300 + 'px';
                this.DebugContainer.style.overflow = 'auto';
                this.Container.appendChild( this.DebugContainer );                    
            }
        }
        this.HasValue = ( this.Valueobject.value.length > 0 ) ? true : false;
        
        this.ResultBlur = function ()
        {
            this.ToogleResult ( false );                
        }

        this.SearchKeyUp = function ( value )            
        {
            this.PerformSearch( value )
        }
     
        this.SearchBlur = function ( value )
        {
            if (this.SearchField.value.length > 0)
            {

            //      this.PerformSearch( value );
            //      this.AssignSingleValue();
                
            }
            else
            {
                this.ClearFields ();
            }
        }
        
        this.ClearFields = function ( )
        {
            this.SearchField.value = "";
            this.Valueobject.value = "";
            this.ClearOptions ();
        }
       
        this.PerformSearch = function ( SearchPhrase )
        {
            SearchPhrase = SearchPhrase.toLowerCase();
            if (SearchPhrase.length == 0)
            {
                return this.ToogleResult ( false );
            }
            else
            {
                this.Search ( SearchPhrase );
                this.PopulateOptions ( );                
            }
        }
        
        this.Search = function ( SearchPhrase )
        {
            var startOfValue = null;
            var startOfText = null;
            this.ResultCollection = [];
            for (var i = 0; i < this.Data.length; i += this.DataSetLength )
            {
                startOfValue = this.Data[ i + this.SearchableA ].substr(0, SearchPhrase.length ).toLowerCase();
                startOfText = this.Data[ i + this.SearchableB ].substr(0, SearchPhrase.length ).toLowerCase();
                 
                if ( (startOfText == SearchPhrase || startOfValue == SearchPhrase) || SearchPhrase == '*' )
                {
                    this.ResultCollection.push( this.Data [ i + this.IdField ] )
                    this.ResultCollection.push( this.Data [ i + this.TextField ] )
                }
            }
        }
        
        this.SearchInID = function ( Id )
        {
            this.ResultCollection = [];
            for (var i = 0; i < this.Data.length; i+= this.DataSetLength )
            {
                if ( this.Data [ i + this.IdField ] == Id )
                {
                    this.ResultCollection.push( this.Data [ i + this.IdField ] )
                    this.ResultCollection.push( this.Data [ i + this.TextField ] )
                }
            }        
        }

        this.ClearOptions = function ( )
        {     
            this.ResultObject.options.length = 0
        }

        this.PopulateOptions = function ( )
        {
            var NewOption;
            var SearchResult = this.ResultCollection;

            this.ClearOptions();
            
            if (SearchResult.length == 2) 
            {
                NewOption = new Option( SearchResult [ 1 ], SearchResult [ 0 ] );
                this.ResultObject.options.add( NewOption );
                this.AssignSingleValue();
                return this.ToogleResult ( false );
            }
            else    
            {
                if (SearchResult.length > 0)
                {
                    for (var i = 0; i< SearchResult.length; i+=2 )
                    {    
                        NewOption = new Option( SearchResult [ i + 1 ], SearchResult [ i ] );
                        this.ResultObject.options.add( NewOption );
                    }
                }   
                else
                {
                    this.ResultObject.options.add( new Option( this.nonefound, -1 ) );
                }
                return this.ToogleResult ( true );
            }            
        }
        
        this.SetFocus = function ( obj ) 
        {
            if (obj && obj.style.display != 'none')
                obj.focus();
        }
     
        this.ToogleResult = function ( DoToggle ) 
        {
            this.ResultObject.style.display = ( DoToggle ) ? "block" : "none";
        }
        
        this.AssignValueOnEnter = function ( ) 
        {
            if ( ( event.which && event.which == 13 ) || ( event.keyCode && event.keyCode == 13 ) )
            {
                if ( this.ResultObject.selectedIndex >= 0 )
                    this.ResultObject.click();    
            }
        }
        
        this.AssignValue = function ( ) 
        {
        
            if ( this.ResultObject.selectedIndex >= 0 )
            {
                var SelectedValue = this.ResultObject.options[ this.ResultObject.options.selectedIndex ].value;
                var SelectedText = this.ResultObject.options[ this.ResultObject.options.selectedIndex ].text;

                this.Valueobject.value = ( SelectedValue != "-1" ) ? SelectedValue : "";    
                this.SearchField.value = ( SelectedText != this.nonefound ) ? SelectedText : "";    
                            
                this.ToogleResult ( false );
            }
            
        }
        
        this.AssignSingleValue = function ( ) 
        {
            if ( this.ResultCollection.length == 2 )
            {
                this.Valueobject.value = this.ResultCollection [ 0 ];    
                this.SearchField.value = this.ResultCollection [ 1 ];
            }
        }

        this.DoDebug = function ( message )
        {
           if ( this.Debug )
           {
                var p = document.createElement ( 'p' );
                p.appendChild ( document.createTextNode( message ) );
                this.DebugContainer.appendChild ( p );
           }
        }
    }