g_emailfilter  = /^(([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}$/;
gOrdQty = /^([1-9]|[1-9]\d|[1-9]\d\d|1000)$/

function Confirmed() {
	var Msg = "";
	var ai = document.getElementsByTagName("input");
	var fControl;
	for (var i = 0; i < ai.length; i++) {
		var oControl = ai[i];
		switch (oControl.type) {
			case "checkbox" :
				if (oControl.className == "required") {
					if (!oControl.checked) {
						if (Msg == "")
							fControl = oControl;
						Msg = "Please confirm the order is correct and that you agree to our terms and conditions";
					}
				}
		}
	}	
	if (Msg != ""){
		alert(Msg);
		return false;
	}else{
		$("continue").value = "please wait";
		return true;
	}
}
function ValidateSearch() {
	if ($("search").value != "") {
		return true;
	} else {	
		alert("Please enter your search criteria");
		$("search").focus();
		return false;
	}
}
function ValidateChangePwd()
{
	if ((ValidateForm("changepwdform")) && (ValidateConfirmPassword("newpassword","confirmpassword")))
	{	
		if (($("newpassword").value.length) < 6) {
			alert("Your password must be at least six characters long");
			$("newpassword").focus();
			return false;
		}else{
			return true;
		}
	}else{
		return false;}
}
function ValidateSignUp()
{
	if ((ValidateForm("signupform")) && (ValidateConfirmPassword("passwordnew","confirmpassword")))
	{	
		if (($("passwordnew").value.length) < 6) {
			alert("Your password must be at least six characters long");
			$("passwordnew").focus();
			return false;
		}else{
			return true;
		}
	}else{
		return false;}
}
function ValidateConfirmPassword(Pwd1, Pwd2)
{
	if ($(Pwd1).value == $(Pwd2).value) {
		return true;
	}else{
		alert("Passwords do not match, please enter again");
		$(Pwd1).value = $(Pwd2).value = "";
		$(Pwd1).focus();
		return false;
	}
}
function ValidateForm(form)
{
	var Msg = "";
	var ai = Form.getElements(form);
	var fControl;
	for (var i = 0; i < ai.length; i++) {
		var oControl = ai[i];
		switch (oControl.type) {
			case "text" :
			case "password" :
				if (oControl.name.indexOf("search") < 0)
				{
					if (oControl.className == "required") {
						if (oControl.value == "") {
							if (Msg == "")
								fControl = oControl;
							Msg += " - " + oControl.name + "\r\n";
						}
					}
					if ((oControl.name.indexOf("email") > -1) && (oControl.value != "")) {
						if (!g_emailfilter.test(oControl.value)) {
							alert("Invalid email - " + oControl.name);
							oControl.focus();
							return false;
						}
					}
				}
				break;
			case "radio" :
		//		if (oControl.type == "radio") {
		//			if (oControl.checked) {
		//				var id = oControl.id;
		//			}
		//		}
				break;
			case "checkbox" :
				if (oControl.className == "required") {
					if (!oControl.checked) {
						if (Msg == "")
							fControl = oControl;
						Msg = "Please confirm the order is correct and that you agree to our terms and conditions";
					}
				}
				break;
			case "submit" :
				break;
		}
	}
	/*ai = document.getElementsByTagName("textarea");
	for (var i = 0; i < ai.length; i++) {
		var oControl = ai[i];
		if (oControl.className == "required") {
			if (oControl.value == "") {
				if (Msg == "")
					fControl = oControl;
				Msg += " - " + oControl.name + "\r\n";
			}
		}
	}*/
	if (form == "signupform") {
		ai = document.getElementsByTagName("select");
		for (var i = 0; i < ai.length; i++) {
			var oControl = ai[i];
			if (oControl.className == "required") {
				if (oControl.options[oControl.selectedIndex].value == "") {
					if (Msg == "")
						fControl = oControl;
					Msg += " - " + oControl.name + "\r\n";
				}
			}
		}
	}	
	if (Msg == "") {
		return true;
	} else {	
		alert("Please enter :\r\n" + Msg);
		if (typeof(fControl != "undefined"))
			fControl.focus();
		return false;
	}
}

function show(form) {
	switch (form)
	{
		case "returning" :
			Element.hide("newcustomer");
			Element.hide("forgotpassword");	
			Element.show("returning");		
			break;
		case "newcustomer" :
			Element.hide("returning");
			Element.hide("forgotpassword");	
			Element.show("newcustomer");				
			break;
		case "forgotpassword" :
			Element.hide("returning");
			Element.hide("newcustomer");	
			Element.show("forgotpassword");				
			break;
	}
	Form.focusFirstElement(form);		
}

function ValidateNewsletter() {
	var Msg = "";
	if ($("email").value != "") {
		if (!g_emailfilter.test($("email").value)) {
			alert("Invalid email - " + $("email").name);
			$("email").focus();
			return false;
		}
		var ai = document.getElementsByTagName("input");
		bSelected = false;
		for (var i = 0; i < ai.length; i++) {
			var oControl = ai[i];
			switch (oControl.type) {
				case "radio" :		
					if (oControl.checked)
						bSelected = true;
					break;
				default :
					break;
			}
		}
		if (!bSelected) {
			alert("Please select subscribe or unsubscribe");
			return false;
		}
	}else{
		Msg += " - " + $("email").name + "\r\n";
	}
	if (Msg == "") {
		return true;
	} else {	
		alert("Please enter :\r\n" + Msg);
		return false;
	}
}

function Validate() {
	var Msg = "";
	var ai = document.getElementsByTagName("input");
	var fControl;
	for (var i = 0; i < ai.length; i++) {
		var oControl = ai[i];
		switch (oControl.type) {
			case "text" :
				if (oControl.name.indexOf("search") < 0)
				{
					if (oControl.className == "required") {
						if (oControl.value == "") {
							if (Msg == "")
								fControl = oControl;
							Msg += " - " + oControl.name + "\r\n";
						}
					}
					if ((oControl.name.indexOf("email") > -1) && (oControl.value != "")) {
						if (!g_emailfilter.test(oControl.value)) {
							alert("Invalid email - " + oControl.name);
							oControl.focus();
							return false;
						}
					}
				}
				break;
			case "radio" :
		//		if (oControl.type == "radio") {
		//			if (oControl.checked) {
		//				var id = oControl.id;
		//			}
		//		}
				break;
			case "checkbox" :
				if (oControl.className == "required") {
					if (!oControl.checked) {
						if (Msg == "")
							fControl = oControl;
						Msg = "Please confirm the order is correct and that you agree to our terms and conditions";
					}
				}
				break;
			case "submit" :
				break;
		}
	}
	ai = document.getElementsByTagName("textarea");
	for (var i = 0; i < ai.length; i++) {
		var oControl = ai[i];
		if (oControl.className == "required") {
			if (oControl.value == "") {
				if (Msg == "")
					fControl = oControl;
				Msg += " - " + oControl.name + "\r\n";
			}
		}
	}
	ai = document.getElementsByTagName("select");
	for (var i = 0; i < ai.length; i++) {
		var oControl = ai[i];
		if (oControl.className == "required") {
			if (oControl.options[oControl.selectedIndex].value == "") {
				if (Msg == "")
					fControl = oControl;
				Msg += " - " + oControl.name + "\r\n";
			}
		}
	}	
	if (Msg == "") {
		return true;
	} else {	
		alert("Please enter :\r\n" + Msg);
		if (typeof(fControl != "undefined"))
			fControl.focus();
		return false;
	}
}//validate()

function launchWindow(Url) {
	window.open(Url,'','scrollbars=1;resizeable=1,height=660,width=650');
}

function addTo(Destination) {
	Url = "Default.aspx?p=";
	if (Destination == "Wishlist")
		Url += "13";
	else
		Url += "5";
	document.forms[1].action = Url;
	document.forms[1].submit();
}

function qtyChanged(el) {
	if (el.value == "") {
		el.select();
		el.focus();			
		alert("please specify a quantity");
		return false;
	}
	if (!gOrdQty.test(el.value)) {
		alert("quantity must be between 1 and 1000");
		el.select();
		el.focus();		
		return false;
	}
	EndPos = el.id.indexOf("_")+1;
	StartPos = el.id.length;
	ProductId = el.id.substring(StartPos,EndPos);
	QtyId = "Qty_" + ProductId;
	UnitId = "Unit_" + ProductId;
	TotalId = "Total_" + ProductId;
	TaxId = "Tax_" + ProductId;	
	ATSId = "ATS_" + ProductId;
	Qty = el.value;
	Qty *= 1;
	ATS = $(ATSId).value;
	ATS *= 1;
	if ((ATS - Qty) < 0) {
		alert("we're sorry but the we are unable to fulfil the quantity you've requested");
		$(QtyId).select();
		$(QtyId).focus();
		return false;	
	}
	SubTotalId = "SubTotal";
	UnitPrice = $(UnitId).innerHTML;
	UnitPrice *= 1;
	Tax = $(TaxId).innerHTML;
	Tax *= 1;
	ExtdPrice = (UnitPrice + Tax) * el.value;
	OrderSub = $(SubTotalId).innerHTML;
	OrderSub -= $(TotalId).innerHTML;
	OrderSub += ExtdPrice;
	$(TotalId).innerHTML = formatAsMoney(ExtdPrice);
	$(SubTotalId).innerHTML = formatAsMoney(OrderSub);
	
	var url = "services/ShoppingCart.asmx/UpdateItemInCart";
	var request = "CartId="+$F('CartId')+"&ProductId="+ProductId+"&Quantity="+el.value;
	var options = { method : 'post',
									postBody : request,																		
									asynchronous : true,
									onSuccess: qtyChangedSuccess,
									onFailure: qtyChangedFailure };
	var myAjax = new Ajax.Request(url, options);
}// qtyChanged()

function qtyChangedSuccess(r) {
}
function qtyChangedFailure(r) {
}
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 confirmDelete(el){
	if (confirm("Are you sure you wish to remove this item from your cart?")) {
		var url = "services/ShoppingCart.asmx/RemoveItemFromCart";
		EndPos = el.id.indexOf("_")+1;
		StartPos = el.id.length;
		ProductId = el.id.substring(StartPos,EndPos);		
		var request = "CartId="+$F('CartId')+"&ProductId="+ProductId;
		var options = { method : 'post',
									postBody : request,																		
									asynchronous : true,
									onSuccess: confirmDeleteSuccess,
									onFailure: confirmDeleteFailure };
		var myAjax = new Ajax.Request(url, options);
	}
} //confirmDelete
function confirmDeleteSuccess(r) {
	window.location.href="Default.aspx?p=5";
}
function confirmDeleteFailure(r) {
}
function emptyCart(){
	if (confirm("Are you sure you wish to empty your entire cart?")) {
		var url = "services/ShoppingCart.asmx/EmptyCart";
		var request = "CartId="+$F('CartId');
		var options = { method : 'post',
									postBody : request,																		
									asynchronous : true,
									onSuccess: emptyCartSuccess,
									onFailure: emptyCartFailure };
		var myAjax = new Ajax.Request(url, options);
	}else{
	}
} //emptyCart
function emptyCartSuccess(r) {
	window.location.href="Default.aspx?p=5";
}
function emptyCartFailure(r) {
}
function checkout() {
	window.location.href="Default.aspx?p=15";
}
function backToCart() {
	window.location.href="Default.aspx?p=5";
}
function backToDelivery() {
	window.location.href="Default.aspx?p=15";
}
function backToBilling() {
	window.location.href="Default.aspx?p=16";
}
function clearAddress() {
  $("firstname").value = "";
	$("lastname").value = "";
	$("phone").value = "";
	$("email").value = "";
	$("addressline1").value = "";
	$("addressline2").value = "";
	$("city").value = "";
	$("postcode").value = "";
	$("country").value = $("dc").value;
}
function setAddress() {
	var oJSON = JSON.eval($("dd").value);
  $("firstname").value = oJSON.firstname;
	$("lastname").value = oJSON.lastname;
	$("phone").value = oJSON.phone;
	$("email").value = oJSON.email;
	$("addressline1").value = oJSON.line1;
	$("addressline2").value = oJSON.line2;
	$("city").value = oJSON.city;
	$("postcode").value = oJSON.postcode;
	$("country").value = oJSON.country;
}
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)) {
    $("firstname").value = "Ian";
		$("lastname").value = "Tester";
		$("phone").value = "02087589055";
		$("email").value = "ian@ko-de.com";
		$("addressline1").value = "9/38 Kew Bridge Rd";
		$("addressline2").value = "Brentford";
		$("city").value = "Middlesex";
		$("postcode").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 ) );
}