gOrdQty = /^([6-9]|[1-9]\d|[1-9]\d\d|1000)$/
gEmail  = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
gValidate = true;

function checkPaper() {
	selected = false;
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if (oControl.checked) {
				selected = true;
				break;
			}
		}
	}
	if (selected){
		return true;
	}else{
		alert("please choose your paper");
		return false;
	}
} // checkPaper()

function checkFoil() {
	if (gValidate) {
		selected = false;
		var aControls = document.getElementsByTagName("input");
		for (var i = 0; i < aControls.length; i++) {
			var oControl = aControls[i];
			if (oControl.type == "radio") {
				if (oControl.checked) {
					selected = true;
					break;
				}
			}
		}
		if (selected){
			return true;
		}else{
			alert("please choose your foil");
			return false;
		}
	}
} // checkFoil()

function setOverride() {
	gValidate = false;
} // setOverride()

function checkQuantity() {
	if (gValidate) {
		if ($("txtQty").value == "") {
			alert("please specify a quantity");
			return false;
		}
		if (!gOrdQty.test($("txtQty").value)) {
			alert("quantity must be between 6 and 1000");
			return false;
		}
	}
	return true;
} // checkQuantity()

function checkQuantities() {
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "text") {
			if (oControl.value == "") {
				alert("please specify a quantity");
				return false;
			}
			if (!gOrdQty.test(oControl.value)) {
				alert("quantity must be between 6 and 1000");
				return false;
			}
		}
	}
	return true;
} // checkQuantities()

function qtyChanged(el) {
	if (el.value == "") {
		alert("please specify a quantity");
		return false;
	}
	if (!gOrdQty.test(el.value)) {
		alert("quantity must be between 6 and 1000");
		return false;
	}
	EndPos = el.id.indexOf("_")+1;
	StartPos = el.id.length;
	CartId = el.id.substring(StartPos,EndPos);
	UnitId = "Unit_" + CartId;
	TotalId = "Total_" + CartId;
	SubTotalId = "SubTotal";
	UnitPrice = $(UnitId).innerHTML;
	ExtdPrice = UnitPrice * el.value;
	OrderSub = $(SubTotalId).innerHTML;
	OrderSub -= $(TotalId).innerHTML;
	OrderSub += ExtdPrice;
	$(TotalId).innerHTML = formatAsMoney(ExtdPrice);
	$(SubTotalId).innerHTML = formatAsMoney(OrderSub);
}// qtyChanged()

function confirmDelete(){
	if (confirm("Are you sure you wish to delete this?"))
		return true;
	else
		return false;
} //confirmDelete

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
} //formatAsMoney

function twoSelected(el) {
	resetAllFlavours(el);
}
function oneSelected(cl,el) {
	resetTwoFlavours(el);
	resetOtherFillers(el,cl);
}
function resetAllFlavours(el) {
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if (oControl.name != el.name) {
				if (oControl.value == "0")
					oControl.checked = true;
			}
		}
	}
} // resetAllFlavours()
function resetTwoFlavours(el) {
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if (oControl.name != el.name) {
				if ((oControl.value == "2") && (oControl.checked)) {
					oControl = aControls[i-2];
					oControl.checked = true;
				}
			}
		}
	}
}	// resetTwoFlavours()
function resetOtherFillers(el,cl) {
	var aControls = document.getElementsByTagName("input");
	for (var i = 0; i < aControls.length; i++) {
		var oControl = aControls[i];
		if (oControl.type == "radio") {
			if ((oControl.name != el.name) && (oControl.name.substring(0,1) != cl)){
				if ((oControl.value == "1") && (oControl.checked)) {
					oControl = aControls[i-1];
					oControl.checked = true;
				}
			}
		}
	}
}	// resetOtherFillers()
function checkFiller() {
	if (gValidate) {
		qty = 0;
		var aControls = document.getElementsByTagName("input");
		for (var i = 0; i < aControls.length; i++) {
			var oControl = aControls[i];
			if (oControl.type == "radio") {
				if (oControl.checked) {
					qty += Number(oControl.value);
				}
			}
		}
		if ((qty < 2) || (qty > 2)){
			alert("please select two chocolates");
			return false;
		}
	}
	return true;
} // checkFiller()		

function goPaper() {
	location.href="Occasion.aspx?c="+$('hidCategoryId').value;
}
function goQty() {
	location.href="Quantity.aspx";
}
function goFilling() {
	location.href="Filling.aspx";
}
function goFoil() {
	location.href="Foil.aspx";
}
function goPlaceCard() {
	location.href="PlaceCard.aspx";
}
function goReview() {
	location.href="Review.aspx";
}
function goCheckout() {
	location.href="Checkout.aspx";
}
function goOccasion(el) {
	location.href=el.options[el.selectedIndex].value;
}

function validateContact() {
	if ($F('txtName') == "") {
		alert("please enter your name");
		$('txtName').focus();
		return false;
	}
	if ($F('txtEmail') == "") {
		alert("please enter your email address");
		$('txtEmail').focus();
		return false;
	}
	if ($F('txtComments') == "") {
		alert("please enter your enquiry");
		$('txtComments').focus();		
		return false;
	}
	if (!gEmail.test($F('txtEmail'))) {
		alert("invalid E-mail address");
		return false;
	}
	return true;
} //validateContact()

