function fromPostcode(e) {
        // Only request a lookup if the user pressed enter from the city field.
        var keynum;
        if(window.event) // IE
        {
                keynum = window.event.keyCode;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
                keynum = e.which;
        }

        var KEY_TAB = 9;
        var KEY_RETURN = 13;
        if (keynum == KEY_RETURN || keynum == KEY_TAB)
        {
                lookupPostcode();

        }
}

function lookupPostcode(e) {
	// Perform a postcode lookup based on postcode

	// Enable spinner, so the user knows the lookup is being performed.
	document.getElementById('store_results').innerHTML = '<img src="http://84.53.97.19/retourette/images/design/ajax-loader.gif" alt="Loading..." style=" vertical-align:bottom;" /> Loading...';


	// The cityname defined by the user.
	var start_postcode = document.getElementById('start_postcode').value;
        // The postcodes of stores defined by you.
        var destination_postcodes = document.getElementById('stores').value;
	// The maximum number of stores to return.
	var max_stores = 1;	
	loadScript('http://6pp.kvdb.net/services/distance?start_postcode=' + start_postcode + '&destination_postcodes=' + destination_postcodes + '&per_page=' + max_stores + '&tg_format=json&jsonp=showStoreResults');
}

function fromCity(e) {
        // Only request a lookup if the user pressed enter from the city field.
        var keynum;
        if(window.event) // IE
        {
                keynum = window.event.keyCode;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
                keynum = e.which;
        }

        var KEY_TAB = 9;
        var KEY_RETURN = 13;
        if (keynum == KEY_RETURN || keynum == KEY_TAB)
        {
                lookupCity();

        }
}

function lookupCity(e) {
	// Perform a postcode lookup based on postcode

	// Enable spinner, so the user knows the lookup is being performed.
	document.getElementById('store_results').innerHTML = '<img src="http://84.53.97.19/retourette/images/design/ajax-loader.gif" alt="Loading..." style=" vertical-align:bottom;" /> Loading...';


	// The cityname defined by the user.
	var start_city = document.getElementById('start_city').value;
        // The postcodes of stores defined by you.
        var destination_postcodes = document.getElementById('stores').value;
	// The maximum number of stores to return.
	var max_stores = 1;	
	loadScript('http://6pp.kvdb.net/services/distance?start_city=' + start_city + '&destination_postcodes=' + destination_postcodes + '&per_page=' + max_stores + '&tg_format=json&jsonp=showStoreResults');
}

function loadScript(url)
{
        // Insert javascript dynamically for JSONP cross-site callback
        var script = document.createElement('script');
        script.src = url;
        document.getElementsByTagName('head')[0].appendChild(script);
}

function resetResults() {
	// Empty the result element
	document.getElementById('store_results').innerHTML = "";
}

