var ProjectItems = new Array();
var ProjectItemsDetail = new Array();
var MoreDetailWin = null;
var ConfigID = null;
var GlobalDetailed = false;
var CurrentCategory = null;
var CategoryOver = false;

var CurrentLevel = 1;

function CategoryChange(Command)
{
    if (LoadingOn(CategoryChange.bind(this, Command)))
    {
        Command.AddArgument("control", "BottomItemSelection")
        ChangeControl(BYOResources.BYO_RESOURCE_ITEMSELECTIONJS_ERROR_BOTITEMSEL, "BottomItemSelection_ItemSelection", ItemSelection.UpdateControl(Command));
        
        LoadingOff();
    }
}

function Continue()
{
    if (LoadingOn(Continue.bind(this)))
    {
        window.location.href = 'Configuration.aspx?' + BYOResources.BYO_QUERYSTRING_CONFIGURATION;
        
        //'setTimeout("LoadingOff();", 1500);
    }
}

function LevelChange(Command)
{
    if (LoadingOn(LevelChange.bind(this, Command)))
    {
        var Response;
        
        Response = ItemSelection.LevelChange(Command);
        
        CurrentLevel = Command.GetArgument("Level");
        
        if (!ValidateResponse(BYOResources.BYO_RESOURCE_ITEMSELECTIONJS_ERROR_PRODUCT, Response))
        {
            LoadingOff();
            return false;
        }
        else
        {
            $(ProductCatalog_SelectionAreaID).innerHTML = Response.value;
            eval(ProductCatalog_ContainerID).InitializeSelectionArea();
        }
        
        LoadingOff();
        return true;
    }
}

function SelectionChange(Command, AutoSelectProduct)
{
    if (LoadingOn(SelectionChange.bind(this, Command, AutoSelectProduct)))
    {
        var Response;
        
        Response = ItemSelection.SelectionChange(Command);
        
        if (!ValidateResponse(BYOResources.BYO_RESOURCE_ITEMSELECTIONJS_ERROR_PRODUCT, Response))
        {
            LoadingOff();
            return false;
        }
        else
        {
            var Responses = Response.value.split("~!~");
            if (Responses[0].length > 0)
            {
                $(ProductCatalog_SelectionAreaID).innerHTML = Responses[0];
                eval(ProductCatalog_ContainerID).InitializeSelectionArea();
                
                CurrentLevel = parseInt(CurrentLevel) + 1;
                eval(ProductCatalog_ContainerID).UpdateLevel(CurrentLevel);
            }
            
            $(ProductCatalog_LevelAreaID).innerHTML = Responses[1];
            eval(ProductCatalog_ContainerID).InitializeLevelArea();
            
            if (AutoSelectProduct != null)
            {
                eval(AutoSelectProduct);
            }
        }
        
        LoadingOff();
        return true;
    }
}

/*
function ProductChange(Command, HandledEvent)
{
    if (LoadingOn(ProductChange.bind(this, Command, HandledEvent)))
    {
        var Success;
        
        Success = ItemSelection.UpdateProduct(Command);
        
        if (!ValidateResponse(BYOResources.BYO_RESOURCE_ITEMSELECTIONJS_ERROR_PRODUCT, Success))
        {
            LoadingOff();
            return false;
        }
        
        LoadingOff();
        return Success.value;
    }
}
*/

function RebuildLeftSection(UseLogin, Detailed)
{
    if (LoadingOn(RebuildLeftSection.bind(this, UseLogin, Detailed)))
    {
        var Command;
        Command = new ControlCommand("");
        Command.SetName("Rebuild");
        Command.AddArgument("action", 0);
        Command.AddArgument("control", "MyProjects");
        
        if (Detailed != null)
        {
            GlobalDetailed = Detailed;
        }

        Command.AddArgument("detailed", GlobalDetailed)

        var Response = ItemSelection.UpdateControl(Command);
        
        if (ValidateResponse(BYOResources.BYO_RESOURCE_CONFIGURATIONJS_ERROR_MYPROJECTS, Response))
        {
            $("MyProjects_Container").innerHTML = Response.value;
        }
        
        InitDynamicMyProjects();
        
        LoadingOff();
    }
}

function ItemSelectionLogIn(Username, Password)
{
    if (LogIn(Username, Password))
    {
        RebuildLeftSection(false, false);

        return true;
    }
    
    return false;
}

function ItemSelectionLogOut()
{
    if (LoadingOn(ItemSelectionLogOut.bind(this)))
    {    
        if (LogOut())
        {
	        RebuildLeftSection(true, false);

            LoadingOff();
            return true;
        }
        
        LoadingOff();
        return false;
    }
}


/* Project Maintenance Section */

function Reconfig(id)
{
    if (LoadingOn(Reconfig.bind(this, id)))
    {
        if (ValidateResponse(BYOResources.BYO_RESOURCE_ITEMSELJS_RECONFIG_ERROR, Common.ReconfigureProject(id)))
        {
            window.location.href = 'Configuration.aspx?NewConfig=true';
        }
            
        LoadingOff();
    }
}

function DisplayInfoCategory(InfoCategory)
{
    var InfoCategoryControl = null;
    if (isDefined(window, InfoCategory))
    {
        InfoCategoryControl = $(InfoCategory);
        if (Element.hasClassName(InfoCategoryControl, "Hidden"))
        {
            Element.toggleClassName(InfoCategoryControl, "Hidden")
            Element.toggleClassName(InfoCategoryControl, "InfoCategory")
        }
        
        if (CurrentCategory != null)
        {
            HideInfoCategory(CurrentCategory)
        }
        CurrentCategory = InfoCategory;
        CategoryOver = true;
    }
}

function HideInfoCategory(InfoCategory)
{
    if (InfoCategory != null && isDefined(window, InfoCategory))
    {
        InfoCategory = $(InfoCategory);
        if (!Element.hasClassName(InfoCategory, "Hidden"))
        {
            Element.toggleClassName(InfoCategory, "Hidden")
            Element.toggleClassName(InfoCategory, "InfoCategory")
        }
        
        if (CurrentCategory == InfoCategory)
        {
            CurrentCategory = null;
            CategoryOver = false;
        }
    }
    else if (CurrentCategory != null && isDefined(window, CurrentCategory))
    {
        HideInfoCategory(CurrentCategory);
    }
}

function HideAllCategories()
{
    if (CurrentCategory != null && isDefined(window, CurrentCategory) && !CategoryOver)
    {
        HideInfoCategory(CurrentCategory);
        CurrentCategory = null;
    }
}

function AlmostHideCategory(InfoCategory)
{
    if (InfoCategory != null && isDefined(window, InfoCategory) && CurrentCategory == InfoCategory)
    {
        CategoryOver = false;
    }
}
