﻿function requireConfirmation(text)
{
    return confirm(text);
}

function enableButton(controlId, status)
{
    var buttonElement = window.document.getElementById(controlId);
    if(buttonElement != null)
    {
        buttonElement.disabled = status;
    }
}

function setOnClickHandler(controlId, handler)
{
    var control = document.all ? document.all[controlId] : document.getElementById(controlId);
    if (control != null)
    {
        control.onclick = handler;
    }
}

function disableAnchor(controlId)
{
    var control = document.all ? document.all[controlId] : document.getElementById(controlId);
    if (control != null)
    {
        control.href = '#';
        control.onclick = 'return false;';
    }
}

//process order with confirmation
function processOrder(confirmationText, controlId, waitMessage, waitMessageControlId)
{
    var sendElement = window.document.getElementById(controlId);
    var messageElement = window.document.getElementById(waitMessageControlId);
    
    if ((sendElement != null) && (messageElement != null))
    {
        var isConfirmed = requireConfirmation(confirmationText);
        if (isConfirmed)
        {
            var expression = 'enableButton(\'' + controlId + '\', true)';

            window.setTimeout(expression, 500);
            
            messageElement.innerText = waitMessage;
        }
        
        return isConfirmed;
    }
    
    return false;
}

//process order without confirmation
function processOrderNoConfirm(controlId, waitMessage, waitMessageControlId)
{
    var sendElement = window.document.getElementById(controlId);
    var messageElement = window.document.getElementById(waitMessageControlId);
    
    if ((sendElement != null) && (messageElement != null))
    {
        var expression = 'disableAnchor(\'' + controlId + '\')';
        
        window.setTimeout(expression, 500);
        
        messageElement.innerText = waitMessage;
        
        return true;
    }
    
    return false;
}

function showHideShipmentFeeDue(controlId, value)
{
    var element = window.document.getElementById(controlId);
    if (element != null)
    {
        var splitValues = value.split(':', 2);
        
        if(splitValues.length != 2) return;
        
        var hasFeeOptions = (splitValues[1].toLowerCase() == "true") ? true : false;
        if (hasFeeOptions)
        {
            element.style.display = "inline";
        }
        else
        {
            element.style.display = "none";
        }
    }
}

function validatorsEnable(validators, status)
{
    for (var i = 0; i < validators.length; i++)
    {
        var validator = document.all ? document.all[validators[i]] : document.getElementById(validators[i]);
        
        ValidatorEnable(validator, status);
    }
}

function controlsEnable(controls, status)
{
    for (var i = 0; i < controls.length; i++)
    {
        var control = document.all ? document.all[controls[i]] : document.getElementById(controls[i]);
        
        control.disabled = !status;
    }
}