	// global functions
	function GetXmlHttpObject()
	{ 
		var objXMLHttp = null
		if (window.XMLHttpRequest)
		{
			objXMLHttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return objXMLHttp;
	}

	function AjaxRefreshLingerieSearchMatches()
	{
		var index, element, size, colour, type;
		
		// size
		element = document.getElementById("lingerie_search_size");
		index = element.selectedIndex;
		size = element[index].value;
		
		// colour
		element = document.getElementById("lingerie_search_colour");
		index = element.selectedIndex;
		colour = element[index].value;
		
		// range
		element = document.getElementById("lingerie_search_type");
		index = element.selectedIndex;
		type = element[index].value;
		
		// fire off request
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null)
		{
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "ajax/ajax_lingerie_search.php";
		url = url + "?size=" + size;
		url = url + "&colour=" + colour;
		url = url + "&type=" + type;
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = stateChangedAjaxRefreshLingerieSearchMatches;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	} 

	function stateChangedAjaxRefreshLingerieSearchMatches() 
	{ 
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			var result;
			var elements = new Array();
			var output_string;
			
			result = xmlHttp.responseText;
			
			document.getElementById("searchMatches").innerHTML = result;
		} 
		return;
	}
	
	// updates heard about us when order complete
	function AjaxUpdateHeardAboutUs(order_ref, heardaboutus)
	{
		// fire off request
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null)
		{
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "ajax/ajax_checkout_complete_heard_about_us.php";
		url = url + "?orderref=" + order_ref;
		url = url + "&heardaboutus=" + heardaboutus;
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = stateChangedAjaxUpdateHeardAboutUs;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	} 

	function stateChangedAjaxUpdateHeardAboutUs() 
	{ 
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			var result;
			result = xmlHttp.responseText;
			document.getElementById("heardaboutus_container").innerHTML = result;
		} 
		return;
	}

	// basket function - change discount code
	function changeDiscountCode()
	{
		document.getElementById("discount_code").value = "";
		document.basket.submit();
	}
	
	// basket function - verify that discount code has been updated before proceeding to checkout
	function proceedToCheckout()
	{
		var discount = document.getElementById("discount_code").value;
		if (document.getElementById("discount_code").type == "text" && discount.length > 0)
		{
			//	prompt user to update basket before proceeding
			alert("You've entered a discount code - please ensure you choose update basket first to verify and activate the discount code before proceeding to checkout");
			return false;
		}
		else
		{
			// proceed to checkout
			window.location.href = "/checkout?pg=address";
		}
	}
	
	function checkDeclarationBeforeProceedingAndPlaceOrder()
	{
		var declaration = document.getElementById("declaration");
		if (declaration.checked)
		{
			placeOrder();
		}
		else
		{
			alert("Please tick the box to confirm that you have accepted our terms & conditions and returns policy");
			declaration.focus();
			return false;
		}
		
		return false;
	}
	
	function toggleDeclaration()
	{
		var declaration_label = document.getElementById("lbl_declaration_required");
		var declaration = document.getElementById("declaration");
		if (declaration.checked)
		{
			declaration_label.style.color = "#009900";
			declaration_label.innerHTML = "Thanks";
		}
		else
		{
			declaration_label.style.color = "#CC0000";
			declaration_label.innerHTML = "(Required)";
		}
	}

	function placeOrder()
	{
		// hide button and show waiting graphic
		$("#btn_place_order_container").html("<img src=\"img/loader.gif\" alt=\"Please wait...\" width=\"16\" />"); 	
	
		// check if we have a mastectomy checkbox and append value if necessary
		var extra_get_params = "";
		var value = "";
		if ($("#mastectomy_declaration"))
		{
			if ($("#mastectomy_declaration").attr("checked"))
			{
				value = 1;	
			}
			else
			{
				value = 0;	
			}
			extra_get_params = "?mastectomy_declaration=" + value;
		}
		else
		{
			extra_get_params = "";	
		}
		
		// store order information and retreive json 
		$.ajax({ type: "GET", url: "ajax/ajax_place_order.php" + extra_get_params, async: false, dataType: "json", success: function(data) {
			// now redirect user
			if (data.status == "success")
			{
				window.location.href = "/make-payment?order_ref=" + data.order_ref;
			}
		}});
	}
	
	function cancelOrder(order_ref)
	{
		// cancel order
		$.ajax({ type: "GET", url: "ajax/ajax_cancel_order.php?order_ref=" + order_ref, async: false, dataType: "json", success: function(data) {
		
			if (data.status == "success")
			{
				window.location.href = "/checkout?pg=cancelled&order_ref=" + order_ref;	
			}
		}});
	}
	
	function confirmMarkAsPaid(order_ref)
	{
		if (confirm("Are you sure you wish to mark this order as paid?\n\nNOTE:  It is important that you have definitely received the money for this order either via telephone/terminal as once this is done, the order will be marked as paid and sync into Sage 50 and be processed."))
		{
			window.location.href = "/admin-console?page=orders&mark_as_paid=1&order_ref=" + order_ref;
			return true;
		}
		else
		{
			return false;	
		}
	}
	
	function toggleOtherPleaseSpecifyBox()
	{	
		// show/hide other when required
		if ($("#heardaboutus").val() == "Other")
		{
			$("#other_please_specify_container").show();
		}
		else
		{
			$("#other_please_specify_container").hide();
		}	
	}
	
	function toggleDeliveryMethod()
	{
		var country;
		if ($("#f_use_billing_address").attr("checked"))
		{
			country = $("#f_billing_country").val();
		}
		else
		{
			country = $("#f_shipping_country").val();	
		}
		
		if (country == "826|United Kingdom|GB")
		{
			$("#delivery_method").slideDown("fast");
		}
		else
		{
			$("#delivery_method").slideUp("fast");	
		}
	}
	
	function toggleUSState(billing_or_shipping)
	{
		country = $("#f_" + billing_or_shipping + "_country").val();
		if (country == "840|United States|US")
		{
			$("#f_" + billing_or_shipping + "_state_container").slideDown("fast");
		}
		else
		{
			// hide state
			$("#f_" + billing_or_shipping + "_state_container").slideUp("fast");
		}
	}
	
	function handleIrelandPostcode(billing_or_shipping)
	{
		// Ireland do not use postcode, but SagePay enforce it as a required field.  As such, put "None" for the safest option
		country = $("#f_" + billing_or_shipping + "_country").val();
		if (country == "372|Ireland|IE")
		{
			postcode = $("#f_" + billing_or_shipping + "_postcode").val();
			if (postcode == "" || postcode == "N/a" || postcode == "n/a" || postcode == "N / a" || postcode == "n / a" || postcode == "Na" || postcode == "na")
			{
				$("#f_" + billing_or_shipping + "_postcode").val("None");					
			}
		}
	}
	
	function showNextDayWarning(next_day_value)
	{
		// check customer has selected next day
		if ($("#delivery_service").val() == next_day_value)
		{
			// check what time is it
			$.ajax({
				url: "ajax/ajax_server_date_time.php",
				async: false,
				dataType: "json",
				success: function(day_time) {
					if (day_time.day == "Friday" || day_time.day == "Saturday" || day_time.day == "Sunday")
					{
						var position_left = $("#delivery_service").offset().left + 400;
						var position_top = $("#delivery_service").offset().top;
						$("#delivery_service").position().top;
						Tip(document.getElementById("nextDayDeliveryDayOfTheWeekHTML").innerHTML, FIX, [position_left, position_top], WIDTH, 300, TITLE, "Next day delivery - Friday to Sunday", SHADOW, false, FADEIN, 300, FADEOUT, 300, STICKY, 1, CLOSEBTN, true, TITLEBGCOLOR, '#ff708e', BORDERCOLOR, '#ff708e', BGCOLOR, '#FFFFFF', CLOSEBTNCOLORS, ['', '', 'white', 'black']);
					}
					if (day_time.day == "Thursday" && day_time.time >= 12)
					{
						var position_left = $("#delivery_service").offset().left + 400;
						var position_top = $("#delivery_service").offset().top;
						$("#delivery_service").position().top;
						Tip(document.getElementById("nextDayDeliveryTimeOfTheDayThursdayHTML").innerHTML, FIX, [position_left, position_top], WIDTH, 300, TITLE, "Next day delivery - Thursday after 12pm", SHADOW, false, FADEIN, 300, FADEOUT, 300, STICKY, 1, CLOSEBTN, true, TITLEBGCOLOR, '#ff708e', BORDERCOLOR, '#ff708e', BGCOLOR, '#FFFFFF', CLOSEBTNCOLORS, ['', '', 'white', 'black']);
					}
					else if(day_time.time >= 12)
					{
						Tip(document.getElementById('nextDayDeliveryTimeOfTheDayHTML').innerHTML, WIDTH, 300, TITLE, 'Next Day Delivery - After 12pm', SHADOW, false, FADEIN, 300, FADEOUT, 300, STICKY, 1, CLOSEBTN, true, CLICKCLOSE, true, TITLEBGCOLOR, '#ff708e', BORDERCOLOR, '#ff708e', BGCOLOR, '#FFFFFF', CLOSEBTNCOLORS, ['', '', 'white', 'black']);
					}
				}
			});	
		}
		else
		{
			UnTip();	
		}
	}

	// XHTML strict target attribute replacement.  uses rel="external" 
	function externalLinks() 
	{   
		if (!document.getElementsByTagName) return;   
		var anchors = document.getElementsByTagName("a");   
		for (var i=0; i<anchors.length; i++) 
		{   
			var anchor = anchors[i];   
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")   
			{
				anchor.target = "_blank";   
			}   
		}   
	}
	
	window.onload = externalLinks;
