﻿function addCart(cartLayer, qty, stock, errorDiv, messageDiv, multQty, message, message2, message3)
{
    var intQty = parseInt(document.getElementById(qty).value);
    var intStock = parseInt(document.getElementById(stock).value);
    var intLimit = parseInt(document.getElementById('hidLimit').value);
    var intMultQty = parseInt(document.getElementById(multQty).value);
    var divError = document.getElementById(errorDiv);
    var divErrorControls = document.getElementById('divErrorControls');
    var selCart = document.getElementById('selCart');
    
    if (intQty > 0)
    {
        var ok = true;
    
        if (intQty > intStock && selCart.options[selCart.selectedIndex].value == '0' && intLimit == 0)
        {
            var divError = document.getElementById(errorDiv);
            if (divError)
            {
                var divMsg = document.getElementById(messageDiv);
                divError.style.display = 'block';
                divErrorControls.style.display = 'block';
                divMsg.innerHTML = message.replace('[0]', intStock).replace('[1]', intQty - intStock);
            }
            ok = false;
        }
        if (intQty > intStock && selCart.options[selCart.selectedIndex].value == '0' && intLimit == 1)
        {
            var divError = document.getElementById(errorDiv);
            if (divError)
            {
                var divMsg = document.getElementById(messageDiv);
                divError.style.display = 'block';
                divMsg.innerHTML = message3.replace('[0]', intStock);
            }
            ok = false;
        }
        if ((intMultQty > 0) && (intQty % intMultQty != 0))
        {
            var divError = document.getElementById(errorDiv);
            if (divError)
            {
                var divMsg = document.getElementById(messageDiv);
                divError.style.display = 'block';
                divMsg.innerHTML = message2.replace('[0]', intMultQty);
            }
            ok = false;
        }
        if (ok)
        {
            if (divError)
            {
                divError.style.display = 'none';
            }
            addToCart('txtPopQty');
        }
    }
    
}

function resetCartForm()
{

    var txtQty =  document.getElementById('txtPopQty');
    var divMsg = document.getElementById('divMsg');
    var divAdd = document.getElementById('divAdd');
    var divProcess = document.getElementById('divProcess');
    var divFinishProcess = document.getElementById('divFinishProcess');
    var divProcessError = document.getElementById('divProcessError');
    var divErrorControls = document.getElementById('divErrorControls');
    
    divMsg.style.display = 'none';
    divAdd.style.display = 'block';
    divProcess.style.display = 'none';
    divFinishProcess.style.display = 'none';
    divProcessError.style.display = 'none';
    divErrorControls.style.display = 'none';
    
    //txtQty.value = '1';
    txtQty.select();
    txtQty.focus();
    
}

function checkCartKey(e, message, message2)
{

    hideWarning();

    if(typeof event!= 'undefined')
    {
        var pressedkey = window.event.keyCode;
    }
    else
    {
        var pressedkey = e.charCode;
        if (pressedkey == 0)
        {
            pressedkey = e.keyCode;
        }
    }

    if (pressedkey == 13)
    {
        addCart('divAddCart', 'txtPopQty', 'hidPopStock', 'divMsg', 'divStockMsg', 'hidMultQty', message, message2, message3);
        return false;
    }
        
}

function UpdateCart()
{
    callAjax('ajax/updateCart.aspx', 'divShoppingCart');
}

function addToCartOK(requestMessage)
{
    if (requestMessage == 'OK')
    {
        var objProcessing = document.getElementById('divProcess')
        var objAfterProcessing = document.getElementById('divFinishProcess')
        
        if (objProcessing)
        {
            objProcessing.style.display = 'none';
        }
        if (objAfterProcessing)
        {
            objAfterProcessing.style.display = 'block';
        }
        
        UpdateCart();
    }
    else
    {
        addToCartNOK(requestMessage);
    }
}

function addToCartNOK(requestMessage, errorStatus)
{
    var objProcessing = document.getElementById('divProcess');
    var objError = document.getElementById('divProcessError');
    
    if (objProcessing)
    {
        objProcessing.style.display = 'none';
    }

    if (objError)
    {
        objError.style.display = 'block';
    }
    else
    {
        alert('Erro: ' + errorStatus);
    }
}

function addToCart(total, stock)
{
    var txtTotal = document.getElementById(total);
    var txtStock = document.getElementById(stock);
    var total = parseInt(txtTotal.value);
    if (txtStock)
    {
        total = parseInt(txtStock.value);
    }
    
    // Efectua a chamada
    var hidPriceField = document.getElementById('hidPrice');
    var hidEcoPriceField = document.getElementById('hidEcoPrice');
    var hidRef = document.getElementById('hidRef');
    var selCart = document.getElementById('selCart');
    var strURL = '';
    
    if (selCart.options[selCart.selectedIndex].value == '0')
    {
        strURL = 'ajax/addToCart.aspx?ref=' + hidRef.value + '&qty=' + total + '&unPr=' + hidPriceField.value + '&eco=' + hidEcoPriceField.value;
    }
    else
    {
        strURL = 'ajax/addTOList.aspx?ref=' + hidRef.value + '&qty=' + total + '&lst=' + selCart.options[selCart.selectedIndex].value;
    }
    
    var divAdd = document.getElementById('divAdd');
    var divProcess = document.getElementById('divProcess');
    divAdd.style.display = 'none';
    divProcess.style.display = 'block';
    
    callAjax(strURL, '', addToCartOK, addToCartNOK);
    
}
