﻿var DirectionEnum = { Left:0, Right:1, Up:2, Down:3};
var SearchResultListItemViewMode = { Detail:0, List:1};

var Intellisense = Class.create(); 
Intellisense.prototype = 
{   
    initialize: function(textboxId, tildeReplacement) 
    {
        //Shortcut to the search field
        this.TextBox = $(textboxId);
        this.TextBoxPosition = Element.cumulativeOffset(this.TextBox);
        this.TildeReplacement = tildeReplacement;
        this.Cache = null;
        
        //Javascript classes
        this.Utility = new Utility();
        this.Categories = new Array();
        
        //Result from server
        this.ResultFromServer = null;
        this.ErrorCodes = null;
        this.PerformaceCounters = null;
        this.Error = null;
        this.AjaxProRequestDuration = null;
        
        this.CancelFlag = this.ActiveRequests > 1;
        this.ActiveRequests = 0;
        this.DelayTime = 500; //milliseconds
        
        this.ArrowDirection = null;
        this.CurrentSelectCategory = null;
        
        //HTML        
        this.WaitScreen = null;
        this.IntellisenseContainer = null;
        this.IntellisenseBox = null;
        this.IntellisenseContentBody = null;
        this.FooterRight = null;
        this.TopRight = null;
        this.Body = null;
        
        //ERROR MSG
    	this.ShowErrorInformation = true;
        this.ErrorInformationControl = null;
    	
    	//DEBUG MSG        
        this.ShowDebugInformation = true;
        this.DebugInformationControl = null;
                    
        //Event handler        
        this.OnSuccesHandler = this.OnSucces.bindAsEventListener(this);
        this.OnKeyUpHandler = this.OnKeyUp.bindAsEventListener(this);
        this.OnKeyPressHandler = this.OnKeyPress.bindAsEventListener(this);
        this.PropertyChangeHandler = this.propertyChanged.bindAsEventListener(this);
        
        //this.OnKeyPressHandlerBox = this.OnKeyPress.bindAsEventListener(this);
        this.OnDocumentClickHandler = this.OnDocumentClick.bindAsEventListener(this);
        
        //Set the context
        Intellisense["Context"] = this;
        
        //Observe
        Event.observe(this.TextBox, 'keyup', this.OnKeyUpHandler); 
        //Event.observe(this.TextBox, 'keypress', this.OnKeyPressHandler); 
        //Event.observe(document, 'keyup', this.OnKeyPressHandlerBox); 
        Event.observe(document, 'click', this.OnDocumentClickHandler); 
        Event.observe(document, 'resizestart', this.OnDocumentClickHandler); 
        //Event.observe(document, 'scroll', this.OnDocumentClickHandler); 
        Event.observe(document, 'PropertyChange', this.PropertyChangeHandler); 
        
    },
    propertyChanged : function(eventArgs)
    {
        debugger;
    },
    dispose : function()
    {
        this.OnSuccesHandler = null;
        this.OnKeyPressHandler = null;
        this.TextBox = null;
        
    },
    OnKeyPress : function(eventArgs)
    {
    },
    OnKeyUp : function(eventArgs)
    { 
        if(this.Utility.IsArrowKey(eventArgs.keyCode))
        {
            this.Navigate(this.Utility.GetArrowDirection(eventArgs.keyCode));
        }
        else
        {   
            if(this.TextBox.value.length > 0 && 
                this.Utility.IsNumberKey(eventArgs.keyCode) || 
                this.Utility.IsLetterKey(eventArgs.keyCode) ||
                this.Utility.IsAllowedSpecialKey(eventArgs.keyCode))
            {
                this.ActiveRequests++;
                setTimeout(function(){ Intellisense.Context.KeyPressDelayed()}, Intellisense.Context.DelayTime);    
            }
        }
    },
    KeyPressDelayed : function()
    { 
        if(this.ActiveRequests == 1)
        {
            this.ShowWait();
            
            if(this.Cache != null && this.Cache[this.TextBox.value] != null)
            {
                this.OnSucces(this.Cache[this.TextBox.value]);
            }
            else
            {                
                IntellisenseAjax.Suggest(this.TextBox.value, decodeURIComponent(location.pathname).toString().replace(/\+/g," "), this.OnSuccesHandler);
            }
        }
        this.ActiveRequests--;
    },
    OnDocumentClick : function(eventArgs)
    {
        if(this.IntellisenseBox != null && this.IntellisenseBox.style.visibility != "hidden")
        {
            if(eventArgs.target.id == this.TextBox.id ||
            this.IntellisenseBox == null ||
             eventArgs.target.id == this.IntellisenseBox.id ||
             this.ClickInsideBox(eventArgs.target))
            {
                return false;
            }
            this.Hide();
        }
    },
    ClickInsideBox : function(target)
    {
        var current = target;
        
        while(current != null)
        {
            if(current.tagName == "body")
            {
                return false;
            }
            if(current.className == "intellisense")
            {
                return true;
            }
            
            current = current.parentNode;
        }
        
        return false;
    },
    OnSucces : function(transport)
    {   
        //Save value in client cache
        if(this.Cache == null)
        {
            this.Cache = new Object();
        }
        if(this.Cache[transport.request.args["seachKey"]] == null)
        {
            this.Cache[transport.request.args["seachKey"]] = transport;
        }
        
        this.ErrorCodes
        this.ResultFromServer
        this.Error = null;
        this.PerformaceCounters
        this.AjaxProRequestDuration
        
        //TODO: If fejl => RESET
        
        if(transport.value != null)
        {
            this.ResultFromServer = transport.value.first;
            this.ErrorCodes = transport.value.second;
            this.PerformaceCounters = transport.value.third;
        }
        this.AjaxProRequestDuration = transport.duration;
        
        if(transport.value.fourth != null) 
        {
            this.Error = transport.value.fourth; 
        }
                
        if(this.TextBox.value == transport.request.args["seachKey"])
        {
            this.Render();
            this.HideWait();
            this.TextBox.focus();    
        } 
    },
    OnFailure : function(error)
    {
        alert(error);
    },
	Hide : function()
	{
	    if(this.IntellisenseContainer != null)
	    {
	        this.IntellisenseContainer.style.visibility = "hidden";
	    }
	},
	Show : function()
	{
	    if(this.IntellisenseContainer != null)
	    {
	        this.IntellisenseContainer.style.visibility= "visible";
        }
	},
    Render : function()
    {
        this.TextBoxPosition = Element.cumulativeOffset(this.TextBox.parentNode);
        
        this.EnsureIntellisenseBox(this.IntellisenseBox);
        
        
        if(this.ShowDebugInformation)
        {
            if(window.console && window.console.firebug)
            {
                if(this.PerformaceCounters != null)
                {
                    for(var i=0; i < this.PerformaceCounters.length; i++)
                    {
                        var current = this.PerformaceCounters[i];
                        console.log(current.first + current.second);
                    }
                }
                
                if(this.Error != null && this.Error != "" && this.ShowDebugInformation)
                {
                    console.log(this.Error);
                }            
            }
        }
        
        if(this.ShowErrorInformation)
        {   
            this.ErrorInformationControl.innerHTML = "";
            if(this.ErrorCodes == 0)
            {
                this.ErrorInformationControl.innerHTML = "Søgestrengen er for kort. Prøv at indtaste flere bogstaver";
            }
            else if(this.ResultFromServer == null || (this.ResultFromServer != null && this.ResultFromServer.length == 0))
            {
                this.ErrorInformationControl.innerHTML = "Ingen resultater";
            }
        }
        
        /* BEREGNINGEN SKAL SKE EFTER ELEMENTET ER TILFيET TIL DOM!!!! */
        if(this.ResultFromServer != null && this.ResultFromServer.length > 0)
        {
            this.IntellisenseContainer.style.width = (this.ResultFromServer.length * 210) + 20 + "px";
        }else{
            this.IntellisenseContainer.style.width = "500px";
        }
        
        this.IntellisenseContainer.style.left = - ((this.IntellisenseBox.offsetWidth - this.TextBox.offsetWidth)/2) + "px";
        this.IntellisenseContainer.style.top = this.TextBox.offsetHeight + "px";
                
        this.Show();
    },
    EnsureIntellisenseBox : function()
    {
        if(this.IntellisenseContainer == null)
        {
            this.IntellisenseContainer = document.createElement("div");
            this.IntellisenseContainer.id = "intellisenseContainer";
        
            //Main div around all 3 categories
    	    this.IntellisenseBox = document.createElement("div");
    	    this.IntellisenseBox.className = "intellisense";
    	    this.IntellisenseBox.id = "intellisense";
            
            //SLIDING DOOR DIV - BEGIN  
    	    var top = document.createElement("div");
    	    top.className = "top";
    	    
    	    this.TopRight = document.createElement("div");
    	    this.TopRight.className = "top_right";
    	    
    	    this.Body = document.createElement("div");
    	    this.Body.className = "body";
    	    
    	    var bodyRight = document.createElement("div");
    	    bodyRight.className = "body_right";
    	    
    	    var footer = document.createElement("div");
    	    footer.className = "footer";
    	    
    	    this.FooterRight = document.createElement("div");	    
    	    this.FooterRight.className = "footer_right";
    	    this.IntellisenseContentBody = document.createElement("div");
    	    this.IntellisenseContentBody.className = "contentbody";
    	        
            var info = document.createElement("div");
            info.className = "ErrorAndDebug";
            
    	    //ERROR MSG
    	    if(this.ShowErrorInformation)
    	    {        
    	        this.ErrorInformationControl = document.createElement("div");
    	        this.ErrorInformationControl.className = "errorMsg";
                info.appendChild(this.ErrorInformationControl);
                info.appendChild(this.Utility.GetClearDiv());
    	    }
    	    if(this.ShowDebugInformation)
    	    {
    	        this.DebugInformationControl = document.createElement("div");
    	        this.DebugInformationControl.className = "debugMsg";
    	        info.appendChild(this.DebugInformationControl);
                info.appendChild(this.Utility.GetClearDiv())
    	    }
    	        	    
    	    top.appendChild(this.TopRight);
    	    this.Body.appendChild(bodyRight);
    	    bodyRight.appendChild(info);
    	    bodyRight.appendChild(this.IntellisenseContentBody); // <-- BODY GOES IN HERE!!
    	    bodyRight.appendChild(this.Utility.GetClearDiv());      
    	    
    	    footer.appendChild(this.FooterRight);
    	    
    	    //SLIDING DOOR - END
           
            var floatContainer = document.createElement("div");
            floatContainer.className = "floatcontainer";
    	    floatContainer.appendChild(top);
    	    floatContainer.appendChild(this.Body);
    	    floatContainer.appendChild(footer);    	    
    	    
    	    this.IntellisenseBox.appendChild(floatContainer);
    	    
    	    this.IntellisenseContainer.appendChild(this.IntellisenseBox);
    	    
    	    //document.body.appendChild(this.IntellisenseContainer);
    	    this.TextBox.parentNode.appendChild(this.IntellisenseContainer);
        }
        
        //Categories
        this.CreateIntellisenseBody();
        for(var i=0; i < this.Categories.length; i++)
        {
            var current = this.Categories[i];
            current.Render(this.IntellisenseContentBody);
        }
    },
    CreateIntellisenseBody : function()
    {
        this.ClearCategories();
        if(this.ResultFromServer != null)
        {   
            for(var i=0; i < this.ResultFromServer.length; i++)
            {   
                var current = this.ResultFromServer[i];
                if(current != null)
                {
                    var category = new Category(current.Name, current.ImageUrl, current.VisAlleLink, current.Items);
                    this.Categories.push(category);
                    /*
                    var existingCategory = this.TryGetExistingCategory(current.Name);
                    if(existingCategory != null)
                    {
                        var category = new Category(current.Name, current.ImageUrl, current.Items);
                        this.Categories.push(category);
                    }
                    else//If category exists update it
                    {
                        existingCategory.UpdateList(current.Items);
                    }
                    */
                }
            }
        }
    },
    ClearCategories : function()
    {
        for(var i=0; i < this.Categories.length; i++)
        {
            this.Categories[i].Clear2();
        }
        this.Categories.clear();
    },
    TryGetExistingCategory : function(name)
    {
        for(var i=0; i < this.Categories.length; i++)
        {
            if(this.Categories[i].Name == name)
            {
                return this.Categories[i];
            }
        }
        return null;
    },
    Update : function()
    {
        this.CreateIntellisenseBody();
        for(var i=0; i < this.Categories.length; i++)
        {
            var current = this.Categories[i];
            current.Render(this.IntellisenseContentBody);
        }
    },
    ShowWait : function()
    {
        this.EnsureWait();
        this.WaitScreen.style.display = "block";
    },
    HideWait : function()
    {
        if(this.WaitScreen != null)
        {
            this.WaitScreen.style.display = "none";
        }
    },
    EnsureWait : function()
    {
        if(this.WaitScreen == null)
        {
            this.WaitScreen = document.createElement("div");
            this.WaitScreen.className = "spinner";
            this.WaitScreen.style.left = this.TextBox.offsetWidth - 37 + "px";/*32=spinner image size + 5 padding*/
            
            this.TextBox.parentNode.appendChild(this.WaitScreen);
        }
    },
    Navigate : function(direction)
    {
        /*
        switch (direction)
        {
            case DirectionEnum.Left:
                break;
                this.CurrentSelectCategory.Navigate(DirectionEnum.Left);
                
            case DirectionEnum.Up:
                this.CurrentSelectCategory.Navigate(DirectionEnum.Up);
                break;
            case DirectionEnum.Right:
                this.CurrentSelectCategory.Navigate(DirectionEnum.Right);
                break;
            case DirectionEnum.Down:
            {
                if(this.CurrentSelectCategory == null)
                {
                    this.CurrentSelectCategory = this.Categories[0];
                }
            }
        }
        
        this.CurrentSelectCategory.Navigate(direction);
        */
    }
}; 



