var RemoveItemOnDel = false;
var RemoveTROnDel = false;
var WlstProdId = 0;
var WlstPts = 0;
var WlstTbl;
var WlstMinPts =  0;
var WlstPay = 0;
var ShowDlg = 0;
var ResetDlg = true;
var FndTbl = false;
var WlstVal = "";
var FindTbl = false;

$(function() {

   var ShowWlstBtn=function(pay_table) {$(pay_table).find("tr:eq(3)").show();$(pay_table).find("tr:eq(0), tr:eq(1), tr:eq(2)").hide(); $(pay_table).find(".slider").hide();} 

   $(".wlst-pay-table").each(function(){
        var PayTbl = $(this);
        var DelWlst = $(this).find(".btn-wlst-del"); 
        var ProdId = $(this).find("input[name='productid']"); 
        ShowWlstBtn(PayTbl);
       
        $(DelWlst).click(function(){
            var WlstFld = $(".wlst-data"); 
            $(WlstFld).val($(ProdId).val());           
        });   
           
   }); 
   

    $("#dialog-wlst-points").dialog({
        bgiframe: true,autoOpen: false,resizable: false,height:275,width:400,modal: true,
        overlay: {backgroundColor: '#000',opacity: 0.5},
        open: function() {
            SetDialogStyle(this);
            var wst =  $(this).find(".dialog-btn-wishlist");
            var crt =  $(this).find(".dialog-btn-wishlist-cart");
            $(wst).click(function(){
                 $("#dialog-wlst-points").dialog('close');
                 AddToWlst();
            })
            $(crt).click(function(){
                $("#dialog-wlst-points").dialog('close');
                AddToCrtWlst(); 
            })
        }
    });

    $("#dialog-wlst-error").dialog({
        bgiframe: true,autoOpen: false,resizable: false,height:275,width:400,modal: true,
        overlay: {backgroundColor: '#000',opacity: 0.5},
        open: function() {
            SetDialogStyle(this);
            var btn = $(this).find(".dialog-btn-error");
            $(btn).click(function(){
                $("#dialog-wlst-error").dialog('close');
            })
       }
    })   
   
});

var ToggleWlstBtn=function(pay_table) {
    if (FndTbl)
    {
        var ProdId;
        $(".point-pay-table").each(function(){
            ProdId = $(this).find("input[name='productid']");
            if (parseInt(ProdId.val()) == parseInt(WlstProdId)) { pay_table = $(this); FindTbl = false; }   
        }); 
   } 

    var InWlst = $(pay_table).find(".btn-in-wlst");
    var AddWlst =  $(pay_table).find(".btn-add-wlst");
    $(InWlst).css("display", "inline");
    (TglWsltBtn)?$(AddWlst).css("display", "none"):$("#inline-wlst-added").show();
};

var AddWlstItem = function(Req,PayTbl) {
    $.ajax({ 
        type: "POST",
        url: "../default.aspx/AddWishlistItem",
        data: "{'WishlistProduct':" + JSON.stringify(Req) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(Result) { 
            var Response = Result.Response;
            switch(Result.ResponseText){
                case "AddedWishlistItem":
                    (ResetDlg)?ShowDlg = 0:ShowDlg++; 
                    ToggleWlstBtn(PayTbl);
                    break;
             case "ErrorWishlistItem":
                    (ResetDlg)?ShowDlg = 0:ShowDlg++; 
                    $("#dialog-wlst-error").dialog('open');
                    break;
            }
        },
        failure: function() {
            (ResetDlg)?ShowDlg = 0:ShowDlg++; 
           alert("abc"); 
            $("#dialog-wlst-error").dialog('open');
        },
         error: function(xhr, thrownError) {
               alert(xhr.responseText)         
            (ResetDlg)?ShowDlg = 0:ShowDlg++; 
            $("#dialog-wlst-error").dialog('open');
        } 
    });
};

function AddToWlst(){
   if (WlstVal.length > 0){ 
        var arr1 = WlstVal.split("~");
        for(i = 0; i < arr1.length; i++){
            var arr2 = arr1[i].split("|");
            var ReqItem = new Object(); 
            ReqItem.ProductId = parseInt(arr2[0]);
            ReqItem.Points = arr2[1];  
            WlstPts =  arr2[1];
            AddWlstItem(ReqItem, WlstTbl); 
	   }
    }
};

function AddToCrtWlst(){
   if (WlstVal.length > 0)
   { 
        var arr1 = WlstVal.split("~");
        for(i = 0; i < arr1.length; i++){
            var arr2 = arr1[i].split("|");
            
            var Req = new Object();
            Req.ProductId = parseInt(arr2[0]);
            Req.Quantity = 1;
            Req.Points = arr2[1];
            Req.Pay = 0;
            Req.VPP = true;
            AddCartItem(Req,"p",true,null,null,WlstMinPts);
	   }
    }
};



