if (typeof(window.RadControlsNamespace) == "undefined")
{
	window.RadControlsNamespace = new Object();
};

RadControlsNamespace.AppendStyleSheet = function(callback, clientID, pathToCssFile)
{
	if (!pathToCssFile) 
	{ 
		return; 
	}

	if (!callback)
	{
		document.write("<" + "link" + " rel='stylesheet' type='text/css' href='" + pathToCssFile + "' />");
	}
	
	else
	{
		var linkObject = document.createElement("LINK");
		linkObject.rel = "stylesheet";
		linkObject.type = "text/css";
		linkObject.href = pathToCssFile;
		document.getElementById(clientID + "StyleSheetHolder").appendChild(linkObject);
	}
};

function RadComboItem()
{
	this.ComboBox = null;
	this.ClientID = null;
	this.Highlighted = false;
	this.Index = 0;
	this.Enabled = 1;
	this.Selected = 0;	
	this.Text = "";
	this.Value = null;
	this.Attributes = new Array();	
};
		
RadComboItem.prototype.Initialize = function(itemData)
{
	for (var property in itemData)
    {
        this[property] = itemData[property];
    } 
};

RadComboItem.prototype.AdjustDownScroll = function()
{
	var verticalPadding = 0;
	var dropDown = document.getElementById(this.ComboBox.ClientID + "_DropDown");
	
	if (dropDown.offsetWidth != dropDown.scrollWidth + 16)
	{
		verticalPadding = 16;
	}	
	
	if (this.ComboBox.Items.length > 0)
	{
		var totalHeight = 0;
		for (var i=0; i<=this.Index; i++)
		{
			var currentItemElement = document.getElementById(this.ComboBox.Items[i].ClientID);
			if (currentItemElement)
			{
				totalHeight += currentItemElement.offsetHeight;
			}
		}
		dropDown.scrollTop = totalHeight - dropDown.offsetHeight + verticalPadding;	
	}
};

RadComboItem.prototype.AdjustUpScroll = function()
{
	if (this.ComboBox.Items.length > 0)
	{
		var totalHeight = 0;
		for (var i=0; i<this.Index; i++)
		{
			var currentItemElement = document.getElementById(this.ComboBox.Items[i].ClientID);
			if (currentItemElement)
			{
				totalHeight += currentItemElement.offsetHeight;
			}
		}
		var scrollTop = document.getElementById(this.ComboBox.ClientID + "_DropDown").scrollTop;
		if (scrollTop > totalHeight)
		{
			document.getElementById(this.ComboBox.ClientID + "_DropDown").scrollTop = totalHeight;
		}
	}
};

RadComboItem.prototype.Highlight = function()
{	
	if (!this.Highlighted && this.Enabled)
	{		
		this.ComboBox.UnHighlightAll();
		if (!this.ComboBox.IsTemplated || this.ComboBox.HighlightTemplatedItems)
		{		
			var element = document.getElementById(this.ClientID);
			if (element)
			{		
				if (!this.ComboBox.HighlightedItem)
				{	
					if (element.className != this.ComboBox.ItemCssClassHover)
					{
					    this.CssClass = element.className;
					}
					else
					{
					    this.CssClass = this.ComboBox.ItemCssClass;
					}					
					
				}
				element.className = this.ComboBox.ItemCssClassHover;				
			    
			}
		}
		this.Highlighted = true;
		this.ComboBox.HighlightedItem = this;
	}
};

RadComboItem.prototype.UnHighlight = function()
{	
	if (this.Highlighted && this.Enabled && document.getElementById(this.ClientID))
	{  
		document.getElementById(this.ClientID).className = this.CssClass;
		this.Highlighted = false;
		this.ComboBox.HighlightedItem = null;
	}
};

RadComboItem.prototype.Select = function()
{	
	this.ComboBox.SelectedItem = this;		
	this.ComboBox.SetState(this);
	this.ComboBox.HideDropDown();	
	this.ComboBox.PostBackActive = true;
	this.ComboBox.PostBack();	
};

function RadComboBox(comboBoxID, comboBoxClientID, showWhileLoading)
{	
	var oldCombo = window[comboBoxClientID];
	if (oldCombo != null && !oldCombo.tagName)
	{	
		oldCombo.Dispose(true);
	}
	
	if (window.tlrkComboBoxes == null)
	{
		window.tlrkComboBoxes = new Array();
	}
	
	tlrkComboBoxes[tlrkComboBoxes.length] = this;
	
	this.Items = new Array();
	this.Created = false;
	this.ID = comboBoxID;
	this.ClientID = comboBoxClientID;
	this.TagID = comboBoxClientID;	
	this.DropDownID = comboBoxClientID + "_DropDown";
	this.InputID = comboBoxClientID + "_Input";
	this.ImageID = comboBoxClientID + "_Image";
	this.DropDownPlaceholderID = comboBoxClientID + "_DropDownPlaceholder";
	this.MoreResultsBoxID = comboBoxClientID + "_MoreResultsBox";	
	this.MoreResultsBoxImageID = comboBoxClientID + "_MoreResultsBoxImage";	
	this.MoreResultsBoxMessageID = comboBoxClientID + "_MoreResultsBoxMessage";
	this.Header = comboBoxClientID + "_Header";	
	
	this.InputDomElement = document.getElementById(this.InputID);
	this.ImageDomElement = document.getElementById(this.ImageID);
	this.DropDownPlaceholderDomElement = document.getElementById(this.DropDownPlaceholderID);
	
	this.TextHidden = document.getElementById(this.ClientID + "_text");
	this.ValueHidden = document.getElementById(this.ClientID + "_value");
	this.IndexHidden = document.getElementById(this.ClientID + "_index");
	
	this.ClientWidthHidden = document.getElementById(this.ClientID + "_clientWidth");
	this.ClientHeightHidden = document.getElementById(this.ClientID + "_clientHeight");
	
	this.Enabled = true;
	this.DropDownVisible = false;	
	this.LoadOnDemandUrl = null;
	this.PollTimeOut = 0;
	this.HighlightedItem = null;
	this.SelectedItem = null;
	this.ItemRequestTimeout = 300;
	this.EnableLoadOnDemand = false;	
	this.AutoPostBack = false;
	this.ShowMoreResultsBox = false;
	this.OpenDropDownOnLoad = false;
	this.CurrentlyPolling = false;
	this.MarkFirstMatch = false;
	this.IsCaseSensitive = false;
	this.SelectOnTab = true;
	
	this.PostBackReference = null;
	this.LoadingMessage = "Loading...";		
	this.ScrollDownImage = null;
	
	this.ScrollDownImageDisabled = null;
	
	this.IFrameShim = null;
	this.RadComboBoxImagePosition = "Right";
		
	this.ItemCssClass = null;
	this.ItemCssClassHover = null;
	this.ItemCssClassDisabled = null;
	this.ImageCssClass = null;
	this.ImageCssClassHover = null;
	this.InputCssClass = null;
	this.InputCssClassHover = null;	
	this.LoadingMessageCssClass = "ComboBoxLoadingMessage";	
	this.AutoCompleteSeparator = null;
	this.ExternalCallBackPage = null;
	
	this.OnClientSelectedIndexChanging = null;	
	this.OnClientDropDownOpening = null;	
	this.OnClientDropDownClosing = null;	
	this.OnClientItemsRequesting = null;
	this.OnClientSelectedIndexChanged = null;
	this.OnClientItemsRequested = null;
	this.OnClientKeyPressing = null;
	
	this.Skin = "Classic";
	
	this.HideTimeoutID = 0;
	this.RequestTimeoutID = 0;
	this.IsDetached = false;
	this.TextPriorToCallBack = null;
	this.AllowCustomText = false;
	this.ExpandEffectString = null;
	this.HighlightTemplatedItems = false;
	this.CausesValidation = false;
	this.ClientDataString = null;
	this.ShowDropDownOnTextboxClick	= true;
	this.ShowWhileLoading = showWhileLoading;
	this.MoreResultsImageHovered = false;
	
	this.PostBackActive = false;	
	this.SelectedIndex = -1;
	this.IsTemplated = false;
	this.CurrentText = null;
	this.OffsetX = 0;
	this.OffsetY = 0;
	this.Disposed = false;
	
	var comboInstance = this;	
	this.DetermineDirection();
	this.HideOnClickHandler = function () 
	{ 
		comboInstance.HideOnClick(); 
	};	
	
	if (document.attachEvent)
	{		
		document.attachEvent("onclick",  this.HideOnClickHandler);
	}
	else
	{
		document.addEventListener("click", this.HideOnClickHandler , false);
	}	
	
	this.OnBlurHandler = function (e) 
	{ 
		comboInstance.HandleBlur(e || event); 
	};	
	
	if (document.attachEvent)
	{			
		document.getElementById(this.InputID).attachEvent("onblur", this.OnBlurHandler);
	}
	else
	{
		document.getElementById(this.InputID).addEventListener("blur", this.OnBlurHandler, false);
	}
	
	this.OnFocusHandler = function () { comboInstance.HandleFocus(); };	
	if (document.attachEvent)
	{
		document.getElementById(this.InputID).attachEvent("onfocus", this.OnFocusHandler);
	}
	else
	{
		document.getElementById(this.InputID).addEventListener("focus", this.OnFocusHandler, false);
	}
	
	document.getElementById(this.InputID).setAttribute("autocomplete", "off");
	document.getElementById(this.DropDownPlaceholderID).onselectstart = function() { return false; };	
	
	if (typeof(RadCallbackNamespace) != "undefined")
	{
		window.setTimeout(function() { comboInstance.FixUp(document.getElementById(comboInstance.InputID), true) }, 100);	
	}
	else
	{		
		var netscapeFlag = false;
		if (window.addEventListener)
		{
			if (window.opera)
				window.addEventListener("load", function () { comboInstance.FixUp(document.getElementById(comboInstance.InputID), true); }, false);
			else
				this.FixUp(document.getElementById(this.InputID), true);
		}
		else
		{	
			if (document.getElementById(this.ClientID).offsetWidth == 0)
			{
			    window.attachEvent("onload", function () { comboInstance.FixUp(document.getElementById(comboInstance.InputID), true); });
			}
			else if (!netscapeFlag)
			{	
				this.FixUp(document.getElementById(this.InputID), true);
			}
			else
			{
				this.ShowWrapperElement();
			}
		}
	}
	
	if (window.attachEvent)
	{
		window.attachEvent("onunload", function() { comboInstance.Dispose(false); });
	}
	else
	{
		window.addEventListener("unload", function() { comboInstance.Dispose(false); } , false);
	}
};

