﻿var PurchaseOptionBar;
var PurchaseOptionBarDisabled;
$(document).ready(function(){

    GetUserShoppingList();
    var cookieValue = $.cookie('UserShoppingListCookie');
    var cookieItemId;
    var btnShowId;
    var btnDisableId;

    if(cookieValue != null){
        cookieValue = cookieValue.split("&");
        for(var i=0;i<cookieValue.length;i++)
        {
            PurchaseOptionBar           = "PurchaseOptionBar_";
            PurchaseOptionBarDisabled   = "PurchaseOptionBarDisabled_";
            
            btnShowId       = "cart_";
            btnDisableId    = "cartDisable_";
            
            cookieItemId    = cookieValue[i].substring(cookieValue[i].indexOf("="),cookieValue[i].indexOf(","));
            
            PurchaseOptionBar           = PurchaseOptionBar + cookieItemId;
            PurchaseOptionBarDisabled   = PurchaseOptionBarDisabled + cookieItemId;
            
            btnShowId       = btnShowId + cookieItemId
            btnDisableId    = btnDisableId + cookieItemId
            $("#"+btnShowId).hide();
            $("#"+btnDisableId).show();
            
            $("#"+PurchaseOptionBarDisabled).hide();
            $("#"+PurchaseOptionBar).show();
        }
    }
    
     $(".ExamsList .ExamItems").hover(
          function (){
            $(this).addClass("ExamItemsOpen");
            $(this).removeClass("ExamItemsClose");
            $(this).next().show();
          },
          function (){
          }
        );
    
    $(".ExamsList .GetMoreExam").hover(
          function (){
          },
          function (){
            $(this).prev().removeClass("ExamItemsOpen");
            $(this).prev().addClass("ExamItemsClose");
            $(this).hide();
          }
        );

    
});


function HidePurchaseStatusBar()
{
    $("#AdToShoppingCartMsg").fadeOut();
}

function AddItemToShoppingCart(id , operation)
{
    var cartBtn = "cart_" + id;
    var cartDisableBtn = "cartDisable_" + id;
    $.post(baseurl + 'Content/Handlers/UpdateShoppingCart.ashx',
    { 
       itemId: id,
       operation:operation
    },
    function(data)
    {
        if(data != "")
        {
            $("#"+cartBtn).hide();
            $("#"+cartDisableBtn).show();
            GetUserShoppingList();
            ShowPurchaseStatusBar();
            // AFTER USER ADDED AN ITEM TO THE SHOPPING CART WE NEED TO ENABLE PURCHASEOPTIONBAR BUTTONS
            // AND TO HIDE THE "ADD TO SHOPPING CART" BUTTON
            $("#PurchaseOptionBar_"+id).show();
            $("#PurchaseOptionBarDisabled_"+id).hide();
            $(".HPShoppingItemsCounter").html('<span class="HPShoppingItemsCounter">('+data+' פריטים)</span>');
        }
    });
}
function ShowPurchaseStatusBar()
{
   $("#AdToShoppingCartMsg").fadeIn("slow");
   var mytime=setTimeout('HidePurchaseStatusBar()',3000);
}

function GetUserShoppingList()
{
    $.post(baseurl + 'Content/Subviews/ShoppingCartItems.aspx',
    {},
    function(data)
    {
        if(data != "")
        {
            $($(".ShoppingCartBlock").get(0)).html(data).animate({}, 'slow' );
        }
    });
}

function showAdditionalValues(index)
{
    $('#ParameterOptions' + index + ' .ParameterOptionHidden').show();
}

function addSearchWord()
{
    var location = window.location.toString();
    var newValue = encodeURI($('#txtSearchWord').val());
    if (window.location.search.indexOf('q=') > 0)
    {
        var location = window.location.toString();
        var qIndex = location.indexOf("q=");
        var qIndexEnd = location.indexOf("&", qIndex + 1);
        if (qIndexEnd > 0)
        {
            window.location = location.substring(0, qIndex + 2) + newValue + location.substring(qIndexEnd);
        }
        else
        {
            window.location = location.substring(0, qIndex + 2) + newValue;
        }
    }
    else
    {            
        window.location = location + (window.location.search.length == 0 ? "?" : "&") + "q=" + newValue;
    }
}
function SaveAgent()
{
    var url = baseUrl  + "Users/SaveAgent.aspx";
   // $("#FilterForm").append("<input type='hidden' name='boardId' value='" +  boardid  + "' />");
    $("#form1").attr("action",url);
    document.forms['FilterForm'].submit();
}























//function showAdditionalValues(index)
//{
//    $('#ParameterOptions' + index + ' .ParameterOptionHidden').show();
//}

//function addSearchWord()
//{
//    var location = window.location.toString();
//    var newValue = encodeURI($('#txtSearchWord').val());
//    if (window.location.search.indexOf('q=') > 0)
//    {
//        var location = window.location.toString();
//        var qIndex = location.indexOf("q=");
//        var qIndexEnd = location.indexOf("&", qIndex + 1);
//        if (qIndexEnd > 0)
//        {
//            window.location = location.substring(0, qIndex + 2) + newValue + location.substring(qIndexEnd);
//        }
//        else
//        {
//            window.location = location.substring(0, qIndex + 2) + newValue;
//        }
//    }
//    else
//    {            
//        window.location = location + (window.location.search.length == 0 ? "?" : "&") + "q=" + newValue;
//    }
//}    

function ContentHandler(id,type)
{
    
    if(confirm("?האם אתה בטוח"))
    {
          $.post('Handlers/ContentHandler.ashx',
            {
                contentId : id,
                ProcessType : type
            },
            function(data)
            {
                if (data =='true')
                {
                    if (type == "Delete")
                        $('.Content' + id).remove();
                    location.reload();
                }
            }
        );
    }

}
