// var activeButton = '';


var submitButtonList = null;

var isOpen = false;
//Value of the company search box is loaded on the page
var findCompanyElementId = '';
//Comma separated list of divs, which are to have their select boxes hidden, when find company is opened
var dropDownLayers;

try
{
	document.onkeydown = MicrosoftEventHandler_KeyDown;
}
catch(e)
{
}

function RegisterDropDownLayer( layerId)
{
    dropDownLayers = layerId;
}

function RegisterFindCompanyBox( layerId )
{
    findCompanyElementId = layerId;
    
    var findCompany = document.getElementById(findCompanyElementId);
    
    if (findCompany)
        findCompany.style.zIndex = '1000';
        
    if (isOpen)
    {
        layerObj.showBox(layerId);
        HideSelectBoxes(dropDownLayers);
    }
    else 
    {
        layerObj.hideBox(layerId);
        ShowSelectBoxes(dropDownLayers);
    }
}

function NetscapeEventHandler_KeyDown(e)
{
	if (e.which == 13)
	{	return false;	}
	
	return true;
}

function MicrosoftEventHandler_KeyDown()
{
	switch (event.keyCode)
	{
	    case 13:

	        if (event.srcElement && event.srcElement.type == "textarea")
	        {
	            event.returnValue = true;
	            return true;
	        }
	        else if (submitButtonList != null &&
		             submitButtonList.Count() > 0)
	        {
	            var submitButton = document.getElementById(submitButtonList.LastItem());

	            if (submitButton)
	            {
	                submitButtonList.Remove(submitButtonList.LastItem());

	                jQuery(submitButton).click();

	                jQuery(submitButton).blur();
	            }
	        }

	        event.returnValue = false;

            if (typeof KeyDown_Enter_13 == 'function')
                KeyDown_Enter_13();

	        return false;

	        break;
		case 27:

		    HideFindCompany();

		    if (typeof KeyDown_Escape_27 == 'function')
		        KeyDown_Escape_27();
		        
			break;
		
		case 123:
		    ShowFindCompany();

		    if (typeof KeyDown_F12_123 == 'function')
		        KeyDown_F12_123();

		    break;

		case 38:

		    if (typeof KeyDown_ArrowUp_38 == 'function')
		        KeyDown_ArrowUp_38();

		    break;

		case 40:

		    if (typeof KeyDown_ArrowDown_40 == 'function')
		        KeyDown_ArrowDown_40();

		    break;

		case 32:

		    if (typeof KeyDown_SpaceBar_32 == 'function')
		        KeyDown_SpaceBar_32();

		    break;
	}
	
	return true;
}

function SetSubmitButton(submitId)
{
    if (submitButtonList == null)
    {
        submitButtonList = new ArrayList();
    }
    
    submitButtonList.Add(submitId);
}

function FindCompany() // Form LinkButtom
{
    if (isOpen)
        HideFindCompany();
    else
        ShowFindCompany();
}

function ShowFindCompany()
{
    var hiddenInputValueShowFindCompany = document.getElementById('ctl00_hiddenInputValueShowFindCompanyControl');
	
    if (! hiddenInputValueShowFindCompany)
        return true;
		
    if (hiddenInputValueShowFindCompany.value.length > 0)
        return true;
        
    if (isOpen)
    {
        if (CurrentUserCompanyLink != null)
        {
            SelectedCompanyChanged(CurrentUserCompanyLink);
        }
    }
    
    document.getElementById("ctl00_ucContentHeader_divFindCompanyPopup").style.display = "inline";

    HideSelectBoxes(dropDownLayers);

    layerObj.showBox(findCompanyElementId);
    layerObj.CenterLayer(findCompanyElementId);
    
	var searchBox = document.getElementById('ctl00_ucContentHeader_findCompanyPopup_ucFindCompany_radComboCompanyList_Input');

	if (searchBox)
	{
		searchBox.select();
		searchBox.focus();
	}
	
	isOpen = true;
}

function HideFindCompany()
{
    var hiddenInputValueShowFindCompany = document.getElementById('ctl00_hiddenInputValueShowFindCompanyControl');
	
    if (! hiddenInputValueShowFindCompany)
        return true;
		
    if (hiddenInputValueShowFindCompany.value.length > 0)
        return true;
        
    var companyDropDown = document.getElementById('ctl00_ucContentHeader_findCompanyPopup_ucFindCompany_radComboCompanyList_DropDown');
    if (companyDropDown)
        companyDropDown.style.display = "none";
        
    layerObj.FadeOutLayer(findCompanyElementId, 0.5);
    
    document.getElementById("ctl00_ucContentHeader_divFindCompanyPopup").style.display = "none";
    
    ShowSelectBoxes(dropDownLayers);
    
    isOpen = false;
}

function SelectedCompanyChanged(companyLink, e)
{
	var textBox = document.getElementById("ctl00_ucContentHeader_findCompanyPopup_ucFindCompany_hiddenSelectedCompanyLink");
	
	if (textBox)
	{
		textBox.value = companyLink;
		
		if (e)
		    e.returnValue = false;
		    
		__doPostBack('','');
		
		return false;
	}
}