RadComboBox.prototype.ClearItems = function() 
{
    this.Items = [];
    document.getElementById(this.DropDownID).innerHTML = "";
};

RadComboBox.prototype.GetViewPortSize = function() 
{
	var width = 0;
	var height = 0;
	
	var canvas = document.body;
	
	if (window.innerWidth) 
	{
		width = window.innerWidth;
		height = window.innerHeight;
	} 
	else
	{
		if (document.compatMode && document.compatMode == "CSS1Compat")
		{
		    canvas = document.documentElement;
		}
		
		width = canvas.clientWidth;
		height = canvas.clientHeight;
	}
	width += canvas.scrollLeft;
	height += canvas.scrollTop;
	
	return { width : width - 6, height : height - 6 };
};
		
RadComboBox.prototype.GetElementPosition = function (el)
{
	var parent = null;
	var pos = {x: 0, y: 0};
	var box;

	if (el.getBoundingClientRect) 
	{ 
		// IE
		box = el.getBoundingClientRect();
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

		pos.x = box.left + scrollLeft - 2;
		pos.y = box.top + scrollTop - 2;
	    
		return pos;
	}
	else if (document.getBoxObjectFor) 
	{ 
		// gecko
		box = document.getBoxObjectFor(el);
		pos.x = box.x - 2;
		pos.y = box.y - 2;
	}
	else 
	{ 
		// safari/opera
		pos.x = el.offsetLeft;
		pos.y = el.offsetTop;
		parent = el.offsetParent;
		if (parent != el)
		{
			while (parent) 
			{
				pos.x += parent.offsetLeft;
				pos.y += parent.offsetTop;
				parent = parent.offsetParent;
			}
		}
	}

	if (window.opera)
	{
		parent = el.offsetParent;
		
		while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') 
		{
			pos.x -= parent.scrollLeft;
			pos.y -= parent.scrollTop;
			parent = parent.offsetParent;
		}
	}
	else
	{
		parent = el.parentNode; 
		while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') 
		{
			pos.x -= parent.scrollLeft;
			pos.y -= parent.scrollTop;

			parent = parent.parentNode;
		}
	}

	return pos;
};

RadComboBox.prototype.Dispose = function(disposing)
{	
	try
	{
		if (disposing)
		{
			this.HideDropDown();
			//var placeHolder = document.getElementById(this.DropDownPlaceholderID);
			//if ((placeHolder != null) && (placeHolder.parentNode != null))
			//{
			//	placeHolder.parentNode.removeChild(placeHolder);
			//}
		}

	    tlrkComboBoxes[this.ID] = null;
    	
	    this.Items = null;
    	
	    this.InputDomElement = null;//document.getElementById(this.InputID);
	    this.ImageDomElement = null;//document.getElementById(this.ImageID);
	    this.DropDownPlaceholderDomElement = null; //document.getElementById(this.DropDownPlaceholderID);
    	
	    this.TextHidden = null;// document.getElementById(this.ClientID + "_text");
	    this.ValueHidden = null; //document.getElementById(this.ClientID + "_value");
	    this.IndexHidden = null; //document.getElementById(this.ClientID + "_index");
    	
	    this.IFrameShim = null;
    	
	    this.OnClientSelectedIndexChanging = null;	
	    this.OnClientDropDownOpening = null;	
	    this.OnClientDropDownClosing = null;	
	    this.OnClientItemsRequesting = null;
	    this.OnClientSelectedIndexChanged = null;
	    this.OnClientItemsRequested = null;
	    this.OnClientKeyPressing = null;
    	
	    var comboInstance = this;	
	    if (document.detachEvent)
	    {	
		    document.detachEvent("onclick", this.HideOnClickHandler);
	    }
	    else
	    {
		    document.removeEventListener("click", this.HideOnClickHandler , false);
	    }
    	
	    if (document.detachEvent)
	    {	
		    document.getElementById(this.InputID).detachEvent("onblur", this.OnBlurHandler);
	    }
	    else
	    {
		    document.getElementById(this.InputID).removeEventListener("blur", this.OnBlurHandler , false);
	    }
    	
	    if (document.detachEvent)
	    {	
		    document.getElementById(this.InputID).detachEvent("onfocus", this.OnFocusHandler);
	    }
	    else
	    {
		    document.getElementById(this.InputID).removeEventListener("focus", this.OnFocusHandler , false);
	    }
    		
	    if (window.removeEventListener)
	    {		
		    window.removeEventListener("load", function () { comboInstance.FixUp(document.getElementById(comboInstance.InputID), true); } , false);
	    }	
    	
        var input = document.getElementById(this.InputID);
        if (input != null)
	        input.onblur = null;		
        input = null;
        
        var dropDownPlaceHolder = document.getElementById(this.DropDownPlaceholderID);
        if (dropDownPlaceHolder != null)
	    {
	        dropDownPlaceHolder.onselectstart = null;
	    }
        dropDownPlaceHolder = null;
    }
    catch (e)
    {
    }
    this.Disposed = true;
};


