function recalculate_prices(product_ref,priceArray,quantity){
	var form_name = "add_to_basket_" + product_ref; 
	var form = document.forms[form_name];

	var product_quantity = form.product_quantity.value;
	var product_base_price = form.product_base_price.value;
	var new_total = "Price: &pound;" + format_currency(product_quantity * product_base_price);

	var product_modifier = form.product_modifier; 

	if(product_modifier != undefined){
		product_modifier = form.product_modifier[form.product_modifier.selectedIndex].value;
		var parts = product_modifier.split("||");
		var product_modifier_price = parts[parts.length - 1];
		new_total = "Price: &pound;" + format_currency(product_quantity * product_modifier_price);
	} else if (priceArray.constructor.toString().indexOf("Array") > 0){
		for(i = 0; i < priceArray.length; i++){
			var parts = priceArray[i].split("||");
			var minimum = parts[0];
			var maximum = parts[1];
			var quantity_price = parts[2];
			if(maximum == 0){
				maximum = 99999999;
			}
			if(product_quantity >= minimum && product_quantity <= maximum){
				new_total = "Price: &pound;" + format_currency(product_quantity * quantity_price);
			}
		}
	}

	var price_text_name = "price_" + product_ref;
	
	document.getElementById(price_text_name).innerHTML = new_total;
}

function format_currency(amount){
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function check_modifier_selected(product_ref,modifier_type,safety_confirm){
	var form_name = "add_to_basket_" + product_ref; 
	var form = document.forms[form_name];
	var product_modifier = form.product_modifier; 
	
	if(product_modifier != undefined){
		product_modifier = form.product_modifier[form.product_modifier.selectedIndex].value;
		if(product_modifier.length < 1 || product_modifier == 0){
			alert("Please select " + modifier_type  + " for product ref " + product_ref);
			document.valid = false;
			return false;
		} else {
			if (safety_confirm == 1){
				productSafetyConfirmation(product_ref);
			} else {
				document.valid = true;
				form.submit();
				return true;
			}
		
		}
	} else {
		if (safety_confirm == 1){
			productSafetyConfirmation(product_ref);
		} else {
			document.valid = true;
			form.submit();
			return true;
		}
	}

}

function productSafetyConfirmation(product_ref) {
	
		var form_name = "add_to_basket_" + product_ref; 
	
		$.fancybox(
			'<div id="product_safety_confirm"><p><strong>Do you have enough information on how to use this product safely and effectively prior to administering?</strong></p><p><small>If you select \'No\', you will be contacted by one of our SQP\'s who can discuss this with you further before we will dispatch your order.<br/>(Please note, some of the products you order may not be dispatched and your money refunded if you have ticked \'No\' and we are unable to contact you).</small></p><p id="safety_no_p"><a href="#" class="safety_no" >No, please contact me</a></p><p class="promo-button"><a href="#" class="safety_yes" >Yes, I Understand</a></p></div>',
			{
				'modal': true,
				'centerOnScroll': true
			}
		);
		
		$('.safety_no').click(function() {
  			productSafetyNo(form_name);
		});
		
		$('.safety_yes').click(function() {
  			productSafetyYes(form_name);
		});
		
}

function productSafetyYes(form_name) {
	document.forms[form_name].product_safety_confirmation.value = '1';
	$.fancybox.close();
	document.forms[form_name].submit();
}

function productSafetyNo(form_name) {
	$.fancybox.close();
	document.forms[form_name].submit();
}