/* CATEGORY */
var Category = Class.create(); 
Category.prototype = 
{
    initialize: function(name, imageUrl, visAlleLink, items) 
    {
        this.Utility = new Utility();     
        this.Items = new Array();
        
        this.UpdateList(items);

        this.SelectedItem = null;
        this.SelectedIndex = -1;
        
        
        this.Name = name;
        this.ImageUrl = imageUrl;
        this.VisAlleLink = visAlleLink;
        
        //HTML
        this.CategoryContainer = null;
        this.CategoryHeader;
        this.CategoryImage;
    },
    Add : function(name, discription, imageurl)
    {
        var newItem = new Item(name, discription, imageurl);
        this.Items.push(newItem);
    },
    Clear : function()
    {
        for(var i=0; i < this.Items.length; i++)
        {
            var current = this.Items[i];
            current.Clear();
        }
    },
    Clear2 : function()
    {
        if($(this.CategoryContainer) != null)
        {
            Element.remove($(this.CategoryContainer));
        }
    },
    Render : function(parent)
    {
        this.EnsureHTML(parent);
 
        //Render children!
        for(var i=0; i < this.Items.length; i++)
        {
            var current = this.Items[i];
            current.Render(this.CategoryContainer);
        }  
        

        var link = document.createElement("a");
        link.innerHTML = ">> Se alle " + this.Name;
        link.href = this.VisAlleLink;
        this.CategoryContainer.appendChild(link);
    },
    EnsureHTML : function(parent)
	{
	    if(this.CategoryContainer == null)
	    {
            this.CategoryContainer = document.createElement("div");
            this.CategoryContainer.className = "seachresult_category_main "+this.Name ;
            
            this.CategoryContainer.appendChild(this.CreateCategoryHeader());	
                        
            parent.appendChild(this.CategoryContainer);
	    }
        
	},
	CreateCategoryHeader : function()
	{
        var categoryHeader = document.createElement("div");
        categoryHeader.className = "seachresult_category_header";
        
        this.CategoryHeader = document.createElement("h1");
        this.CategoryHeader.innerHTML = this.Name;
        
        categoryHeader.appendChild(this.CategoryHeader);
        
        return categoryHeader;
	},
	Remove : function()
	{
	    if($(this.CategoryContainer) != null)
	    {
	        Element.remove($(this.CategoryContainer));
	        this.CategoryContainer = null;
	    }   
	},
	UpdateList : function(list)
	{
	    this.Clear();
        for(var i=0; i < list.length; i++)
        {
            var current = list[i];
            if(current != null)
            {
                if(this.Items[i] == null)//If position is empty create new item!
                {                 
                    this.Items.push(new Item(current.Name, current.Discription, current.ImageUrl, current.NameLink, current.ImageLink, current.Children));
                }
                else
                {
                    //this.Items[i].UpdateContent(current.Name, current.Discription, current.ImageUrl);
                }
            }
        }
	},
	GetNumberOfVisible : function()
	{
	    var number = 0;
	    for(var i=0; i < this.Items.length; i++)
	    {
	        if(this.Items[i].IsVisible())
	        {
	            number++;
	        }
	    }
	    return number;
	},
    Navigate : function(direction)
    {
                

        var length = this.GetNumberOfVisible();

        switch (direction)
        {
            case DirectionEnum.Left:
                break;
            case DirectionEnum.Up:  
            {
                if(this.SelectedIndex == 0)
                {
                    this.SelectedIndex = length - 1;  
                }
                else
                {
                    this.SelectedIndex = (this.SelectedIndex-1)%length;  
                }
                break;
            }
            case DirectionEnum.Right:
                break;
            case DirectionEnum.Down:
            {
                this.SelectedIndex = (this.SelectedIndex+1)%length;
            }
        }
        
        $('intellisenseStatus').innerHTML = "Direction: "+direction + " Selected index: "+this.SelectedIndex +  " Items length: " + length;
   
        for(var i=0; i < length; i++)
        {
            this.Items[i].DeSelect();
        }
        
        if(this.SelectedIndex >= 0)
        {
            this.Items[this.SelectedIndex].Select();
        }
    }
}

