var discountTimes = 0, form = null, scroll=0; 

function productValidation() {
	var errors = [];
	$('.requiredp').each(function() {
	    var $this = $(this);
	    if(!$this.val()) { 
	    	$this.addClass('has-error');  
	    	errors.push($this.attr("data-error-message"));
	    }else{
	    	$this.removeClass('has-error'); 	 
	    }
	});
    return errors;
}

//added by Surya
function showStripeButton(){ 
	$("#paypalSubmitButton").hide();
	$("#stripeSubmitButton").show();
}

//added by Surya
function showPaypalButton(){ 
	$("#paypalSubmitButton").show();
	$("#stripeSubmitButton").hide();
}
$("#paypal_mobilebtn,#paypal_mobtn").click(function(){
    
    var product_1_id = $("#product_1_id").val();

	if(product_1_id == ""){
		var errors = productValidation();
		if(errors.length) { 
			error_handler(errors); 
			return false;
		}	
	} 
	//$('input[name=product_1_id]').val($(this).data('id')); 
	 updatePopupOrderDetails(discountTimes); 
	 //$('#paypalPopup').fadeIn();
	$('#stripePopup').fadeIn();
	 showPaypalButton(); //added by Surya
});


		
$("#paypalDetails1").click(function(){ 
    //$('input[name=product_1_id]').val($(this).data('id')); 
	 updatePopupOrderDetails(discountTimes); 
	  $('#stripePopup').fadeIn(); 
	  
	  showPaypalButton(); //added by Surya
  });

$("#paypalDetails2").click(function(){ 
	///$('input[name=product_1_id]').val($(this).data('id'));
	updatePopupOrderDetails(discountTimes); 
	$('#stripePopup').fadeIn(); 
	showPaypalButton(); //added by Surya
});

