var searchBox = {
	oSearchTextBox : Object,
	oSearchForm : Object,
	oAdv : Object,
	oAdvToggle : Object,
	oSearchOptions : Object,
	oFirstSearch : null,
	sID : String,

  init : function () {
		searchBox.oSearchTextBox = document.getElementById('searchTextBox');
		if (searchBox.oSearchTextBox) {
			addEvent(searchBox.oSearchTextBox, 'click', searchBox.action);
		}
		searchBox.oSearchForm = document.getElementById('seachForm');

		searchBox.oAdv = document.getElementById('advancedSearch');
		if (searchBox.oAdv) {
			searchBox.oAdvToggle = document.getElementById('revealSwitch');
			if (searchBox.oAdvToggle) {
				searchBox.oSearchOptions = document.getElementById('searchOptions');
				if (searchBox.oSearchOptions) {
					searchBox.oSearchOptions.style.display = 'none';
					addEvent(searchBox.oAdvToggle, 'click', searchBox.toggleAdvancedSearch);
				}
			}
		}

		storeFinder.resetRadius();
		searchBox.sID = 'all';
	},

  toggleAdvancedSearch : function () {
		if (searchBox.oSearchOptions.style.display === 'none') {
			searchBox.oSearchOptions.style.display = 'block';
		} else {
			searchBox.oSearchOptions.style.display = 'none';
		}
	},

  action : function () {
		var sSearchTextBoxValue = searchBox.oSearchTextBox.getAttribute('value');
		if (sSearchTextBoxValue !== '') {
			searchBox.oSearchTextBox.value = '';
		}
	},

	showAddress : function (address, disambig, updateBox) {
    var rs = document.getElementById('rs'),
      oStoreInformation,
      oLHN;
    if (rs) {
      rs.innerHTML = '<img style="margin:0 auto; padding-top:100px; display:block;" src="icons/loading.results.gif" width="32" height="32" title="" alt="Loading results. Please wait" />';
      rs.style.display = "block";
    }
    if (disambig) {
      resultDisambiguation.showDisambig = null;
    } else {
      resultDisambiguation.showDisambig = 'no';
    }

    if (updateBox) {
      searchBox.oSearchTextBox.value = updateBox;
    }

    searchBox.loadingFlip();

    storeFinder.clearPins();
    resultDisambiguation.clearDisambigResults();

    storeFinder.iLastResults = 0;

    oStoreInformation = document.getElementById('storeInformation');
    if (oStoreInformation) {
      oStoreInformation.style.display = 'none';
    }

    oLHN = document.getElementById('lhn2');
    if (oLHN) {oLHN.style.display = 'none'; }
    
    storeFinder.resetRadius();
    storeFinder.map.Find(null, address + ',uk', null, null, null, null, null, null, null, true, searchBox.onFoundResults);
	},

  onFoundResults : function (a, b, c, d, e) {


//console.log("onFoundResults: a:" + a +", b:" + b + ", c:" + c + ", d:" + d + ", e:" + e);
//console.log(searchBox.sID);


		searchBox.loadingFlip();

		if (searchBox.oFirstSearch !== true) {
			searchBox.oFirstSearch = true;
			var oResults = document.getElementById('rTitleJS');
			if (oResults) {
				oResults.style.display = 'block';
			}
		}

		function radioId(btnNameSet) {
		// returns checked radio button value from a name set
			var c = -1,
        i;
			for (i = btnNameSet.length - 1; i > -1; i--) {
				if (btnNameSet[i].checked) {
					c = i;
					i = -1;
			  }
      }
			if (c > -1) {return btnNameSet[c].id; }
			return null;
		}

		var storeInstore = radioId(document.getElementsByName('defaultSearchOptions'));
		if (storeInstore !== null) {searchBox.sID = storeInstore; }
		storeFinder.findStores(searchBox.sID);
	},

  loadingFlip : function () {
    if (document.getElementById('mapsAppDP')) {
      var oSubmit = document.getElementById('submit');
      if (oSubmit) {
        var sSrc = oSubmit.getAttribute('src');
        if (sSrc === 'icons/button.locate_store.png') {
          oSubmit.setAttribute('src', 'icons/loading.gif');
        } else {
          oSubmit.setAttribute('src', 'icons/button.locate_store.png');
        }
      }
    }
	}
};

var pageViewOptions = {
	init : function () {
	},
	updateURL : function () {
		var sSearchTextBoxValue = searchBox.oSearchTextBox.value;
		var sSearchTextBoxLngLat = searchBox.oSearchTextBox.className;

		if (sSearchTextBoxValue !== 'Enter Postcode or Placename') {
			window.location = '?location=' + sSearchTextBoxValue + '&coord=' + sSearchTextBoxLngLat;
		}
	}
};

// recentre map on link click
var reCentreSearch = {
	reCentre : Object,
	init : function () {
		reCentreSearch.reCentre = document.getElementById('reCentre');
		if (reCentreSearch.reCentre) {
			addEvent(reCentreSearch.reCentre, 'click', reCentreSearch.doIt);
		}
	},
	doIt : function (e) {
		e.returnValue = false; // for IE
		if (typeof e.preventDefault === 'function') {e.preventDefault(); }
		storeFinder.resetRadius();
		storeFinder.findStores(searchBox.sID);
	}
};

var route = {
	onGotRoute : function (route) {
		var steps = '<ol>',
      len = route.Itinerary.Segments.length,
      i,
      routeinfo,
      oRoute;
		for (i = 0; i < len; i++) {
			steps += "<li class='rP" + i + "'>" + route.Itinerary.Segments[i].Instruction + " -- <strong>(";
			steps += route.Itinerary.Segments[i].Distance + ') ';
			steps += route.Itinerary.DistanceUnit + '</strong></li>';
		}
		routeinfo = '<h3>Route Info: <strong>Total Distance: ' + route.Itinerary.Distance + ' ' + route.Itinerary.DistanceUnit + ' ' + len + ' Steps</strong></h3>' + steps + '</ol>';

		oRoute = document.getElementById('rt');
		oRoute.style.display = 'block';

		oRoute.innerHTML = routeinfo;
	}
};


