$(document).ready(function() {

var sitename =$("#siteurl").val();

    $('#btn_wish').on('click', function (e) {
        e.preventDefault();
        var userId =  $('#USER_ID').val();
        var sitename =$("#siteurl").val();
//alert(userId);
        if(userId=='')
            {

                            window.location.href=sitename+"/login.php";
            }
                       else{
                                $.confirm({
                                    title: MSG_CONFIRM_TITLE,
                                    content: MSG_ADD_TO_FAVOURITES_CONFIRMATION,
                                    confirmButton: MSG_CONFIRM_YES,
                                    confirmButtonClass: 'btn-info',
                                    cancelButton: MSG_CONFIRM_NO,
                                    cancelButtonClass: 'btn-info',
                                    icon: 'fa fa-warning',
                                    animation: 'top',
                                     confirm: function () {
                                                addtoFavouriteslist();
                                            }
                                });
                       }
                            });


 // Add sellers to favourites list

  $('#btn_favourites').on('click', function (e) {

                             e.preventDefault();
        var userId =  $('#USER_ID').val();
        var sitename =$("#siteurl").val();
//alert(userId);
        if(userId=='')
            {

                            window.location.href=sitename+"/login.php";
            }
                       else{
                                $.confirm({
                                    title: MSG_CONFIRM_TITLE,
                                    content: MSG_SELLER_ADD_TO_FAVOURITES_TEXT,
                                    confirmButton: MSG_CONFIRM_YES,
                                    confirmButtonClass: 'btn-info',
                                    cancelButton: MSG_CONFIRM_NO,
                                    cancelButtonClass: 'btn-info',
                                    icon: 'fa fa-warning',
                                    animation: 'top',
                                     confirm: function () {
                                                addsellertoFavouriteslist();
                                            }
                                });
                              }
                            });



   // Fucntion to check the seller account activated or not 
$('.activate_seller').on('click', function (e) {

                           
                 e.preventDefault();
                                $.confirm({
                                    title: MSG_CONFIRM_TITLE,
                                    content: MSG_ACTIVATE_SELLER_ACCOUNT_CONFIRM,
                                    confirmButton: MSG_CONFIRM_YES,
                                    confirmButtonClass: 'btn-info',
                                    cancelButton: MSG_CONFIRM_NO,
                                    cancelButtonClass: 'btn-info',
                                    icon: 'fa fa-warning',
                                    animation: 'top',
                                     confirm: function () {
                                       
                                                window.location.href=sitename+"/activateselleraccount.php";
                                            }
                                });
                            });

// Function to check the seller is approved and re-direct to seller dashboard

  // Product Subscription Status Updation

   $(".jqSellerRatingCancel").click(closeSubscription);

    $(".jqSubscriptionUpdate").click(submitProductSubscription);


 });


