function RecalculateCart() {
	var quantity = document.getElementsByTagName("input");
	for (var i=0;i<quantity.length;i++) {
		if (quantity[i].id == "txtQuantity") {
			if (isNaN(parseInt(quantity[i].value)) || parseInt(quantity[i].value) == 0) {
				alert("Please enter a valid quantity");
				quantity[i].select();
				return false;
			}
		}
	}
	document.getElementById("formCheckout").submit();
}


function UpdateDeliveryFee(dropdown, Category, SubCategory, checkout) {
	var deliveryFee = parseFloat(dropdown[dropdown.selectedIndex].value);
	document.location.href = "updatebasket.asp?Category=" + Category + "&SubCategory=" + SubCategory + "&DeliveryFee=" + deliveryFee + (checkout ? "&Checkout=true" : "");
}

function copyDeliveryInfo() {
	var checkbox = document.getElementById("chkCopyDeliveryInfo");
	var formPayment = document.getElementById("formPayment");
	if (checkbox && formPayment) {
		if (checkbox.checked) {
			formPayment.Billing_Name.readOnly = true;
			formPayment.Billing_Name.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Company_Name.readOnly = true;
			formPayment.Billing_Company_Name.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Address.readOnly = true;
			formPayment.Billing_Address.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Street.readOnly = true;
			formPayment.Billing_Street.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Town.readOnly = true;
			formPayment.Billing_Town.style.backgroundColor = '#eeeeee';
			formPayment.Billing_City.readOnly = true;
			formPayment.Billing_City.style.backgroundColor = '#eeeeee';
			formPayment.Billing_County.readOnly = true;
			formPayment.Billing_County.style.backgroundColor = '#eeeeee';
			formPayment.Billing_PostCode.readOnly = true;
			formPayment.Billing_PostCode.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Country.readOnly = true;
			formPayment.Billing_Country.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Telephone.readOnly = true;
			formPayment.Billing_Telephone.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Fax.readOnly = true;
			formPayment.Billing_Fax.style.backgroundColor = '#eeeeee';
			formPayment.Billing_Email.readOnly = true;
			formPayment.Billing_Email.style.backgroundColor = '#eeeeee';
			
			formPayment.Billing_Name.value = formPayment.Delivery_Name.value;
			formPayment.Billing_Company_Name.value = formPayment.Delivery_Company_Name.value;
			formPayment.Billing_Address.value = formPayment.Delivery_Address.value;
			formPayment.Billing_Street.value = formPayment.Delivery_Street.value;
			formPayment.Billing_Town.value = formPayment.Delivery_Town.value;
			formPayment.Billing_City.value = formPayment.Delivery_City.value;
			formPayment.Billing_County.value = formPayment.Delivery_County.value;
			formPayment.Billing_PostCode.value = formPayment.Delivery_PostCode.value;
			formPayment.Billing_Country.value = formPayment.Delivery_Country.value;
			formPayment.Billing_Telephone.value = formPayment.Delivery_Telephone.value;
			formPayment.Billing_Fax.value = formPayment.Delivery_Fax.value;
			formPayment.Billing_Email.value = formPayment.Delivery_Email.value;
		} else {
			formPayment.Billing_Name.readOnly = false;
			formPayment.Billing_Name.style.backgroundColor = '#ffffff';
			formPayment.Billing_Company_Name.readOnly = false;
			formPayment.Billing_Company_Name.style.backgroundColor = '#ffffff';
			formPayment.Billing_Address.readOnly = false;
			formPayment.Billing_Address.style.backgroundColor = '#ffffff';
			formPayment.Billing_Street.readOnly = false;
			formPayment.Billing_Street.style.backgroundColor = '#ffffff';
			formPayment.Billing_Town.readOnly = false;
			formPayment.Billing_Town.style.backgroundColor = '#ffffff';
			formPayment.Billing_City.readOnly = false;
			formPayment.Billing_City.style.backgroundColor = '#ffffff';
			formPayment.Billing_County.readOnly = false;
			formPayment.Billing_County.style.backgroundColor = '#ffffff';
			formPayment.Billing_PostCode.readOnly = false;
			formPayment.Billing_PostCode.style.backgroundColor = '#ffffff';
			formPayment.Billing_Country.readOnly = false;
			formPayment.Billing_Country.style.backgroundColor = '#ffffff';
			formPayment.Billing_Telephone.readOnly = false;
			formPayment.Billing_Telephone.style.backgroundColor = '#ffffff';
			formPayment.Billing_Fax.readOnly = false;
			formPayment.Billing_Fax.style.backgroundColor = '#ffffff';
			formPayment.Billing_Email.readOnly = false;
			formPayment.Billing_Email.style.backgroundColor = '#ffffff';

			formPayment.Billing_Name.value = "";
			formPayment.Billing_Company_Name.value = "";
			formPayment.Billing_Address.value = "";
			formPayment.Billing_Street.value = "";
			formPayment.Billing_Town.value = "";
			formPayment.Billing_City.value = "";
			formPayment.Billing_County.value = "";
			formPayment.Billing_PostCode.value = "";
			formPayment.Billing_Country.value = "";
			formPayment.Billing_Telephone.value = "";
			formPayment.Billing_Fax.value = "";
			formPayment.Billing_Email.value = "";
		}
	}
}

