function popup(w,h,url, winName) {
  	var sWid = screen.width;
  	var sHi = screen.height;
	var wid = w;
	var hi = h;
	var tp = (sHi/2)-(hi/2)
	var lft = (sWid/2)-(wid/2)	
	
	if (scroll && document.all && (navigator.userAgent.indexOf("Mac") > -1)) wid = wid+17;
	newwin=window.open(url,winName,"width=" + wid + ",height=" + hi + ",scrollBars=yes,resizable=yes,top = "+ tp +",left ="+ lft + ",screenX=" + lft +",screenY= "+tp);
	newwin.focus();
}

function getBy(field,value,order) {
	eval("document.forms.reorder." + field + ".value='" + value + "'");
	eval("document.forms.reorder.orderby.value='" + order + "'");
	document.forms.reorder.submit();
}

function reorder(field,order) {
	eval("document.forms.reorder.orderby.value='" + field + "'");
	if (order != "") {
		eval("document.forms.reorder.order.value='" + order + "'");
	}
	document.forms.reorder.submit();
}

function goToPage(page) {
	eval("document.forms.reorder.page.value='" + page + "'");
	document.forms.reorder.submit();
}

function detailsPage(id) {
	eval("document.forms.details.id.value='" + id + "'");
	document.forms.details.submit();
}

function removeItem(item) {
	document.forms.items_basket.remove.value = item;
	document.forms.items_basket.include.value = "";
	document.forms.items_basket.submit();
}

function includeItem(item) {
	document.forms.items_basket.remove.value = "";
	document.forms.items_basket.include.value = item;
	document.forms.items_basket.refresh.value = 0;
	document.forms.items_basket.quantities.value = "";
	document.forms.items_basket.submit();
}

function refreshItems(items) {
	document.forms.items_basket.remove.value = "";
	document.forms.items_basket.include.value = items;
	document.forms.items_basket.refresh.value = 1;
	
	var itemsString = items;
	var listItems = itemsString.split(",");
	document.forms.items_basket.quantities.value = "";
	for (i=0; i<listItems.length; i++ ) {
		var item = listItems[i];
		
		eval("num = document.forms.items_basket.quantity_" + item + ".value");
		if (document.forms.items_basket.quantities.value == "") {
			document.forms.items_basket.quantities.value = num;
		} else {
			document.forms.items_basket.quantities.value = document.forms.items_basket.quantities.value + "," + num;
		}
	}
	document.forms.items_basket.submit();
}

function confirmAdd(formNumber) {
	eval("desc = document.forms.shopForm"+formNumber+".title.value");
	eval("price = document.forms.shopForm"+formNumber+".price.value");
	eval("quantity = document.forms.shopForm"+formNumber+".quantity.value");
	
	msg = "You have chosen:\n\n";
	msg += desc + "\n\n";
	msg += "Price: " + price + "\n";
//	msg += "Price: " + unescape("%A3") + price + "\n";
	msg += "Quantity: " + quantity + "\n\n";
	msg += "Click ok to add this to your basket and continue shopping.";
//	msg += "Click ok to add this to your basket and continue shopping.\n\n";
//	msg += "When you have finished shopping press View Basket to go to the checkout.";
	
	if (confirm(msg)) {
		eval("document.forms.shopForm"+formNumber+".submit()");
	}
}

function goToCheckout(num,items) {
	if (num > 0) {
		//location.href = "checkout_1.php";
		document.forms.goto_checkout.items.value = items;
		document.forms.goto_checkout.refresh.value = 1;
		
		var itemsString = items;
		var listItems = itemsString.split(",");
		document.forms.goto_checkout.quantities.value = "";
		for (i=0; i<listItems.length; i++ ) {
			var item = listItems[i];
			eval("num = document.forms.items_basket.quantity_" + item + ".value");
			if (document.forms.goto_checkout.quantities.value == "") {
				document.forms.goto_checkout.quantities.value = num;
			} else {
				document.forms.goto_checkout.quantities.value = document.forms.goto_checkout.quantities.value + "," + num;
			}
		}
		document.forms.goto_checkout.submit();		
	} else {
		alert("Your Shopping Basket is empty.");	
	}
}

function changeCost(_price,_value) {
	_id = Number(_value.substring(0,_value.indexOf(":")));
	_shippingCost = Number(_value.substring(_value.indexOf(":")+1),_value.length);
	document.forms.checkout.shippingcost.value=Number(_shippingCost);
	if (document.forms.checkout.shippingcost.value.indexOf(".") < 0) {
		document.forms.checkout.shippingcost.value += ".00";
	}
	if (document.forms.checkout.shippingcost.value.indexOf(".") == document.forms.checkout.shippingcost.value.length-2) {
		document.forms.checkout.shippingcost.value += "0";
	}	
	document.forms.checkout.totalprice.value=Number(_price)+Number(_shippingCost);
	if (document.forms.checkout.totalprice.value.indexOf(".") < 0) {
		document.forms.checkout.totalprice.value += ".00";
	}
	if (document.forms.checkout.totalprice.value.indexOf(".") == document.forms.checkout.totalprice.value.length-2) {
		document.forms.checkout.totalprice.value += "0";
	}	
	document.forms.checkout.shippingid.value=_id;
}

