﻿/// <reference path="Common.js" />
/// <reference path="ElementStyles.js" />
/// <reference path="Messages_en.js" />

var elementErrorManager;

/*
NOTES:
elementErrorManager is a global variable.  

Each dialog should have a single method used to validate the dialog's elements.  This single 
validation method should instantiate elementErrorManager and pass to it a reference to the
div that will contain the error messages (errorContainer).

The dialog validation method is responsible for validating all elements.  If an element
is found to be invalid, call  elementErrorManager.AddErrorMessage(element, errorMessage).  
If the element is found valid, call elementErrorManager.RemoveErrorMessage(element).

elementErrorManager.AddErrorMessage:
 - add passed element and message to an internal container (ErrorMessages)
 - change the className of the passed element to "error"
 
elementErrorManager.RemoveErrorMessage:
 - removes passed element from internal container (ErrorMessages)
 - change the className of the passed element to ""
 
elementErrorManager.ShowErrors:
 - loops through internal container (ErrorMessages) and populates errorContainer with error messages

elementErrorManager.ClearErrors:
 - removes all error messages from errorContainer
 - resets internal container (ErrorMessages)

elementErrorManager.ErrorCount:
 - returns a count of errors in internal container (ErrorMessages)
 
NOTE: 
* you can pass in a single element to AddErrorMessage and RemoveErrorMessage or an array of elements
* if you pass in an array of elements, then the error message is associated with just the first element
in the array, but each element in the array will have it's classname modified.
 
*/

function ErrorManager(errorContainer)
{
    //PROPERTIES
    this.ErrorContainer = errorContainer;
    this.ErrorMessages = new Object();
}

ErrorManager.prototype.AddErrorMessage = 
    function(element, message, className)
    {
        if(className == null)
        {
            className = TextStyles.Error;
        }
        
        if(element != null)
        {
            if(isArray(element))
            {
                for(var arrayIndex = 0; arrayIndex < element.length; arrayIndex++)
                {
                    element[arrayIndex].className = className;
                }
                
                this.ErrorMessages[element[0].id] = message;
            }
            else
            {
                element.className = className;
                this.ErrorMessages[element.id] = message;
            }
        }
    };

ErrorManager.prototype.RemoveErrorMessage = 
    function(element, className)
    {
        if(className == null)
        {
            className = TextStyles.Blank;
        }
        
        if(element != null)
        {
            if(isArray(element))
            {
                for(var arrayIndex = 0; arrayIndex < element.length; arrayIndex++)
                {
                    element[arrayIndex].className = className;
                }
                
                this.ErrorMessages[element[0].id] = "";
            }
            else
            {    
                element.className = className;
                this.ErrorMessages[element.id] = "";
            }
        }
    };
    
ErrorManager.prototype.ClearErrors = 
    function()
    {
        var thisList = this.ErrorContainer.getElementsByTagName('ul')[0];//get the ul tag
        var allLIs = thisList.getElementsByTagName('li');
        var l = allLIs.length;
    	
        this.ErrorMessages = new Object();
    		
        //if there are items already there...
        if(l > 0)
        {
            while (l > 0)
            {//clear them out
                thisList.removeChild(allLIs[0]);
                l--;
            }
        }

        hideContent(this.ErrorContainer);
    };

ErrorManager.prototype.ErrorCount = 
    function()
    {
        var count = 0;
        
        for(errorElementId in this.ErrorMessages)
        {
            var errorMessage = trimString(this.ErrorMessages[errorElementId]);
            if(errorMessage.length > 0)
            {
                count++;
            }
        }    
        
        return count;
    };

    ErrorManager.prototype.ShowErrors =
    function()
    {
        if (this.ErrorCount() > 0)
        {
            showContent(this.ErrorContainer);

            var thisList = this.ErrorContainer.getElementsByTagName('ul')[0]; //get the ul tag
            var allLIs = thisList.getElementsByTagName('li');
            var l = allLIs.length;

            //if there are items already there...
            if (l > 0)
            {
                while (l > 0)
                {//clear them out
                    thisList.removeChild(allLIs[0]);
                    l--;
                }
            }

            for (errorElementId in this.ErrorMessages)
            {
                var errorMessage = trimString(this.ErrorMessages[errorElementId]);
                if ((errorMessage != null) && (errorMessage.length > 0))
                {
                    var newText = document.createTextNode(errorMessage);
                    var newItem = document.createElement('li');
                    newItem.appendChild(newText);
                    thisList.appendChild(newItem);
                }
            }
        }
        else
        {
            this.ClearErrors();
        }
    };    

function isArray(variable) 
{
    return (variable.constructor == Array);
}
