﻿function sumRow(quantityCellId, totalCellId, priceCellId, productTableId, totalQuantityId, totalSumId, grossSumId, maxFreight, freightCost, freightSum)
{
    QuantityTable(productTableId, totalQuantityId);
    
    var priceCellValue = document.getElementById(priceCellId).innerHTML.replace(",",".").replace("&nbsp;","").replace(" ","");
    var rowSum = parseFloat(document.getElementById(quantityCellId).value) * parseFloat(priceCellValue);
    document.getElementById(totalCellId).innerHTML = FormatValue(rowSum.toString());

    var total = Total(productTableId, totalSumId, freightCost, false);
    if (total > maxFreight) freightCost = 0;
    Total(productTableId, totalSumId, freightCost, false)

    //Set number of decimals to two.
    rowSum = Math.round(rowSum * 100) / 100;


    document.getElementById(grossSumId).innerHTML = total;
    document.getElementById(freightSum).innerHTML = freightCost;
}

function sumRowTrade(quantityCellId, totalCellId, priceCellId, productTableId, totalQuantityId, totalSumId, grossId, vatId, maxFreight, freightCost, freightSum) {
    QuantityTable(productTableId, totalQuantityId);

    var priceCellValue = document.getElementById(priceCellId).innerHTML.replace(",", ".").replace("&nbsp;", "").replace(" ", "");
    var rowSum = parseFloat(document.getElementById(quantityCellId).value) * parseFloat(priceCellValue);
    document.getElementById(totalCellId).innerHTML = FormatValue(rowSum.toString());

    var total = Total(productTableId, totalSumId, freightCost, true);// parseFloat(document.getElementById(totalSumId).innerHTML);
    if (total > maxFreight) freightCost = 0;

    Total(productTableId, totalSumId, freightCost, true);

    var vat = (parseFloat(total) + parseFloat(freightCost)) * 0.25;
    var gross = total;

    document.getElementById(grossId).innerHTML = gross;
    document.getElementById(vatId).innerHTML = vat;
    document.getElementById(freightSum).innerHTML = freightCost;
}

function chkMax(quantityCellId, maxLimitCellId, productName, cellDose) {
   
    var maxLimit = parseFloat(document.getElementById(maxLimitCellId).innerHTML);
    var qty = parseFloat(document.getElementById(quantityCellId).value);

    if (qty >= maxLimit && maxLimit != '0') {
        var total = cellDose * qty;
        var msg = 'Du har angivit ett stort antal förpackningar av ' + productName + ', vilket ger ' + total + ' doser. \nKontrollera om antalet förpackningar är korrekt.';
        alert(msg);
    }
}

function QuantityTable(productTableId, totalQuantityId)
{
    var table = document.getElementById(productTableId);
    var tablequantity = document.getElementById(totalQuantityId); 
    tablequantity.innerHTML = "";
    var amount = 0;         
    for(i = 0; i < table.rows.length; i++)  
    {   
        if(parseInt(table.rows[i].cells[5].firstChild.value))                      
            amount = parseInt(amount) + parseInt(table.rows[i].cells[5].firstChild.value);
    }
    document.getElementById(totalQuantityId).innerHTML = amount + " st";   
}

function Total(productTableId, totalSumId, freightCost, calcVat)
{
    var table = document.getElementById(productTableId);
    var total = 0;
    var realtotal = 0;
    document.getElementById(totalSumId).innerHTML = "";
    
    for(i = 0; i < table.rows.length; i++) 
    {
        if(parseInt(table.rows[i].cells[5].firstChild.value))
        {
            var str = table.rows[i].cells[6].innerHTML;

            while(str.indexOf('&nbsp;') >= 0)
                str = str.replace('&nbsp;', '');
                total = parseFloat(total) + parseFloat(str.replace(",",".").replace(' ', ''));
        }
    }

    total = Math.round(total * 100) / 100;
    if (calcVat) {
        realtotal = (total + parseFloat(freightCost)) * 1.25;
        document.getElementById(totalSumId).innerHTML = FormatValue(realtotal.toString());
    }
    else {
        realtotal = total + parseFloat(freightCost);
        document.getElementById(totalSumId).innerHTML = FormatValue(realtotal.toString());
    }

    return total;


}


