﻿//********************************************************************//
//*                                                                  *//
//*       AnyKey dotNet Classes                                      *//
//*       By Any Key Software Solutions                              *//
//*                                                                  *//
//*       Copyright (c) 2006 Any Key Software Solutions              *//
//*       ALL RIGHTS RESERVED                                        *//
//*                                                                  *//
//*   Portions of Microsoft Corporation                              *//
//*                                                                  *//
//*   The entire contents of this file is protected by Dutch, U.S.   *//
//*   and International Copyright Laws. Unauthorized reproduction,   *//
//*   reverse-engineering, and distribution of all or any portion of *//
//*   the code contained in this file is strictly prohibited and may *//
//*   result in severe civil and criminal penalties and will be      *//
//*   prosecuted to the maximum extent possible under the law.       *//
//*                                                                  *//
//*   RESTRICTIONS:                                                  *//
//*                                                                  *//
//*   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES          *//
//*   (DCU, OBJ, DLL, EXE ETC.) ARE CONFIDENTIAL AND                 *//
//*   PROPRIETARY TRADE SECRETS OF ANY KEY SOFTWARE SOLUTIONS.       *//
//*                                                                  *//
//*   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED     *//
//*   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE       *//
//*   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE      *//
//*   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT *//
//*   AND PERMISSION FROM ANY KEY SOFTWARE SOLUTIONS.                *//
//*                                                                  *//
//********************************************************************//


function AnyKeyLookupCtrl(lookupCtrl, timerCtrl, progressImg) {
    this.LookupCtrl = lookupCtrl;
    this.TimerCtrl = timerCtrl;
    this.ProgressImg = progressImg;
    this._backupText = "";
}

AnyKeyLookupCtrl.prototype =
{

    timerTick : function(s, e) 
        {
            s.SetEnabled(false);
            this._backupText = this.LookupCtrl.inputElement.value;
            if ((this._backupText) && (this._backupText.length >= 3))
            {
                if (!this.findText(this._backupText)) {
                    this.LookupCtrl.BeginUpdate();
                    this.LookupCtrl.PerformCallback(this._backupText);
                }
            }
        },
    lookupBeginCallback : function(s, e) 
        {
            s.inputElement.value = this._backupText;
            if (this.ProgressImg)
                this.ProgressImg.SetClientVisible(true);
        },
    lookupEndCallback : function(s, e) 
        {
            if (this.ProgressImg)
                this.ProgressImg.SetClientVisible(false);
            //do the dropdown if not allready down
            if(!s.droppedDown) {
                s.ShowDropDown();
            }
            s.SetText(this._backupText);
            s.EndUpdate();
            s.SetSelectedIndex(-1);
        },
    findText : function(str) 
        {
          var result = false;
          if ((str) && /*(this.LookupCtrl.droppedDown) &&*/ (this.LookupCtrl.GetItemCount() > 0)) {
            this.LookupCtrl.BeginUpdate();
            for (var j = 0; j < this.LookupCtrl.GetItemCount(); j++) 
            {
                if (this.LookupCtrl.GetItem(j).text.indexOf(str) >= 0)
                {
                    this.LookupCtrl.SetSelectedIndex(j);
                    result = true;
                    break;
                }
            }
          
            this.LookupCtrl.EndUpdate();
            return result;
          }
        },
    lookupKeyPress : function(s, e)
        {
            this.TimerCtrl.SetEnabled(false);
            //document.title = "Key: " + e.htmlEvent.keyCode + ";Char: " + e.htmlEvent.charCode;
            var k = (e.htmlEvent.keyCode && (e.htmlEvent.keyCode != 0)) ? e.htmlEvent.keyCode : e.htmlEvent.charCode;
            if (((k >= 97) && (k <= 122)) || ((k >= 65) && (k <= 90))) {
                    this.TimerCtrl.SetEnabled(true);
            }
        },
     lookupLostFocus : function(s, e)
        {
            this.TimerCtrl.SetEnabled(false);
            this.findText(s.GetText());
        }   
    
}
