// based on script created by krstofer@teamshibby.com. Based off of script found @ w3schools.com
var xmlHttp

function showData() { 
	document.getElementById(whichElement).innerHTML = "<div align=center><b>Collecting Available Dates....</b></div>"
	var theForm = ""
	var howManyElement = ""
	var daString = ""

	theForm = document.deliveryAddressBook
	howManyElement = theForm.elements.length;
	xmlHttp=GetXmlHttpObject();


	for (i=0; i<howManyElement; i++){
	 daString = daString + theForm.elements[i].name+ "="+theForm.elements[i].value+"&";
	}

	if (xmlHttp==null) {  alert ("Your browser does not support AJAX!");  return;  } 
 
	//the 'whichLink' variable is assigned on the page making the request. 
	var url=whichLink;
	url=url+"?"+daString;
	url=url+"sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}
	function stateChanged() { 
		if (xmlHttp.readyState==4) { document.getElementById(whichElement).innerHTML=xmlHttp.responseText; } 
	}

	// ****************************************************
	// ======= Function to show the tweets on the home page
	function showTweets() { 
		document.getElementById("showTweet").innerHTML = "<div align=center><b>Just getting the tweets for you....</b></div>"
	
		xmlHttp=GetXmlHttpObject();
	
		if (xmlHttp==null) {  alert ("Your browser does not support AJAX!");  return;  } 
	 
		var url="tweet.asp";
			xmlHttp.onreadystatechange=tweetsChanged;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(null);
		
	}
	
	function tweetsChanged() { 
		if (xmlHttp.readyState==4) { document.getElementById("showTweet").innerHTML=xmlHttp.responseText; } 
	}


	// ************************************

	function showCart() { 
		document.getElementById("showCheckout").innerHTML = "<div align=center><b>Just getting the shopping cart for you....</b></div>"
	
		xmlHttp=GetXmlHttpObject();
	
		if (xmlHttp==null) {  alert ("Your browser does not support AJAX!");  return;  } 
	 
		var url="/shop/UD__cart.external.asp";
			xmlHttp.onreadystatechange=cartChanged;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(null);
		
	}
	
	function cartChanged() { 
		if (xmlHttp.readyState==4) { document.getElementById("showCheckout").innerHTML=xmlHttp.responseText; } 
	}


	// ************************************

	function fetchDeliveryAddressData() { 
	document.getElementById("addressList").innerHTML = "<div align=center><strong>Collecting Available Addresses....</strong><br /><img src=\"/images/build/ajax-loader.gif\" /></div>"
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {  alert ("Your browser does not support AJAX!");  return;  } 
	 
		var url="/ajax/delivery.address.asp?pcodeLookUp=" + escape(document.deliveryAddressBook.pcodeLookUp.value);
			xmlHttp.onreadystatechange=DeliveryAddressData;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(null);
		
	}
	
	function DeliveryAddressData() { 
		//if (xmlHttp.readyState==4) { document.getElementById("addressListSelect").innerHTML=xmlHttp.responseText; } 
		if (xmlHttp.readyState==4) { 
		//JavaScript:fetchDeliveryAddressById(this.value);
			if(xmlHttp.responseText==0) {
				alert("Sorry but we do not delivery to that postcode, why not try an agent or collection from our shop\n\nYou could always try our National Website which offers next day delivery and deliveres anywhere in the UK\n\nhttp://www.cakesnextday.co.uk"); 
				document.getElementById("LinkToExternalWeb").innerHTML = '<a href="http://www.cakesnextday.co.uk" target="_blank">Click here to navigate to our national next day cake delivery service web site</a>'; 
				document.deliveryAddressBook.postcodeA.value = ""; 
				document.deliveryAddressBook.postcodeB.value = "";
				document.getElementById("addressList").innerHTML='<center><a style="color:red;" href="http://www.cakesnextday.co.uk" target="_blank">Click here to navigate to our national next day cake delivery service web site</a></center>';
				document.getElementById("innerfetchPostCode").style.height = '50px';
				}else{
				document.getElementById("addressList").innerHTML='<div style="border:0"><select id="addressListSelect" onChange="JavaScript:fetchDeliveryAddressById(this.value);">' + xmlHttp.responseText + '</select></div>'; 
			}
		} 
	}


	// ************************************

	function fetchDeliveryAddressById( id ) { 
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {  alert ("Your browser does not support AJAX!");  return;  } 
	 
		var url="/ajax/delivery.getAddressById.asp?id=" + id;
			xmlHttp.onreadystatechange=DeliveryAddressById;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(null);
		
	}
	
	function DeliveryAddressById() { 
		if (xmlHttp.readyState==4) { 
			document.getElementById("deliveryAddress").style.display = 'block'; 
			document.getElementById("fetchPostCode").style.display = 'none'; 
			document.getElementById("deliveryAddress").style.height = '310px'; 
			document.getElementById("deliveryAddress").innerHTML=xmlHttp.responseText; 
				if(document.getElementById("whichsite").value == "CND") {
							showData();
						}else{
							dPrice(document.getElementById("postcodeA").value);
						}
		} 
	}

	// ************************************
	// Process the London deliveries in BackOffice
	function processAjax( oid ) { 
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {  alert ("Your browser does not support AJAX!");  return;  } 
		var url="/ajax/orderProcess.deliveryprocessFinal.asp?" + oid;
			xmlHttp.onreadystatechange=processAjaxChanged;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(null);
		
	}
	
	function processAjaxChanged() { 
		if (xmlHttp.readyState==4) { document.getElementById("EntryList").innerHTML=xmlHttp.responseText; } 
	}
	// ************************************


	// UNIVERSAL HTTP Object Code
		function GetXmlHttpObject(){
			try{
			xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
			return xmlHttp;
			}
				catch (e){
					try{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
					return xmlHttp;
					}
						catch (e){
							try{
							xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
							return xmlHttp;
							}
							catch (e){
							alert("Your browser does not support AJAX.");
							return false;
							}
						}
				}
		}