function submitPaymentForm() {
	var formPayment = document.getElementById("formPayment");
	if (formPayment && validatePaymentForm(formPayment)) {
		formPayment.submit();
	}
}

function validatePaymentForm(theForm) {
	if (theForm.Delivery_Name.value == ""){
		alert("Please enter the \"Delivery Name\".");
    	theForm.Delivery_Name.focus();
    	return (false);
	}
	if (theForm.Delivery_Address.value == ""){
    	alert("Please enter the \"Delivery Building Number\".");
	    theForm.Delivery_Address.focus();
    	return (false);
	}
	if (theForm.Delivery_Street.value == ""){
    	alert("Please enter the \"Delivery Street\".");
    	theForm.Delivery_Street.focus();
	    return (false);
  	}
  	if (theForm.Delivery_City.value == ""){
    	alert("Please enter the \"Delivery City\".");
	    theForm.Delivery_City.focus();
    	return (false);
  	}
  	if (theForm.Delivery_PostCode.value == ""){
    	alert("Please enter the \"Delivery PostCode\".");
    	theForm.Delivery_PostCode.focus();
	    return (false);
  	}             
  	if(theForm.Delivery_Country.value == "")
  	{
  		alert("Please enter the \"Delivery Country\".");
  		theForm.Delivery_Country.focus();
	  	return (false);
  	}
  	if (theForm.Delivery_Telephone.value == ""){
    	alert("Please enter the \"Delivery Telephone\".");
		theForm.Delivery_Telephone.focus();
    	return (false);
	} 
	if (theForm.Delivery_Email.value == ""){
    	alert("Please enter the \"Delivery Email\".");
    	theForm.Delivery_Email.focus();
		return (false);
	}
	if (document.getElementById("spanDeliveryFee")) {
		var deliveryFee = parseFloat(document.getElementById("spanDeliveryFee").innerHTML);
		if (theForm.Delivery_Country.value == "United Kingdom")
		{
			if (deliveryFee > 19.50) {
				alert("The carriage you have chosen is for delivery outside the UK mainland.\nPlease amend your delivery address or return to the shopping cart & amend your carriage selection.");
				return (false);
			}
		} else if (theForm.Delivery_Country.value == "Isle Of Man" || theForm.Delivery_Country.value == "Guernsey" || theForm.Delivery_Country.value == "Jersey") {
			if (deliveryFee != 35.00) {
				alert("You must choose the carriage for delivery to UK islands.\nPlease return to shopping cart & amend your selection.");
				return (false);
			}
		} else {
			if (deliveryFee < 50.00) {
				alert("You must choose the carriage for overseas delivery.\nPlease return to shopping cart & amend your selection.");
				return (false);
			}
		}
	}
	
	if (theForm.chkCopyDeliveryInfo.checked) {
		theForm.Billing_Name.value = theForm.Delivery_Name.value;
		theForm.Billing_Company_Name.value = theForm.Delivery_Company_Name.value;
		theForm.Billing_Address.value = theForm.Delivery_Address.value;
		theForm.Billing_Street.value = theForm.Delivery_Street.value;
		theForm.Billing_Town.value = theForm.Delivery_Town.value;
		theForm.Billing_City.value = theForm.Delivery_City.value;
		theForm.Billing_County.value = theForm.Delivery_County.value;
		theForm.Billing_PostCode.value = theForm.Delivery_PostCode.value;
		theForm.Billing_Country.value = theForm.Delivery_Country.value;
		theForm.Billing_Telephone.value = theForm.Delivery_Telephone.value;
		theForm.Billing_Fax.value = theForm.Delivery_Fax.value;
		theForm.Billing_Email.value = theForm.Delivery_Email.value;
	} else {
		if (theForm.Billing_Name.value == ""){
			alert("Please enter the \"Billing Name\".");
			theForm.Billing_Name.focus();
			return (false);
		}
		if (theForm.Billing_Address.value == ""){
			alert("Please enter the \"Billing Building Number\".");
			theForm.Billing_Address.focus();
			return (false);
		}
		if (theForm.Billing_Street.value == ""){
			alert("Please enter the \"Billing Street\".");
			theForm.Billing_Street.focus();
			return (false);
		}
		if (theForm.Billing_City.value == ""){
			alert("Please enter the \"Billing City\".");
			theForm.Billing_City.focus();
			return (false);
		}
		if (theForm.Billing_PostCode.value == ""){
			alert("Please enter the \"Billing PostCode\".");
			theForm.Billing_PostCode.focus();
			return (false);
		}             
		if(theForm.Billing_Country.value == ""){
			alert("Please enter the \"Billing Country\".");
			theForm.Billing_Country.focus();
			return (false);
		}
		if (theForm.Billing_Telephone.value == ""){
			alert("Please enter the \"Billing Telephone\".");
			theForm.Billing_Telephone.focus();
			return (false);
		} 
		if (theForm.Billing_Email.value == ""){
			alert("Please enter the \"Billing Email\".");
			theForm.Billing_Email.focus();
			return (false);
		}
	}

	if (theForm.CardHolder.value == ""){
    	alert("Please enter the \"Card Holder Name\".");
		theForm.CardHolder.focus();
    	return (false);
	} 
  	if (theForm.CardNumber.value == ""){
    	alert("Please enter the \"Card Number\".");
		theForm.CardNumber.focus();
    	return (false);
	} 
  	if (theForm.ExpiryDate.value == ""){
    	alert("Please enter the \"Expiry Date\".");
		theForm.ExpiryDate.focus();
    	return (false);
	} 
  	if (theForm.CV2.value == ""){
    	alert("Please enter the \"CV2 Number\".");
		theForm.CV2.focus();
    	return (false);
	}
	
	if (!theForm.TermsConditions.checked) {
    	alert("You must agree to and fully understand the terms and conditions of your online purchase.")
		theForm.TermsConditions.focus();
    	return (false);
	}
	
	//Set complete addresses
	theForm.CompleteDeliveryAddress.value = theForm.Delivery_Address.value + " " + theForm.Delivery_Street.value;
	if (theForm.Delivery_Town.value != "") theForm.CompleteDeliveryAddress.value += "\n" + theForm.Delivery_Town.value;
	theForm.CompleteDeliveryAddress.value += "\n" + theForm.Delivery_City.value;
	if (theForm.Delivery_County.value != "") theForm.CompleteDeliveryAddress.value += "\n" + theForm.Delivery_County.value;
	theForm.CompleteDeliveryAddress.value += "\n" + theForm.Delivery_Country.value;
	
	if (theForm.chkCopyDeliveryInfo.checked) {
		theForm.CompleteBillingAddress.value = theForm.CompleteDeliveryAddress.value
	} else {
		theForm.CompleteBillingAddress.value = theForm.Billing_Address.value + " " + theForm.Billing_Street.value;
		if (theForm.Billing_Town.value != "") theForm.CompleteBillingAddress.value += "\n" + theForm.Billing_Town.value;
		theForm.CompleteBillingAddress.value += "\n" + theForm.Billing_City.value;
		if (theForm.Billing_County.value != "") theForm.CompleteBillingAddress.value += "\n" + theForm.Billing_County.value;
		theForm.CompleteBillingAddress.value += "\n" + theForm.Billing_Country.value;
	}
	
	return (true);
}

