﻿// **************************************************************************************************************************
// Save PRODUCT RATING
// **************************************************************************************************************************

function SaveProduct2Favourites(productId, userId)
{   
    if (!ajaxLoadingData)
    {
        AjaxTransfer("http://"+GetUrlBase()+"/Services/Save_UserFavouriteProducts.ashx?productid="+productId+"&userid="+userId);
        SaveProduct2FavouritesProcess();
    }
    else
    {
        window.setTimeout("SaveProduct2Favourites('"+productId+"', '"+userId+"');", 10);
    }
}

function SaveProduct2FavouritesProcess()
{
    if (!ajaxResultReady)
    {
        window.setTimeout("SaveProduct2FavouritesProcess();", 10);
    }
    else
    {
        alert(" Produkt byl přidán do Vašich oblíbených. ");

        ajaxResultReady = false;
        ajaxLoadingData = false;
    }
}

// **************************************************************************************************************************
// Save PRODUCT RATING
// **************************************************************************************************************************

function SaveProductRating(productId, rating)
{
    // control of rating one by day
    if (MyCookie.Read("QuestionnaireOK_"+ productId.toString()) == "True") {
        alert("Produkt lze hodnotit pouze jednou za den!");
        return;
    } else {
        MyCookie.Write("QuestionnaireOK_"+ productId.toString(), "True", 1);
    }
    
    if (!ajaxLoadingData)
    {
        AjaxTransfer("http://"+GetUrlBase()+"/Services/Save_ProductRating.ashx?productid="+productId+"&rating="+rating);
        SaveProductRatingProcess(productId, rating);
    }
    else
    {
        window.setTimeout("SaveProductRating('"+productId+"', '"+rating+"');", 10);
    }
}

function SaveProductRatingProcess(productId, rating)
{
    if (!ajaxResultReady)
    {
        window.setTimeout("SaveProductRatingProcess('"+productId+"', '"+rating+"');", 10);
    }
    else
    {
        var ratingCountObj = document.getElementById("RatingCountSpan");
        var ratingObj = document.getElementById("RatingSpan");
        
        var ratingCount = parseInt(ratingCountObj.innerHTML) + 1;
        var rating = Math.round((parseInt(ratingObj.innerHTML) + parseInt(rating)) / 2);

        ratingCountObj.innerHTML = ratingCount.toString();
        ratingObj.innerHTML = rating.toString();

        ajaxResultReady = false;
        ajaxLoadingData = false;
    }
}

// **************************************************************************************************************************
// Save PRODUCT TO CART
// **************************************************************************************************************************

function SaveProduct2Cart(productId,sessionId,itemsCount,userId,productName,productPrice,showInfoMessage,onlyChangeNoAdd)
{
    if (showInfoMessage == null) { showInfoMessage = true; }
    if (onlyChangeNoAdd == null) { onlyChangeNoAdd = "0"; }

    if (!(parseInt(itemsCount) > 0))
    {
        alert("Počet produktů k objednání má nesprávný formát!");
        return;
    }
    if (!ajaxLoadingData)
    {
        AjaxTransfer("http://"+GetUrlBase()+"/Services/Save_Product2Cart.ashx?productid="+productId+"&sessionid="+sessionId+"&itemscount="+itemsCount+"&userid="+userId+"&behav="+onlyChangeNoAdd);
        SaveProduct2CartProcess(productId,sessionId,itemsCount,userId,productName,productPrice,showInfoMessage);
    }
    else
    {
        window.setTimeout("SaveProduct2Cart('"+productId+"', '"+sessionId+"', '"+itemsCount+"', '"+userId+"', '"+productName+"', '"+productPrice+"',"+showInfoMessage+",'"+onlyChangeNoAdd+"');", 10);
    }
}