$("#paypalDetails3").click(function(){  
	//$('input[name=product_1_id]').val($(this).data('id')); 
	 updatePopupOrderDetails(discountTimes); 
	 $('#stripePopup').fadeIn(); 	 
	 showPaypalButton(); //added by Surya
});

		// $('.removePack').on('click', function() {
		// 	var selectedPack = $(this).closest('.acto_package');
		// 	 selectedPack.removeClass('active');
		// 	$('input[name=product_1_id]').val("");
        //     $("#product_6_qty").val("");
		// 	$('.cta-btn').html('<i class="fa fa fa-mouse-pointer" aria-hidden="true"></i> ADD TO CART');
		// 	$(this).hide();
		// 	updatePopupOrderDetails(discountTimes);
		// }); 
		
        //$('.selectPack').on('click', function() {
		$('.acto_package').on('click', function() {
			//var selectedPack = $(this).closest('.acto_package');
            var product_id = $(this).data('id');
            var product_qty = $(this).data('qty');
            $('input[name=product_1_id]').val(product_id);
            $("#product_6_qty").val(product_qty);
            $('.acto_package').removeClass('active');
            $(this).addClass('active');  
			$('.cta-btn').html('<i class="fa fa fa-mouse-pointer" aria-hidden="true"></i> SELECT');
			$(this).find('.cta-btn').text('SELECTED'); 
			
			//selectedPack.find('.removePack').show();
			
            updatePopupOrderDetails(discountTimes); 
            console.log("scroll:"+scroll);
			if(scroll==1){
				if (isMobileDevice()) {
						$('html, body').animate({
							scrollTop: $('.popupBtn_mob').offset().top-parseInt(100)
						}, 100);        
				}
			}
			scroll = 1;
        }); 

        /* Paypal Popup */ 
            //$('.pop-open').click(function(e) {
            //$('#paypalPopup').fadeIn();
            //});
            
            //$('.pop-close').click(function(e) {
             //  $('#paypalPopup').hide();
             //  $('#paypalpop').val(0);
           //});

            //$('.pop-edit').click(function(e) {
              //  $('#paypalPopup').hide();
            //}); 

        function submitPaypal() {
            //we skip form validation with this step
            removeRequiredFromCardFields();
            removeRequiredFromShipFields();
            
            /*
			if($('#paypalpop').val() == 0){
            $('#paypalPopup').fadeIn();
            $('#paypalpop').val(1);
            //return false;
            }
        
            if($('#paypalpop').val() == 1){
                $('#paypalPopup').hide();  
            }
			*/
            
            $('[name=payment_method]').val('PAYPAL'); 
			//$('#paypalPopup').hide();
			$('#stripePopup').hide(); //added by Surya
            SubmitOneStepCheckoutForm('orderform');

         }
         

         function updatePopupOrderDetails(discountTimes=0){ 
                let obj =  $('.acto_package.active');
                var reg_price   =  obj.length ? (obj.data('price') || 0) : 0;
                var buy         =  obj.length ? (obj.data('price') || 0) : 0;
                var buy_each    =  obj.length ? (obj.data('each-price') || 0) : 0; 
                var saved       =  obj.length ? (obj.data('price') || 0) : 0;
                var ship_price  =  obj.length ? (obj.data('shipping') || 0) : 0;  
                var qty         = obj.length ? (obj.data('qty') || 0) : 0; 
                var product_name = obj.length ? (obj.data('product-name') || 0) : 0; 
                //var jp_price    = $('input[name=bump_product_1_each_price]').val();

                var product_4_id=$("#gift_btn").data("id");

                $("#product_3_qty").val(qty);
                
                var total_jp_price = 0; 
			
                 //Code for stripe upsell popup
                 var additional_id = $("#additional").val();
                 var warranty_plan = $("#warranty_plan").val();
                 var add_rescue_price=0;
                 var warranty_plan_price=0;
                 var total_upsell_price = 0;
             
             
                 if(additional_id !="" || warranty_plan !=""){
                     if(additional_id !=""){
                     add_rescue_price = funnelData[additional_id].price;
                     var add_rescue_name = funnelData[additional_id].product_name;
                     $(".pop_add").html(add_rescue_name);
                     $(".pop_add_price").html('$'+ add_rescue_price.toFixed(2));
                     }
                     else{
                        add_rescue_price = 0;  
                     }
                     if(warranty_plan!=""){
                     warranty_plan_price = funnelData[warranty_plan].price * qty;
                     warranty_plan_name = funnelData[warranty_plan].product_name;
                     $(".pop_warranty").html(warranty_plan_name);
                     $(".pop_warranty_price").html('$'+ warranty_plan_price.toFixed(2));
                     }
                     else{
                     warranty_plan_price = 0;  
                     }
 
                     var total_upsell_price=parseFloat(add_rescue_price) + parseFloat(warranty_plan_price);
                     $('.pop_item_total').html('$'+(parseFloat(buy) + parseFloat(ship_price) + parseFloat(total_upsell_price)).toFixed(2));
               }
               else{
                     $('.pop_item_total').html('$'+(parseFloat(buy) + parseFloat(ship_price)).toFixed(2));
               }
 
            //End Code for stripe upsell popup


            if(discountTimes==1){ 
                var buy  =   obj.data('discount-price'); 
                buy_each =   obj.data('discount-each');
            } 

            var total1 = parseFloat(buy) + parseFloat(ship_price) + parseFloat(total_jp_price) + parseFloat(total_upsell_price);
            //var total1 = parseFloat(buy) + parseFloat(total_jp_price);
            var total = parseFloat(total1).toFixed(2);
			
			if(total >= 100){
            $("#gift_img").css("display","block");
            $("#product_4_id").val(product_4_id);
            $(".pop_giftcard").html(funnelData[product_4_id].product_name);
            $("#giftcard").css("display","block");
            $("#giftcard1").css("display","block");
            $(".pop_giftcard_price").html('$'+funnelData[product_4_id].price.toFixed(2));
            $("#gift_messege").hide();
            }
            else{ 

            $("#gift_img").css("display","none");
            $("#product_4_id").val('');
            $("#gift_val").html('$'+ (100 - parseFloat(total)).toFixed(2));
            $("#giftcard").css("display","none");
            $("#giftcard1").css("display","none");
            $("#gift_messege").show();
            }
			
			
            $('.product_each_price').html('$'+buy_each);
            $('.pop_item_subtotal').html('$'+(buy_each*qty).toFixed(2));
            $('.item_subtotal').html('$'+(buy_each*qty).toFixed(2));
            $('.pop_item_price').html('$'+buy_each);
            $('.pop_item_qty').html(qty);  
            $('.pop_item_sh').html('$'+ship_price.toFixed(2)); 
            $('.pop_item_total').html('$'+total);
            $(".prod-nm").html(product_name);
			$(".prod-nm").html(product_name);
			

            //New script for stripe upsell pop 22.05.24
            $("#main_product").html(product_name);
            $("#main_price").html('$'+ parseFloat(buy).toFixed(2));
			
			//update main product images. Added by Surya 
			$("#three_img,#one_img,#four_img").hide();
			if(qty == 1){
			  $("#one_img").show(); 
			}
			else if(qty == 4){
			  $("#four_img").show();				
			}
			else {
			$("#three_img").show(); 
			}
	
            if(ship_price== '0'){
            $("#main_shipping").html('Free');
            }else{
            $("#main_shipping").html('$'+ parseFloat(ship_price).toFixed(2));
            }
            $("#totalPrice").html('$'+total); 

    }

/*  End Paypal */ 

//By default product load when loading page
$(document).ready(function(){ 

	$('.acto_package.active').trigger('click');
	
	$("#edit_cart").click(function(){   
        $(".minCardMenu").fadeOut(); 	 
    });
	
	
    $(".credit_card").click(function(){ 
        updatePopupOrderDetails(discountTimes); 
        $('#stripePopup').fadeIn();
        $(".minCardMenu").fadeOut(); 	
        showStripeButton(); //added by Soumitra
        $("body").addClass("mfp-active");
    });
	
    $("#paypal_mobtn").click(function(){  
         updatePopupOrderDetails(discountTimes); 
         $('#stripePopup').fadeIn(); 
         $(".minCardMenu").fadeOut(); 	 
         showPaypalButton(); //added by Soumitra
         $("body").addClass("mfp-active");

    }); 
});

function isMobileDevice() {
                return /Mobi|Android/i.test(navigator.userAgent);
	}