function displayCV2Info() {
    var html = "<html><head><link  rel=\"stylesheet\" href=\"cssstyles/cssstyles.css\" type=\"text/css\"></head><body style=\"background-color: #FFFFFF\" leftmargin=\"0\" rightmargin=\"0\" topmargin=\"0\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"10\"><tr><td valign=\"top\">&nbsp;</td><td valign=\"top\"><br/>All credit and debit cards carry a security code number. This number is known to the bank and printed on the card, but is not stored or printed anywhere else. Therefore, it can be used as a check that when you make your purchase you are in physical possession of the card, or have at least seen the card at some time. On most cards, the security code number is the last three digits of the number printed on the back, at the top right of the signature strip. You will be required to enter this additional security number before your transaction can be completed.</td></tr></table></body>";
    var openWindow = window.open('','','width=600,height=200,left=100,top=100,resizable=yes,scrollbars=yes,status=yes');
    openWindow.document.write(html);
	openWindow.document.close();
}

function printTermsConditions(){
	var div = document.getElementById("divTermsConditions");
	if (div) {
	    var html = "<html><head><link  rel=\"stylesheet\" href=\"cssstyles/cssstyles.css\" type=\"text/css\"></head><body style=\"background-color: #FFFFFF\" leftmargin=\"0\" rightmargin=\"0\" topmargin=\"0\"><div style=\"padding:10px\">" + div.innerHTML + "</div></body>";
	    var openWindow = window.open('','','width=800,height=600,left=100,top=100,resizable=yes,scrollbars=yes,status=yes');
	    openWindow.document.write(html);
		openWindow.document.close();
	    openWindow.print();
	}
}

