var ProjectItems = new Array();
var ProjectItemsDetail = new Array();
var MoreDetailWin = null;
var ConfigID = null;
var GlobalDetailed = false;
var CurrentCategory = null;
var CategoryOver = false;

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()
{
    window.location.href = 'Configuration.aspx?' + BYOResources.BYO_QUERYSTRING_CONFIGURATION;
}

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 InitializeMyProjects()
{
    var MyProjects = null;
    
    ProjectItems.length = 0;

    MyProjects = ChildNodes($("LoginForm_Container"), 0);
    
    ProjectItems = ConvertToArray(MyProjects.getAttribute("GraphicButtons"), "|");
    
    for (Index = 0; Index < ProjectItems.length; Index++)
    {
        ProjectItems[Index] = eval(ProjectItems[Index]);
    }
}

function RebuildLeftSection(UseLogin, Detailed)
{
    if (LoadingOn(RebuildLeftSection.bind(this, UseLogin, Detailed)))
    {
        var Command;
        Command = new ControlCommand("");
        Command.SetName("Rebuild");
        Command.AddArgument("control", "LeftSection")
        
        if (Detailed != null)
        {
            GlobalDetailed = Detailed;
        }
        
        Command.AddArgument("detailed", GlobalDetailed)

        var Response = ItemSelection.UpdateControl(Command);
        var MyProjects = null;
        var Index = 0;
        
        if (ValidateResponse(BYOResources.BYO_RESOURCE_ITEMSELECTIONJS_ERROR_LOGINFORM, Response))
        {
            $("LoginForm_Container").innerHTML = Response.value;
        }
        
        if (UseLogin)
        {
            var Control = eval("LoginForm");
            
            if (Control.initialize != null)
            {
                Control.initialize("LoginForm");
            }
        }
        else
        {
            InitializeMyProjects();
        }
        
        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;
    }
}