﻿function HideShowItemPrice( cbOptionId, dPriceDivId, dTextDivId, dPlanSectionId, price )
{
    var oOptionCb = document.getElementById(cbOptionId);
    var oPriceDiv = document.getElementById(dPriceDivId);
    var oTextDiv = document.getElementById(dTextDivId);
    
    if( oOptionCb != null && oPriceDiv != null && oTextDiv != null )
    {
        if( oOptionCb.checked )
        {
            oPriceDiv.innerHTML = Number(price);
            oTextDiv.innerHTML = Number(price).numberFormat("0.00 $").replace(".",",");
        }
        else
        {
            oPriceDiv.innerHTML = "0";
            oTextDiv.innerHTML = "";
        }
    }
    
    UpdatePlanPrice(dPlanSectionId);
    UpdateCartPrices();
}

function HideShowItemPriceQuantity( cbOptionId, dPriceDivId, dTextDivId, dPlanSectionId, price, dQtyDdlId )
{
    var oOptionCb = document.getElementById(cbOptionId);
    var oPriceDiv = document.getElementById(dPriceDivId);
    var oTextDiv = document.getElementById(dTextDivId);
    var oQtyDdlId = document.getElementById(dQtyDdlId);
    
    if( oOptionCb != null && oPriceDiv != null && oTextDiv != null && oQtyDdlId != null )
    {
        if( oOptionCb.checked )
        {
            var dTotal = Number(price) * Number(oQtyDdlId.value);
            oPriceDiv.innerHTML = Number(dTotal);
            oTextDiv.innerHTML = Number(dTotal).numberFormat("0.00 $").replace(".",",");
            oQtyDdlId.disabled = false;
        }
        else
        {
            oPriceDiv.innerHTML = "0";
            oTextDiv.innerHTML = "";
            oQtyDdlId.disabled = true;
        }
    }
    
    UpdatePlanPrice(dPlanSectionId);
    UpdateCartPrices();
}

function UpdatePlanPrice(dPlanSectionId)
{
    var prices = $$("#" + dPlanSectionId + " .price");
    var subtotal = 0;
    
    for(var nIdx=0; nIdx < prices.length; nIdx++)
    {
        if( prices[nIdx].innerHTML != "" && !isNaN(prices[nIdx].innerHTML) )
            subtotal += Number(prices[nIdx].innerHTML);
    }
    
    $$("#" + dPlanSectionId + " .divSousTotal")[0].innerHTML = subtotal.numberFormat("0.00$").replace(".",",");
    $$("#" + dPlanSectionId + " .divSousTotalNum")[0].innerHTML = subtotal;
}

function UpdateCartPrices()
{
    var subtotal = 0;
    var subtotals = $$(".divSousTotalNum");
    for(var nIdx=0; nIdx < subtotals.length; nIdx++)
    {
        if( subtotals[nIdx].innerHTML != "" && !isNaN(subtotals[nIdx].innerHTML) )
            subtotal += Number(subtotals[nIdx].innerHTML);
    }

    $$("#soustotal")[0].innerHTML = subtotal.numberFormat("0.00 $").replace(".",",");
    
    subtotal += 25;
    $$("#soustotal2")[0].innerHTML = subtotal.numberFormat("0.00 $").replace(".",",");
    
    var tps = subtotal * 0.05;
    tps = Math.ceil(tps * 100) / 100;
    $$("#tps")[0].innerHTML = tps.numberFormat("0.00 $").replace(".",",");
    
    subtotal += tps;
    var tvq = subtotal * 0.075;
    tvq = Math.ceil(tvq * 100) / 100;
    $$("#tvq")[0].innerHTML = tvq.numberFormat("0.00 $").replace(".",",");
    
    subtotal += tvq;
    
    $$("#grandtotal")[0].innerHTML = subtotal.numberFormat("0.00 $").replace(".",",");
}