/* ITEM */

var Item = Class.create(); 
Item.prototype = 
{
    initialize: function(name, discription, imageurl, nameLink, imageLink, children) 
	{
	    this.Utility = new Utility();    
	    
	    //Event handlers
        this.OnMouseOverHandler = null;
        this.OnMouseOutHandler = null; 
        
        //Hook mouse out and over            
        this.OnMouseOverHandler = this.OnMouseOver.bindAsEventListener(this);
        this.OnMouseOutHandler = this.OnMouseOut.bindAsEventListener(this);
	    
	    //Content
	    this.Name = name;
	    this.Discription = discription;
	    this.ImageUrl = imageurl;	    
	    this.NameLink = nameLink;
	    this.ImageLink = imageLink;
	    this.Children = children;
	    
	    //Css names
	    this.ClassNameDetailMode = "seachresult_detailmode";
	    this.ClassNameListMode = "seachresult_listmode";
	    	
	    //HTML
	    this.ItemContainer = null;
        //Common to detail & list mode
	    this.HeaderControl = null;
	    this.HeaderLinkControl = null;
	    //Detailmode
	    this.ImageControl = null;
	    this.ImageLinkControl = null;
	    this.DiscriptionControl = null;
        //Listmode	    
	    this.ImageListControl = null
	    
	    this.ChildrenControls = null;
	},
    Clear : function()
	{
	    //Update dom
	    this.ItemContainer = null;
        this.HeaderControl.innerHTML = null;
        this.HeaderLinkControl.href = null;	    
        this.ChildrenControls = null;
        this.DiscriptionControl = null;
        this.ImageControl.src = null;
        this.ImageControl.alt = null;
        this.ImageLinkControl.href = null;
        
        this.OnMouseOverHandler = null;
        this.OnMouseOutHandler = null; 
	},
	RemoveFromDom: function()
	{
	    if($(this.ItemContainer) != null)
	    {
	        Element.remove($(this.ItemContainer));
	        this.Clear();
	    }
	},
	Hide : function()
	{
	    this.ItemContainer.style.display = "none";
	},
	Show : function()
	{
	    this.ItemContainer.style.display = "block";
	},
	IsVisible : function()
	{
	    return (this.ItemContainer.style.display != "none");
	},
	Select : function()
	{   
	    Element.toggleClassName(this.ItemContainer, 'selected');
	},
	DeSelect : function()
	{
	    Element.toggleClassName(this.ItemContainer, 'selected');
	},
	Render : function(parent)
	{
	    this.EnsureHTML(parent);
	    this.UpdateContent(this.Name, this.Discription, this.ImageUrl, this.NameLink, this.ImageLink, this.Children);
	},
	IsInListMode : function()
	{
	    return (this.Children != null && this.Children.length > 0);
	},
	EnsureHTML : function(parent)
	{
	    if(this.ItemContainer == null)
	    {
	    	//Product container
            this.ItemContainer = document.createElement("div");

            //Event.observe($(this.ItemContainer), 'mouseover', this.OnMouseOverHandler); 
            //Event.observe($(this.ItemContainer), 'mouseout', this.OnMouseOutHandler); 
            
            if(!this.IsInListMode())
            {
                this.CreateDetailMode();
                //alert("CreateDetailMode");
            }
            else
            {
                this.CreateListMode();
                //alert("CreateListMode");
            }
            
            parent.appendChild(this.ItemContainer)
        }
        else
        {
            //Check type
            if((this.IsInListMode() && this.ItemContainer.className != this.ClassNameListMode) || 
                (!this.IsInListMode() && this.ItemContainer.className != this.ClassNameDetailMode))
            {
                //alert("Changing mode ->");
                this.RemoveFromDom();//Remove old 
                this.Render(parent); //Create new
            }
        }
	},
	CreateListMode : function()
	{
	    //TODO: Mebbe split into 2 classes instead of 1 class supporting 2 modes.
	    
    	this.ItemContainer.className = this.ClassNameListMode;
    	    
	    //Product title
        this.HeaderControl = document.createElement("h2");
        //Product title anchor 
        this.HeaderLinkControl = document.createElement("a");
        
        //Div that is gonna contain all the images
        this.ImageListControl = document.createElement("div");
        
        this.ChildrenControls = new Object();
        for(var i=0; i < this.Children.length; i++)
        {
            var current = this.Children[i];
            if(current != null)
            {   
                var imageLink = document.createElement("a");
                //imageLink.href = this.Children[i].ImageLink;
                
                var image = new Image();
                //image.src = this.Children[i].ImageUrl;
                
                imageLink.appendChild(image);
                this.ImageListControl.appendChild(imageLink);
                
                //This object holds reference to the image element on each of the child elements
                var element = new Object();
                element.ImageControl = image;
                element.ImageLinkControl = imageLink;
                
                this.ChildrenControls[current.NameLink] = element;
            }
        }
        
        this.HeaderLinkControl.appendChild(this.HeaderControl)             
        
        this.ItemContainer.appendChild(this.HeaderLinkControl);
        this.ItemContainer.appendChild(this.ImageListControl);
	},
	CreateDetailMode : function()
	{   
    	this.ItemContainer.className = this.ClassNameDetailMode;
    	    
	    //Product title
        this.HeaderControl = document.createElement("h2");
        //Product title anchor 
        this.HeaderLinkControl = document.createElement("a");
                        
        //Product image
        this.ImageControl = new Image();
        //Product image anchor
        this.ImageLinkControl = document.createElement("a");   	 	        
    	
        //Description
        this.DiscriptionControl = document.createElement("span");

        //Append to dom
        this.HeaderLinkControl.appendChild(this.HeaderControl)             
        this.ImageLinkControl.appendChild(this.ImageControl);
        
        this.ItemContainer.appendChild(this.HeaderLinkControl); 
        this.ItemContainer.appendChild(this.ImageLinkControl);
        this.ItemContainer.appendChild(this.DiscriptionControl);
	},
	UpdateContent : function(name, discription, imageurl, nameLink, imageLink, children)
	{
	    //Update content
	    this.Name = name;
        this.Discription = discription;
	    this.ImageUrl = imageurl;    	    
	    this.NameLink = nameLink;
	    this.ImageLink = imageLink;
	    this.Children = children;
	    
	    //Update dom
        this.HeaderControl.innerHTML = this.Name;
        this.HeaderLinkControl.href = this.NameLink;	    
        
	    if(this.IsInListMode()) //list mode
	    {   
	        for(var i=0; i < this.Children.length; i++)
	        {   
	            var current = this.Children[i];
	            if(current != null && this.ChildrenControls[current.NameLink] != null)
	            { 
	                this.ChildrenControls[current.NameLink].ImageControl.src = current.ImageUrl;
	                this.ChildrenControls[current.NameLink].ImageControl.alt = current.Name;
	                this.ChildrenControls[current.NameLink].ImageLinkControl.href = current.ImageLink;
	            }
	        }
	    }
	    else //detail mode 
	    {   
            //this.DiscriptionControl.innerHTML = this.Discription;
            this.ImageControl.src = this.ImageUrl;
            this.ImageControl.alt = this.Name;
            this.ImageLinkControl.href = this.ImageLink;
	    }
	    this.Show();
	},
	OnMouseOver : function(eventArg)
	{
	    this.Select();
	},
	OnMouseOut : function(eventArg)
	{   
	    this.DeSelect();    
	}
}