function showStoreResults(json) {
	
	// Clear fields.
	resetResults();
	
	// Check the amount of results, or show an error.
	try
	{
		var count = json.result['distances'].length;
	}
	catch(err)
	{
		alert(json.result);
	}

	// Show the stores nearby
	var store_results = document.getElementById('store_results');
	for (var i = 0; i < count; i++)
	{
		var city_name = json.result['distances'][i]['city2'];
		var lat = json.result['distances'][i]['lat2'];
		var lng = json.result['distances'][i]['lng2'];
		
		if (lat && lng) {
			var map = document.getElementById('map');
			// Map was not visible, show it now.
			map.src = 'http://84.53.97.19/retourette/kaart.php?size=small&lat=' + lat + '&long=' + lng;
		}
		
                if(city_name == "Waalre")
                {
        	        document.getElementById('store_results').innerHTML = "<strong>Retourette Aalst/"+ city_name +"</strong><br /><br />Adres: Den Hof 4/6<br />Postcode: 5582 JZ<br />Tel: 040-2212354<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' /><div id='link'><a href='http://84.53.97.19/retourette/index.php/site/loc/' class='site' title='Aalst/"+ city_name +"'>Ga naar site</a></div>";
       	        }
		else if(city_name == "Apeldoorn")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Korianderplein 61/1<br />Postcode:7322 NB<br />Tel: 055-3681818<br /><img src='http://84.53.97.19/retourette/images/design/logos/C1000.png' alt='C1000' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Appingedam")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Overdiep 2<br />Postcode: 9902 BX<br />Tel: 0596-622343<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Asten")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Prins Bernhardstraat 65<br />Postcode: 5721 GE<br />Tel: 0493-696633<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Barendrecht")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Onderlangs 44<br />Postcode: 2991 EM<br />Tel: 0180-482160<br /><img src='http://84.53.97.19/retourette/images/design/logos/Superdeboer.png' alt='Super de Boer' width='50' height='50' hspace=40' />";
		}
                else if(city_name == "Bergeijk")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Bokkerijder 14<br />Postcode: 5571 MX<br />Tel: 0497-577019<br />";
		}
		else if(city_name == "Bergschenhoek")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: De Vlashoek 24<br />Postcode: 2661 LM<br />Tel: 010-5212252<br /><img src='http://84.53.97.19/retourette/images/design/logos/Plus.png' alt='Plus' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Bladel")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Sniederslaan 53<br />Postcode: 5531 EH<br />Tel: 0497-381338<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Den Dolder")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Paduaweg 3<br />Postcode: 3734 GG<br />Tel: 030-2282327<img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Gemert")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Kapelaanstraat 25<br />Postcode: 5421 DD<br />Tel: 0492-361861<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Gennep")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Europaplein 10<br />Postcode: 6591 AZ<br />Tel: 0485-514104<br /><img src='http://84.53.97.19/retourette/images/design/logos/Janlinders.png' alt='Jan Linders' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "'s-Gravendeel")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Langestraat 5-9<br />Postcode: 3295 BJ<br />Tel: 078-6734600<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Hillegom")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Jonkheer Mockkade 6<br />Postcode: 2181 JH<br />Tel: 0252-535020<br /><img src='http://84.53.97.19/retourette/images/design/logos/C1000.png' alt='C1000' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Hoevelaken")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Kantemarsweg 10<br />Postcode: 3871 AP<br />Tel: 033-2571711<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Hoogland")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Kraailandhof 40/20<br />Postcode: 3828 JM<br />Tel: 033-4801261<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
                else if(city_name == "Mill")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette Langenboom</strong><br /><br />Adres: Oranjeboomstraat 1A<br />Postcode: 5451 BK<br />Tel: 0485-454440<br /><img src='http://84.53.97.19/retourette/images/design/logos/Janlinders.png' alt='Jan Linders' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Leek")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: S Leviestraat 21<br />Postcode: 9351 BM<br />Tel: 0594-515244<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
                else if(city_name == "Mierlo")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Dorpsstraat 168<br />Postcode: 5731 JL<br />Tel: 0492 -662115<br /><img src='http://84.53.97.19/retourette/images/design/logos/Plus.png' alt='Plus' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Maassluis")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Koningshoek 93100<br />Postcode: 3144 BA<br />Tel: 010-5919669<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Neede")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Borculoseweg 11<br />Postcode: 7161 GP<br />Tel: 0545-291616<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Nijkerk")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Frieswijkstraat 72<br />Postcode: 3862 BM<br />Tel: -<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Oirschot")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: De Loop 28<br />Postcode: 5688 EW<br />Tel: 0499-320690<br /><img src='http://84.53.97.19/retourette/images/design/logos/C1000.png' alt='C1000' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Oostzaan")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Klaverweide 6/24<br />Postcode: 1511 XW<br />Tel: 075-6841219<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Paterswolde")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Hoofdweg 202<br />Postcode: 9765 CK<br />Tel: 0485-514104<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Schiedam")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Hagastraat 1<br />Postcode: 3114 NC<br />Tel: 010-4731422<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Terneuzen")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Lorentzlaan 2<br />Postcode: 4532 KP<br />Tel: 0115-696519<br /><img src='http://84.53.97.19/retourette/images/design/logos/Jumbo.png' alt='Jumbo' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Veldhoven")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Meiveld 8<br />Postcode: 5501 KA<br />Tel: 040-2542787<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Westervoort")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Dorpsplein 6<br />Postcode: 6931 CZ<br />Tel: 026-3118224<br /><img src='http://84.53.97.19/retourette/images/design/logos/AH.png' alt='AH' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Zeist")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Vrijheidsplein 2/20<br />Postcode: 3705 PS<br />Tel: 030-6994388<br /><img src='http://84.53.97.19/retourette/images/design/logos/Superdeboer.png' alt='Super de Boer' width='50' height='50' hspace=40' />";
		}
		else if(city_name == "Zetten")
		{
			document.getElementById('store_results').innerHTML = "<strong>Retourette "+ city_name +"</strong><br /><br />Adres: Beatrixstraat 7<br />Postcode: 6671 AC<br />Tel: 0488-420451/420056<br /><img src='http://84.53.97.19/retourette/images/design/logos/Janlinders.png' alt='Jan Linders' width='50' height='50' hspace=40' />";
		}
	}
}