// JavaScript Document

/*-------------------------------------------------------------------------------*/
/*	PRODUCTS
/*-------------------------------------------------------------------------------*/
function viewPage(page, ctgid){
	ctgid = (ctgid != null) ? ctgid : '';
	$('#products_loader').show();
	$('#products_list').load('/includes/store/ajax/products.php', {'page':page,'ctgid':ctgid}, function(){ $('#products_loader').fadeOut(); });
}

/*-------------------------------------------------------------------------------*/
/*	SHOPPING CART
/*-------------------------------------------------------------------------------*/
function addToCart(){
	var isValidated = true;
	$('#product_attributes select').each(function(i,elem){
		if($(elem).val().trim() == "") isValidated = false;
	});
	if(!isValidated){
		alert("Please select one of each of the available product options before adding this item to your cart.");
	}else{
		$('#cart_loader').fadeIn();
		$("#cart_response").fadeOut("medium", function(){
			var options = {
				target: "#cart_response",
				success: function(response){
					var responseText = "";
					if(response == 1){
						responseText = "Item successfully added to your cart!";
					}else{
						responseText = "We're having a bit of trouble adding this item to your cart. Please try again and if you continue to experience problems, please call 866-869-9704.";
					}
					$('#cart_response').html(responseText);
					$('#cart_response').fadeIn();
					$('#cart_loader').fadeOut();
					refreshCartTotal();
				}
			};
			$("#product_form").ajaxForm();
			$("#product_form").ajaxSubmit(options);
		});
	}
}

function removeFromCart(id){
	if(confirm("Are you sure you want to remove this item from your cart?")){
		$('#cart_loader').fadeIn();
		$.ajax({
			type: "POST",
			url: "/includes/store/ajax/cart.php",
			data: "action=REMOVE&itemid="+id,
			success: function(response){
				$('#cart_item'+id).fadeOut("medium", function(){
					$('#cart_item'+id).remove();
					$('#cart_loader').fadeOut();
					refreshCartTotal();
				});
			}
		});
	}
}

function updateCart(){
	$('#cart_loader').fadeIn();
	$("#cart_response").fadeOut("medium", function(){
		var options = {
			target: "#cart_response",
			success: function(response){
				var responseText = "";
				if(response == 1){
					responseText = "Your cart has been successfully updated!";
				}else{
					responseText = "We're having a bit of trouble updating your cart. Please try again and if you continue to experience problems, please call 866-869-9704.";
				}
				$('#cart_response').html(responseText);
				refreshCartItems();
			}
		};
		$("#cart_form").ajaxForm();
		$("#cart_form").ajaxSubmit(options);
	});
}

function refreshCartItems(){
	$('#cart_loader').fadeIn();
	$('#shopping_cart table tbody').load('/includes/store/ajax/cart.php', {'action':'REFRESH_CART'}, function(response){
		var tmp = response.split('%%');
		$('#shopping_cart table tbody').html(tmp[0]);
		$('#cart_subtotal').text('$'+tmp[1]);
		$('#cart_loader').fadeOut();
		$('#cart_response').fadeIn();
		refreshCartTotal();
	});
}

function refreshCartTotal(){
	$.ajax({
		type: "POST",
		url: "/includes/store/ajax/cart.php",
		data: "action=REFRESH_TOTAL",
		success: function(response){
			var tmp = response.split('%%');
			var cartTotal = (intval(tmp[0]) > 0) ? tmp[0]+' item(s)' : 'Cart is empty';
			var isEmpty = (intval(tmp[0]) > 0) ? false : true;
			var href = (!isEmpty) ? 'https://www.cherrybomb.com/checkout/' : '#';
			var btnClass = (!isEmpty) ? 'active' : 'inactive';
			$('#view_cart #items').text(cartTotal);
			$('#checkout a').attr('href', href);
			$('#checkout a').attr('class', btnClass);
			$('#cart_subtotal').text('$'+tmp[1]);
		}
	});
}



/*-------------------------------------------------------------------------------*/
/*	CHECKOUT
/*-------------------------------------------------------------------------------*/
function loadStates(type, selected){
	var country = $('#'+type+'_country').val();
	$('#'+type+'_sel_state').load('/includes/store/ajax/states.php', {'type':type, 'country':country, 'selected':selected}, function(response){
		if($('#'+type+'_state option').length == 0){
			$('#'+type+'_state option').attr('readonly', true);
		}
	});
}

function toggleForm(type){
	if($('#'+type+'_use_other').attr('checked') == true || $('#'+type+'_use_other').attr('checked') == "checked"){
		$('#'+type+'_form input:text').attr('disabled', true);
		$('#'+type+'_form select').attr('disabled', true);
	}else{
		$('#'+type+'_form input:text').attr('disabled', false);
		$('#'+type+'_form select').attr('disabled', false);
	}
}