function clear_location()
{
	setCookie("location", "", 0, "/");
	setCookie("region", "", 0, "/");
	setCookie("location_current", "", 0, "/");
	setCookie("weather_city", "", 0, "/");

	document.getElementById("store_search").innerHTML = '<div class="heading">Craving some KFC?</div><div class="instructions">Find your nearest store<br><span class="sub">Enter your city, town or suburb</span></div><form id="store_search_form" action="store_search.php"><p><input type="text" id="store_search_field" maxlength="18" value="Enter location" onfocus="if(this.value == \'Enter location\') {this.value = \'\'; this.style.color = \'#757575\'}" onblur="if(this.value == \'\') {this.value = \'Enter location\'; this.style.color = \'#cdcdcd\'}" onkeyup="showLocationList(this);"></p><div id="store_search_location_list"></div></form>';
	document.getElementById("store_search").style.backgroundImage = "url('/images/widgets/store_search/bg-1.png')";
}

function set_location(location, region, weather_city)
{
	// you are in...

	document.getElementById("store_search").innerHTML = '<div class="heading">Location</div><div class="location">'+location+'</div><div class="stores">Your nearest store(s)</div><a class="cta_narrow change" href="#" onclick="clear_location();">Change</a><div id="store_search_store_list"></div>';
	document.getElementById("store_search").style.backgroundImage = "url('/images/widgets/store_search/bg-2.png')";
	
	showStoreList(location, region);
	
	setCookie("location", location, 365, "/");
	setCookie("weather_city", weather_city, 365, "/");
	setCookie("region", region, 365, "/");
	setCookie("location_current", "true", null, "/");
	
	update_weather(weather_city);
}


function set_store(store, update_location)
{	
	if(update_location)
	{
			if (window.XMLHttpRequest)
			{// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			}
			else
			{// code for IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					values = xmlhttp.responseText.split(",");

					setCookie("location", values[0], 365, "/");
					setCookie("region", values[1], 365, "/");
					setCookie("weather_city", values[2], 365, "/");

					setCookie("location_current", "true", null, "/");					

					window.location = "/store/"+store;
				}
			}
			xmlhttp.open("GET","/widgets/store_search_location_lookup.php?store="+store,true);
			xmlhttp.send();		
	}
	else
	{
					window.location = "/store/"+store;
	}
}

function check_location()
{
		
	saved_location = getCookie("location");

	if(saved_location != "")
	{
		saved_region = getCookie("region");
		location_current = getCookie("location_current");
		weather_city = getCookie("weather_city");
		
		document.getElementById("store_search").innerHTML = '<div class="heading">Location</div><div class="location">'+saved_location+'</div><div class="stores">Your nearest store(s)</div><a class="cta_narrow change" href="#" onclick="clear_location();">Change</a><div id="store_search_store_list"></div>';

		document.getElementById("store_search").style.backgroundImage = "url('/images/widgets/store_search/bg-2.png')";
	
		showStoreList(saved_location, saved_region);
	
		setCookie("location", saved_location, 365, "/"); // reset 365 days
		setCookie("region", saved_region, 365, "/"); // reset 365 days
		setCookie("weather_city", weather_city, 365, "/"); // reset 365 days
	}
	
	document.getElementById("store_search").style.visibility = "visible";
}


	//selected = -1;

	function showLocationList(element)
	{
		location_list = document.getElementById('store_search_location_list');
		if(element.value.length > 2)
		{
					if (window.XMLHttpRequest)
			{// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			}
			else
			{// code for IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					location_list.innerHTML = xmlhttp.responseText;
					if(xmlhttp.responseText.length > 0)
					{
					location_list.style.visibility = 'visible';
					}
					else
					{
						location_list.style.visibility = 'hidden';
					}
				}
			}
			xmlhttp.open("GET","/widgets/store_search_location_list.php?store="+document.getElementById('store_search_field').value,true);
			xmlhttp.send();
		}
		else
		{
			location_list.style.visibility = 'hidden';
		}
	}





	function showStoreList(location, region)
	{
		store_list = document.getElementById('store_search_store_list');
			if (window.XMLHttpRequest)
			{// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			}
			else
			{// code for IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					store_list.innerHTML = xmlhttp.responseText;

				}
			}
			xmlhttp.open("GET","/widgets/store_search_store_list.php?location="+location+"&region="+region,true);
			xmlhttp.send();
	}

	//function selectLocation(element, event)
	//{
	//	if(event.which == 38)
	//	{
	//		selected = 1;
	//	}
	//	if(event.which == 40)
	//	{
	//		selected = 1;
	//	}
	//	store_search_location_list.childNodes[selected - 1].style.backgroundColor = '#c4102f';
	//	element.value = store_search_location_list.childNodes[selected - 1].childNodes[0].innerHTML;
	//}