RadComboBox.prototype.Initialize = function (configObject, itemData)
{	
	this.LoadConfiguration(configObject);
	this.CreateItems(itemData);
	this.InitCssNames();
	this.HighlightSelectedItem();	
};

RadComboBox.prototype.LoadConfiguration = function (configObject)
{
    for (var property in configObject)
    {
        this[property] = configObject[property];
    } 
};

RadComboBox.prototype.InitCssNames = function()
{
	this.ItemCssClass =				 "ComboBoxItem_" + this.Skin;
	this.ItemCssClassHover =		 "ComboBoxItemHover_" + this.Skin;
	this.ItemCssClassDisabled =		 "ComboBoxItemDisabled_" + this.Skin;
	this.ImageCssClass =			 "ComboBoxImage_" + this.Skin;
	this.ImageCssClassHover =		 "ComboBoxImageHover_" + this.Skin;
	this.InputCssClass =			 "ComboBoxInput_" + this.Skin;
	this.InputCssClassHover =		 "ComboBoxInputHover_" + this.Skin;
	this.LoadingMessageCssClass =	 "ComboBoxLoadingMessage_" + this.Skin;
};

RadComboBox.prototype.SetState = function(item)
{	
	if (item != null)
	{		
		this.SetTextAfterLastDelimiter(item.Text);
		this.SetValue(item.Value);
		this.SetIndex(item.Index);		

	}
	else
	{
		this.SetText("");
		this.SetValue("");
		this.SetIndex("-1");
	}
};

RadComboBox.prototype.PostBack = function()
{	
	if (this.AutoPostBack)
	{
		if (this.CausesValidation)
		{
			if (typeof(WebForm_DoPostBackWithOptions) != 'function' && !(typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()))
			{
				return;
			}
		}		
		eval(this.PostBackReference);
	}
};

RadComboBox.prototype.HandleBlur = function(e)
{	
	var oldItem = this.SelectedItem;
	var newItem = this.HighlightedItem;
	if (oldItem != null && newItem != null && oldItem != newItem)
	{
		if (this.FireEvent(this.OnClientSelectedIndexChanging, newItem, e) == false)
		{       			
        	return;
		}
		this.SetState(newItem);
		this.ExecuteAction();
		if (this.HighlightedItem == null)
		{
			newItem.Select();
			this.FireEvent(this.OnClientSelectedIndexChanged, newItem, e);
		}
	}
	
	if (this.MoreResultsImageHovered)
		return;
	
	var oldText = this.CurrentText;
	var newText = this.GetText();
	if (oldText != newText && this.AllowCustomText)
	{
		this.SetText(this.GetText());
		if (!this.PostBackActive)
		{
			if (newItem != null || oldText != newText)
			{
			    if (this.FireEvent(this.OnClientSelectedIndexChanging, newItem, e) == false)
		        {       			
        			return;
		        }	        
		        
		        // Fix. Gemini ID: RCOM-7754
		        // Old: 
		        // if (newItem != null && newItem.Text != this.GetText())
		        if (newItem != null)
		        {
		            this.SetText(newItem.Text);
		            this.SetValue(newItem.Value);
		        }
		        
			    this.PostBack();
			}
		}
		else
		{
			this.PostBackActive = false;
		}		
	}
};

RadComboBox.prototype.HandleFocus = function(e)
{
	this.CurrentText = this.GetText();
	this.RaiseOnClientFocus();
}

RadComboBox.prototype.FindParentForm = function()
{
	var currentElement = document.getElementById(this.TagID);	
	while (currentElement.tagName != "FORM")
	{
		currentElement = currentElement.parentNode;
	}
	
	return currentElement;
};

RadComboBox.prototype.DropDownRequiresForm = function()
{
	var dropDownPlaceHolder = document.getElementById(this.DropDownPlaceholderID); 
	var inputTags = dropDownPlaceHolder.getElementsByTagName("input");
	
	return inputTags.length > 0;
}

RadComboBox.prototype.DetachDropDown = function()
{
	if ( (!document.readyState || document.readyState == "complete") && (!this.IsDetached))
	{
		var parentElement = document.body;
		
		if (this.DropDownRequiresForm())
		{
			parentElement = this.FindParentForm();
		}
		
		//var dropDownPlaceHolder = document.getElementById(this.DropDownPlaceholderID); 
		var dropDownPlaceHolder = document.getElementById(this.TagID).getElementsByTagName("div")[0]; 
		dropDownPlaceHolder.parentNode.removeChild(dropDownPlaceHolder);
		dropDownPlaceHolder.style.marginLeft = "0";
		
		var oldDropDown = document.getElementById(this.DropDownPlaceholderID);
		if (oldDropDown)
		{
			//Korchev: r.a.d.callback
			oldDropDown.parentNode.removeChild(oldDropDown);
		}
		
		if (parentElement.firstChild)
		{
		    parentElement.insertBefore(dropDownPlaceHolder, parentElement.firstChild);
		}
		else
		{
		    parentElement.appendChild(dropDownPlaceHolder);
		}
		
		this.IsDetached = true;
		
		this.DropDownPlaceholderDomElement = document.getElementById(this.DropDownPlaceholderID);
	}
};



RadComboBox.prototype.HideOnClick = function()
{
	var comboInstance = this;	
	this.HideTimeoutID = window.setTimeout(function () { comboInstance.DoHideOnClick(); }, 5);
};

RadComboBox.prototype.DoHideOnClick = function()
{
	if (this.HideTimeoutID)
	{
		this.HideDropDown();
	}
};

RadComboBox.prototype.ClearHideTimeout = function()
{
	this.HideTimeoutID = 0;
};

RadComboBox.prototype.GetLastSeparatorIndex = function(text)
{	
	var lastIndex = -1;	
	for (var i=0; i<this.AutoCompleteSeparator.length;i++)
	{
		var currentSeparator = this.AutoCompleteSeparator.charAt(i);		
		var currentIndex = text.lastIndexOf(currentSeparator);
		if (currentIndex > lastIndex)
		{			
			lastIndex = currentIndex;
		}
	}	
	return lastIndex;
};

RadComboBox.prototype.SetTextAfterLastDelimiter = function(theText)
{
	var lastSeparatorIndex = -1;
	var currentText = this.GetText();
	if (this.AutoCompleteSeparator != null)
	{		 
		 lastSeparatorIndex = this.GetLastSeparatorIndex(currentText);
	}
	var newText = currentText.substring(0, lastSeparatorIndex + 1) + theText;
		
	this.SetText(newText);
};

RadComboBox.prototype.ClearSelection = function()
{
	this.SetState(null);
	this.SelectedItem = null;
	this.HighLightedItem = null;
};

