﻿// JScript File
//callback function for header basket total callback
var shippingTotalProductsNo = 1;
var currentShippingItem = 1;

function BasketTotalCallback(res)
{
    //debugger
    if(res && res.value)
    {
        var totalsContainer = document.getElementById("headerBasket");
        if(totalsContainer)
        {
            totalsContainer.innerHTML = res.value;
        }
    }
}
function CalculateBasketTotals()
{
    Header.GetBasketTotal(BasketTotalCallback);
}
function SetPageCallBack(res)
{
}
function SetLastVisitedPage()
{
    MasterPage.SetLastVisitedPage(window.location.pathname,SetPageCallBack);
}
function ErrorMessageCallBack(res)
{
    var ajaxErrorDiv = document.getElementById("ajaxError");
    if(ajaxErrorDiv && res.value)
    {
        var errorUL = ajaxErrorDiv.getElementsByTagName("ul");
        if(errorUL[0])
        {
            errorUL[0].innerHTML = "";
            for(var i=0; i<res.value.length; i++)
            {
                if(res.value[i].Field != null)
                {
                    errorUL[0].innerHTML+="<li>"+res.value[i].Field+" - "+res.value[i].Message+"</li>";
                }
                else
                {
                    errorUL[0].innerHTML+="<li>"+res.value[i].Message+"</li>";
                }
            }
        }
        ajaxErrorDiv.className = ajaxErrorDiv.className.replace("hide","");
    }
}
function GetErrorMessage()
{
    if(BasketPage)
    {
        BasketPage.GetErrorMessage(ErrorMessageCallBack);
    }
}
function CouponsCallBack(res)
{
    var basketCoupons = document.getElementById("basketCoupons");
    var getErrorMessage = false;
    if(basketCoupons)
    {
        if(res && res.value)
        {
            if(res.value.length > 0)
            {
                var htmlCoupons = basketCoupons.getElementsByTagName("DIV");
                if(htmlCoupons.length > 0)
                {
                    for(var i=0; i< htmlCoupons.length; i++)
                    {
                        var div = htmlCoupons[i];
                        if(div.id != "")
                        {
                            if(!CouponExistsInCollection(div.id,res.value))
                            {
                                basketCoupons.removeChild(div);
                                getErrorMessage = true;
                            }
                        }
                    }
                }
            }
            else
            {
                basketCoupons.innerHTML = "";
                basketCoupons.className +=" hide";
                getErrorMessage = true;
            }            
        }
    }
    if(getErrorMessage)
    {
        GetErrorMessage();
    }
}
function GetBasketCoupons()
{
    var basketCoupons = document.getElementById("basketCoupons");
    if(basketCoupons && BasketPage)
    {
        BasketPage.GetRemovedCoupons(CouponsCallBack);
    }
}
function CouponExistsInCollection(couponCode, couponCollection)
{
    if(couponCollection == null || couponCollection.length == 0)
        return false;
    else
    {
        for(var i=0; i<couponCollection.length; i++)        
        {
            if(couponCollection[i].Code == couponCode)
            {
                return true;
            }
        }
    }
    return false;
}
function GetShippingTaxCallBack(res)
{
    if(res && res.value)
    {
        HideUsingClass('rnk_loader');
        RemoveIframe('rnk-shipping-popup');
        HideUsingClass('rnk-shipping-popup');
        //var outputElem = document.getElementById('rnk-ship-value');
        if(shippingTotalProductsNo == 1)
        {
            if(res.value.length > 1)
            {
                var productPageOutput = document.getElementById('tax-'+currentShippingItem);
                var prodPageLink = document.getElementById('shippLink-'+currentShippingItem);
                if(productPageOutput)
                {
                    productPageOutput.innerHTML = res.value[1]+'&nbsp;';
                }
                if(prodPageLink)
                {
                    prodPageLink.innerHTML = 'alt oras';
                }
            }
    //        if(outputElem)
    //        {
    //            outputElem.innerHTML = res.value[0];
    //        }
    //        else
    //        {
    //            alert(res.value[0]);
    //        }        
        }
        else
        {
            for(var i = 0; i<shippingTotalProductsNo; i++)
            {
                var productPageOutput = document.getElementById('tax-'+(i+1));
                var prodPageLink = document.getElementById('shippLink-'+(i+1));
                if(productPageOutput)
                {
                    productPageOutput.innerHTML = res.value[i]+'&nbsp;';
                }
                if(prodPageLink)
                {
                    prodPageLink.innerHTML = 'alt oras';
                }
            }
        }
    }
    shippingTotalProductsNo = 1;
}
function GetShippingTax(cityDDL, stateDDLId, productWeight)
{
    var weigths = productWeight.split("^");
    shippingTotalProductsNo = weigths.length;
    var stateDDL = document.getElementById(stateDDLId);
    if(typeof Header != "undefined" && Header != null && cityDDL != null && stateDDL != null)
    {
        if(cityDDL.options[cityDDL.selectedIndex].value > 0 && stateDDL.options[stateDDL.selectedIndex].value)
        {
            ShowUsingClass('rnk_loader');
            var cityId = cityDDL.options[cityDDL.selectedIndex].value;
            var cityName = cityDDL.options[cityDDL.selectedIndex].text;
            var stateId = stateDDL.options[stateDDL.selectedIndex].value;
            var stateName = stateDDL.options[stateDDL.selectedIndex].text;
            if(shippingTotalProductsNo == 1)
            {
                Header.GetShippingTotal(stateId, stateName, cityId, cityName, productWeight, GetShippingTaxCallBack);
            }
            else
            {
                Header.GetMultiShippingTotal(stateId, stateName, cityId, cityName, weigths, GetShippingTaxCallBack);
            }            
        }
    }
}