function SaveProduct2CartProcess(productId,sessionId,itemsCount,userId,productName,productPrice,showInfoMessage)
{
    if (!ajaxResultReady)
    {
        window.setTimeout("SaveProduct2CartProcess('"+productId+"', '"+sessionId+"', '"+itemsCount+"', '"+userId+"', '"+productName+"', '"+productPrice+"',"+showInfoMessage+");", 10);
    }
    else
    {
        // refresh of cart preview
        RefreshCart();
        
        if (showInfoMessage)
        {
            //<input alt="#TB_inline?height=300&width=400&inlineId=myOnPageContent" title="add a caption to title attribute / or leave blank" class="thickbox" type="button" value="Show" />  
            //<a href="#TB_inline?height=155&width=300&inlineId=hiddenModalContent&modal=true" class="thickbox">Show hidden modal content.</a>
        
            
        
            // show message box about adding to cart
            var MB_message =
                "<h1>Zboží bylo přidáno do košíku</h1><br/>"+
                "<table style=\"margin: 15px;font-size:100%;\">"+
                "<tr>"+
                "   <td style=\"width: 80px;\"><b>Zboží:</b></td>"+
                "   <td>"+ productName +"</td>"+
                "</tr>"+
                "<tr>"+
                "   <td><strong>Množství:</strong></td>"+
                "   <td>"+ itemsCount.toString() +"</td>"+
                "</tr>"+
                "<tr>"+
                "   <td><strong>Cena:</strong></td>"+
                "   <td>"+ Math.round(parseInt(itemsCount) * parseFloat(productPrice)).toString() +",- Kč</td>"+
                "</tr>"+
                "</table><br />"+
                "<input type=\"button\" value=\"Pokračovat v nákupu\" onclick=\"javascript:tb_remove();\" class=\"messageBoxObjectInputButton\" style=\"margin-right: 20px;\" />"+
                " &nbsp; &nbsp; "+
                "<input type=\"button\" value=\"Přejít do košíku\" onclick=\"javascript:tb_remove();window.location='/Kosik.aspx';\" class=\"messageBoxObjectInputButton\" />";
                             
            document.getElementById("thickboxMessageElement").innerHTML = MB_message;
                             
            tb_show("", "#TB_inline?height=170&width=500&inlineId=thickboxMessageElement&modal=true", null);
            //MessageBox("globalErrorBox4JS", MB_message, MB_buttons, "zavřít");
        }
        
        // setting variables to default state
        ajaxResultReady = false;
        ajaxLoadingData = false;
    }
}

// **************************************************************************************************************************
// Remove PRODUCT FROM CART
// **************************************************************************************************************************

function RemoveProductFromCart(productId,sessionId,itemsCount)
{
    if (!ajaxLoadingData)
    {
        AjaxTransfer("http://"+GetUrlBase()+"/Services/Delete_Product2Cart.ashx?productid="+productId+"&sessionid="+sessionId+"&itemscount="+itemsCount);
        RemoveProductFromCartProcess(productId,sessionId,itemsCount);
    }
    else
    {
        window.setTimeout("RemoveProductFromCart('"+productId+"', '"+sessionId+"', '"+itemsCount+"');", 10);
    }
}

function RemoveProductFromCartProcess(productId,sessionId,itemsCount)
{
    if (!ajaxResultReady)
    {
        window.setTimeout("RemoveProductFromCartProcess('"+productId+"', '"+sessionId+"', '"+itemsCount+"');", 10);
    }
    else
    {
        //location.reload(false);
        //__doPostBack('ctl00$ContentPlaceHolder1$CartWizard$RecountCartPrices','');
        window.location = window.location;
        
        // setting variables to default state
        ajaxResultReady = false;
        ajaxLoadingData = false;
    }
}

// **************************************************************************************************************************
// Show CART PREVIEW
// **************************************************************************************************************************

function RefreshCart()
{
    if (!ajaxLoadingData)
    {
        AjaxTransfer("http://"+GetUrlBase()+"/Services/Get_CartPreview.ashx?sessionid="+actualUserSessionId);
        RefreshCartProcess();
    }
    else
    {
        window.setTimeout("RefreshCart();", 10);
    }
}

function RefreshCartProcess()
{
    if (!ajaxResultReady)
    {
        window.setTimeout("RefreshCartProcess();", 10);
    }
    else
    {
        if (document.getElementById("CartPreviewTotalPrice") != null)
        {
            document.getElementById("CartPreviewTotalPrice").innerHTML = ajaxResultsText.split(";")[0] + " Kč";
            document.getElementById("CartPreviewTotalCount").innerHTML = ajaxResultsText.split(";")[1] + " ks";
        }

        ajaxResultReady = false;
        ajaxLoadingData = false;
    }
}