﻿/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace("Simanfor.ClientControls");

Simanfor.ClientControls.ToggleControl = function(sourceId, targetId, expandClassName, collapseClassName, collapse)
{
    this._sourceElement = $get(sourceId);
    if (this._sourceElement == null) return null;
    
    this._targetElement = $get(targetId);
    if (this._targetElement == null) return null;
    
    this._sourceElement._toggleHandler = this;
        
    this._targetDefaultDisplayValue = this._targetElement.style.display;
    this._expandClassName = expandClassName;
    this._collapseClassName = collapseClassName;

    this._isCollapsed = !collapse;

    this._sourceElement.onclick = function()
    {
        this._toggleHandler.toggle();
    }
    
    if (collapse)
    {
        this._sourceElement.onclick();
    }
}

Simanfor.ClientControls.ToggleControl.prototype =
{
    get_isCollapsed: function()
    {
        return this._isCollapsed;
    },
    
    toggle: function()
    {
        var targetElement = this._targetElement;
        var targetStyle = targetElement.style;
            
        this._isCollapsed = !this._isCollapsed;
            
        if (this._isCollapsed)
        {
            Sys.UI.DomElement.removeCssClass(this._sourceElement, this._collapseClassName);
            Sys.UI.DomElement.addCssClass(this._sourceElement, this._expandClassName);
            targetStyle.display = "none";
        }
        else
        {
            Sys.UI.DomElement.removeCssClass(this._sourceElement, this._expandClassName);
            Sys.UI.DomElement.addCssClass(this._sourceElement, this._collapseClassName);
            targetStyle.display = this._targetDefaultDisplayValue;
        }
    }
}

Simanfor.ClientControls.ToggleControl.registerClass('Simanfor.ClientControls.ToggleControl');

Simanfor.ClientControls.Dialog = function(dialogId, barricadeId)
{
    this._dialog = $get(dialogId);
    if (this._dialog == null) return null;
    this._initialParent = this._dialog.parentNode;
    
    this._barricade = $get(barricadeId);
    if (this._barricade == null) return null;
    
    this._dialog.style.display = "none";
    this._barricade.style.display = "none";
    
    this._isVisible = false;
}

Simanfor.ClientControls.Dialog.prototype = 
{
    show: function()
    {
        this._barricade.style.display = this._dialog.style.display = "block";
        this._initialParent.removeChild(this._dialog);
        this._barricade.parentNode.appendChild(this._dialog);
    },
    
    hide: function(reset)
    {
        this._barricade.style.display = this._dialog.style.display = "none";
        this._barricade.parentNode.removeChild(this._dialog);
        this._initialParent.appendChild(this._dialog);
        
        if (reset != null || reset == true)
        {
            var inputs = document.getElementsByTagName("input");
            var textareas = document.getElementsByTagName("textarea");
            
            for (var input in inputs)
            {
                inputs[input].value = "";
            }
            
            for (var textarea in textareas)
            {
                textareas[textarea].value = "";
            }
        }
    },
    
    toggle: function()
    {
        this._isVisible = !this._isVisible;
        if (this._isVisible)
        {
            this.show();
        }
        else
        {
            this.hide();
        }
    }
}

Simanfor.ClientControls.Dialog.registerClass('Simanfor.ClientControls.Dialog');

Simanfor.ClientControls.MultiView = function(viewsId, activeViewIndex, parent)
{
    this._activeViewIndex = (activeViewIndex ? activeViewIndex : 0);
    this._views = new Array();
    this._parent = parent;
    
    if (!viewsId || viewsId.length == 0)
    {
        return;
    }
    
    for (var i = 0; i < viewsId.length; i++)
    {
        var view = $get(viewsId[i], parent);
        if (view)
        {
            view.style.display = "none";
            this._views.push(view);
            
            var inputs = view.getElementsByTagName("input");
            for (var j = 0; j < inputs.length; j++)
            {
                inputs[j].disabled = "disabled";
            }
        }
    }
    
    this.setActiveViewIndex(0);
}

Simanfor.ClientControls.MultiView.prototype =
{
    getActiveViewIndex : function()
    {
        return(this._activeViewIndex);
    },
    
    setActiveViewIndex : function(index)
    {
        if (index > this._views.length - 1)
        {
            return;
        }
        
        this._views[this._activeViewIndex].style.display = "none";
        this._activeViewIndex = index;
        
        if (index >= 0)
        {
            this._views[this._activeViewIndex].style.display = "";
        }
    },
    
    notifyEnabledChange : function(isEnabled)
    {
        var disabledValue = (isEnabled ? "" : "disabled");
        
        var inputs = this._parent.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++)
        {
            inputs[i].disabled = disabledValue;
        }
    }
}

Simanfor.ClientControls.MultiView.registerClass('Simanfor.ClientControls.MultiView');

Simanfor.ClientControls.MultiViewCollection = function()
{
    this._multiViews = new Object();
}

Simanfor.ClientControls.MultiViewCollection.prototype = 
{
    initialize : function(containerId, viewsId)
    {
        var container = $get(containerId);
        if (!container) return;
        
        for (var i = 0; i < container.childNodes.length; i++)
        {
            var node = container.childNodes[i];
            if (!node.tagName || node.tagName.toUpperCase() != "DIV") continue;
            
            this._multiViews[node.id] = new Simanfor.ClientControls.MultiView(viewsId, 0, node);
        }
    }
}

Simanfor.ClientControls.MultiViewCollection.registerClass('Simanfor.ClientControls.MultiViewCollection');

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