function validateAccount(sellerid,status,delstatus)
{
     var sitename    =   $("#siteurl").val();
   
    if(status=='NN')
        {
                                    $.alert({
                                    title: '',
                                    content: MSG_SELLER_NOT_APPROVED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                               
        }
        if(status=='N')
        {
           
                                    $.alert({
                                    title: '',
                                    content: MSG_SELLER_NOT_APPROVED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                               
        }
       else if(delstatus=='Y')
        {
                                    $.alert({
                                    title: '',
                                    content: MSG_SELLER_ACCOUNT_DELETED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                               
        }else if(status=='Y'){
            window.location.href                =       sitename+'/sellers/activate_seller_session.php?sellerid='+sellerid;
        }
        else{
             
                //  window.location.href                =       sitename+'/sellers/activate_seller_session.php?sellerid='+sellerid;
       
        }
}
function closeSubscription()
{
    //$("#jqSellerRatingPop").fadeOut("slow");
    //$("#txtShareEmail").val("");
    return false;
}
// Product Subscription status Updation
function submitProductSubscription()
{
   
    var productid                       = 	$("#jQproductId").val();
    var product_option_id               = 	$("#jQproductOptionId").val();
    var txtQty                          =       $("#jQQuanity").val();
    var txtSubscriptionPeriod           =       $("#jQSubscriptionPeriod").val();
    var txtSubscriptionShippingVal      =       $("#txtSubscriptionShipPrice").val();
    var siteurl                         =       $("#siteurl").val();
    var txtSubscriptionShipName         =       $("#txtSubscriptionShipName").val();
    var action='subscription';
    var sitename                        =       $("#siteurl").val();
var dataString = {"action":action,"productId":productid,"product_option_Id":product_option_id,"subsciption_ship_price":txtSubscriptionShippingVal,"subscription_ship_name":txtSubscriptionShipName};

    $.ajax({

        url		:sitename +"/utils.php",

        type		:"POST",

        data		:dataString,

        dataType        : "json",

           success: function(data) {

                    var flag =data.flag;
                   
                       if(flag=='exists')
                      {
                        
                           $.alert({
                                    title: '',
                                    content: MSG_CART_ITEM_EXISTS,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                                 window.location.href                =       siteurl+'/viewcart.php';
                     }else{
                             
                             window.location.href                =       siteurl+'/homepage_addtocart.php?txtSubscriptionFlag=Y&productid='+productid+'&product_option_id='+product_option_id+'&txtQty='+txtQty+'&txtSubscriptionPeriod='+txtSubscriptionPeriod;
                          }
                  
              }


    });

  
   
}

// End Updation

// Add Item to Wish List
function cartconfirm(message, callback) {

    // var userId     =  $('#USER_ID').val();

    // var sitename   =  $("#siteurl").val();


          //   if(parseInt(userId)>0)
          //       {
                              $('#confirm').modal({
                               closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
                               position: ["80%",],
                               overlayId: 'confirm-overlay',
                               containerId: 'confirm-container',
                               onShow: function (dialog) {
                                       var modal = this;

                                       $('.message', dialog.data[0]).append(message);

                                       // if the user clicks "yes"
                                       $('.yes', dialog.data[0]).click(function () {
                                               // call the callback

                                               if ($.isFunction(callback)) {
                                                       callback.apply();
                                               }
                                               // close the dialog
                                               modal.close(); // or $.modal.close();
                                       });
                               }
                       });
                /* }
                 else
                     {
                         window.location.href=sitename+"/login.php";
                     }*/
}




// Add item to favourites list if not yet added
function addtoFavouriteslist(){

var userId =  $('#USER_ID').val();

var sitename =$("#siteurl").val();

var productId =parseInt($("#product_id").val());

var product_option_Id =     $("#product_option_id").val();
var product_seller_Id   =   $("#PRODUCT_SELLER_ID").val();
var action='favourites';

var dataString = {"action":action,"userId":userId,"productId":productId,"product_option_Id":product_option_Id,"sellerId":product_seller_Id};

    $.ajax({

        url		:sitename +"/utils.php",

        type		:"POST",

        data		:dataString,

        dataType        : "json",

           success: function(data) {

                  var flag =data.flag;
                    
                      if(flag=='SUCCESS')
                      {
                         // CheckWishCount();
                         // popup('div_popupmessage', 'pcm_messageboxo_popup_div','Item Added to Wish List');
                         // alert("Item Added to Wish List");
                           $.alert({
                                    title: '',
                                    content: MSG_FAVOURITES_ADDED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                           $("#content").hide();
                      }else if(flag=='exists')
                      {
                          $.alert({
                                    title: '',
                                    content: MSG_FAVOURITES_ITEM_EXISTS,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                                
                           $("#content").hide();
                      }else if(flag=='notlogin')
                      {
                           $.alert({
                                    title: '',
                                    content: MSG_LOGIN_INFO,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                           window.location.href=sitename+"/login.php";
                      }
                      else if(flag=='nopermission')
                      {
                           $.alert({
                                    title: '',
                                    content: MSG_FAVORITES_NO_PERMISSION,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                          // window.location.href=sitename+"/login.php";
                      }


                  return false;
              }


    });
}


// Function to check the whether the quanity is updated in cart

function checkOutCart()
    {
            var QuantityCount           = 0;

            var StockAvailable          = 0;

        $(".clsQuantity").each(function (){

            var cartID  =  $(this).val();

            var cartQuantity            =   $("#hdQty_"+cartID).val();

            var changedcartQuantity     =   $(".clstxtQty_"+cartID).val();

            var currentStock            =   $("#currentStock_"+cartID).val();

           // alert("cartquantiy"+cartQuantity+"- cahnged quantity "+changedcartQuantity);

            if(parseInt(cartQuantity) != parseInt(changedcartQuantity)) {
                QuantityCount = QuantityCount+1;

                $("#tr_"+cartID).addClass('trbg-color');
            }
            else if(parseInt(cartQuantity)>parseInt(currentStock)){
                 StockAvailable =  StockAvailable+1;
             // Highligt the stock not available row in listing, display available stock
              $("#tr_"+cartID).addClass('trbg-color');

              $("#trhide_"+cartID).removeClass('nodisplay');


              $("#trhide_"+cartID).addClass('display bg-hidecolor');


              $("#trhide_"+cartID).html(currentStock + " Item(s)in Stock ");


            }

            });

        if(QuantityCount>0)
        {
            alert("Please update the quantity of highlighted items before you proceed");
            return false;
        }
        else if(StockAvailable>0)
        {
            alert("Sorry stock not available ! Please update the quantity or remove  highlighted items from basket before you proceed");
            return false;
        }
        else
        {
            return true;
        }




    }

// End function


function popup(divId,divContentId,message)
{

  var windowWidth             = $(window).width();
    var windowHeight            = $(window).height();
    var popupHeight             = $("#"+divId).height();
    var popupWidth              = $("#"+divId).width();
    var pageScrollTop           = document.documentElement.scrollTop ?
document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop
: 0);
    var addCommentBoxTop        = pageScrollTop + (windowHeight - popupHeight) /2;
    var addCommentBoxLeft       = (windowWidth - popupWidth) / 2;

$("#"+divContentId).html(message);

    $("#"+divId).css({
                    position : "absolute",
                    left : addCommentBoxLeft,
                    top: addCommentBoxTop
                }).fadeIn("slow");

      return false;
}

function closepopup(divId)
{
    $("#"+divId).fadeOut("slow");
      return false;
}

//function to move favourite item to cart

function addfavouriteitemtoCart(productId,product_option_Id,wishId){

var userId =  $('#USER_ID').val();

var sitename =$("#siteurl").val();


var action='checkstock';

var dataString = {"action":action,"userId":userId,"productId":productId,"product_option_Id":product_option_Id};

    $.ajax({

        url		:sitename +"/utils.php",

        type		:"POST",

        data		:dataString,

        dataType        : "json",

           success: function(data) {

                  var flag =data.flag;
                    
                      if(flag=='SUCCESS')
                      {
                          window.location.href=sitename+"/homepage_addtocart.php?productid="+productId+"&product_option_id="+product_option_Id+"&wishId="+wishId;
                      }else if(flag=='ERROR')
                      {
                        $.alert({
                                    title: '',
                                    content: MSG_STOCK_LESS,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                      
                          
                      }else if(flag=='notlogin')
                      {
                         
                           window.location.href=sitename+"/login.php";
                      }

                  return false;
              }


    });

}
// function to delete favourites items 

function deletefavouriteitems(wishId){
                    $.confirm({
                                    title: MSG_CONFIRM_TITLE,
                                    content: MSG_DELETE_FAVOURITES_ITEMS_CONFIRMATION,
                                    confirmButton: MSG_CONFIRM_YES,
                                    confirmButtonClass: 'btn-info',
                                    cancelButton: MSG_CONFIRM_NO,
                                    cancelButtonClass: 'btn-info',
                                    icon: 'fa fa-warning',
                                    animation: 'top',
                                     confirm: function () {
                                                deletefavouriteitemslist(wishId);
                                            }
                                });

}
// Delete favourites db opreations 
function deletefavouriteitemslist(wishId){

var userId =  $('#USER_ID').val();

var sitename =$("#siteurl").val();


var action='deletefavouritelist';

var dataString = {"action":action,"userId":userId,"wishId":wishId};

    $.ajax({

        url		:sitename +"/utils.php",

        type		:"POST",

        data		:dataString,

        dataType        : "json",

           success: function(data) {

                  var flag =data.flag;
                    
                      if(flag=='SUCCESS')
                      {
                          $.alert({
                                    title: '',
                                    content: MSG_FAVOURITES_ITEMS_DELETED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                                $('#jQwish'+wishId).remove();
                          //window.location.href=sitename+"/myfavourites.php?productid="+productId+"&product_option_id="+product_option_Id+"&wishId="+wishId;
                      }else if(flag=='ERROR')
                      {
                        $.alert({
                                    title: '',
                                    content: MSG_STOCK_LESS,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                      
                          
                      }else if(flag=='notlogin')
                      {
                         
                           window.location.href=sitename+"/login.php";
                      }

                  return false;
              }


    });
}



// Add seller to favourites list if not yet added
function addsellertoFavouriteslist(){

var sellerId =  $('#SELLER_ID').val();

var userId  =   $('#USER_ID').val();

var sitename =$("#siteurl").val();

var action='sellerfavourites';

var dataString = {"action":action,"userId":userId,"sellerId":sellerId};

    $.ajax({

        url		:sitename +"/utils.php",

        type		:"POST",

        data		:dataString,

        dataType        : "json",

           success: function(data) {

                  var flag =data.flag;

                      if(flag=='SUCCESS')
                      {
                         // CheckWishCount();
                         // popup('div_popupmessage', 'pcm_messageboxo_popup_div','Item Added to Wish List');
                         // alert("Item Added to Wish List");
                           $.alert({
                                    title: '',
                                    content: MSG_SELLER_FAVOURITES_ADDED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                           $("#content").hide();
                      }else if(flag=='exists')
                      {
                          $.alert({
                                    title: '',
                                    content: MSG_FAVOURITES_SELLER_EXISTS,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });

                           $("#content").remove();
                      }else if(flag=='notlogin')
                      {
                           $.alert({
                                    title: '',
                                    content: MSG_LOGIN_INFO,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                           window.location.href=sitename+"/login.php";
                      }



                  return false;
              }


    });
}



// Delete favourite sellers Confirmation

function deletefavouritesellerentry(wishId){
                    $.confirm({
                                    title: MSG_CONFIRM_TITLE,
                                    content: MSG_DELETE_FAVOURITES_SELLER_CONFIRMATION,
                                    confirmButton: MSG_CONFIRM_YES,
                                    confirmButtonClass: 'btn-info',
                                    cancelButton: MSG_CONFIRM_NO,
                                    cancelButtonClass: 'btn-info',
                                    icon: 'fa fa-warning',
                                    animation: 'top',
                                     confirm: function () {
                                                deletefavouritesellers(wishId);
                                            }
                                });

}


// Delete Favourite seller db action


function deletefavouritesellers(wishId){

var userId =  $('#USER_ID').val();

var sitename =$("#siteurl").val();


var action='deletefavouritesellers';

var dataString = {"action":action,"userId":userId,"wishId":wishId};

    $.ajax({

        url		:sitename +"/utils.php",

        type		:"POST",

        data		:dataString,

        dataType        : "json",

           success: function(data) {

                  var flag =data.flag;

                      if(flag=='SUCCESS')
                      {
                          $.alert({
                                    title: '',
                                    content: MSG_FAVOURITES_SELLER_DELETED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });
                                $('#jQwish'+wishId).remove();
                          //window.location.href=sitename+"/myfavourites.php?productid="+productId+"&product_option_id="+product_option_Id+"&wishId="+wishId;
                      }else if(flag=='ERROR')
                      {
                        $.alert({
                                    title: '',
                                    content: MSG_FAVOUURITES_DELETED,
                                    confirmButton: 'okay',
                                    confirmButtonClass: 'btn-info',
                                    animation: 'bottom',
                                    icon: 'fa fa-check',
                                    backgroundDismiss: true
                                });


                      }else if(flag=='notlogin')
                      {

                           window.location.href=sitename+"/login.php";
                      }

                  return false;
              }


    });
}


// Product Image Zoom