function validateShip() {
	var a = new Array();
	a[0] = document.forms.checkout.ship_name.value;
	a[1] = document.forms.checkout.ship_address1.value;
	a[2] = document.forms.checkout.ship_city.value;
	a[3] = document.forms.checkout.ship_county.value;
	a[4] = document.forms.checkout.ship_country.value;
	a[5] = document.forms.checkout.ship_postcode.value;
	a[6] = document.forms.checkout.ship_telephone.value;
	
	var msg = new Array();
	msg[0] = "Check Name\n";
	msg[1] = "Check Address 1\n";
	msg[2] = "Check City\n";
	msg[3] = "Check County\n";
	msg[4] = "Check Country\n";
	msg[5] = "Check Post Code\n";
	msg[6] = "Check Telephone\n";	

	txt = "";
	for (i=0;i<a.length;i++){
		if (a[i] == "") {
			txt += msg[i];
		}
	}
	
	var email = document.forms.checkout.ship_email.value
	if((email == "")||(email == null)||(email.indexOf("@") == -1)||(email.indexOf(".") == -1)||(email.indexOf(" ") != -1)){
		txt += "Check Email\n";
	}	

	if (txt != "") {
		alert(txt);
		return false;		
	} else {	
		return true;
	}
}

function validateBill() {
	var a = new Array();
	a[0] = document.forms.checkout.bill_name.value;
	a[1] = document.forms.checkout.bill_address1.value;
	a[2] = document.forms.checkout.bill_city.value;
	a[3] = document.forms.checkout.bill_county.value;
	a[4] = document.forms.checkout.bill_country.value;
	a[5] = document.forms.checkout.bill_postcode.value;
	a[6] = document.forms.checkout.bill_telephone.value;

	var msg = new Array();
	msg[0] = "Check Name\n";
	msg[1] = "Check Address 1\n";
	msg[2] = "Check City\n";
	msg[3] = "Check County\n";
	msg[4] = "Check Country\n";
	msg[5] = "Check Post Code\n";
	msg[6] = "Check Telephone\n";
	
	txt = "";
	for (i=0;i<a.length;i++){
		if (a[i] == "") {
			txt += msg[i];
		}
	}

	var email = document.forms.checkout.bill_email.value
	if((email == "")||(email == null)||(email.indexOf("@") == -1)||(email.indexOf(".") == -1)||(email.indexOf(" ") != -1)){
		txt += "Check Email\n";
	}
	
	if (txt != "") {
		alert(txt);
		return false;		
	} else {	
		return true;
	}
}

function goToNextStep() {
	document.forms.checkout.submit();
}

function goToNextStepValidateShip() {
	if (validateShip()) {
		document.forms.checkout.submit();
	}
}

function goToNextStepValidateBill() {
	if (validateBill()) {
		document.forms.checkout.submit();
	}
}

function goToPayment(page) {
	popup(800,600,"about:blank", "PaymentWindow");
	document.forms.payment.submit();
	location.href = "callback.php";
}

function confirmSubscription() {
	indexTypesubs = document.forms.subscription.typesubs.selectedIndex;
	indexDelivery = document.forms.subscription.delivery.selectedIndex;
	cases = Number(document.forms.subscription.cases.value);
	
	if (indexTypesubs <= 0) {
		alert("You must specify a Case Option.");
	} else if (indexDelivery <= 0) {
		alert("You must specify a Delivery Option.");
	} else if ((isNaN(cases)) || (cases <= 0)) {
		alert("You must specify a No. of Cases.");		
	} else {
	
		typesubs = document.forms.subscription.typesubs.options[indexTypesubs].value;
		delivery = document.forms.subscription.delivery.options[indexDelivery].value;
	
		silverCost = 72.00   
		goldCost = 95.00;
		platinumCost = 120.00;
		
		if (typesubs == "Silver") {
			document.forms.subscription.price.value = silverCost * cases;
		} else if (typesubs == "Gold") {
			document.forms.subscription.price.value = goldCost * cases;
		} else {
			document.forms.subscription.price.value = platinumCost * cases;
		}
		if (document.forms.subscription.price.value.indexOf(".") < 0) {
			document.forms.subscription.price.value += ".00";
		}
		if (document.forms.subscription.price.value.indexOf(".") == document.forms.subscription.price.value.length-2) {
			document.forms.subscription.price.value += "0";
		}		
		document.forms.subscription.idItem.value = typesubs + ":" + delivery + ":" + cases;
		document.forms.subscription.desc.value = "Subs. " + typesubs + " (" + delivery + " x " + cases + "cases)";
		document.forms.subscription.title.value = "Subscription Case Option: " + typesubs + "\n Deliver every: " + delivery + "\n No. of Cases: " + cases;
	
		desc = document.forms.subscription.title.value;
		price = document.forms.subscription.price.value;
		quantity = document.forms.subscription.quantity.value;
		
		msg = "You have chosen:\n\n";
		msg += desc + "\n\n";
		msg += "Price: " + price + "\n";
	//	msg += "Price: " + unescape("%A3") + price + "\n";
		msg += "Click ok to add the subscription to your basket and continue shopping.";
	//	msg += "Click ok to add the subscription to your basket and continue shopping.\n\n";
//		msg += "When you have finished shopping press View Basket to go to the checkout.";
		
		if (confirm(msg)) {
			eval("document.forms.subscription.submit()");
		}
	}
}