/******************************************************************
 * Store-locator. DWR Stuff.
 * Oct 30 2007
 ******************************************************************/
   
     /**
     * DWR callback to find stores details.
     */

    var g_StoreIdx = 0;
    
    function ajaxFindStoresGetDetail(StoreIndex) {
    	g_StoreIdx = StoreIndex; // pass to the callback
       var storeId = results[StoreIndex].id;
       StoreLocatorDwr.getStore(storeId, pageLocale, loadStoreDetailCallback);
    }
    
    function loadStoreDetailCallback(StoreDetail) {

		document.getElementById("storeInfo_StoreName").innerHTML = StoreDetail.name.toUpperCase();
		document.getElementById("storeInfo_Mall").innerHTML = (StoreDetail.mall == null ? "": StoreDetail.mall);
	
		var addr = StoreDetail.addr.split("|"); // "MAGASIN BELL, 1600 Le Corbusier|Laval (Québec)|H7S 1Y9"
		var storeStreet = addr[0];
		var storeCity = addr[1];
		var storePostal = addr[2];
		
		var getDirectionsStr = 	SLMap.IsSearchSuccess() ? ("<a href=# onclick=\"SLRoute.showDirection(elemval('txtCloseTo'), " 
		+ g_StoreIdx + ");storeLocator.sendOSTPageNameMoreInfoGetDirections();\">"
		+ SLMesg.getDirections + " ></a>") : "&nbsp;"; 
 
	
		document.getElementById("storeInfo_address").innerHTML = "<table width='75%' border='0'>"
				+ "<tr><td>" + StoreDetail.phone + "</td></tr>" 
				+ "<tr><td>" + storeStreet + "</td><td rowspan='3' style='vertical-align:top;' nowrap>" 
				+ getDirectionsStr
				+ "</td></tr>"
				+ "<tr><td>" + storeCity + " " + storePostal + "</td></tr>"
				+ "</table>";

		/**
		 * Store hours
		 */
		var hoursArray = StoreDetail.hours.split("|");
		var newhours = '<table border="0" cellspacing="0" cellpadding="0">';
		for (i = 0; i < hoursArray.length; i++) {
			newhours += '<tr><td>' + hoursArray[i] + '</td></tr>';
		}
		newhours += '</table> ';
	
		document.getElementById("storeInfo_hours").innerHTML = newhours;
	
		/**
		 * Store products
		 */
		var prodsArray = StoreDetail.prods.split("|");
	
		var prodslist = '<ul>';
		for (i = 0; i < prodsArray.length; i++) {
			prodslist += '<li>' + prodsArray[i] + '</li>';
		}
		prodslist += '</ul> ';
	
		document.getElementById("storeInfo_products").innerHTML = prodslist;
	}
    
    /**
     * Callback invoked by DWR if some error happened (for example, timeout if server side does not respond).
     * @param msg Message, either by DWR itself, or Exception message thrown by our back end.
     *
     */
    function slDwrErrorHandler(ex) {
    	//window.status = ex;
    	//storeLocator.ResetAll();
    	SLErr.ShowStoreLocatorUnavailableError("slDwrErrorHandler", ex);
    }
    
    /**
	 * DWR callback called to find stores from the centerpoint
	 */
    function ajaxFindStoresFromCenter() {
    	
//       	if (!SLMap.isSearchActive()) { 		
//    	    return;
//    	}
      SLMap.DeactivateSearchFlag();
      
      dwr.engine.setErrorHandler(slDwrErrorHandler);
      dwr.engine.setTimeout(8000);// 8 secs timeout
      
      var centerPoint = SLMap.GetMap().GetCenter();
      StoreLocatorDwr.searchFromCenterWithLimit(centerPoint.Latitude, centerPoint.Longitude,
	                                            elemval("txtStoreType"), elemval("txtStoreOffering"), 
	                                            SLConfig.maxStoresReturned,
											    SLConfig.maxSearchRadiusInKm,
												pageLocale,
												loadStoresFoundCallback);
    }
    
    /**
     * Callback function invoked on return from Dwr-Ajax call to the server.
     * storeArray is the Java equivalent of com.bell.domain.Store.
     * @param storeArray Array of StoresSummaryVO objects.
     */ 	
    function loadStoresFoundCallback(storeArray) {
    	
//       	if (!SLMap.isSearchActive()) { 		
//    	    return;
//    	}
    	
    	/**
    	 * If there is no stores found, show message.
    	 */
    	if (storeArray.length == 0) {
    		
    		SLCursor.ResetCursor();
    		SLMap.DeactivateSearchFlag();
    		SLMap.SetSearchFailed();
            
    		var errmsg = SLMesg.noStoresWithinXkm.split("|");
    		var msg1 = errmsg[0] + SLConfig.maxSearchRadiusInKm + " km " + errmsg[1];
    		var msg2 = errmsg[2];
    		storeLocator.ShowSearchMessage(msg1, msg2);
    		
            storeLocator.sendOSTPageNameSearchResultsError();
    		return;
    	}
    	
        var newLayer = new VEShapeLayer();
		var resultmenuDisplayArray=new Array()

		
        // Add store from the end of the array because we want the farthest stores below
        for (var i = storeArray.length - 1; i >= 0 ; i--) {
           var store = storeArray[i];
           
           store.name = store.name.toUpperCase();// name in uppercase.
           var addr = store.addr.split("|"); // "MAGASIN BELL, 1600 Le Corbusier #9 |Laval (Québec)|H7S 1Y9"
           store.street = addr[0];
           store.city = addr[1];
           store.postal = addr[2];
           
		   var shape=SLShape.CreateStoreShape(store, i, true);		   
           newLayer.AddShape(shape);
		   
		   // create the crosslinks.
		   store.ShapeId=shape.GetID();
		   store.Shape=shape;
		   shape.StoreId=i;
		   shape.Store=store;
        }
        
		storeLocator.result(storeArray); // keep storeArray in storeLocator object.
		SLMap.SetShapeLayer(newLayer);
		
		document.getElementById("sumResultsFound").innerHTML = "<strong>" + storeArray.length + "</strong> "
				+ SLMesg.xStoresFoundNearY + " <strong>" + document.getElementById("txtCloseTo").value + "</strong>"; 
		
        var farthest = storeArray[storeArray.length - 1].dist;
        
        /**
         * Zoom to the level we deem best to show our stores.
         * (NOTE: The stores are only shown at end of zooming)
         */
        SmoothZoom.Zoom(getZoomLevel(farthest));

		yyy=1;
    }
	    
