var W3CDOM = document.createElement && document.getElementById;
var __progressSpeed = 100; // Delay in ms

function ProgressGetMainSpan(control, setVisible)
{
    var result = null;
    var child;
        
    for (var i = 0; i < control.childNodes.length; i++)
    {
        child = control.childNodes[i];
        
        if (child.style)
        {
            if (child.id == 'scrollSpan__')
            {
                if (setVisible) 
                {
                    result = child;
                    child.style.visibility = 'visible';
                }
                else return child;
            }
            else if (setVisible) 
                child.style.visibility = 'hidden';
        }
    }
    
    return result;
}

var startScrollWidth = null;

function ProgressStart(progressControlID)
{
    if (!W3CDOM) return;
    
    var control = document.getElementById(progressControlID);
    if (!control) return;
    
    var child = ProgressGetMainSpan(control, true);
    if (!child) return;
    
    if (startScrollWidth == null) startScrollWidth = child.style.width;
    else
        child.style.width = startScrollWidth;
        
    var leftValue = parseInt(child.style.width);
    child.style.left = -leftValue + 'px';
    
    var funcName = 'ProgressStep("' + progressControlID + '")';
   
    if (control.intervalID) clearInterval(control.intervalID);
    
    control.intervalID = setInterval(funcName, __progressSpeed);
}
function ProgressStep(progressControlID)
{
    if (!W3CDOM) return;
    
    var control = document.getElementById(progressControlID);
    if (!control) return;
    
    var child = ProgressGetMainSpan(control, false);
    if (!child) return;
    
    var leftValue = parseInt(child.style.left);
    var heightValue = parseInt(child.style.height);
    var widthValue = parseInt(child.style.width);
    var startWidthValue = parseInt(startScrollWidth);
    var controlWidthValue = parseInt(control.style.width);
    
    leftValue = leftValue + heightValue;
   
    var goBack = false;
    
    if (leftValue  > controlWidthValue) 
    {
        leftValue = -startWidthValue;
        
        goBack = true;
    }
        
    child.style.left = leftValue + 'px';
    
    if (goBack) child.style.width = startScrollWidth;
    
    var rightValue = leftValue + startWidthValue;
    
    if (rightValue > controlWidthValue)
    {
        child.style.width = (controlWidthValue - leftValue) + 'px';
    }
}
function ProgressStop(progressControlID)
{
    if (!W3CDOM) return;
    
    var control = document.getElementById(progressControlID);
    if (!control) return;
    
    var child = ProgressGetMainSpan(control, false);
    if (!child) return;
    
    var leftValue = parseInt(child.style.width);
    child.style.left = -leftValue + 'px';
    child.style.visibility = 'hidden';
    
    if (control.intervalID) clearInterval(control.intervalID);
}

function ErrorGetMainSpan(control, setVisible)
{
    var result = null;
    var child;
        
    for (var i = 0; i < control.childNodes.length; i++)
    {
        child = control.childNodes[i];
        
        if (child.style)
        {
            if (child.id == 'errorSpan__')
            {
                if (setVisible) 
                {
                    result = child;
                    
                    child.style.backgroundColor = control.style.backgroundColor;
                    child.style.visibility = 'visible';
                }
                else return child;
            }
            else if (setVisible) 
                child.style.visibility = 'hidden';
        }
    }
    
    return result;
}
function CreateTextNode(control)
{
    var result = document.createTextNode('');
    
    control.appendChild(result);
    
    return result;
}
function HandleCallbackError(controlID, errorTitle, showErrorAlert, errorMessage)
{
    if (!W3CDOM) return;
    
    var control = document.getElementById(controlID);
    if (!control) return;
    
    var child = ErrorGetMainSpan(control, true);
    if (!child) return;
    
    if (errorTitle.length > 0)
    {
        var textChild = child.hasChildNodes() ? child.childNodes[0] : CreateTextNode(child);
        textChild.nodeValue = errorTitle;
    }
    else
    {
        child.style.backgroundColor = control.style.color;
    }
    
    while (errorMessage.indexOf('|') >= 0)
    {
        errorMessage = errorMessage.replace('|', '\r\n\r\n');
        }
    
    control.title = errorMessage;
    
    if (showErrorAlert) alert(errorMessage);
}
function ClearStateControl(controlID)
{
    if (!W3CDOM) return;
    
    var control = document.getElementById(controlID);
    if (!control) return;
    
    var child;
    
    for (var i = 0; i < control.childNodes.length; i++)
    {
        child = control.childNodes[i];
        
        if (child.style) child.style.visibility = 'hidden';
    }
    
    control.title = '';
}

top.__callbackStateScriptSet = true;