/* 
 * @rewrite: 04-01-13
 @usage : MIC
 */
 var currentCartItemID;
 var currentQuantity;
 var currentTextboxID;
 var currentProductOptionID;
$(document).ready(function () {
	/* login popup*/
	/* if(!$("#cartUser").val()){
		$("#submitCoupon").addClass('popuplogin');
		$("#complete").addClass('popuplogin');
	} */
	/*login popup*/
	$('#promoCode').keypress(function(e){ var code = e.keyCode || e.which;if ( code == 13 ) return false;});
	$(".defaultError").click(function(){$('.default_message_error').fadeOut('fast');});
	$(document).on("change",'.quantity', function () 
	{
		 thisElm = this;
		 var cartItemID = $(this).attr('rel');
		 var quantity 	= this.value;
		 var textboxID  = $(this).attr('id');
		 currentCartItemID=cartItemID;
 		 currentQuantity=quantity;
                 $('.pop_loader').removeClass('hide');
		$.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH + "/cart/ajaxUpdateCart/",
			data: { cartItemID: cartItemID,mode: 'update_quantity',quantity: quantity},
			success: function (data) {
                            $('.pop_loader').addClass('hide');
			try 
			{
				var x = $.parseJSON(data);
			}catch (e) 
			{
				window.location = location.href;
				return false;
			}	
                       
			if(x.isError=='Yes')
			{
				$('#'+textboxID).val(x.orderItemQuantity);
				jNotifyShowWithClose(x.error,"common_message", "message_error"); 
			}
			else
			{
				$('#cart_ajax_section').html('');
				$('#cart_ajax_section').append(x.cartHtml);
                                if(x.cartLeftBlockHtml!=''){
                                $('.carttotal').html('');
				$('.carttotal').append(x.cartLeftBlockHtml);
                                }        
				setPromoCharityBlock(x);
				assignJsonDataToCartJS(x.cartData);
				updateCartNotification();
				jNotifyShowWithClose(x.error,"common_message", "message_info"); 
			}
			}
		});
	});
        $(document).on("click",'.free-gift-product-cart', function () {	
            $(".modal-content").load(GLOBAL_BASE_PATH+"/catalog/ajaxFreeProducts/ct/1/",function(){
                $("#myModal").modal({show:!0})
            })
        });
	$(document).on("click",'.update_cart_item', function () 
	{
		var cartItemID = $(this).attr('rel');
		 $("#orderQuantity_"+cartItemID).trigger('change');
	});
	
	$(document).on("click",'#submitCoupon', function () 
	{
		if(!$("#cartUser").val()>0){
			return false;
		}
		var couponCodeValue = $('#couponCode').val();
		$.ajax({
		  type: "POST",
		  url: GLOBAL_BASE_PATH + "/cart/ajaxApplyCoupon/",
		  data: { couponCode: couponCodeValue},
		  success: function (data) {
				try 
				{
					var x = $.parseJSON(data);
				}catch (e) 
				{
					window.location = location.href;
					return false;
				}
			  if(x.isError=='Yes')
			  {				  
				$('#couponCode').val('');
				jNotifyShowWithClose(x.error,"common_message", "message_error"); 
			  }
			  else
			  {
				$('#cart_ajax_section').html('');
				$('#cart_ajax_section').append(x.cartHtml);
				assignJsonDataToCartJS(x.cartData);
				updateCartNotification();
				jNotifyShowWithClose(x.error,"common_message", "message_info"); 
			  }
		  }
		});
	});
	$(document).on("click","#btnUnsetPomo", function () 
	{
		if($(this).hasClass('special'))
		{
			var special = true;
		}
		else
		{
			var special = false;
		}
		$.ajax({
		type: "POST",
		url: GLOBAL_BASE_PATH + "/cart/ajaxUpdateCart/",
		data: {mode: 'remove_promo'},
		success: function (data) 
		{
			try 
			{
				var x = $.parseJSON(data);
			}catch (e) 
			{
				window.location = location.href;
				return false;
			}
			if(x.isError=='Yes')
			{
				jNotifyShowWithClose(x.error,"common_message", "message_error"); 
			}
			else
			{
				if(special)
				{
					location.reload();
				}
				else
				{
					$('#cart_ajax_section').html('');
					$('#cart_ajax_section').append(x.cartHtml);
                                        if(x.cartLeftBlockHtml!='')
                                        {$('.carttotal').html('');
					$('.carttotal').append(x.cartLeftBlockHtml);}
                                        
					assignJsonDataToCartJS(x.cartData);
					updateCartNotification();
					setPromoCharityBlock(x,'No');
					jNotifyShowWithClose(x.error,"common_message", "message_info"); 
				}
			}
		}
	  });
    });
    $(document).on("click","#promo", function () 
	{
		applyPromoCode();
    });	
	$('#promoCode').bind("keyup", function(e) 
	{
		var code = e.keyCode || e.which; 
		if (code  == 13) 
		{ 
			applyPromoCode();
			return false;
		}
	});
	$('.quantity').keypress(function(e) 
	{
		var a = [];
		var k = e.which;
		for (i = 48; i < 58; i++)
			a.push(i);
		if (!(a.indexOf(k)>=0))
			e.preventDefault();
	});
});
function handleSubmit()
{
        /******************/
        var chkDisable = "Yes";
        $('.shippingmethodchk :selected').each(function(i, selectedChk){
            //console.log($(selectedChk).val());
            if($(selectedChk).val() == "")
                {chkDisable = "No";}
        });
        if(chkDisable == "Yes"){
        $('#promo').attr('disabled','disabled');
        $('#submitCoupon').attr('disabled','disabled');
        $("#cartRedirectCheckout").hide();
        $("#completeLoader").show();
        $("#complete").hide();
        }

        /*********************/
	return true;
	

}
function applyPromoCode()
{
	var promoCode 	= $("#promoCode").val();
	$.ajax({
		type: "POST",
		url: GLOBAL_BASE_PATH + "/cart/ajaxUpdateCart/",
		data: {mode: 'apply_promo',promoCode: promoCode},
		success: function (data) {
			try 
			{
				var x = $.parseJSON(data);
			}catch (e) 
			{
				window.location = location.href;
				return false;
			}
			if(x.isError=='Yes')
			{
				jNotifyShowWithClose(x.error,"common_message", "message_error"); 
				$("#promoCode").val('');
			}
			else
			{			
				$('#cart_ajax_section').html('');
				$('#cart_ajax_section').append(x.cartHtml);
                                if(x.cartLeftBlockHtml!=''){
                                $('.carttotal').html('');
				$('.carttotal').append(x.cartLeftBlockHtml);
                                }   
				assignJsonDataToCartJS(x.cartData);
				updateCartNotification();
				setPromoCharityBlock(x,'No');
				jNotifyShowWithClose(x.error,"common_message", "message_info"); 
			}		  
		}
	});
	return false;
}
function setPromoCharityBlock(x,customPromoError)
{
	$('#charity_promo_ajax_section').html('');
	$('#charity_promo_ajax_section').append(x.cartCharityPromoBlock);
	if(customPromoError=='Yes')
	{
		if(x.promoData.promoError=='Yes')
		{
			jNotifyShowWithClose(x.promoData.promoErrMsg,"promo_message", "message_error"); 
		}
	}	
}
function CallDeleteCart(cartItemID)
{					
	jConfirm(msgConfirmDelete, AlertText,function (r) {
	if (r == true) {
		$.ajax({
			  type: "POST",
			  url: GLOBAL_BASE_PATH + "/cart/ajaxUpdateCart/",
			  data: { cartItemID: cartItemID,mode: 'delete_item'},
			  success: function (data) {
					try 
					{
						var x = $.parseJSON(data);
					}catch (e) 
					{
						window.location = location.href;
						return false;
					}
				  if(x.isError=='Yes')
				  {
					 jNotifyShowWithClose(x.error,"common_message", "message_error"); 
				  }
				  else
				  {
					  if(x.cartEmpty=='Yes')
					  {						   
						   if($('.iframe', window.parent.document).is(":visible")){
							parent.$.fancybox.close();
							parent.location.reload(true); 
						   }
						   
							window.location=GLOBAL_BASE_PATH+'/cart/show/n';
					  }
					  else
					  {
							$('#cart_ajax_section').html('');
							$('#cart_ajax_section').append(x.cartHtml);
                                                        
                                                        if(x.cartLeftBlockHtml!=''){
                                                        $('.carttotal').html('');
                                                        $('.carttotal').append(x.cartLeftBlockHtml);
                                                        }   
                                
							assignJsonDataToCartJS(x.cartData);
							updateCartNotification();
							setPromoCharityBlock(x);
							jNotifyShowWithClose(x.error,"common_message", "message_info"); 
                                                        var carcount=$("#hidcartitemcount").val();
                                                        $("#hidcartitemcount").val(carcount-1);
                                                        $("#cartitemcount").html($("#hidcartitemcount").val()+"  item(s) ");
					  }
				  }
			  }
			  });
	}
	else
	{
		return false;
	}
	},okText,cancelText);
	

	
}
 
 // FUNCTION FOR DELETE CART All
function CallDeleteCartAll() {
    jConfirm(msgConfirmDeleteAll, AlertText,function (r) {
	if (r == true) {
            window.location=GLOBAL_BASE_PATH+'/cart/removeCart/';
        }else
	{
		return false;
	}
    },okText,cancelText);
}

function lightbox_close(){
	$(".zoomContainer").hide();
	$("#CategoryPopup, #CategoryFade").fadeOut();
	//$('body').css("overflow","auto");
}

$(document).on('click', '.delivery-taxi-info', function() {
    var frameSrc = popup_iframe_url + '/page/deliveryTaxiInfo/';
    setIframePopup(frameSrc, '', 300);
    showModalPopup();
	
})
