
// for update product quantity with Ajax, builds the HTTP call for Ajax
function calculateTotalProductPrice(url, productID, quantity) {
	if (quantity == "" || quantity == null)
		quantity = 1;
	
	var query = "pid=" + encodeURIComponent(productID) + "&Quantity=" + encodeURIComponent(quantity);
	
	if ($("productprice-total")) {

		jQuery.ajax({
			url: url,
			method: 'post',
			data: query,
			success: function(html){
				$("#productprice-total").html(html);
				jQuery('#skuProductNumber').html(productID);
				jQuery('#pid').val(productID);
			}
		});
	}
}

// for update product quantity with AJAX, builds the HTTP call for AJAX
function calculateQuantity(url, productID, ajaxViaClick) {
	var query = "pid=" + encodeURIComponent(productID);

	jQuery.ajax({
		url: url,
		method: 'post',
		data: query,
		success: function(html){
			jQuery("#in_stock_message").html(html);
			jQuery("#in_stock_message").show();
			jQuery('#skuProductNumber').html(productID);
			jQuery('#pid').val(productID);

			if ( jQuery('#in_stock_message').text().match('out of stock') ) {
				ColourSwatch.AddToBagButton.disable('notInStock');
			} else {
				ColourSwatch.AddToBagButton.enable(productID);
			}
			
			checkStock();
		}
	});

	updateQuantityList(document.forms.cartaddproduct.update_url_quantity_list.value, productID, null, ajaxViaClick);
}

function updateQuantityList(url, productID, quantity, ajaxViaClick) {
	if (quantity == "" || quantity == null) {
		quantity = 1;
	}

	var query = "pid=" + encodeURIComponent(productID) + "&quantity=" + encodeURIComponent(quantity);
	
	var open_id_val = '';
	if (jQuery('[name=open_pid]').length > 0) {
		open_id_val = jQuery('[name=open_pid]').val();
	}

	// If ajaxViaClick == 'true' this means the customer has
	// actually clicked something to trigger the AJAX call
	// instead of this request being a result from the page load
	if (ajaxViaClick == 'true' || ajaxViaClick === true) {
		query += '&send_tagman_data=true';
	} else {
		query += '&send_tagman_data=false';
	}

	jQuery.ajax({
		url: url,
		method: 'post',
		data: query,
		success: function(html){
			jQuery("#quantity_prod_select").html(html);
			jQuery('#skuProductNumber').html(productID);
			jQuery('#pid').val(productID);
		}
	});
}

function getProductPromotionName(url, productID) {
	var query = "pid=" + encodeURIComponent(productID);

	jQuery.ajax({
		url: url,
		method: 'post',
		data: query,
		success: function(html){
			$("#product_promotion").html(html);
			jQuery('#skuProductNumber').html(productID);
			jQuery('#pid').val(productID);
		}
	});
	
}

