function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function popup_detail(prod_id) {
	cAddress = 'product-ingredient.php?product_id=' + prod_id;
	MM_openBrWindow(cAddress,'product_ingredient','scrollbars=yes,resizable=yes,width=440,height=200');
}

function load_example(cImage) {
	cAddress = 'show-example.php?theimage=' + cImage;
	MM_openBrWindow(cAddress,'image-example','scrollbars=no,resizable=yes,width=410,height=380');
}

function check_status(isChecked) {
	
	if (isChecked) {		
		document.getElementById("DeliverTo").disabled = true;
		document.getElementById("DeliveryAddress").disabled = true;
		document.getElementById("DeliveryPostCode").disabled = true;		
	} else {
		document.getElementById("DeliverTo").disabled = false;
		document.getElementById("DeliveryAddress").disabled = false;
		document.getElementById("DeliveryPostCode").disabled = false;		
	}
}
function check_delete_order(customer_id) {
	if (confirm("Delete Order, are you sure?")) {
		document.location.href = "order_process.php?func=delete&customer_id=" + customer_id;
	}
}
function check_delete_category(category_id) {
	if (confirm("Delete Category, are you sure? \nNOTE: All Products within this category will remain, but be unassigned from this category.")) {
		document.location.href = "category_process.php?func=delete&category_id=" + category_id;
	}
}

function check_delete_product(product_id, category_id, int_cur_position)
{
	if (confirm("Delete Product, are you sure?")) {
		document.location.href = "product_process.php?func=delete&category_id=" + category_id + "&product_id=" + product_id + "&int_cur_position=" + int_cur_position;
	}
}

function check_delete_ingredient(product_id, int_cur_position)
{
	if (confirm("Delete Ingredient, are you sure?")) {
		document.location.href = "ingredient_process.php?func=delete&product_id=" + product_id + "&int_cur_position=" + int_cur_position;
	}
}

function check_delete_gift_cert_order(voucher_id)
{
	if (confirm("Delete Gift Voucher, are you sure?")) {
		document.location.href = "gift_cert_order_process.php?func=delete&voucher_id=" + voucher_id;
	}	
}

function check_delete_code(discount_id, int_cur_position) {
	if (confirm("Delete Discount Code, are you sure?")) {
		document.location.href = "code_process.php?func=delete&discount_id=" + discount_id + "&int_cur_position=" + int_cur_position;
	}
}

function check_voucher_type(theType)
{
	if (theType == 1) {
		document.getElementById("address_to").disabled = true;
		document.getElementById("postcode_to").disabled = true;
		document.getElementById("email_to").disabled = false;
		document.getElementById("email_to_2").disabled = false;
	} else {
		document.getElementById("address_to").disabled = false;
		document.getElementById("postcode_to").disabled = false;
		document.getElementById("email_to").disabled = true;
		document.getElementById("email_to_2").disabled = true;
	}
}	

var cCurCode = '';

function check_gift_code()
{
	theCode = document.getElementById("gift_code").value;
	
	if (theCode == '') {
		alert("No Gift Code entered");
	} else {
		if (cCurCode != theCode) {
			cCurCode = theCode;
			theUrl = "get-gift-code.php?gift_code=" + theCode;
			document.getElementById("check_gift_code").src = theUrl;
		} else {
			alert("Cannot re-use same code");
		}
	}
	
}


function apply_discount(cDiscount, cIsValid, cGiftCode)
{
	if (cIsValid == 1) {
		cCurAmount = document.getElementById("amount_to_debit").value;
		cCurAmount = cCurAmount - 0;
		cNewAmount = cCurAmount - cDiscount;		
		if (document.getElementById("gift_codes_applied").value == '') {
			document.getElementById("gift_codes_applied").value = cGiftCode;
		} else {
			document.getElementById("gift_codes_applied").value = document.getElementById("gift_codes_applied").value + "#" + cGiftCode;
		}			
		if (cNewAmount < 0) {
			cNewAmount = 0;
		}
		document.getElementById("amount_to_debit").value = cNewAmount;
		document.getElementById("Amount").value = cNewAmount;
		document.getElementById("gift_code").value = '';
		document.getElementById("gift_code_html").innerHTML = document.getElementById("gift_code_html").innerHTML + "Voucher applied: " + cGiftCode + " (&pound;" + cDiscount + ")<br>";
	} else {
		alert("Invalid gift code.");		
	}
}