function FormatValue(str)
{
    if(str == 'NaN')
        return "0";

    if (str.toString().indexOf(".") >= 0 && str.toString().charAt(str.toString().length - 1) != '0') {
        str = Math.round(str * 100) / 100;
        str += '0';
    }
    str = str.replace(".", ",");
    return str;
}

function Disable(id)
{
    document.getElementById(id).disabled = true;
    return true;
}

function IntervetTabs() {
    $('#TradeProductsDiv').hide();
    $('#order-summary-trade').hide();

    var unselected_height = 32,
        selected_height = 34,
        $tab_Trade = $('#tab_TradeProducts').height(unselected_height),
        $tab_Vaccin = $('#tab_VaccinProducts'),
        $trade_summary = $('#trade-summary'),
        $vaccin_summary = $('#vaccin-summary');

    $('#showProducts').click(function() {
        $('#TradeProductsDiv').hide();
        $('#VaccinProductsDiv').show();
        $tab_Trade.height(unselected_height);
        $tab_Vaccin.height(selected_height);
        $trade_summary.hide();
        $vaccin_summary.show();
        $('#order-summary-trade').hide();
        $('#order-summary').show();
    });
    
    $('#showTradeProd').click(function() {
        $('#VaccinProductsDiv').hide();
        $('#TradeProductsDiv').show();
        $tab_Trade.height(selected_height);
        $tab_Vaccin.height(unselected_height);
        $trade_summary.show();
        $vaccin_summary.hide();
        $('#order-summary').hide();
        $('#order-summary-trade').show();
        $('#ProductsDivider').hide();
    });
    

    //Om man är på steg2.
    if ($('.user-info-step-two').length > 0) {
        $('#TradeProductsDiv').show();
        $('#order-summary').show();
        $('#order-summary-trade').show();
        $('#ProductsDivider').show();
        $('.IntervetDirektHeader').show();
        
    }
}


//Taget från head i masterpage
function Expand(clickid, panelid) {
    $(document).ready(function () {
        $(panelid).slideToggle("slow");
    });
}
function ChangeClass(clickid) {
    $(document).ready(function () {
        if ($(clickid).hasClass('vacciplan-vaccitable')) {
            $(clickid).addClass('vacciplan-vaccitable-clicked');
            $(clickid).removeClass('vacciplan-vaccitable');
        }
        else {
            $(clickid).addClass('vacciplan-vaccitable');
            $(clickid).removeClass('vacciplan-vaccitable-clicked');
        }
    });
}
function UpdateTotal(packId, qtyId, totalId, productId) {
    $(document).ready(function () {
        var qty = $("#" + qtyId).val();
        var pack = $("#" + packId).val();
        var total = parseInt(qty) * parseInt(pack);
        if (total.toString() == 'NaN') {
            $("#" + totalId).html("0");
        }
        else {
            $("#" + totalId).html(total);
        }
        UpdateSum(productId);
    });
}
function UpdateSum(productId) {
    $(document).ready(function () {
        var totals = $(".totalLabel" + productId).val();
        var sum = $(".sumLabel" + productId).val();
        var total = 0;
        $(".totalLabel" + productId).each(function (index) {
            current = parseInt($(this).html());
            if (current.toString() != "NaN") {
                total = parseInt(total) + parseInt(current);
            }
        });
        if (total.toString() == 'NaN') {
            $(".sumLabel" + productId).html("0");
        }
        else {
            $(".sumLabel" + productId).html(total);
        }
    });
}
function UpdateAll() {
    $(document).ready(function () {
        if (typeof (ids) != 'undefined' && ids != "") {//typeof(ids)!='undefined'
            var aProductIds = ids.split(";");
            for (i = 0; i < aProductIds.length; i++) {
                var id = aProductIds[i];
                if (id != "") {
                    //alert(id);
                    var totals = $(".totalLabel" + id).val();
                    var sum = $(".sumLabel" + id).val();
                    var total = 0;
                    $(".totalLabel" + id).each(function (index) {
                        current = parseInt($(this).html());
                        if (current.toString() != "NaN") {
                            total = parseInt(total) + parseInt(current);
                        }
                    });
                    if (total.toString() == 'NaN') {
                        $(".sumLabel" + id).html("0");
                    }
                    else {
                        $(".sumLabel" + id).html(total);
                    }
                }
            }
        }
    });
}
