﻿function getposOffset(overlay, offsettype)
{
    var totaloffset = (offsettype == 'left') ? overlay.offsetLeft : overlay.offsetTop;
    var parentEl = overlay.offsetParent;
    while (parentEl != null)
    {
        totaloffset = (offsettype == 'left')? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop;
        parentEl = parentEl.offsetParent;
    }
    return totaloffset;
}

function ShowPopup(control, layer, popupPosition)
{

    if (document.getElementById)
    {
        var subobj = document.getElementById(layer);
        subobj.style.display = 'none';
        subobj.style.display = (subobj.style.display != 'block')? 'block' : 'none';
        var xpos = getposOffset(control, 'left') + ((typeof popupPosition != 'undefined' && popupPosition.indexOf('right') != -1) ? -(subobj.offsetWidth - control.offsetWidth) : 0);
        var ypos = getposOffset(control, 'top') + ((typeof popupPosition != 'undefined' && popupPosition.indexOf('bottom') != -1)? control.offsetHeight : 0);
        subobj.style.left = xpos + 'px';
        subobj.style.top = ypos + 'px';
        
        return false;
    }
    else
    {
        return true;
    }

}

function closePopup(control)
{
    var objControl = document.getElementById(control);
    objControl.style.display = 'none';
}

/* PESQUISA */
function resetManufacturers(layer)
{
    var divLayer = document.getElementById(layer);
    divLayer.style.display = 'none';
}

function searchManufacturers(control, layer)
{
    if(control.value.length >= 3)
    {
        callAjax('ajax/listManufacturers.aspx?man=' + control.value, layer);
        ShowPopup(control, layer, 'bottomleft');
    }
    else
    {
        var divLayer = document.getElementById(layer);
        if (divLayer.style.display == 'block')
        {
            divLayer.style.display = 'none';
        }
    }
}

function fillTextBox(control, text)
{
    var txtTextBox = document.getElementById(control);
    if(control)
    {
        txtTextBox.value = text;
    }
}

/* CARRINHO DE COMPRAS */

function ShowPopupCart(control, layer, popupPosition, item, qty, stock, limit, unitPrice, ecoPrice, multipleqty, stockField, qtyField)
{

    ShowPopup(control, layer, popupPosition);
    
    // Quantidade
    var litTxtPopQty = document.getElementById(qtyField);
    if (litTxtPopQty)
    {
        litTxtPopQty.value = 1;
        var objQty = document.getElementById(qty);
        if (objQty)
        {
            litTxtPopQty.value = objQty.value;
        }
    }
        
    // Referência
    var objRef = document.getElementById('divCartRef');
    objRef.innerHTML = item;
    
    // Adiciona o valor do stock
    if (stockField != '')
    {
        var hidStockField = document.getElementById(stockField);
        var hidPriceField = document.getElementById('hidPrice');
        var hidEcoPriceField = document.getElementById('hidEcoPrice');
        var hidMultQty = document.getElementById('hidMultQty');
        var hidRef = document.getElementById('hidRef');
        var hidLimit = document.getElementById('hidLimit');
        if (hidStockField)
        {
            hidStockField.value = stock;
        }
        if (hidPriceField)
        {
            hidPriceField.value = unitPrice;
        }
        if (hidEcoPriceField)
        {
            hidEcoPriceField.value = ecoPrice;
        }
        if (hidMultQty)
        {
            hidMultQty.value = multipleqty;
        }
        if (hidRef)
        {
            hidRef.value = item;
        }
        if (hidLimit)
        {
            hidLimit.value = limit;
        }
    }
    
    resetCartForm();
    
}

function hideWarning()
{
    var divError = document.getElementById('divMsg');
    if (divError && divError.style.display == 'block')
    {
        divError.style.display = 'none';
    }
}