// results added to #rs and #storeInformation
var results = {
	oResultContainer : Object,
	oMap : Object,
	oMapPins : Object,
	storeInformation : Object,
	sCurrentClick : String,
	sStore : String,
	aLngLat : Array,
	jsonStore : null,
	jsonClothing : null,
	jsonPhamacy : null,
	jsonChristmas : null,
	jsonChristmas2 : null,
	jsonEasterHours : null,
	jsonBankHolidayHours : null,
	jsonDirectDesk : null,
	jsonBusPDF : null,
	init : function () {
    var i,
      oP,
      oAnext,
      oAprev,
      oPtext,
      oAnexttext,
      oSeparator,
      oAprevtext,
      oPr,
      oPinny;
		results.oResultContainer = document.getElementById('rs');
		results.storeInformation = document.getElementById('storeInformation');

		if (results.oResultContainer && results.storeInformation) {
			oResultItem = results.oResultContainer.getElementsByTagName('li');
			if (oResultItem) {
				for (i = 0; i < oResultItem.length; i++) {
					if (i < 7) {
						if (i === oResultItem.length) {
							oP = results.oResultContainer.getElementsByTagName('p');
							if (oP) {results.oResultContainer.removeChild(oP[0]); }
						}
					} else if (i === (oResultItem.length - 1)) {
						oResultItem[i].className = 'hide';

						oP = document.getElementById('moreStores');
						if (!oP) {
							oP = document.createElement('p');
							oAnext = document.createElement('a');
							oAprev = document.createElement('a');

							oPtext = document.createTextNode(oResultItem.length + ' local stores, ');
							oAnexttext = document.createTextNode('Next');
							oSeparator = document.createTextNode(' : ');
							oAprevtext = document.createTextNode('Prev');

							oP.setAttribute('id', 'moreStores');
							oAnext.setAttribute('title', 'view next set of stores');
							oAprev.setAttribute('title', 'view previous set of stores');
							oAnext.setAttribute('href', '#zoom');
							oAprev.setAttribute('href', '#zoom');

							addEvent(oAnext, 'click', results.showNextResults);
							addEvent(oAprev, 'click', results.showPrevResults);

							oP.appendChild(oPtext);
							oAnext.appendChild(oAnexttext);
							oAprev.appendChild(oAprevtext);

							oP.appendChild(oAprev);
							oP.appendChild(oSeparator);
							oP.appendChild(oAnext);

							results.oResultContainer.appendChild(oP);
						}
					} else {
						oResultItem[i].className = 'hide';
					}
				}

				oPr = document.getElementById('moreResults');
				if (!oPr && (oResultItem.length < 100)) {
					var oPmoreResults = document.createElement('p');
					var oAmoreResults = document.createElement('a');
					var oAmoreResultstext = document.createTextNode('see more results');

					var oPclothingTable = document.createElement('p');
					var oAclothingTable = document.createElement('a');
					var oAclothingTableText = document.createTextNode('Clothing Comparison');

					oPmoreResults.setAttribute('id', 'moreResults');
					oAmoreResults.setAttribute('title', 'increase the catchment area of the stores');
					oAmoreResults.setAttribute('href', '#zoom');

					addEvent(oAmoreResults, 'click', storeFinder.getNewPointOnMoveAndRedrawIncClick);

					oAmoreResults.appendChild(oAmoreResultstext);
					oPmoreResults.appendChild(oAmoreResults);

					results.oResultContainer.appendChild(oPmoreResults);
					results.oResultContainer.appendChild(oPclothingTable);
				}
			}

			oResultlink = results.oResultContainer.getElementsByTagName('a');
			if (oResultlink) {
				for (i = 0; i < oResultlink.length; i++) {
					if (oResultlink[i].className > 4) {
						addEvent(oResultlink[i], 'mouseover', results.highlightPin);
						addEvent(oResultlink[i], 'click', results.initiateDetailedStoreInformation);
						addEvent(oResultlink[i], 'mouseout', results.resetPin);
					}

/* Trying to auto run store details for first returned list result 
          if (i==0){
            results.initiateDetailedStoreInformation;
            var sBranch=oResultlink[0].className;
            alert('fetching store: '+sBranch)
            results.jsonStore=null;
            AJAX.getMoreStoreInfo(sBranch);
          }
*/
				}
			}

			for (i = 0; i < oResultItem.length; i++) {
				oPinny = document.getElementById(i);
				if (oPinny) {
					addEvent(oPinny, 'click', results.initiateDetailedStoreInformation);
				}
			}
		}
	},
	PanMap : function (latlong) {
    if (latlong !== null) {
      storeFinder.map.PanToLatLong(latlong);
    }
	},
	initiateDetailedStoreInformation : function (e) {
    var sBranch;
		e.returnValue = false; // for IE
		if (typeof e.preventDefault === 'function') {e.preventDefault(); }
		if (this.className === 'VEAPI_Pushpin') {
			var sID = this.id;
			results.sCurrentClick = 'PIN' + sID;

			var oLi = document.getElementById('m' + sID);
			if (oLi) {
				var oA = oLi.getElementsByTagName('a')[0];
				if (oA) {
					results.sStore = oA.innerHTML;
					results.aLngLat = oA.title.split(',');
					sBranch = oA.className;
				}
			}
		} else if (this.innerHTML !== 'see more results') {
			results.sCurrentClick = this.parentNode.parentNode.getAttribute('rel');
			results.sStore = this.innerHTML;
			results.aLngLat = this.title.split(',');

			sBranch = this.className;
		}
		results.jsonStore = null;
		AJAX.getMoreStoreInfo(sBranch);
	},



// replace this old method with the same used by bank holidays.
	showChristmasHours : function () {
		var oOpen = document.getElementById('rChristmas'),
      sChristmasContent;
		if (oOpen) {
			if (results.jsonChristmas !== null) {
				var sD = eval('(' + results.jsonChristmas + ')');
				if (sD.sI.length > 1) {
					
					var sbID = sD.sI[1].bID;
					var sO21 = sD.sI[1].O21;
					var sC21 = sD.sI[1].C21;
					var sO22 = sD.sI[1].O22;
					var sC22 = sD.sI[1].C22;
					var sO23 = sD.sI[1].O23;
					var sC23 = sD.sI[1].C23;
					var sO24 = sD.sI[1].O24;
					var sC24 = sD.sI[1].C24;
					var sO25 = sD.sI[1].O25;
					var sC25 = sD.sI[1].C25;
					var sO26 = sD.sI[1].O26;
					var sC26 = sD.sI[1].C26;
					var sO27 = sD.sI[1].O27;
					var sC27 = sD.sI[1].C27;
					var sO28 = sD.sI[1].O28;
					var sC28 = sD.sI[1].C28;
					var sO29 = sD.sI[1].O29;
					var sC29 = sD.sI[1].C29;
					var sO30 = sD.sI[1].O30;
					var sC30 = sD.sI[1].C30;
					var sO31 = sD.sI[1].O31;
					var sC31 = sD.sI[1].C31;
					var sO01 = sD.sI[1].O01;
					var sC01 = sD.sI[1].C01;
					var sO02 = sD.sI[1].O02;
					var sC02 = sD.sI[1].C02;

					var s21 = "";
					var s22 = "";
					var s23 = "";
					var s24 = "";
					var s25 = "";
					var s26 = "";
					var s27 = "";
					var s28 = "";
					var s29 = "";
					var s30 = "";
					var s31 = "";
					var s01 = "";
					var s02 = "";

					if ((sO21 === "0") && (sC21 === "23.59")) {s21 = '24 hours'; } else if ((sO21 === "CLOSED") && (sC21 === "CLOSED")) {s21 = 'CLOSED'; } else if (sO21 === "0") {s21 = 'open until ' + uti.fFig(parseFloat(sC21) * 100); } else if (sC21 === "23.59") {s21 = uti.fFig(parseFloat(sO21) * 100) + ' - midnight'; } else {s21 = uti.fFig(parseFloat(sO21) * 100) + ' - ' + uti.fFig(parseFloat(sC21) * 100); }
					if ((sO22 === "0") && (sC22 === "23.59")) {s22 = '24 hours'; } else if ((sO22 === "CLOSED") && (sC22 === "CLOSED")) {s22 = 'CLOSED'; } else if (sO22 === "0") {s22 = 'open until ' + uti.fFig(parseFloat(sC22) * 100); } else if (sC22 === "23.59") {s22 = uti.fFig(parseFloat(sO22) * 100) + ' - midnight'; } else {s22 = uti.fFig(parseFloat(sO22) * 100) + ' - ' + uti.fFig(parseFloat(sC22) * 100); }
					if ((sO23 === "0") && (sC23 === "23.59")) {s23 = '24 hours'; } else if ((sO23 === "CLOSED") && (sC23 === "CLOSED")) {s23 = 'CLOSED'; } else if (sO23 === "0") {s23 = 'open until ' + uti.fFig(parseFloat(sC23) * 100); } else if (sC23 === "23.59") {s23 = uti.fFig(parseFloat(sO23) * 100) + ' - midnight'; } else {s23 = uti.fFig(parseFloat(sO23) * 100) + ' - ' + uti.fFig(parseFloat(sC23) * 100); }
					if ((sO24 === "0") && (sC24 === "23.59")) {s24 = '24 hours'; } else if ((sO24 === "CLOSED") && (sC24 === "CLOSED")) {s24 = 'CLOSED'; } else if (sO24 === "0") {s24 = 'open until ' + uti.fFig(parseFloat(sC24) * 100); } else if (sC24 === "23.59") {s24 = uti.fFig(parseFloat(sO24) * 100) + ' - midnight'; } else {s24 = uti.fFig(parseFloat(sO24) * 100) + ' - ' + uti.fFig(parseFloat(sC24) * 100); }
					if ((sO25 === "0") && (sC25 === "23.59")) {s25 = '24 hours'; } else if ((sO25 === "CLOSED") && (sC25 === "CLOSED")) {s25 = 'CLOSED'; } else if (sO25 === "0") {s25 = 'open until ' + uti.fFig(parseFloat(sC25) * 100); } else if (sC25 === "23.59") {s25 = uti.fFig(parseFloat(sO25) * 100) + ' - midnight'; } else {s25 = uti.fFig(parseFloat(sO25) * 100) + ' - ' + uti.fFig(parseFloat(sC25) * 100); }
					if ((sO26 === "0") && (sC26 === "23.59")) {s26 = '24 hours'; } else if ((sO26 === "CLOSED") && (sC26 === "CLOSED")) {s26 = 'CLOSED'; } else if (sO26 === "0") {s26 = 'open until ' + uti.fFig(parseFloat(sC26) * 100); } else if (sC26 === "23.59") {s26 = uti.fFig(parseFloat(sO26) * 100) + ' - midnight'; } else {s26 = uti.fFig(parseFloat(sO26) * 100) + ' - ' + uti.fFig(parseFloat(sC26) * 100); }
					if ((sO27 === "0") && (sC27 === "23.59")) {s27 = '24 hours'; } else if ((sO27 === "CLOSED") && (sC27 === "CLOSED")) {s27 = 'CLOSED'; } else if (sO27 === "0") {s27 = 'open until ' + uti.fFig(parseFloat(sC27) * 100); } else if (sC27 === "23.59") {s27 = uti.fFig(parseFloat(sO27) * 100) + ' - midnight'; } else {s27 = uti.fFig(parseFloat(sO27) * 100) + ' - ' + uti.fFig(parseFloat(sC27) * 100); }
					if ((sO28 === "0") && (sC28 === "23.59")) {s28 = '24 hours'; } else if ((sO28 === "CLOSED") && (sC28 === "CLOSED")) {s28 = 'CLOSED'; } else if (sO28 === "0") {s28 = 'open until ' + uti.fFig(parseFloat(sC28) * 100); } else if (sC28 === "23.59") {s28 = uti.fFig(parseFloat(sO28) * 100) + ' - midnight'; } else {s28 = uti.fFig(parseFloat(sO28) * 100) + ' - ' + uti.fFig(parseFloat(sC28) * 100); }
					if ((sO29 === "0") && (sC29 === "23.59")) {s29 = '24 hours'; } else if ((sO29 === "CLOSED") && (sC29 === "CLOSED")) {s29 = 'CLOSED'; } else if (sO29 === "0") {s29 = 'open until ' + uti.fFig(parseFloat(sC29) * 100); } else if (sC29 === "23.59") {s29 = uti.fFig(parseFloat(sO29) * 100) + ' - midnight'; } else {s29 = uti.fFig(parseFloat(sO29) * 100) + ' - ' + uti.fFig(parseFloat(sC29) * 100); }
					if ((sO30 === "0") && (sC30 === "23.59")) {s30 = '24 hours'; } else if ((sO30 === "CLOSED") && (sC30 === "CLOSED")) {s30 = 'CLOSED'; } else if (sO30 === "0") {s30 = 'open until ' + uti.fFig(parseFloat(sC30) * 100); } else if (sC30 === "23.59") {s30 = uti.fFig(parseFloat(sO30) * 100) + ' - midnight'; } else {s30 = uti.fFig(parseFloat(sO30) * 100) + ' - ' + uti.fFig(parseFloat(sC30) * 100); }
					if ((sO31 === "0") && (sC31 === "23.59")) {s31 = '24 hours'; } else if ((sO31 === "CLOSED") && (sC31 === "CLOSED")) {s31 = 'CLOSED'; } else if (sO31 === "0") {s31 = 'open until ' + uti.fFig(parseFloat(sC31) * 100); } else if (sC31 === "23.59") {s31 = uti.fFig(parseFloat(sO31) * 100) + ' - midnight'; } else {s31 = uti.fFig(parseFloat(sO31) * 100) + ' - ' + uti.fFig(parseFloat(sC31) * 100); }
					if ((sO01 === "0") && (sC01 === "23.59")) {s01 = '24 hours'; } else if ((sO01 === "CLOSED") && (sC01 === "CLOSED")) {s01 = 'CLOSED'; } else if (sO01 === "0") {s01 = 'open until ' + uti.fFig(parseFloat(sC01) * 100); } else if (sC01 === "23.59") {s01 = uti.fFig(parseFloat(sO01) * 100) + ' - midnight'; } else {s01 = uti.fFig(parseFloat(sO01) * 100) + ' - ' + uti.fFig(parseFloat(sC01) * 100); }
					if ((sO02 === "0") && (sC02 === "23.59")) {s02 = '24 hours'; } else if ((sO02 === "CLOSED") && (sC02 === "CLOSED")) {s02 = 'CLOSED'; } else if (sO02 === "0") {s02 = 'open until ' + uti.fFig(parseFloat(sC02) * 100); } else if (sC02 === "23.59") {s02 = uti.fFig(parseFloat(sO02) * 100) + ' - midnight'; } else {s02 = uti.fFig(parseFloat(sO02) * 100) + ' - ' + uti.fFig(parseFloat(sC02) * 100); }
																																								
					sChristmasContent = '<h3><a href="#" title="Store opening hours" onclick="openClose($(this));return false;">AChristmas Opening Hours</a></h3>';
					sChristmasContent += '<!--h3><span></span>AChristmas Opening Hours</h3--><ol><li><strong>21st Dec:</strong> ' + 
						s21 + '</li><li><strong>22nd Dec:</strong> ' + s22 + '</li><li><strong>23rd Dec:</strong> ' + 
						s23 + '</li><li><strong>24th Dec:</strong> ' + s24 + '</li><li><strong>25th Dec:</strong> ' + 
						s25 + '</li><li><strong>26th Dec:</strong> ' + s26 + '</li><li><strong>27th Dec:</strong> ' + 
						s27 + '</li></ol><ol><li><strong>28th Dec:</strong> ' + s28 + '</li><li><strong>29th Dec:</strong> ' + 
						s29 + '</li><li><strong>30th Dec:</strong> ' + s30 + '</li><li><strong>31th Dec:</strong> ' + 
						s31 + '</li><li><strong>01st Jan:</strong> ' + s01 + '</li><li><strong>02nd Jan:</strong> ' + 
						s02 + '</li></ol>';					
				} else {
					var oStoreType = document.getElementById('rType');
					if (oStoreType.innerHTML.indexOf('express') !== -1) {
						//The store is an Express so it will operate normal opening hours.						
						sChristmasContent = '<h3><a href="#" title="Store opening hours" onclick="openClose($(this));return false;">BChristmas Opening Hours</a></h3><p>This store will operate normal opening hours.</p>';
					} else {
						sChristmasContent = '<h3><a href="#" title="Store opening hours" onclick="openClose($(this));return false;">CChristmas Opening Hours</a></h3><p>Please contact the store</p>';
					}					
				}
				var oooo = document.getElementById('rChristmas');
				oooo.innerHTML = sChristmasContent;
			}			
		}
	},



	hideChristmas : function () {
		var oIn = document.getElementById('rChristmas');
		if (oIn) {oIn.innerHTML = '<td colspan="4"></td>'; }
	},

	showDetailedStoreInformation : function () {

    function buildTableRow(day, op, cl) {
      // build store opening time table
      var str = '<tr><td class="day">' + day + '</td>';
      if ((op === "0001") && (cl === "2359")) {
        str += '<td colspan="2">24 hours</td>';
      } else if ((op === "0001") && (cl === "0000")) {
        str += '<td colspan="2">open until midnight</td>';
      } else if (op === "0001") {
        str += '<td colspan="2">open until ' + cl + '</td>';
      } else if ((op === "0000") && (cl === "2359")) {
        str += '<td colspan="2">open midnight onwards</td>';
      } else if ((op === "0000") && (cl === "0000")) {
        str += '<td colspan="2">midnight to midnight</td>';
      } else if ((op === "0000")) {
        str += '<td>midnight</td>' + '<td>' + cl + '</td>';
      } else if (cl === "2359") {
        str += '<td colspan="2">open ' + op + ' onwards</td>';
      } else if (cl === "0000") {
        str += '<td>' + op + '</td><td>midnight</td>';
      } else if (cl.match("am")) {
        str += '<td>' + op + '</td><td>closes ' + cl.replace(/^[0]+/g, "") + '</td>';
      } else {
        str += '<td>' + op + '</td>' + '<td>' + cl + '</td>';
      }
      str += '</tr>\n';
      return str;
    }

    function removeDivContent(id) {
      // remove instore opening times
      replaceContent(id, '');
      jsCSS('remove', $id(id), 'showIt');
    }
    
    
    if (results.sCurrentClick) {
			//only show current store
			resultDisambiguation.clearDisambigResults();

// data is stuffed into the following element IDs:
			var oStoreHead = results.storeInformation.getElementsByTagName('h2');
			var oAddr = document.getElementById('rAddr');
			var	oTele = document.getElementById('rTele');
			var	oType = document.getElementById('rType');
			var oFacilities = document.getElementById('rFacilities');
			var oProductRange = document.getElementById('rProductRange');

			var oAccess = document.getElementById('rAccess');
			var oBus = document.getElementById('rBus');

			//Hide and Delete Route Information
			var oRoute = document.getElementById('rt');
			oRoute.style.display = 'none';
//#			storeFinder.map.DeleteRoute();

			if (oAddr && oType && oStoreHead && oFacilities && oProductRange) {
				oAddr.innerHTML = '';
				oTele.innerHTML = '';
				oType.innerHTML = '';
//				oOpen.innerHTML = '';
				oBus.innerHTML = '';

				var sLng = results.aLngLat[0];
				var sLat = results.aLngLat[1];
				results.PanMap(new VELatLong(sLng, sLat));

// main content build from json
				if (results.jsonStore !== null) {
//alert(results.jsonStore)
					var sD = eval('(' + results.jsonStore + ')');
					if (sD.sI.length > 0) {
						// store details
						var xbranch = sD.sI[1].bID;
//alert(xbranch)
						var xbranch_name = sD.sI[1].branch_name;
						var xstreet = sD.sI[1].address_1;
						var xtown = sD.sI[1].town_city;
						var xcounty = sD.sI[1].county;
						var xpostcode = sD.sI[1].postcode;
						var xphone_number = sD.sI[1].telephone;

						var xstoretype = sD.sI[1].store_type;

						// store opening times
						var xopen_mon = sD.sI[1].mon_o;
						var xclosed_mon = sD.sI[1].mon_c;
						var xopen_tue = sD.sI[1].tue_o;
						var xclosed_tue = sD.sI[1].tue_c;
						var xopen_wed = sD.sI[1].wed_o;
						var xclosed_wed = sD.sI[1].wed_c;
						var xopen_thu = sD.sI[1].thu_o;
						var xclosed_thu = sD.sI[1].thu_c;
						var xopen_fri = sD.sI[1].fri_o;
						var xclosed_fri = sD.sI[1].fri_c;
						var xopen_sat = sD.sI[1].sat_o;
						var xclosed_sat = sD.sI[1].sat_c;
						var xopen_sun = sD.sI[1].sun_o;
						var xclosed_sun = sD.sI[1].sun_c;

						// instore stores
						var xpharmacy = sD.sI[1].pharmacy;
						var xopticians = sD.sI[1].optician;
						var xcafe = sD.sI[1].cafe;
						var xdirectdesk = sD.sI[1].direct_desk;
						var xtelecoms = sD.sI[1].telecoms;
						var xtravel_desk = sD.sI[1].travel_desk;
						var xtechSupport = sD.sI[1].techSupport;
						var xwifi = sD.sI[1].wifi;

						// facilities
						var xgiftcard = sD.sI[1].giftcard;
						var xkindle = sD.sI[1].kindle;
						var xcosta = sD.sI[1].costa;
						var xapple = sD.sI[1].apple;
						var xipad = sD.sI[1].ipad;
						var xgames = sD.sI[1].games;
						var xbank = sD.sI[1].bank;
						var xcatalogue_shop = sD.sI[1].catalogue_shop;
						var xhour_24 = sD.sI[1].hour_24;
						var xpetrol = sD.sI[1].petrol;
						var xoctane99 = sD.sI[1].octane99;
						var xcarwash = sD.sI[1].carwash;
						var xjetwash = sD.sI[1].jetwash; // Added but not coded in JS
						var xtoilets = sD.sI[1].toilets;
						var xbaby_change = sD.sI[1].babychange;
						var xrecycling = sD.sI[1].recycling;
						var xrecyc_centre = sD.sI[1].recyc_centre;
						var xeuro_cash = sD.sI[1].euro_atm;
						var xatm = sD.sI[1].atm;

						// product ranges
						var xchicken_counter = sD.sI[1].chicken;
						var xworld_foods = sD.sI[1].world_foods;
						var xfish_counter = sD.sI[1].fish;
						var xmeat_counter = sD.sI[1].meat;

						var xphoto_processing = sD.sI[1].photo_1hr;
						var xphoto_processing_2_days = sD.sI[1].photo_2day;
						var xclothing = sD.sI[1].clothes;
						var xjewellery = sD.sI[1].fr;

						var xasian = sD.sI[1].asian;
						var xhalal = sD.sI[1].halal;
						var xkosher = sD.sI[1].jewish;
						var xafro_caribbean = sD.sI[1].afro_caribbean;
						var xgreek = sD.sI[1].greek;
						var xpolish = sD.sI[1].polish;

						// free bus
						var xfree_bus = sD.sI[1].free_bus;
						var xbus_operator = sD.sI[1].operator;
						var xbus_tel = sD.sI[1].bus_tel;


						var xhome_plus = sD.sI[1].be;

					}


// Exception advertising (also in utils.js and default.asp):
					if (xcounty === "ESSEX" && idExists('essex')) {jsCSS('remove', $id('essex'), 'hideIt'); } else {jsCSS('add', $id('essex'), 'hideIt'); }
					if (xcounty === "GLOUCESTERSHIRE" && idExists('gloucestershire')) {jsCSS('remove', $id('gloucestershire'), 'hideIt'); } else {jsCSS('add', $id('gloucestershire'), 'hideIt'); }
					if (xcounty === "HAMPSHIRE" && idExists('hampshire')) {jsCSS('remove', $id('hampshire'), 'hideIt'); } else {jsCSS('add', $id('hampshire'), 'hideIt'); }
					if (xcounty === "HERTFORDSHIRE" && idExists('hertfordshire')) {jsCSS('remove', $id('hertfordshire'), 'hideIt'); } else {jsCSS('add', $id('hertfordshire'), 'hideIt'); }
					if (xpostcode.toUpperCase().substring(0, 2) === "NG" && idExists('nottingham')) {jsCSS('remove', $id('nottingham'), 'hideIt'); }
					if (xpostcode.toUpperCase().substring(0, 2) === "EH" && idExists('edinburgh')) {jsCSS('remove', $id('edinburgh'), 'hideIt'); }



// Output store details
          var sStreet;
					if (xstreet === "") {sStreet = ""; } else {sStreet = xstreet + ',<br/>'; }
					if (xtown !== "") {xtown = xtown + ',<br />'; }
					if (xcounty !== "") {xcounty = xcounty + ',<br />'; }
					oAddr.innerHTML = '<p>' + xbranch_name + ',<br />' + sStreet + xtown + xcounty + xpostcode + '</p>';
					oTele.innerHTML = '<p><strong>' + xphone_number + '</strong></p>';

					var sIMG = "";
					if (xstoretype.toLowerCase() === "extra") {sIMG = "tesco_extra_logo.gif"; }
					if (xstoretype.toLowerCase() === "superstore") {sIMG = "tesco_logo.gif"; }
					if (xstoretype.toLowerCase() === "homeplus") {sIMG = "tesco_homeplus_logo.gif"; }
					if (xstoretype.toLowerCase() === "express") {sIMG = "tesco_express_logo.gif"; }
					if (xstoretype.toLowerCase() === "metro") {sIMG = "tesco_metro_logo.gif"; }

// Note branch id number is set in the title of the store type image:
					oType.innerHTML = '<p><img src=icons/store_type/' + sIMG + ' title="Branch: ' + xbranch + '" alt="Store Type: ' + xstoretype + '" /></p>';


// add Store opening times
					if (idExists('rStore')) {
						var ST = '', Stop, Stail = '';
						STop = '<h3><a href="#" title="Store opening hours" onclick="openClose($(this));return false;">Store Times</a></h3>';
						STop += '<table id="store_times" class="timetable">';
						ST += '<thead><tr><th scope="col" class="day">Day</th><th scope="col">Opening</th><th scope="col">Closed</th></tr></thead>\n';
						ST += '<tbody>\n';
						ST += buildTableRow('Monday', xopen_mon, xclosed_mon);
						ST += buildTableRow('Tuesday', xopen_tue, xclosed_tue);
						ST += buildTableRow('Wednesday', xopen_wed, xclosed_wed);
						ST += buildTableRow('Thursday', xopen_thu, xclosed_thu);
						ST += buildTableRow('Friday', xopen_fri, xclosed_fri);
						ST += buildTableRow('Saturday', xopen_sat, xclosed_sat);
						ST += buildTableRow('Sunday', xopen_sun, xclosed_sun);
						ST += '</tbody>';
						STail = '</table>';
						
						$("#rStore").html(STop + ST + STail);
						$('#instoreTimes li').removeClass('current');
						$("#rStore").addClass('current');
						$('#times').empty().html(ST);
					}


// add holiday opening times:
// Note: to remove a timetable rename its id in default.asp eg: <div id="rEasterXXX"></div>
// if HTML has an element id="rEaster" then display opening times:
					if (idExists('rChristmas')) {AJAX.getChristmas2(xbranch); }
					if (idExists('rEaster')) {AJAX.getEaster(xbranch); }
					if (idExists('rBankHolMayDay')) {AJAX.getBankHolMayDay(xbranch); }
					if (idExists('rBankHolMay')) {AJAX.getBankHolMay(xbranch); }
					if (idExists('rBankHolAug')) {AJAX.getBankHolAug(xbranch); }



// add instore opening times:
          if (xtechSupport === "Y") {
            if (idExists('rTechSupport')) {AJAX.getTechSupport(xbranch); }
          } else {removeDivContent('rTechSupport'); }

					if (xtravel_desk === "Y") {
            if (idExists('rTravel')) {AJAX.getTravel(xbranch); }
          } else {removeDivContent('rTravel'); }

					if (xbank === "Y") {
            if (idExists('rBank')) {AJAX.getBank(xbranch); }
          } else {removeDivContent('rBank'); }

					if (xpharmacy === "Y") {
            if (idExists('rPharmacy')) {AJAX.getPharmacy(xbranch); }
          } else {removeDivContent('rPharmacy'); }

					if (xopticians === "Y") {
            if (idExists('rOptician')) {AJAX.getOptician(xbranch); }
          } else {removeDivContent('rOptician'); }

					if (xcafe === "Y") {
            if (idExists('rCafe')) {AJAX.getCafe(xbranch); }
          } else {removeDivContent('rCafe'); }

					if (xdirectdesk === "Y") {
            if (idExists('rDirectDesk')) {AJAX.getDirectDesk(xbranch); }
          } else {removeDivContent('rDirectDesk'); }

					if (xtelecoms === "Y") {
            if (idExists('rTelecoms')) {AJAX.getTelecoms(xbranch); }
          } else {removeDivContent('rTelecoms'); }


// add facilities available
          /*
          var pfs = document.getElementById('PetrolFillingStation');
          if (pfs) {
            if (xpetrol === "Y") {
               pfs.innerHTML = '<a class="fullbox" href="/petrolstation/"><img src="icons/facilities/PETROL.gif" width="46" height="54" alt="Petrol forecourt" /></a>';
            } else {
              pfs.innerHTML = '';
            }
          }
          */
          var mfs = document.getElementById('MomentumFillingStation');
          if (mfs) {
            if (xoctane99 === "Y") {
              mfs.innerHTML = '<a class="fullbox" href="/Momentum99/"><img src="icons/facilities/octane99.png" width="46" height="54" alt="Momentum 99 petrol" /></a>';
            } else {
              mfs.innerHTML = '';
            }
          }
          /* ipad page */
          mfs = document.getElementById('ipadLogo');
          if (mfs) {
            if (xipad === "Y") {
              mfs.innerHTML = '<img src="icons/facilities/ipad.png" width="46" height="54" alt="Apple iPad" />';
            } else {
              mfs.innerHTML = '';
            }
          }
          /* apple page */
          mfs = document.getElementById('appleLogo');
          if (mfs) {
            if (xapple === "Y") {
              mfs.innerHTML = '<img src="icons/facilities/apple.gif" width="46" height="54" alt="Apple" />';
            } else {
              mfs.innerHTML = '';
            }
          }
          /* kindle page */
          mfs = document.getElementById('kindleLogo');
          if (mfs) {
            if (xkindle === "Y") {
              mfs.innerHTML = '<img src="icons/facilities/kindle.png" width="46" height="54" alt="Kindle stockist" />';
            } else {
              mfs.innerHTML = '';
            }
          }
          /* giftcard page */
          mfs = document.getElementById('giftcardLogo');
          if (mfs) {
            if (xgiftcard === "Y") {
              mfs.innerHTML = '<img src="icons/facilities/giftcard.png" width="46" height="54" alt="Gift Card Store" />';
            } else {
              mfs.innerHTML = '';
            }
          }

          /* Tech Support page */
          mfs = document.getElementById('techsupportLogo');
          if (mfs) {
            if (xtechSupport === "Y") {
              mfs.innerHTML = '<img src="icons/instores/techsupport.png" width="46" height="54" alt="In-store Tech support" />';
            } else {
              mfs.innerHTML = '';
            }
          }
		  
		      /* Wifi page */
          mfs = document.getElementById('wifiLogo');
          if (mfs) {
            if (xwifi === "Y") {
              mfs.innerHTML = '<img src="icons/instores/wifiicon.png" width="46" height="54" alt="In-store Wifi" />';
            } else {
              mfs.innerHTML = '';
            }
          }
		  
		      /* Direct collection page */
          mfs = document.getElementById('directCollectionLogo');
          if (mfs) {
            if (xdirectdesk === "Y") {
              mfs.innerHTML = '<img src="icons/facilities/DIRECT_DESK.gif" width="46" height="54" alt="Direct Collection Desk" />';
            } else {
              mfs.innerHTML = '';
            }
          }


					var sFac = '\n<ul>\n';
					if (xhour_24 === "Y") {sFac += '  <li><img src="icons/facilities/HOUR_24.gif" width="46" height="54" alt="twenty four hour trading" /></li>\n'; }
					if (xdirectdesk === "Y") {sFac += '  <li><img src="icons/facilities/DIRECT_DESK.gif" width="46" height="54" alt="Direct Desk" /></li>\n'; }
					if (xtelecoms === "Y") {sFac += '  <li><a class="fullbox" href="/telecoms/" rel="external"><img src="icons/instores/phoneshop.png" width="46" height="54" alt="Phone shop" /></a></li>\n'; }
					if (xpharmacy === "Y") {sFac += '  <li><img src="icons/instores/PHARMACY.gif" width="46" height="54" alt="Pharmacy" /></li>\n'; }
					if (xopticians === "Y") {sFac += '  <li><a class="fullbox" href="http://www.tescoopticians.com/" rel="external"><img src="icons/instores/OPTICIAN.gif" width="46" height="54" alt="Opticians" /></a></li>\n'; }
					if (xtravel_desk === "Y") {sFac += '  <li><img src="icons/instores/travel_money.gif" width="46" height="54" alt="Travel Money" /></li>\n'; }
					if (xwifi === "Y") {sFac += '  <li><img src="icons/instores/wifiicon.png" width="46" height="54" alt="Wifi" /></li>\n'; }
					if (xtechSupport === "Y") {sFac += '  <li><img src="icons/instores/techsupport.png" width="46" height="54" alt="Tech Support" /></li>\n'; }
					if (xpetrol === "Y") {sFac += '  <li><a class="fullbox" href="/petrolstation/"><img src="icons/facilities/PETROL.gif" width="46" height="54" alt="Petrol forecourt" /></a></li>\n'; }
					if (xoctane99 === "Y") {sFac += '  <li><a class="fullbox" href="/Momentum99/"><img src="icons/facilities/octane99.png" width="46" height="54" alt="Momentum 99 petrol" /></a></li>\n'; }
					if (xgiftcard === "Y") {sFac += '  <li><img src="icons/facilities/giftcard.png" width="46" height="54" alt="giftcard" /></li>\n'; }
					if (xcafe === "Y") {sFac += '  <li><img src="icons/instores/CAFE.gif" alt="cafe" /></li>\n'; }
					if (xcosta === "Y") {sFac += '  <li><img src="icons/facilities/costa.png" width="46" height="54" alt="Costa coffee" /></li>\n'; }
					if (xgames === "Y") {sFac += '  <li><img src="icons/facilities/games.png" width="46" height="54" alt="Pre-owned games" /></li>\n'; }
					if (xbank === "Y") {sFac += '  <li><a class="fullbox" href="http://www.tescofinance.com/personal/finance/home.jsp?referrerid=tesco20"><img src="icons/facilities/bank.png" width="46" height="54" alt="In-store bank" /></a></li>\n'; }
					if (xcatalogue_shop === "Y") {sFac += '  <li><img src="icons/facilities/catalogue_shop.gif" width="46" height="54" alt="Catalogue shop" /></li>\n'; }
					if (xcarwash === "Y") {sFac += '  <li><img src="icons/facilities/CARWASH.gif" width="46" height="54" alt="Car wash" /></li>\n'; }
					if (xjetwash === "Y") {sFac += '  <li><img src="icons/facilities/JETWASH.gif" width="46" height="54" alt="Jet wash" /></li>\n'; }
					if (xphoto_processing === "Y") {sFac += '  <li><img src="icons/facilities/PHOTO_1HR.gif" width="46" height="54" alt="1 hour photo processing" /></li>\n'; }
					if (xphoto_processing_2_days === "Y") {sFac += '  <li><img src="icons/facilities/PHOTO_2DAY.gif" width="46" height="54" alt="2 day photo processing" /></li>\n'; }
					if (xrecycling === "Y") {sFac += '  <li><img src="icons/facilities/RECYCLING.gif" width="46" height="54" alt="Recycling" /></li>\n'; }
					if (xrecyc_centre === "Y") {sFac += '  <li><img src="icons/facilities/RECYC_CENTRE.gif" width="46" height="54" alt="Recycling centre" /></li>\n'; }
					if (xbaby_change === "Y") {sFac += '  <li><img src="icons/facilities/BABYCHANGE.gif" width="46" height="54" alt="Baby change" /></li>\n'; }
					if (xtoilets === "Y") {sFac += '  <li><img src="icons/facilities/toilets.gif" width="46" height="54" alt="Toilets" /></li>\n'; }
					if (xeuro_cash === "Y") {sFac += '  <li><img src="icons/facilities/EURO_ATM.gif" width="46" height="54" alt="Euro cash" /></li>\n'; }
					if (xatm === "Y") {sFac += '  <li><a class="fullbox" href="http://www.tescofinance.com/personal/finance/help/cash-machines.html" rel="external"><img src="icons/facilities/ATM.gif" width="46" height="54" alt="Cash machine" /></a></li>\n'; }
					if (xfree_bus === "Y") {sFac += '  <li><img src="icons/free_bus/FREE_BUS.gif" width="46" height="54" alt="Free bus" /></li>\n'; }
					
					sFac += '  <li><a class="fullbox" href="#" onclick="$(\'#rDisabled\').slideToggle();return false;" id="disabled"><img src="icons/disability/disabilityenquiries.gif" width="46" height="54" alt="Show stores disabled access facilities" /></a></li>\n';
					sFac += '</ul>\n';
					sFac = '\n<h3>Facilities</h3>\n' + sFac;
					replaceContent('rFacilities', sFac);
					externalLinks('rFacilities');


// add Product range
					var sPR = "<ul>\n";
					if (xkindle === "Y") {sPR += '  <li><img src="icons/facilities/kindle.png" width="46" height="54" alt="kindle" /></li>\n'; }
					if (xapple === "Y") {sPR += '  <li><img src="icons/facilities/apple.gif" width="46" height="54" alt="Apple" /></li>\n'; }
					if (xipad === "Y") {sPR += '  <li><img src="icons/facilities/ipad.png" width="46" height="54" alt="iPad" /></li>\n'; }
					if (xclothing === "Y") {sPR += '  <li><a class="fullbox" href="http://www.clothingattesco.com/" rel="external"><img src="icons/product_ranges/CLOTHES.gif" width="46" height="54" alt="Clothing" /></a></li>\n'; }
					if (xasian === "Y") {sPR += '  <li><img src="icons/product_ranges/asian.gif" width="46" height="54" alt="asian" /></li>\n'; }
					if (xhalal === "Y") {sPR += '  <li><img src="icons/product_ranges/halal.gif" width="46" height="54" alt="halal" /></li>\n'; }
					if (xkosher === "Y") {sPR += '  <li><img src="icons/product_ranges/jewish.gif" width="46" height="54" alt="kosher" /></li>\n'; }
					if (xafro_caribbean === "Y") {sPR += '  <li><img src="icons/product_ranges/afro_caribbean.gif" width="46" height="54" alt="afro caribbean" /></li>\n'; }
					if (xgreek === "Y") {sPR += '  <li><img src="icons/product_ranges/greek.gif" width="46" height="54" alt="greek" /></li>\n'; }
					if (xpolish === "Y") {sPR += '  <li><img src="icons/product_ranges/polish.gif" width="46" height="54" alt="polish" /></li>\n'; }
					if (xchicken_counter === "Y") {sPR += '  <li><img src="icons/product_ranges/CHICKEN.gif" width="46" height="54" alt="chicken counter" /></li>\n'; }
					if (xworld_foods === "Y") {sPR += '  <li><img src="icons/product_ranges/WORLD_FOODS.gif" width="46" height="54" alt="World foods" /></li>\n'; }
					if (xfish_counter === "Y") {sPR += '  <li><img src="icons/product_ranges/FISH.gif" width="46" height="54" alt="fish counter" /></li>\n'; }
					if (xmeat_counter === "Y") {sPR += '  <li><img src="icons/product_ranges/MEAT.gif" width="46" height="54" alt="meat counter" /></li>\n'; }
					sPR += "</ul>\n";

					if (sPR === "<ul>\n</ul>\n") {
            sPR = "";
          } else {sPR = '\n<h3>Product range</h3>\n' + sPR; }
					replaceContent('rProductRange', sPR);
					externalLinks('rProductRange');


// Free bus details
					var str = "";
					if (xfree_bus === "Y") {
						str += '\n<h3>Free Bus Service</h3>\n'; 
						str += '<img src="icons/free_bus/free_bus.gif" alt="Free Bus" style="float:left; margin-right:1em" />\n';
						str += '<p style="margin-top:0.25em">Bus Operator: <strong>' + xbus_operator + '</strong></p>\n';
						str += '<p><strong>' + xbus_tel + '</strong></p>\n';
						AJAX.getBus(xbranch);
					}
					replaceContent('rBus', str);


// add disabled facilities
// Only show when disabled facilities is clicked

					str = "";
					if (idExists('rDisabledFacilities')) {
						str += '\n<ul>\n';
						if (sD.sI[1].bd) {str += '  <li><img src="icons/disability/accessible_car_parking.gif" alt="" /> Disabled parking facilities are available.</li>\n'; }
						if (sD.sI[1].aa) {str += '  <li><img src="icons/disability/visually_impaired.gif " alt="" /> Facilities are available for people who are visually impaired. Including, audio tapes, Large print, braille etc</li>\n'; }
						if (sD.sI[1].ab) {str += '  <li><img src="icons/disability/assistance_dogs.gif" alt="" /> Assistance dogs are welcome in our store</li>\n'; }
						if (sD.sI[1].ac) {str += '  <li><img src="icons/disability/wheelchair_access.gif" alt="" /> Assistance wheelchair assistance is possible in our store</li>\n'; }
						if (sD.sI[1].ad) {str += '  <li><img src="icons/disability/non_assisted_wheelchair_access.gif" alt="" /> Non assisted wheelchair access is possible</li>\n'; }
						if (sD.sI[1].ae) {str += '  <li><img src="icons/disability/mobility_impairment.gif" alt="" /> Facilities are available for people with mobility impairment</li>\n'; }
						if (sD.sI[1].af) {str += '  <li><img src="icons/disability/accessible_toilets.gif" alt="" /> Fully accessible toilets are available within our store</li>\n'; }
						if (sD.sI[1].ag) {str += '  <li><img src="icons/disability/sign_language.gif" alt="" /> Sign language service is available in store</li>\n'; }
						if (sD.sI[1].ah) {str += '  <li><img src="icons/disability/hearing_impairments.gif" alt="" /> Facilities are available for people with hearing impairments</li>\n'; }
						if (sD.sI[1].ai) {str += '  <li><img src="icons/disability/induction_loop.gif" alt="" /> An induction loop has been installed in our store</li>\n'; }
						if (sD.sI[1].aj) {str += '  <li><img src="icons/disability/accessible_car_parking.gif" alt="" /> Disabled car parking spaces available at our store</li>\n'; }
						if (sD.sI[1].ak) {str += '  <li><img src="icons/disability/intercom.gif" alt="" /> An intercom is available at our store entrance</li>\n'; }
						if (sD.sI[1].al) {str += '\n'; }
						if (sD.sI[1].am) {str += '  <li><img src="icons/disability/trolley_wheelchair.gif" alt="" /> Trolleys for wheelchairs are provided</li>\n'; }
						if (sD.sI[1].an) {str += '  <li><img src="icons/disability/staff_trained_to_help.gif" alt="" /> Our staff are here to help. Please ask if you need help</li>\n'; }
						if (sD.sI[1].ao) {str += '  <li><img src="icons/disability/lap_trays_drop_counters.gif" alt="" /> Lap trays are available at our store</li>\n'; }
						if (sD.sI[1].ap) {str += '  <li><img src="icons/disability/accessible_baby_changing.gif" alt="" /> Accessible baby changing faculties are available in store</li>\n'; }
						if (sD.sI[1].aq) {str += '  <li><img src="icons/disability/automatic_doors.gif" alt="" /> Our store has automatic doors fitted</li>\n'; }
						if (sD.sI[1].ar) {str += '  <li><img src="icons/disability/accessible_customer_lift.gif" alt="" /> A lift is available in store</li>\n'; }
						if (sD.sI[1].ak) {str += '\n'; }
						str += '</ul>\n';
						if (str === '\n<ul>\n</ul>\n') {
							str = '';
						} else {
							str = '<p>We provide the following facilities for this store:</p>\n' + str;
						}
					}
					replaceContent('rDisabledFacilities', str);


					oStoreHead[0].innerHTML = results.sStore; //H2 header
				} else {alert('No data returned from request'); }

				var oStoreInformation = document.getElementById('storeInformation');
				if (oStoreInformation) {
					oStoreInformation.style.display = 'block';
				}
			}

			var iMarkerNumber = parseInt(results.sCurrentClick.substring(3, results.sCurrentClick.length), 10);
		}
	},
	highlightPin : function () {
		var sCurrentOnHover = this.parentNode.parentNode.getAttribute('rel'); //or className??
		if (sCurrentOnHover) {
			//find marker on map
			var sPinNumber = sCurrentOnHover.substring(3, sCurrentOnHover.length);

//alert('l793: ',sPinNumber);

			var oTargetPin = document.getElementById(sPinNumber);
			if (oTargetPin) {
				var ooTargetPinImg = oTargetPin.getElementsByTagName('img')[0];
				if (ooTargetPinImg) {
					var iImg = parseInt(sPinNumber, 10);
					if (iImg < 10) {iImg = '0' + iImg; }
					ooTargetPinImg.setAttribute('src', 'icons/numeric/d/' + iImg + '_b.gif');
				}
			}

			//Make the LHN result change colour.
			var sID = this.parentNode.parentNode.id;
			if (sID.indexOf('d') === -1) {
				this.parentNode.parentNode.id = this.parentNode.parentNode.id + 'd';
			}
		}
	},
	
	resetPin : function () {
		var sCurrentOnHover = this.parentNode.parentNode.getAttribute('rel'); //or className??
		if (sCurrentOnHover) {
			var sID = this.parentNode.parentNode.id;
			if (sID.indexOf('d') !== -1) {
				this.parentNode.parentNode.id = sID.substring(0, sID.length - 1);

				var sPinNumber = sCurrentOnHover.substring(3, sCurrentOnHover.length);

				var oTargetPin = document.getElementById(sPinNumber);
				if (oTargetPin) {
					var ooTargetPinImg = oTargetPin.getElementsByTagName('img')[0];
					if (ooTargetPinImg) {
						var iImg = parseInt(sPinNumber, 10);
						if (iImg < 10) {iImg = '0' + iImg; }
						ooTargetPinImg.setAttribute('src', 'icons/numeric/' + iImg + '.gif');
					}
				}
			}
		}
	},
	
	showNextResults : function (e) {
    var i,
      iCount,
      bBegin;
		e.returnValue = false; // for IE
		if (typeof e.preventDefault === 'function') {e.preventDefault(); }
		oResultItem = results.oResultContainer.getElementsByTagName('li');
		if (oResultItem) {
			iCount = 0;
			bBegin = false;
			for (i = 0; i < oResultItem.length; i++) {
				if ((bBegin === false) && (i > (oResultItem.length - 8))) {
					oResultItem[i].className = '';
				} else {
					if (oResultItem[i].className === '') {bBegin = true; }
					if ((oResultItem[i].className === 'hide') && (iCount < 7) && (bBegin === true)) {
						iCount++;
						oResultItem[i].className = '';
					} else {
						oResultItem[i].className = 'hide';
					}
				}
			}
		}
	},
	
	showPrevResults : function (e) {
    var iCount,
      bBegin,
      i;
		e.returnValue = false; // for IE
		if (typeof e.preventDefault === 'function') {e.preventDefault(); }
		oResultItem = results.oResultContainer.getElementsByTagName('li');
		if (oResultItem) {
			iCount = 0;
			bBegin = false;
			for (i = (oResultItem.length - 1); i > -1; i--) {
				if ((bBegin === false) && (i < 7)) {
					oResultItem[i].className = '';
				} else {
					if (oResultItem[i].className === '') {bBegin = true; }
					if ((oResultItem[i].className === 'hide') && (iCount < 7) && (bBegin === true)) {
						iCount++;
						oResultItem[i].className = '';
					} else {
						oResultItem[i].className = 'hide';
					}
				}
			}
		}
	}
};