function UpdateProductOption(optionTextID, optionPriceID, optionID) {
	var optionUpdateForm = document.getElementById("ProductOptionsForm");
	var getNewOptionText = document.getElementById(optionTextID).value;
	var getNewOptionPrice = document.getElementById(optionPriceID).value;	
	
	//alert("Currently under development!")

	var thisNewText = document.getElementById("UpdateOption");  
	thisNewText.value = getNewOptionText;
	
	var thisNewPrice = document.getElementById("UpdatePrice");  
	thisNewPrice.value = getNewOptionPrice;
	
	var thisID = document.getElementById("OptionId"); 
	thisID.value = optionID;
	
	optionUpdateForm.submit();
	
	
}

function outputList(ar, name, size) {
  var strIDs = "<SELECT SIZE=\"" + size + "\" NAME=\"ro_lst" + name + "\">";
  var sel = " SELECTED";
  for (var i=0;i<ar.length;i++) {
    strIDs += "<OPTION " + sel + " VALUE=\"" + ar[i][0] + "\">" + ar[i][1];
    sel = "";
  }
  strIDs+="</SELECT>";
  strIDs+="<INPUT NAME=\"" + name + "\" TYPE=hidden>";
  return strIDs;
}

function outputButton(bDir,name,val) {
  return "<INPUT TYPE=button VALUE=\"" + val + "\" ONCLICK=\"move(this.form," + bDir + ",'" + name + "')\">"
}

function move(f,bDir,sName) {
 var el = f.elements["ro_lst" + sName]
 var idx = el.selectedIndex
 if (idx==-1) 
  alert("You must first select the item to reorder.")
 else {
  var nxidx = idx+( bDir? -1 : 1)
  if (nxidx<0) nxidx=el.length-1
  if (nxidx>=el.length) nxidx=0
  var oldVal = el[idx].value
  var oldText = el[idx].text
  el[idx].value = el[nxidx].value
  el[idx].text = el[nxidx].text
  el[nxidx].value = oldVal
  el[nxidx].text = oldText
  el.selectedIndex = nxidx
 }
}

function processForm(f) {
  for (var i=0;i<f.length;i++) {	
    var el = f[i]
    // If reorder listbox, then generate value for hidden field
    if (el.name.substring(0,6)=="ro_lst") {
      var strIDs = ""
      for (var j=0;j<f[i].options.length;j++)
        strIDs += f[i].options[j].value + ", "
      f.elements[f.elements[i].name.substring(6)].value = 
        strIDs.substring(0,strIDs.length-2)
    }
  }
}