RadComboBox.prototype.CreateItems = function(itemData)
{	
	for (var i=0; i<itemData.length; i++)
	{
		var item = new RadComboItem();
		item.ComboBox = this;		
		item.Index = this.Items.length;		
		item.Initialize(itemData[i]);
		
		this.Items[this.Items.length] = item;		
		
		if (item.Selected && !this.AllowCustomText)
		{		    
		    this.SetText(item.Text);
		    this.SetValue(item.Value);
		}
	}	
};

RadComboBox.prototype.HighlightSelectedItem = function()
{	
	if (this.SelectedItem != null)
	{		
		this.SelectedItem.Highlight();
	}
	else
	{		
	    var currentItem;
	    	
		var currentValue = this.GetValue();
		currentItem = this.FindItemByValue(currentValue);
		
		if (currentItem == null)
		{
		    var currentText = this.GetText();
		    currentItem = this.FindItemByText(currentText);
		}
		
		if (currentItem != null)
		{
			this.SelectedItem = currentItem;
			this.SelectedItem.Highlight();
		}
	}
	this.Created = true;	
	
	if (this.SelectedItem == null && this.SelectedIndex == -1 && this.Items.length > 0)
	{
		this.SelectedItem = this.Items[0];
		this.Items[0].Selected = true;	
		this.SelectedItem.Highlight();	
	}
	
	var comboInstance = this;
	if (this.OpenDropDownOnLoad)
	{
		if (window.attachEvent)
		{
			window.attachEvent("onload", function () { comboInstance.ShowDropDown(); } );
		}
		else
		{
			window.addEventListener("load", function () { comboInstance.ShowDropDown(); }, false );
		}
	}
};

RadComboBox.prototype.InitializeAfterCallBack = function(itemData, more)
{	
	if (!more)
	{
		this.Items.length = 0;
	}
	this.HighlightedItem = null;
	this.SelectedItem = null;
	this.Created = false;
	
	if (this.Items.length > 0)
	{
		if (this.Items[0].Text == document.getElementById(this.InputID).value)
		{
			this.SetValue(this.Items[0].Value);			
		}
		else
		{
			this.SetValue("");			
		}		
		this.TextPriorToCallBack = this.GetText();		
	}
	
	this.CreateItems(itemData);	
};

RadComboBox.prototype.SetText = function(theText)
{	
	document.getElementById(this.InputID).value = theText;	
	this.TextHidden.value = theText;
};

RadComboBox.prototype.GetText = function()
{
	return document.getElementById(this.InputID).value;
};

RadComboBox.prototype.SetValue = function(value)
{
	if (value || value == "")
	{
		this.ValueHidden.value = value;
	}
};

RadComboBox.prototype.GetValue = function()
{
	return this.ValueHidden.value;
};

RadComboBox.prototype.SetIndex = function(index)
{
	this.IndexHidden.value = index;
};

RadComboBox.prototype.getXY = function(el) 
{  
    var parent = null;
    var pos = [];
    var box;

    if (el.getBoundingClientRect) 
    { 
		// IE
        box = el.getBoundingClientRect();
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

        var x = box.left + scrollLeft - 2;
        var y = box.top + scrollTop - 2;
        
        return [x, y];
    }
    else if (document.getBoxObjectFor) 
    { 
		// gecko
        box = document.getBoxObjectFor(el);
        pos = [box.x - 1, box.y - 1];
    }
    else 
    { 
		// safari/opera
        pos = [el.offsetLeft, el.offsetTop];
        parent = el.offsetParent;
        if (parent != el)
        {
			while (parent) 
			{
				pos[0] += parent.offsetLeft;
				pos[1] += parent.offsetTop;
				parent = parent.offsetParent;
			}
        }
    }


	if (window.opera)
	{
		parent = el.offsetParent;
		
		while (parent && parent.tagName.toUpperCase() != 'BODY' && parent.tagName.toUpperCase() != 'HTML') 
		{
			pos[0] -= parent.scrollLeft;
			pos[1] -= parent.scrollTop;
			parent = parent.offsetParent;
		}
	}
	else
	{
		parent = el.parentNode; 
		while (parent && parent.tagName.toUpperCase() != 'BODY' && parent.tagName.toUpperCase() != 'HTML') 
		{
			
			pos[0] -= parent.scrollLeft;
			pos[1] -= parent.scrollTop;

			parent = parent.parentNode;
		}
    }

    return pos;
};

RadComboBox.prototype.ShowOverlay = function(x, y)
{
	if (document.readyState && document.readyState != "complete")
	{
		return;
	}
	
	var isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);
	var isOpera = window.opera;
	if (isSafari || isOpera || (!document.all))
	{
		return;
	}
	
	if (this.IFrameShim == null)
	{
		this.IFrameShim = document.createElement("IFRAME");
		this.IFrameShim.src="javascript:''";
		this.IFrameShim.id = this.ClientID + "_Overlay";
		this.IFrameShim.frameBorder = 0;
		this.IFrameShim.style.position = "absolute";
		this.IFrameShim.style.display = "none";
		this.DetachDropDown();
		this.DropDownPlaceholderDomElement.parentNode.insertBefore(this.IFrameShim, this.DropDownPlaceholderDomElement);
		this.IFrameShim.style.zIndex = this.DropDownPlaceholderDomElement.style.zIndex - 1;
	}
	
	this.IFrameShim.style.left = x;
	this.IFrameShim.style.top = y;
	var overlayWidth = this.DropDownPlaceholderDomElement.offsetWidth;
	var overlayHeight = this.DropDownPlaceholderDomElement.offsetHeight;	
	
	this.IFrameShim.style.width = overlayWidth + "px";
	this.IFrameShim.style.height = overlayHeight + "px";    
	this.IFrameShim.style.display = "";   
};

RadComboBox.prototype.HideOverlay = function()
{
	var isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);
	var isOpera = window.opera;
	if (isSafari || isOpera || (!document.all))
	{
		return;
	}
	
	if (this.IFrameShim != null)
	{
		this.IFrameShim.style.display = "none";
	}
};

RadComboBox.prototype.HideAllDropDowns = function()
{		
	for (var i=0; i<tlrkComboBoxes.length; i++)
	{
	    if (tlrkComboBoxes[i].ClientID != this.ClientID)
		{		
			tlrkComboBoxes[i].HideDropDown();
		}
	}	
};

RadComboBox.prototype.DetermineDirection = function ()
{
    var el = document.getElementById(this.ClientID + "_wrapper");
    
    while (el.tagName.toLowerCase() != 'html')
    {
        if (el.dir)
        {
            this.RightToLeft = (el.dir.toLowerCase() == "rtl");
            return;
        }
        el = el.parentNode;
    }
    
    this.RightToLeft = false;
}