var resultDisambiguation = {
	showDisambig : null,
	clearDisambigResults : function () {
		var oResultDisambiguation = document.getElementById('resultDisambiguation');
		if (oResultDisambiguation) {
			oResultDisambiguation.innerHTML = '';
		}
	},
	disambigCall : function (c) {
//alert('disambigCall called')
		if (resultDisambiguation.showDisambig !== null) {
			var results = "<h2>Alternative matches</h2><p>More than one location was retruned. Please select the location you were looking for:</p><ul>";
			for (x = 0; x < c.length; x++) {
				var sPlace = c[x].Name.replace(', United Kingdom', '');
				results += "<li><a href='#zoom' onClick='searchBox.showAddress(\"" + c[x].Name + "\",\"no\",\"" + sPlace + "\");'>" + c[x].Name + "</a></li>";
			}
			var oResultDisambiguation = document.getElementById('resultDisambiguation');
			if (oResultDisambiguation) {
				oResultDisambiguation.innerHTML = results + '</ul>';
			}
		}
	}
};




var storeFinder = {
	map : null,
	plottedMarkers : [],
	json : {},
	oResultsContainer : {},
	radius : {},
	msPin : null,
	iLastResults : null,
	BOVshowResults : false,
	originalSearchCentre : null,
	layer : {},
	init : function () {
		storeFinder.loadMap();
	},
	clearPins : function (pinID) {
		try {
			if (pinID === null) {
//#				storeFinder.map.DeletePushpin(pinID);
			} else {
				storeFinder.map.DeleteAllShapeLayers();
			}
			storeFinder.msPin = 1;
		} catch (error) {}
	},
	clearResults : function () {
		if (storeFinder.oResultsContainer) {
			var oOl = storeFinder.oResultsContainer.getElementsByTagName('ol')[0];
			if (oOl) {
				storeFinder.oResultsContainer.removeChild(oOl);
			}
			var oPa = document.getElementById('moreStores');
			if (oPa) {storeFinder.oResultsContainer.removeChild(oPa); }
			var oPb = document.getElementById('moreResults');
			if (oPb) {storeFinder.oResultsContainer.removeChild(oPb); }
			var oPc = document.getElementById('clothingTable');
			if (oPc) {storeFinder.oResultsContainer.removeChild(oPc); }
		}
	},
	createMarker : function (sLat, sLng, titleText, storeType, iStoreIndex, distance) { // Creates a marker at the given point with the given number label
		if (iStoreIndex < 10) {iStoreIndex = '0' + iStoreIndex; }
		var sImagePath = 'icons/numeric/' + iStoreIndex + '.gif';

//+		var sHTML='<div style="width:270px"><img src="i/SL_'+storeType+'.gif" /><p><strong>' + titleText + '</strong><br />' + distance +' miles from search.</p><p><a href="#zoom" onclick="storeFinder.map.GetRoute('+sFrom+', '+sTo+', null, null, route.onGotRoute);">Get Directions</a></p></div>';
		var icon = '<img src="' + sImagePath + '" alt="" />';
		var infobox = '<div><img src="icons/store_type/SL_' + storeType + '.gif" /><p>' + distance + ' miles from search.</p></div>';
					
		storeFinder.map.ClearInfoBoxStyles();

		 //Add a pushpin to the new layer
		var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(sLat, sLng));
		shape.SetCustomIcon(icon);
		shape.SetTitle(titleText);
		shape.SetDescription(infobox);

    storeFinder.layer.AddShape(shape);

    storeFinder.msPin++;
  },

	resetRadius : function () {
		storeFinder.radius.value = parseFloat(0.1);
	},
	findStores : function (storeType) {
    var centre,
      sCentre,
      aLngLat = [],
      Lng,
      Lat;

    storeFinder.clearPins();
    storeFinder.clearResults();

//+++  maybe have one for each type of store or facility
    storeFinder.layer = new VEShapeLayer();
    storeFinder.map.AddShapeLayer(storeFinder.layer);
//+++

    centre = storeFinder.map.GetCenter();
    storeFinder.originalSearchCentre = centre;

    sCentre = centre.toString();
    sCentre = sCentre.replace(' ', '');
    // default location coords?
    searchBox.oSearchTextBox.className = "54.700000,-2.800000";

    aLngLat = sCentre.split(',');
    Lng = aLngLat[0].substring(0, aLngLat[0].length);
    Lat = aLngLat[1].substring(0, aLngLat[0].length);
    if (storeFinder.radius.value === undefined) {
      storeFinder.resetRadius();
    }

    storeFinder.json = null;
    AJAX.sndReq(Lng, Lat, storeFinder.radius.value.toFixed(2), storeType);
	},

	updateResults : function (sPlaceName, sStoreType, i, dist, bID, Lat, Lng) {
		if (storeFinder.oResultsContainer) {
			var oOL = storeFinder.oResultsContainer.getElementsByTagName('ol')[0];
			if (!oOL) {
				oOL = document.createElement('ol');
        if (document.getElementById('rs')) {
          document.getElementById('rs').innerHTML = '';
        }
				storeFinder.oResultsContainer.appendChild(oOL);
			}
			var oLI = document.createElement('li');
			var oH4 = document.createElement('h4');
			var oA = document.createElement('a');
			var oP = document.createElement('p');

			var oAtext = document.createTextNode(sPlaceName);
			var oPtext = document.createTextNode(dist + ' miles from search');

			oA.setAttribute('title', Lat + ',' + Lng);
			oA.setAttribute('href', '#zoom');
			oA.className = bID;
			oLI.setAttribute('rel', 'PIN' + i);
			oLI.id = 'm' + i;

			oH4.appendChild(oA);
			oA.appendChild(oAtext);
			oP.appendChild(oPtext);
			oLI.appendChild(oH4);
			oLI.appendChild(oP);
			oOL.appendChild(oLI);

    }
	},

	mysortfn : function (a,  b) {
		//Sort by 4th param in passed array... i.e. distance from center point
		if (parseFloat(a[3]) < parseFloat(b[3])) {return -1; }
		if (parseFloat(a[3]) > parseFloat(b[3])) {return 1; }
		return 0;
	},

	getNewPointOnMoveAndRedrawIncClick : function (e) {
		e.returnValue = false; // for IE
		if (typeof e.preventDefault === 'function') {e.preventDefault(); }
		storeFinder.getNewPointOnMoveAndRedrawInc();
	},

	getNewPointOnMoveAndRedrawInc : function () {	// add a little on to the current 'radius'
		storeFinder.clearPins();
		if (storeFinder.radius.value < 3.5) { //2 degrees is about 100 miles ish - n/s
			storeFinder.BOVshowResults = false;
			storeFinder.radius.value = storeFinder.radius.value + parseFloat(0.08);
			storeFinder.BOVshowResults = false;
		} else {
			storeFinder.BOVshowResults = true;
		}

		storeFinder.findStores(searchBox.sID);
	},

	handleResults : function () {
		var sD = {markers: []},
      i,
      aSortArray = [],
      sLng,
      sLat,
      sPlaceName,
      sStoreType,
      bID,
      fA,
      fB,
      c,
      iSortArray,
      k;
		sD = eval('(' + storeFinder.json + ')');
		if (((sD.markers.length > 6) && (sD.markers.length !== storeFinder.iLastResults)) || (storeFinder.BOVshowResults === true)) {
			storeFinder.iLastResults = sD.markers.length;
			for (i = 1; i < sD.markers.length; i++) {
				sLng = parseFloat(sD.markers[i].lng);
				sLat = parseFloat(sD.markers[i].lat);
				sPlaceName = sD.markers[i].placeName;
				sStoreType = sD.markers[i].typ;
				bID = sD.markers[i].bID;

				//Long/Lat to miles convertion - FIX - how accurate is this?
				fA = 69.1 * parseFloat(sD.markers[i].A);
				fB = 53.0 * parseFloat(sD.markers[i].B);
				c = Math.sqrt((fA * fA) + (fB * fB));

				aSortArray[i - 1] = [sPlaceName, sStoreType, i, c.toFixed(2), sLat, sLng, bID];
			}

			aSortArray.sort(storeFinder.mysortfn);
			iSortArray = aSortArray.length - 0; // value of 0 repairs issue 1. (orig= -1)

			if (iSortArray > 20) {
				storeFinder.map.SetZoomLevel(14); //FIX - TWEAK
			}
			for (k = 0; k < iSortArray; k++) {
				storeFinder.updateResults(aSortArray[k][0], aSortArray[k][1], k + 1, aSortArray[k][3], aSortArray[k][6], aSortArray[k][4], aSortArray[k][5]);
				storeFinder.createMarker(aSortArray[k][4], aSortArray[k][5], aSortArray[k][0], aSortArray[k][1], k + 1, aSortArray[k][3]);
			}
			results.init();
		} else {
			if (storeFinder.radius.value < 3.5) {
				storeFinder.getNewPointOnMoveAndRedrawInc();//not enough results - find more
			} else {
				storeFinder.BOVshowResults = true;
			}
		}
	},

	handleResultsCoord : function () {
		var sD = {markers: []},
      aSortArray = [],
      i,
      sLng,
      sLat,
      sPlaceName,
      sStoreType,
      bID,
      fA,
      fB,
      c,
      iSortArray,
      k;

		sD = eval('(' + storeFinder.json + ')');
		if (((sD.markers.length > 6) && (sD.markers.length !== storeFinder.iLastResults)) || (storeFinder.BOVshowResults === true)) {
			storeFinder.iLastResults = sD.markers.length;
			for (i = 1; i < sD.markers.length; i++) {
				sLng = parseFloat(sD.markers[i].lng);
				sLat = parseFloat(sD.markers[i].lat);
				sPlaceName = sD.markers[i].placeName;
				sStoreType = sD.markers[i].typ;
				bID = sD.markers[i].bID;

				//Long/Lat to miles convertion - FIX - how accurate is this?
				fA = 69.1 * parseFloat(sD.markers[i].A);
				fB = 53.0 * parseFloat(sD.markers[i].B);
				c = Math.sqrt((fA * fA) + (fB * fB));

				aSortArray[i - 1] = [sPlaceName, sStoreType, i, c.toFixed(2), sLat, sLng, bID];
			}

			aSortArray.sort(storeFinder.mysortfn);
			iSortArray = aSortArray.length - 1;

			if (iSortArray > 20) {
				storeFinder.map.SetZoomLevel(14); //FIX - TWEAK
			}
			for (k = 0; k < iSortArray; k++) {
				storeFinder.updateResults(aSortArray[k][0], aSortArray[k][1], k + 1, aSortArray[k][3], aSortArray[k][6], aSortArray[k][4], aSortArray[k][5]);
				storeFinder.createMarker(aSortArray[k][4], aSortArray[k][5], aSortArray[k][0], aSortArray[k][1], k + 1, aSortArray[k][3]);
			}
			results.init();
		} else {
			if (storeFinder.radius.value < 3.5) {
				storeFinder.getNewPointOnMoveAndRedrawInc();//not enough results - find more
			} else {
				storeFinder.BOVshowResults = true;
			}
		}
	},
	loadMap : function () {
		storeFinder.oResultsContainer = document.getElementById('rs');

		storeFinder.map = new VEMap('mymap');
//#		storeFinder.map.ShowDisambiguationDialog(false); //FIX - Leave this as false until design for popup is given + the thing works in IE
		storeFinder.map.DisambiguationCallback = resultDisambiguation.disambigCall;

		// CoordStr and Target are set from uri parameters in default.asp
		// used by christmas timetable /xmashours/
		if (CoordStr !== "") {
			var coord = CoordStr.split(',');
			storeFinder.map.LoadMap(new VELatLong(coord[0], coord[1]), 8, 'r', false);
			searchBox.sID = Target;
			storeFinder.findStores(searchBox.sID);
		} else {
      // default location coords?
			storeFinder.map.LoadMap(new VELatLong(52.49779, -1.70614), 5, 'r', false);	//r road | h hybrid |   ... //true,false - toggle controls inc. drag map ----------- 54.700000,-2.800000 - centre
		}

		storeFinder.msPin = 1;

		//var oAdminMode = document.getElementById('adminMode');
		//if (oAdminMode) {
    //	storeFinder.map.AttachEvent('onclick', admin.myEventTester); //change to your event
		//}
	}
};


var startMaps = {
	init : function () {
//		access.init();
		pageViewOptions.init();
		searchBox.init();
		reCentreSearch.init();
		storeFinder.init();
	}
};

addEvent(window, 'load', startMaps.init);