var Utility = Class.create(); 
Utility.prototype = 
{  
    initialize: function() 
	{
	},
	GetClearDiv : function()
	{   
        var clear = document.createElement("div");
        clear.style.float = "none";
        clear.style.clear = "both";
        return clear;
	},
	IsNumberKey : function(charCode)
	{
	    if(charCode > 48 && 57 > charCode)
	    {
	        return true;
	    }
	    return false;
	},
	IsLetterKey : function(charCode)
	{
	    if((charCode >= 65 && 90 >= charCode) ||  (charCode >= 97 && 122 >= charCode) ||  
            charCode == 192 || charCode == 221 || charCode == 222)
	    {
	        return true;
	    }
	    return false;
	},
	IsAllowedSpecialKey : function(charCode)
	{
	    //8 = backspace
	    return (charCode == 8);
	},
	IsArrowKey: function(charCode)
	{
        return (charCode <= 40 && charCode >= 37);
	},
	GetArrowDirection : function(charCode)
	{
	    if(this.IsArrowKey(charCode))
	    {
            switch (charCode)
            {
                case 37:
                    return DirectionEnum.Left;
                case 38:
                    return DirectionEnum.Up;
                case 39:
                    return DirectionEnum.Right;
                case 40:
                    return DirectionEnum.Down;
            }	    
	    }
        return null;
	}
}