RadComboBox.prototype.ShowDropDown = function()
{	
	if (this.FireEvent(this.OnClientDropDownOpening, this) == false)
	{
		return;
	}
	
	this.HideAllDropDowns();	
	this.DetachDropDown();		
	
	var startingElement;	
	
	(this.RadComboBoxImagePosition == "Right" && !this.RightToLeft) ? startingElement = this.InputDomElement : startingElement = document.getElementById(this.ImageID);	
	
	var position = this.getXY(startingElement);
	var x = position[0]+ this.OffsetX;
	var y = position[1] + startingElement.offsetHeight + this.OffsetY; 		
	
	var placeholder = document.getElementById(this.TagID);
	
	dropDownWidth = placeholder.offsetWidth;
	
	if (this.ExpandEffectString != null && document.all)
	{		
		try
		{
		    this.DropDownPlaceholderDomElement.style.filter = this.ExpandEffectString;		
		    this.DropDownPlaceholderDomElement.filters[0].Apply();
		    this.DropDownPlaceholderDomElement.filters[0].Play();
		}
		catch(e)
		{
		}
	}	
	if (this.RightToLeft)
	{
	    this.DropDownPlaceholderDomElement.dir = "rtl";
	}
	
	var screenSize = this.GetViewPortSize();		
	
	this.DropDownPlaceholderDomElement.style.position = "absolute";	
	
	if (window.netscape || window.opera)
	{
	    dropDownWidth-= 2;
	}
	
	this.DropDownPlaceholderDomElement.style.width = dropDownWidth  + "px";
	this.DropDownPlaceholderDomElement.style.display = "block";	
	
	if (this.ElementOverflowsBottom(screenSize, this.DropDownPlaceholderDomElement, startingElement))
	{	    
	    var revertedY = y - this.DropDownPlaceholderDomElement.offsetHeight - startingElement.offsetHeight;
	    if (revertedY > 0)
	    {
	        y = revertedY;
	    }
	}
	
	this.DropDownPlaceholderDomElement.style.left = x + "px";	
	this.DropDownPlaceholderDomElement.style.top = y + "px";

	this.ShowOverlay(x + "px", y + "px");
		
	if (this.HighlightedItem != null)
	{
		this.HighlightedItem.AdjustDownScroll();
	}
	if (this.SelectedItem != null)
	{
		this.SelectedItem.AdjustDownScroll();
	}
	
	this.ClearHideTimeout(); 
	this.DropDownVisible = true;
	
	try
	{
		document.getElementById(this.InputID).focus();
	}
	catch (e) {};
	
	if ((this.EnableLoadOnDemand) && (this.Items.length == 0))
	{		
		this.PollServerInterMediate(true, null);
	}
	
	if (this.SelectedItem != null)
	{
		this.SelectedItem.Highlighted = false;
		this.SelectedItem.Highlight();	
		this.SelectedItem.AdjustUpScroll();		
	}
};

RadComboBox.prototype.ElementOverflowsBottom = function (screenSize, element, startingElement)
{
	var bottomEdge = this.GetElementPosition(startingElement).y + element.offsetHeight;		
	return bottomEdge > screenSize.height;
};

RadComboBox.prototype.FindItemByText = function(theText)
{
	for (var i=0; i<this.Items.length; i++)
	{
		if (this.Items[i].Text == theText)
		{
			return this.Items[i];
		}
	}
	
	return null;
};

RadComboBox.prototype.FindItemByValue = function(theValue)
{
	for (var i=0; i<this.Items.length; i++)
	{
		if (this.Items[i].Value == theValue)
		{
			return this.Items[i];
		}
	}
	
	return null;
};

RadComboBox.prototype.HideDropDown = function()
{	
	if (this.DropDownVisible)
	{
		if (this.FireEvent(this.OnClientDropDownClosing, this) == false)
		{
			return;
		}
		
		document.getElementById(this.DropDownPlaceholderID).style.display = "none";		
		this.HideOverlay();	
		
		this.DropDownVisible = false;
		this.RaiseOnClientBlur();
	}
};

/* Petyo. */
RadComboBox.prototype.RaiseOnClientBlur = function ()
{
	this.FireEvent(this.OnClientBlur, this)
}

RadComboBox.prototype.RaiseOnClientFocus = function ()
{
	this.FireEvent(this.OnClientFocus, this)
}


/* */


RadComboBox.prototype.ToggleDropDown = function()
{
	(this.DropDownVisible) ? this.HideDropDown() : this.ShowDropDown();
};

RadComboBox.prototype.HtmlElementToItem = function(obj)
{
	if (obj)
	{
		while (obj != null)
		{			
			if (obj.id && this.IsElementAnItem(obj.id))
			{
				return obj;
			}
			obj = obj.parentNode;
		}
	}
	
	return null;
};

RadComboBox.prototype.IsElementAnItem = function(itemID)
{
	for (var i=0; i<this.Items.length; i++)
	{
		if (this.Items[i].ClientID == itemID)
		{
			return true;
		}
	}
	
	return false;
};

RadComboBox.prototype.ItemToInstance = function(item)
{
	for (var i=0; i<this.Items.length; i++)
	{
		if (this.Items[i].ClientID == item.id)
		{
			return this.Items[i];
		}
	}
	
	return null;
};

RadComboBox.prototype.HandleMouseOver = function(itemInstance)
{
	itemInstance.Highlight();	
};

RadComboBox.prototype.HandleMouseOut = function(itemInstance)
{
	itemInstance.UnHighlight();
};

RadComboBox.prototype.ExecuteAction = function(eventArgs)
{	
	var selectedItem = this.HighlightedItem;
	if (selectedItem != null)
	{	
		if (this.FireEvent(this.OnClientSelectedIndexChanging, selectedItem, eventArgs) == false)
		{		
			return;
		}
		selectedItem.Select();
		this.FireEvent(this.OnClientSelectedIndexChanged, selectedItem, eventArgs);	
	}	
	
	this.HideDropDown();	
};

RadComboBox.prototype.HandleClick = function(eventArgs)
{	
	this.ExecuteAction(eventArgs);
};

RadComboBox.prototype.FindNextAvailableItem = function(index)
{
	var i = index;
	var flag = false;
	while (i < this.Items.length-1)
	{
		i = i + 1;
		if (this.Items[i].Enabled)
		{
			flag = true;
			break;			
		}
	}
	
	if (flag) return i;
	return index;
};

RadComboBox.prototype.FindPrevAvailableItem = function(index)
{
	var i = index;
	var flag = false;
	while (i > 0)
	{
		i = i - 1;
		if (this.Items[i].Enabled)
		{
			flag = true;
			break;			
		}
	}
	
	if (flag) return i;
	return index;
};

