

// validate that input to a text field is numeric
function validateNum(e) {
	var keynum;
	var keychar;
	var numcheck = /^\d?$/; 
	
	if (e.which) keynum = e.which;
	else if (e.keyCode) keynum = e.keyCode;
	keychar = String.fromCharCode(keynum);
	if (keynum==8||keynum==9||keynum==13||keynum==116||keynum==undefined) return true;
	else return numcheck.test(keychar);
}
function validatePrice(e) {
	var keynum;
	var keychar;
	var numcheck = /^\d?$/; 
	if (e.which) keynum = e.which;
	else if (e.keyCode) keynum = e.keyCode;
	keychar = String.fromCharCode(keynum);
	if (keynum==8||keynum==9||keynum==13||keynum==46||keynum==116||keynum==undefined) return true;
	else return numcheck.test(keychar);
}

// function to round numbers to a given number of decimal places
function round(num,dp) {
	return Math.round(num*Math.pow(10,dp))/Math.pow(10,dp)
}
// calculate the cost of X items based on price per item and quantity
// NOTE: if a special applies to this item the price passed to it MUST be the special price or the function will not be accurate
function getCost(price, qty, discount) {
	basePrice = price * qty; 
	if (discount == "") { totalPrice = basePrice; }
	else {
		discount = discount.split("/");
		discountNum = discount[0];
		discountAmount = discount[1];
		temp = Math.floor(qty / discountNum);
		remain = qty - (temp * discountNum);
		
		totalPrice = temp * discountAmount + remain * price;

		if (totalPrice > basePrice) totalPrice = basePrice;
	}
	
  return toCash(totalPrice);
}

// convert any number to a 2dp money amount
function toCash(x) {
	r=/^[0.]+$/;
  if (r.test(x)) return "";
	x = round(x,2)
	r=/\.\d$/;
	r1=/\./;
	if (r.test(x)) return x + "0";
	else if (!r1.test(x)) return x + ".00";
	return x;
}	

// Return to main shopping window from shopping cart 
function continueShopping() {
	var parentWin = window.opener;
	try {
		parentWin.focus();	
	}
	catch(e) {
		alert('ERROR: The window you were using has been closed.');
	}
}

// Function to find the selected radio buttonin a group
function findSelected(form) {
	for (var i=0; i<form.selected.length;i++) {
		if (form.selected[i].checked) { return form.selected[i].value }
	}
}
// function to open the cart window
function cartWin() {
	cart=window.open('cart.php','cart','width=900,height=600,status=yes,location=no,menubar=no,scrollbars=yes,resizable=yes');
	cart.focus();
}

function orderWin() {
	order=window.open('','order','width=500,height=400,status=yes,location=no,menubar=no,scrollbars=yes,resizable=yes');
	order.focus();
}
function popup(location,w,h) {
	popup=window.open(location,'popup','width=' + w + ',height=' + h + ',status=yes,location=no,menubar=no,scrollbars=yes,resizable=yes');
	popup.focus();
}

function show(url) { 
  window.open("popup.php?"+url, "", "address=no,resizable=no,height=200,width=200,left=0,top=0");
}         