function validateSample() {
	if ($F('txtName') == "") {
		alert("please enter your name");
		$('txtName').focus();
		return false;
	}
	if ($F('txtEmail') == "") {
		alert("please enter your email address");
		$('txtEmail').focus();
		return false;
	}
	if ($('lstPaper').selectedIndex == -1) {
		alert("please select paper types you would like samples of");
		$('lstPaper').focus();		
		return false;
	}
	if ($('lstFillings').selectedIndex == -1) {
		alert("please select fillings you would like samples of");
		$('lstFillings').focus();		
		return false;
	}
	if ($F('txtAddress') == "") {
		alert("please enter your delivery address");
		$('txtAddress').focus();		
		return false;
	}
	if (!gEmail.test($F('txtEmail'))) {
		alert("invalid E-mail address");
		return false;
	}
	return true;
} //validateContact()

function validateDelivery() {
	if ($F('txtFirstName') == "") {
		alert("please enter the recipient's first name");
		$('txtFirstName').focus();
		return false;
	}
	if ($F('txtLastName') == "") {
		alert("please enter the recipient's last name");
		$('txtLastName').focus();
		return false;
	}	
	if ($F('txtEmail') == "") {
		alert("please enter the recipient's email address");
		$('txtEmail').focus();
		return false;
	}
	if ($F('txtAddress1') == "") {
		alert("please enter the first line of the recipient's delivery address");
		$('txtAddress1').focus();		
		return false;
	}
	if ($F('txtCity') == "") {
		alert("please enter the city / town of the recipient's delivery address");
		$('txtCity').focus();		
		return false;
	}
	if ($F('txtPostcode') == "") {
		alert("please enter the post code of the recipient's delivery address");
		$('txtPostcode').focus();		
		return false;
	}
	if (!gEmail.test($F('txtEmail'))) {
		alert("invalid E-mail address");
		return false;
	}
	return true;
} //validateDelivery()

function validateBilling() {
	if ($F('txtFirstName') == "") {
		alert("please enter your first name");
		$('txtFirstName').focus();
		return false;
	}
	if ($F('txtLastName') == "") {
		alert("please enter your last name");
		$('txtLastName').focus();
		return false;
	}	
	if ($F('txtEmail') == "") {
		alert("please enter your email address");
		$('txtEmail').focus();
		return false;
	}
	if ($F('txtAddress1') == "") {
		alert("please enter the first line of your billing address");
		$('txtAddress1').focus();		
		return false;
	}
	if ($F('txtCity') == "") {
		alert("please enter the city / town of your billing address");
		$('txtCity').focus();		
		return false;
	}
	if ($F('txtPostcode') == "") {
		alert("please enter the post code of your billing address");
		$('txtPostcode').focus();		
		return false;
	}
	if (!gEmail.test($F('txtEmail'))) {
		alert("invalid E-mail address");
		return false;
	}
	return true;
} //validateBilling()

function checkIAgree() {
	if (!$('chkAgree').checked) {
		alert("please confirm the order is correct and that you agree to our terms and conditions");
		return false;
	}
	return true;
} //checkIAgree()

function txtCnt() {
	if ($F('txtComments').length > 255) {
		$('txtComments').value = $F('txtComments').substring(0, 254);
	}else{
		var Rem = 255 - $F('txtComments').length;
		$('CharsLeft').innerText = Rem + " chars left";
	}
} //txtCnt()

function alertkey(e) {
	if (!e) {
		if (window.event) {
			//DOM
      e = window.event;
    }else{
      //TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
      return;
    }
  }
  var CtrlKeyPressed = e.ctrlKey;
  if (typeof(e.which) == 'number') {
    //NS 4, NS 6+, Mozilla 0.9+, Opera
    e = e.which;
  }else if (typeof(e.keyCode) == 'number') {
    //IE, NS 6+, Mozilla 0.9+
    e = e.keyCode;
  }else if (typeof(e.charCode) == 'number') {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  }else{
		//TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
    return;
  }
  if ((CtrlKeyPressed) && (e == 89)) {
    document.getElementById("txtFirstName").value = "Ian";
		document.getElementById("txtLastName").value = "Tester";
		document.getElementById("txtPhone").value = "02087589055";
		document.getElementById("txtEmail").value = "ian@ko-de.com";
		document.getElementById("txtAddress1").value = "9/38 Kew Bridge Rd";
		document.getElementById("txtAddress2").value = "Brentford";
		document.getElementById("txtCity").value = "Middlesex";
		document.getElementById("txtPostcode").value = "TW8 0EB";
	}
  if ((CtrlKeyPressed) && (e == 88)) {
	//	document.getElementById("btnGo").disabled = false;  
	}
    //window.alert('The key pressed has keycode ' + e + ' and is key ' + String.fromCharCode( e ) );
}