RadComboBox.prototype.HandleKeyPress = function(comboInstance, eventArgs)
{
	this.FireEvent(this.OnClientKeyPressing, this, eventArgs);	
	var keyCode = eventArgs.keyCode;	
	
	// Fix. Gemini ID: RCOM-7760
	// Note: We can create a boolean property indicating whether the Delete and Backspace keys are enabled or not.
	/*	
	if (keyCode == 46 && !this.EnableLoadOnDemand && !this.AllowCustomText)
	{	
		this.PreventDefault(eventArgs);		
	}
	*/

	if (keyCode == 40)
	{		
		if (eventArgs.altKey && (!this.DropDownVisible))
		{
			this.ShowDropDown();
			return;
		}
				
		var index = 0;
		if (this.HighlightedItem != null)
		{
			index = this.FindNextAvailableItem(this.HighlightedItem.Index);
		}
		
		if (index >= 0 && this.Items.length > 0)
		{
			if (this.FireEvent(this.OnClientSelectedIndexChanging, this.Items[index], eventArgs) == false)
			{				
				return;
			}
			this.Items[index].Highlight();
			this.Items[index].AdjustDownScroll();			
			this.SetState(this.Items[index]);
			this.PreventDefault(eventArgs);
		}
		
		return;
	}
	
	if (keyCode == 27 && this.DropDownVisible)
	{
		this.HideDropDown();
		return;
	}
	
	if (keyCode == 38)
	{
		if (eventArgs.altKey && this.DropDownVisible)
		{
			this.HideDropDown();
			return;
		}
		
		var index = -1;
		if (this.HighlightedItem != null)
		{
			index = this.FindPrevAvailableItem(this.HighlightedItem.Index);			
		}
		
		if (index >= 0)
		{
			if (this.FireEvent(this.OnClientSelectedIndexChanging, this.Items[index], eventArgs) == false)
			{
				return;
			}
			
			this.Items[index].AdjustUpScroll();
			this.Items[index].Highlight();			
			this.SetState(this.Items[index]);			
			this.PreventDefault(eventArgs);			
		}
		return;
	}
	
	if ( (keyCode == 13 || keyCode == 9) && this.DropDownVisible)
	{	
		if (keyCode == 13)
		{
			this.PreventDefault(eventArgs);
		}
		if (!this.SelectOnTab && keyCode == 9)
		{
		    if (this.AutoPostBack)
		    {		        
		        this.PostBack();
		    }
		    this.HideDropDown();
		    return;
		}
		
		this.ExecuteAction();		
		return;	
	}	
	
	if (keyCode == 9 && !this.DropDownVisible)
	{
		this.RaiseOnClientBlur();
		return;
	}
	
	if (keyCode == 35 || keyCode == 36 || keyCode == 37 || keyCode == 39)
	{
		return;
	}
	
	if (this.EnableLoadOnDemand && (!eventArgs.altKey) && (!eventArgs.ctrlKey) && (!(keyCode == 16)))
	{
		if (!this.DropDownVisible)
		{
			this.ShowDropDown();
		}
		this.PollServer(false,keyCode);		
		return;
	}	
	
	// Fix. Gemini ID: RCOM-7760
	// Old: 
	// if ( (keyCode < 32 && keyCode != 8) || (keyCode >= 33 && keyCode <= 46) || (keyCode >= 112 && keyCode <= 123) || (eventArgs.altKey == true) ) 
	if ((keyCode < 32) || (keyCode >= 33 && keyCode <= 46) || (keyCode >= 112 && keyCode <= 123) || (eventArgs.altKey == true)) 
	{		
		return;	
	}
	
	var instance = this;	
	window.setTimeout( function() { instance.HighlightMatches() }, 20 );	                        
};

RadComboBox.prototype.HandleKeyDown = function(eventArgs)
{
	if (eventArgs.preventDefault)
	{		
		if (eventArgs.keyCode == 13 || (eventArgs.keyCode == 32 && (!this.EnableLoadOnDemand)) )
		{			
			eventArgs.preventDefault();
		}
	}
};

RadComboBox.prototype.EncodeURI = function(s)
{	
	if(typeof(encodeURIComponent)!="undefined")
	{
		return encodeURIComponent(this.EscapeQuotes(s));
	}
	if (escape)
	{
		return escape(this.EscapeQuotes(s));
	}	
};


RadComboBox.prototype.EscapeQuotes = function(text)
{	
	if (typeof(text) != "number")
	{
		return text.replace(/'/g,"&squote");
	}
};

RadComboBox.prototype.GetXmlHttpRequest = function()
{    
	if (typeof(XMLHttpRequest) != "undefined")
	{
		return new XMLHttpRequest();		
	}
	if (typeof(ActiveXObject) != "undefined")
	{
		return new ActiveXObject("Microsoft.XMLHTTP");		
	}
};

RadComboBox.prototype.GetAjaxUrl = function(requestText, comboText, comboValue, appendItems)
{	
	requestText = requestText.replace(/'/g,"&squote");
	        
    var url = window.unescape(this.LoadOnDemandUrl) + "&text=" + this.EncodeURI(requestText);		

	url = url + "&comboText=" + this.EncodeURI(comboText);	
	url = url + "&comboValue=" + this.EncodeURI(comboValue);
	url = url + "&skin=" + this.EncodeURI(this.Skin);		
	
	if (appendItems)
	{
	    url = url + "&itemCount=" + this.Items.length;
	}
	if (this.ExternalCallBackPage != null)
	{
		url = url + "&external=true";
	}
	if (this.ClientDataString != null)
	{
		url += "&clientDataString=" + this.EncodeURI(this.ClientDataString);
	}
	url = url + "&timeStamp=" + encodeURIComponent((new Date()).getTime());   
	
	return url;
};

RadComboBox.prototype.FetchCallBackData = function(appendItems, text, keyCode)
{	
	if (!this.CurrentlyPolling)
	{
		if (this.Disposed)
			return;
			
		this.CurrentlyPolling = true;
		
		var comboText = this.GetText();
		var comboValue = this.GetValue();
		var requestText = (text) ? text : comboText;
		var ajaxUrl = this.GetAjaxUrl(requestText, comboText, comboValue, appendItems);					
		
		var comboboxInstance = this;
		var xmlRequest = this.GetXmlHttpRequest();	
		xmlRequest.onreadystatechange = function()
		{		
			if (xmlRequest.readyState != 4) return;	
			comboboxInstance.OnCallBackResponse(xmlRequest.responseText, appendItems, requestText, keyCode, xmlRequest.status, ajaxUrl);
		};		
		
		xmlRequest.open("GET", ajaxUrl, true);	
		xmlRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8")
		xmlRequest.send("");
	}
};

RadComboBox.prototype.OnCallBackResponse = function(responseData, appendItems, requestText, keyCode, status, url)
{
	if (status == 500)
	{
		alert("RadComboBox: Server error in the ItemsRequested event handler, press ok to view the result.");
		document.body.innerHTML = responseData;
		return;
	}	
	if (status == 404)
	{
		alert("RadComboBox: Load On Demand Page not found: " + url);
		var errorMessage = "RadComboBox: Load On Demand Page not found: " + url + "<br/>";
		errorMessage += "Please, try using ExternalCallBackPage to map to the exact location of the callbackpage you are using."
		document.body.innerHTML = errorMessage;
		return;
	}
	
	try
	{
	    eval("var callBackData = " + responseData + ";");	
	}
	catch(e)
	{
	    alert("RadComboBox: load on demand callback error. Press Enter for more information");
		var errorMessage = "If RadComboBox is not initially visible on your ASPX page, you may need to use streamers (the ExternallCallBackPage property)";
		errorMessage += "<br/>Please, read our online documentation on this problem for details";
		errorMessage += "<br/><a href='http://www.telerik.com/help/radcombobox/v2%5FNET2/?combo_externalcallbackpage.html'>http://www.telerik.com/help/radcombobox/v2%5FNET2/combo_externalcallbackpage.html</a>"
		document.body.innerHTML = errorMessage;
		return;
	}
	
	if (this.GetText() != callBackData.Text)
	{   
	    this.CurrentlyPolling = false;
	    this.PollServer(false, null);
	    return;
	}
	
	if (this.ShowMoreResultsBox) 
	{		
		document.getElementById(this.MoreResultsBoxMessageID).innerHTML = callBackData.Message;
	}
	
	var oldLength = this.Items.length;	
	this.InitializeAfterCallBack(callBackData.Items, appendItems);
	
	if (appendItems)
	{				
		document.getElementById(this.DropDownID).removeChild(document.getElementById(this.ClientID + "_LoadingDiv"));
		document.getElementById(this.DropDownID).innerHTML += callBackData.DropDownHtml;
		if (this.Items[oldLength+1] != null)
		{				
			this.Items[oldLength+1].AdjustDownScroll();
		}				
	}
	else
	{
		document.getElementById(this.DropDownID).innerHTML = callBackData.DropDownHtml;
	}
		
	this.ShowOverlay(this.DropDownPlaceholderDomElement.style.left, this.DropDownPlaceholderDomElement.style.top);
	
	this.FireEvent(this.OnClientItemsRequested, this, requestText, appendItems);
	this.CurrentlyPolling = false;
	
	var textItem = this.FindItemByText(this.GetText());			
	if (textItem != null)
	{
		textItem.Highlight();
		textItem.AdjustDownScroll();
	}
	
	if (!keyCode) return;	
	
	if (keyCode < 32 || (keyCode >= 33 && keyCode <= 46) || (keyCode >= 112 && keyCode <= 123) && keyCode != 8) 
	{
		return;	
	}	
	
	this.HighlightMatches();
}

RadComboBox.prototype.GetLastWord = function(currentText)
{
	var lastSeparatorIndex = -1;
	if (this.AutoCompleteSeparator != null)
	{
		lastSeparatorIndex = this.GetLastSeparatorIndex(currentText);
	}		
	var lastWord = currentText.substring(lastSeparatorIndex+1, currentText.length);	
	return lastWord;
};

RadComboBox.prototype.CompareWords = function(wordOne, wordTwo)
{
	if (!this.IsCaseSensitive)
	{
		return (wordOne.toLowerCase() == wordTwo.toLowerCase());	
	}
	else
	{
		return (wordOne == wordTwo);
	}
};

RadComboBox.prototype.HighlightMatches = function()
{	
	if (!this.MarkFirstMatch) return;
	
	var currentText = this.GetText();
	var lastWord = this.GetLastWord(currentText);
	if (lastWord.length == 0)
	{
		return;
	}	
	
	for (var i=0; i<this.Items.length; i++)
	{
		var itemText = this.Items[i].Text;
		if (itemText.length >= lastWord.length)
		{			
			var itemSubText = itemText.substring(0, lastWord.length);
			if (this.CompareWords(itemSubText, lastWord))
			{				
				var lastSeparatorIndex = -1;
				if (this.AutoCompleteSeparator != null)
				{
					 lastSeparatorIndex = this.GetLastSeparatorIndex(currentText);
				}
					
				var newText = currentText.substring(0, lastSeparatorIndex+1) + itemText;
				this.SetText(newText);
				this.SetValue(this.Items[i].Value);
				this.SetIndex(this.Items[i].Index);
				
				if (this.FireEvent(this.OnClientSelectedIndexChanging, this.Items[i], null) == false)
				{					
					return;
				}
				
				this.Items[i].Highlight();
				this.Items[i].AdjustDownScroll();
				
				var startIndex = lastSeparatorIndex + lastWord.length + 1;
				var endIndex =  newText.length - startIndex;
				
				if (document.all)
				{
					var textRange = document.getElementById(this.InputID).createTextRange();
					textRange.moveStart("character", startIndex);
					textRange.moveEnd("character", endIndex);
					textRange.select();
				}
				else
				{
					document.getElementById(this.InputID).setSelectionRange(startIndex, startIndex + endIndex);
				}
				return;
			}
			else
			{				
				this.SetValue("");
				this.SetIndex(-1);		
				if (this.HighlightedItem != null)
				{
					this.HighlightedItem.UnHighlight();					
				}
			}
		}
	}
		
	this.SetValue("");
	this.SetIndex("-1");
	
	if (!this.AllowCustomText)
	{
		var oldWord = currentText.substring(0, currentText.length-1);	
		if (this.TextPriorToCallBack != null)
		{			
			this.SetText(this.TextPriorToCallBack);
			return;
		}
		this.SetText(oldWord);
		this.HighlightMatches();
	}
};

RadComboBox.prototype.PollServer = function(more, keyCode)
{	
	if (!this.CurrentlyPolling)
	{		
		var comboInstance = this;
		if (this.RequestTimeoutID)
		{
			window.clearTimeout(this.RequestTimeoutID);
			this.RequestTimeoutID = 0;
		}
		this.RequestTimeoutID = window.setTimeout( function() { comboInstance.PollServerInterMediate(more, keyCode) }, this.ItemRequestTimeout);
	}
};

RadComboBox.prototype.PollServerInterMediate = function(more, keyCode)
{	
	var requestText = document.getElementById(this.InputID).value;
	if (requestText == "") requestText = false;
	if (this.FireEvent(this.OnClientItemsRequesting, this, requestText, more) == false) 
	{
		return;		
	}
	if (!this.CurrentlyPolling)
	{
		if (!document.getElementById(this.ClientID + "_LoadingDiv"))
		{
		    document.getElementById(this.DropDownID).innerHTML = "<div id='" + this.ClientID + "_LoadingDiv'" +  " class='" + this.LoadingMessageCssClass + " '>" + this.LoadingMessage + "</div>" + document.getElementById(this.DropDownID).innerHTML;	
		}
	}
	var comboInstance = this;
	window.setTimeout( function() { comboInstance.FetchCallBackData(more, requestText, keyCode) }, 20);
};


RadComboBox.prototype.RequestItems = function(text, more)
{
	this.FetchCallBackData(more, text, null);
};

RadComboBox.prototype.UnHighlightAll = function()
{
	for (var i=0; i<this.Items.length; i++)
	{
		if (this.Items[i].Highlighted)
		{			
			this.Items[i].UnHighlight();
		}
	}
};


RadComboBox.prototype.HandleInputImageOut = function()
{	
	document.getElementById(this.InputID).className = this.InputCssClass;
	var image = document.getElementById(this.ImageID);
	if (image)
	{
		image.className = this.ImageCssClass;
	}
};

RadComboBox.prototype.HandleInputImageHover = function()
{
	document.getElementById(this.InputID).className = this.InputCssClassHover;
	var image = document.getElementById(this.ImageID);
	if (image)
	{
		image.className = this.ImageCssClassHover;
	}		
};

RadComboBox.prototype.HandleMoreResultsImageOut = function()
{
	document.getElementById(this.MoreResultsBoxImageID).style.cursor = "default";
	document.getElementById(this.MoreResultsBoxImageID).src = this.ScrollDownImageDisabled;
	this.MoreResultsImageHovered = false;
};

RadComboBox.prototype.HandleMoreResultsImageHover = function()
{
	document.getElementById(this.MoreResultsBoxImageID).style.cursor = "hand";
	document.getElementById(this.MoreResultsBoxImageID).src = this.ScrollDownImage;
	this.MoreResultsImageHovered = true;
};

RadComboBox.prototype.HandleMoreResultsImageClick = function()
{
	this.UnHighlightAll();
	this.PollServer(true, null);
	document.getElementById(this.InputID).focus();
};

RadComboBox.prototype.CancelPropagation = function(eventArgs)
{	
	if (eventArgs.stopPropagation)
	{
		eventArgs.stopPropagation();
	}
	else
	{
		eventArgs.cancelBubble = true;
	}	
};

RadComboBox.prototype.PreventDefault = function(eventArgs)
{
	if (eventArgs.preventDefault)
	{
		eventArgs.preventDefault();
	}
	else
	{
		eventArgs.returnValue = false;
	}
};

RadComboBox.prototype.FireEvent = function(handler, a, b, c) 
{
	if (!handler)
		return true;
	
	RadComboBoxGlobalFirstParam = a;
	RadComboBoxGlobalSecondParam = b;
	RadComboBoxGlobalThirdParam = c;
	
	var s = handler;
	s = s + "(RadComboBoxGlobalFirstParam";
	s = s + ",RadComboBoxGlobalSecondParam";
	s = s + ",RadComboBoxGlobalThirdParam";
	s = s + ");";
			
	return eval(s);	
};

RadComboBox.prototype.HandleEvent = function(eventName, eventArgs)
{	
	var itemInstance;
	var srcElement = (document.all) ? eventArgs.srcElement : eventArgs.target;
	var item = this.HtmlElementToItem(srcElement);
	if (item != null)
	{
		itemInstance = this.ItemToInstance(item);
	}
	if (!this.Enabled)
	{
		return;
	}
	
	switch (eventName)
	{
		case "showdropdown" : this.CancelPropagation(eventArgs); this.ShowDropDown(); break;
		case "hidedropdown" : this.CancelPropagation(eventArgs); this.HideDropDown(); break;
		case "toggledropdown" : this.CancelPropagation(eventArgs); this.ToggleDropDown(); break;
		case "mouseover" : if (itemInstance != null) this.HandleMouseOver(itemInstance); break;
		case "mouseout" : if (itemInstance != null) this.HandleMouseOut(itemInstance); break;
		case "keypress" : this.HandleKeyPress(this, eventArgs); break;
		case "keydown" : this.HandleKeyDown(eventArgs); break;
		case "click" : this.HandleClick(eventArgs); break;
		case "inputclick" :
		{
			this.CancelPropagation(eventArgs);
			document.getElementById(this.InputID).select();
			if (this.ShowDropDownOnTextboxClick)
				this.ShowDropDown();
			break;
		}
		case "inputimageout" : this.HandleInputImageOut(); break;
		case "inputimagehover" : this.HandleInputImageHover(); break;
		case "moreresultsimageclick" : this.CancelPropagation(eventArgs); this.HandleMoreResultsImageClick(); break;
		case "moreresultsimagehover" : this.HandleMoreResultsImageHover(); break;
		case "moreresultsimageout" : this.HandleMoreResultsImageOut(); break;
	}
};

RadComboBox.prototype.Enable = function()
{	
	document.getElementById(this.InputID).disabled = false;			
	this.Enabled = true;
};

RadComboBox.prototype.Disable = function()
{	
	document.getElementById(this.InputID).disabled = "disabled";
	
	this.Enabled = false;
	this.TextHidden.value = this.GetText();
};

RadComboBox.prototype.FixUp = function(elem, shouldRunAgain)
{
	//if (document.compatMode && document.compatMode == "CSS1Compat")
	{
		if ((this.ClientWidthHidden.value != "") && (this.ClientHeightHidden.value != ""))
		{
			if (elem.style.width != this.ClientWidthHidden.value)
				elem.style.width = this.ClientWidthHidden.value;
			
			if (elem.style.height != this.ClientHeightHidden.value)	
				elem.style.height = this.ClientHeightHidden.value;

			this.ShowWrapperElement();
			return;
		}
		var image = elem.parentNode.getElementsByTagName("img")[0];
		if (shouldRunAgain && image && (image.offsetWidth == 0))
		{
			var comboInstance = this;
			if (document.attachEvent)
			{
				if (document.readyState == "complete")
					window.setTimeout(function() { comboInstance.FixUp(elem, false); }, 100);
				else
					window.attachEvent("onload", function () { comboInstance.FixUp(elem, false); });
			}
			else
			{
				window.addEventListener("load", function () { comboInstance.FixUp(elem, false); }, false);
			}
			return;
		}

		var computedStyle = null;
		if (elem.currentStyle)
		{
			computedStyle = elem.currentStyle;
		}
		else if (document.defaultView && document.defaultView.getComputedStyle)
		{
			computedStyle = document.defaultView.getComputedStyle(elem, null);
		}
		
		if (computedStyle == null)
		{
			this.ShowWrapperElement();
			return;
		}

		var height = parseInt(computedStyle.height);
		var width = parseInt(elem.offsetWidth);
		
		var paddingTop = parseInt(computedStyle.paddingTop);
		var paddingBottom = parseInt(computedStyle.paddingBottom);
        

		var paddingLeft = parseInt(computedStyle.paddingLeft);

		var paddingRight = parseInt(computedStyle.paddingRight);
		
		var borderTop = parseInt(computedStyle.borderTopWidth);
		if (isNaN(borderTop))
		{
			borderTop = 0;
		}
		var borderBottom = parseInt(computedStyle.borderBottomWidth);
		if (isNaN(borderBottom))
		{
			borderBottom = 0;
		}
		var borderLeft = parseInt(computedStyle.borderLeftWidth);
		if (isNaN(borderLeft))
		{
			borderLeft = 0;
		}
		
		var borderRight = parseInt(computedStyle.borderRightWidth);
		if (isNaN(borderRight))
		{
			borderRight = 0;
		}
		if (document.compatMode && document.compatMode == "CSS1Compat")
		{
		    if (!isNaN(height) && (this.ClientHeightHidden.value == ""))
		    {
			    elem.style.height = height - paddingTop - paddingBottom - borderTop - borderBottom + "px";
			    this.ClientHeightHidden.value = elem.style.height;
		    }
		}

		if (!isNaN(width) && width && (this.ClientWidthHidden.value == ""))
		{
			var imageWidth = 0;
			if (image)
			{
			    imageWidth = image.offsetWidth;
			}
			
			if (document.compatMode && document.compatMode == "CSS1Compat")
			{
				// Fix. Gemini ID: RCOM-7501
				var newElemWidth = width - imageWidth - paddingLeft - paddingRight - borderLeft - borderRight;
//				var tempWidth = "0px";

//				if (newElemWidth >= 0)
//					elem.style.width = newElemWidth + "px";

//			    if (parseInt(elem.style.width) != elem.offsetWidth)
//			    {
//			    	newElemWidth = parseInt(elem.style.width) + parseInt(elem.style.width) - parseInt(elem.offsetWidth);
//					if (newElemWidth >= 0)
//						elem.style.width = newElemWidth + "px";
//			    }

				if (newElemWidth >= 0)
					elem.style.width = newElemWidth + "px";
					
			    this.ClientWidthHidden.value = elem.style.width;
			}
			else
			{
			    elem.style.width = width - imageWidth;
			}
		}
		this.ShowWrapperElement();
	}
};

RadComboBox.prototype.ShowWrapperElement = function()
{
	if (!this.ShowWhileLoading)
		document.getElementById(this.ClientID + "_wrapper").style.visibility = "visible";	
};

function rcbDispatcher(comboBoxClientID, eventName, eventArgs)
{
	var comboInstance = null;
	try
	{
		comboInstance = window[comboBoxClientID]; 		
	}
	catch (e)
	{
	}
		
	if (typeof(comboInstance) == "undefined" || comboInstance == null)
	{
		return;
	}	
	if (typeof(comboInstance.HandleEvent) != "undefined")
	{
		comboInstance.HandleEvent(eventName, eventArgs);
	}
};

function rcbAppendStyleSheet(clientID, pathToCssFile)
{
	var isMacIe = (navigator.appName == "Microsoft Internet Explorer") &&
		((navigator.userAgent.toLowerCase().indexOf("mac") != -1) ||
		(navigator.appVersion.toLowerCase().indexOf("mac") != -1));

	var isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);

	if (isMacIe || isSafari)
	{
		document.write("<" + "link" + " rel='stylesheet' type='text/css' href='" + pathToCssFile + "'>");
	}	
	else
	{
		var linkObject = document.createElement("LINK");
		linkObject.rel = "stylesheet";
		linkObject.type = "text/css";
		linkObject.href = pathToCssFile;		
		document.getElementById(clientID + "StyleSheetHolder").appendChild(linkObject);		
	}		
};

//BEGIN_ATLAS_NOTIFY
if (typeof(Sys) != "undefined")
{
	if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
	{
		Sys.Application.notifyScriptLoaded();
	}
}
//END_ATLAS_NOTIFY
