﻿
/*
----------------------------------------------------------------------------------------------|
this is the restaurant rating container class.  the widgets or other
artifacts from the page will interact with this class to modify 
or change the state of each widget
*/
function RestaurantRatingContainer(userLogin) {
    this.WEB_SERVICE_BASE = "/_WebServices/AjaxServices.asmx/DoRestaurantRating";
    this.UserLogin = userLogin;
    this.MaxRating = 10;
    this.RatingControls = [];
    
    this.StarImageActive = 'http://www.foodfinder.net/_Images/starActive.gif';
    this.StarImageFilled = 'http://www.foodfinder.net/_Images/starFilled.gif';
    this.StarImageUser = 'http://www.foodfinder.net/_Images/starActive.gif';
    this.StarImageEmpty = 'http://www.foodfinder.net/_Images/starEmpty.gif';
    this.StarImageHalf = 'http://www.foodfinder.net/_Images/starHalf.gif';
    
    this.WorkingImage = 'http://www.foodfinder.net/_Images/icon_anim_loadingSmallCircle.gif';
    
    this.AddWidget = function(  restaurantId, 
                                restaurantName, 
                                element, 
                                overallRating, 
                                serviceRating, 
                                atmosphereRating, 
                                userServiceRating, 
                                userAtmosphereRating, 
                                ratingCount) {
                        this.RatingControls[restaurantId] = new RestaurantRatingWidget(
                                                                        restaurantId, 
                                                                        restaurantName, 
                                                                        this, 
                                                                        element, 
                                                                        overallRating, 
                                                                        serviceRating, 
                                                                        atmosphereRating, 
                                                                        userServiceRating, 
                                                                        userAtmosphereRating, 
                                                                        ratingCount)
                    }
                     
    this.RenderWidget = function(restaurantId, isEditable) {
                            this.RatingControls[restaurantId].Render(isEditable);
                    }
                     
    this.SetServiceActiveRating = function(restaurantId, activeRating, toggleLock) {
    
                        this.RatingControls[restaurantId].ServiceActiveRating = activeRating;
                        this.RatingControls[restaurantId].Render(true);    
                                          
                    }
                    
    this.SetAtmosphereActiveRating = function(restaurantId, activeRating, toggleLock) {
  
                        this.RatingControls[restaurantId].AtmosphereActiveRating = activeRating;
                        this.RatingControls[restaurantId].Render(true);
                        
                    }
                    
    /*
    this.SubmitRating = function(restaurantId, description) {
                        
                        this.RatingControls[restaurantId].SubmitRating(this.UserLogin, description, this.WEB_SERVICE_BASE);
                        
                    }
    */
    this.SubmitRating = function(restaurantId) {
                        
                        this.RatingControls[restaurantId].SubmitRating(this.UserLogin, this.WEB_SERVICE_BASE);
                        
                    }
                    
    this.ClearControls = function() {
                        this.RatingControls = new Array();
                    }
}


function RestaurantRatingWidget(    restaurantId, 
                                    restaurantName, 
                                    container, 
                                    element, 
                                    overallRating, 
                                    serviceRating, 
                                    atmosphereRating, 
                                    userServiceRating, 
                                    userAtmosphereRating, 
                                    ratingCount) {
                                    
    this.RestaurantId = restaurantId;
    this.RestaurantName = restaurantName;
    this.Container = container;
    this.ElementToDrawInto = element;
    this.OverallRating = overallRating;
    this.ServiceRating = serviceRating;
    this.AtmosphereRating = atmosphereRating;
    this.RatingCount = parseInt(ratingCount);
    this.UserServiceRating = userServiceRating;
    this.UserAtmosphereRating = userAtmosphereRating;
    this.ServiceActiveRating = 0;
    this.AtmosphereActiveRating = 0;
    this.ActiveDescription = '';
    this.ServiceLocked = false;
    this.AtmosphereLocked = false;
    
    this.Render = RenderRestaurantRatingWidget;    
    
    this.SetActiveRating = function (activeRating) { this.ActiveRating = activeRating; }
    
    this.SubmitRating = function (userLogin, serviceUrl) {
                   
            MessageBuffer.addMessage(
                serviceUrl,
                'DoRestaurantRating',
                'window.RestaurantRatingEngine.RatingControls[' + this.RestaurantId + '].callback_SubmitRating',
                AJAX_REQTYPE_POST,
                AJAX_RETTYPE_JSON,
                'userLogin=' + userLogin + '&restaurantId=' + this.RestaurantId + '&serviceRating=' + this.ServiceActiveRating + '&atmosphereRating=' + this.AtmosphereActiveRating,
                false,
                false
            );
            
            document.getElementById(this.ElementToDrawInto).innerHTML = '<img src="' + this.Container.WorkingImage + '" title="Saving..." border="0" alt="Saving..." align="left" />&nbsp;&nbsp;Saving...';
        }
        
    this.callback_SubmitRating = function(msg) {
            var ratingResults = msg.ReturnValue;

            if(ratingResults && ratingResults.UpdateSuccessful) {
                this.UserServiceRating = ratingResults.UserServiceRating;
                this.UserAtmosphereRating = ratingResults.UserAtmosphereRating;
                this.ServiceRating = ratingResults.ServiceRating;
                this.AtmosphereRating = ratingResults.AtmosphereRating;
                this.OverallRating = ratingResults.OverallRating;
                this.RatingCount = ratingResults.TotalRestaurantResponses;

                this.Render(false, "<span style='color: green'>Thank you for your submission</span>");
            } else {
                this.Render(false, "<span style='color: maroon'>There was a problem with you submission</span>");
            }                
        }
}


function RenderRestaurantRatingWidget(isEditable, userMessage) {
    var o = [];
    var isRated = (this.OverallRating > 0);
    var isServiceRatedByUser = (this.UserServiceRating > 0);
    var isAtmosphereRatedByUser = (this.UserAtmosphereRating > 0);
    
    var serviceRatingToShow = isServiceRatedByUser ? this.UserServiceRating : this.ServiceRating;
    var atmosphereRatingToShow = isAtmosphereRatedByUser ? this.UserAtmosphereRating : this.AtmosphereRating;
    
    var canDoRating = ((this.Container.UserLogin) && (this.Container.UserLogin.length > 0));
    
    var staticServiceRatingIcon = isServiceRatedByUser ? this.Container.StarImageUser : this.Container.StarImageFilled;
    var staticAtmosphereRatingIcon = isAtmosphereRatedByUser ? this.Container.StarImageUser : this.Container.StarImageFilled;

    o.push('<div class="ratingContainerLarge">');
    
    if(userMessage)
        o.push('<div>' + userMessage + '<div>');
    
    if(isRated) {
        
        if(canDoRating) {
            
            o.push('<div style="margin-bottom: 3px; text-align: right;">');
            o.push('<table cellpadding="2" border="0" cellspacing="0" width="250"><tr><td><b>Service</b></td>');	        
	        o.push('<td>' + GetEditableRestaurantStarMarkup(this.Container.MaxRating, serviceRatingToShow, this.ServiceActiveRating, staticServiceRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.RestaurantId, "SetServiceActiveRating"));
        	o.push('</td></tr>');
        	
	        o.push('<tr><td><b>Atmosphere</b></td>');	        
	        o.push('<td>' + GetEditableRestaurantStarMarkup(this.Container.MaxRating, atmosphereRatingToShow, this.AtmosphereActiveRating, staticAtmosphereRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.RestaurantId, "SetAtmosphereActiveRating"));
            o.push('</td></tr></table></div>');	        
        } else {
            o.push('<div style="margin-bottom: 3px; text-align: right;">');	
            o.push('<table cellpadding="2" border="0" cellspacing="0" width="250"><tr><td><b>Service</b></td>');        
	        o.push('<td>' + GetStaticStarMarkup(this.Container.MaxRating, serviceRatingToShow, staticServiceRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf));
        	o.push('</td></tr>');
        	
	        o.push('<tr><td><b>Atmosphere</b></td>');	        
	        o.push('<td>' + GetStaticStarMarkup(this.Container.MaxRating, atmosphereRatingToShow, staticAtmosphereRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf));
            o.push('</td></tr>');	
            
            o.push('<tr><td colspan="2"><a href="Login.aspx">Log in</a> or <a href="Register.aspx">Register</a> to rate</td></tr>');
            
            o.push('</table></div>');
        }
        
    } else {

        if(canDoRating) {
            o.push('<div style="margin-bottom: 3px; text-align: right;">');
            o.push('<table cellpadding="2" border="0" cellspacing="0" width="250"><tr><td><b>Service</b></td>');	        
	        o.push('<td>' + GetEditableRestaurantStarMarkup(this.Container.MaxRating, serviceRatingToShow, this.ServiceActiveRating, staticServiceRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.RestaurantId, "SetServiceActiveRating"));
        	o.push('</td></tr>');
        	
	        o.push('<tr><td><b>Atmosphere</b></td>');       
	        o.push('<td>' + GetEditableRestaurantStarMarkup(this.Container.MaxRating, atmosphereRatingToShow, this.AtmosphereActiveRating, staticAtmosphereRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.RestaurantId, "SetAtmosphereActiveRating"));
            o.push('</td></tr>');
            o.push('<tr><td colspan="2">Nobody has rated this restaurant<br /><b>be the first!</b></td></tr>');
            o.push('</table></div>');	
            
        } else {   
            o.push('<div style="text-align: center;">Nobody has rated this restaurant<br /><b>be the first!</b></div>');       
            o.push('<div style="text-align: center;"><a href="Login.aspx">Log in</a> or <a href="Register.aspx">Register</a> to rate</div>');
        }        
    }
    
    if(canDoRating) {
        if(isServiceRatedByUser || isAtmosphereRatedByUser) {
            o.push('<div style="text-align: center;">Roll over to change rating</div>');            
        } else {    
            o.push('<div style="text-align: center;">Roll over to rate</div>');
        }          
    }

    o.push('</div>');
    
    document.getElementById(this.ElementToDrawInto).innerHTML = o.join('');
}


function GetEditableRestaurantStarMarkup(maxRating, rating, activeRating, filledStar, emptyStar, halfStar, activeStar, restaurantId, action) {
    var o = [];
    
    for(var i = 1; i <= maxRating; i++) {      
        if(i <= activeRating)
            o.push('<a href="javascript:void(0);" onclick="window.RestaurantRatingEngine.SubmitRating(' + restaurantId + ');"><img src="' + activeStar + '" border="0" onmouseout="window.RestaurantRatingEngine.' + action + '(' + restaurantId + ', 0, false);"></a>');
        else if(i <= rating) {
            o.push('<a href="javascript:void(0);" onclick="window.RestaurantRatingEngine.SubmitRating(' + restaurantId + ');"><img src="' + filledStar + '" border="0" onmouseover="window.RestaurantRatingEngine.' + action + '(' + restaurantId + ', ' + i + ', false);"></a>');
        } else if ((i > rating) && (Math.abs(i - rating) < 1)) {
            o.push('<a href="javascript:void(0);" onclick="window.RestaurantRatingEngine.SubmitRating(' + restaurantId + ');"><img src="' + halfStar + '" border="0" onmouseover="window.RestaurantRatingEngine.' + action + '(' + restaurantId + ', ' + i + ', false);"></a>');
        } else {
            o.push('<a href="javascript:void(0);" onclick="window.RestaurantRatingEngine.SubmitRating(' + restaurantId + ');"><img src="' + emptyStar + '" border="0" onmouseover="window.RestaurantRatingEngine.' + action + '(' + restaurantId + ', ' + i + ', false);"></a>'); 
        }      
    }
    
    return o.join('');
}


/*
----------------------------------------------------------------------------------------------|
*/






/*
----------------------------------------------------------------------------------------------|
this is the menu item rating container class.  the widgets or other
artifacts from the page will interact with this class to modify 
or change the state of each widget
*/
function MenuItemRatingContainer(userLogin) {
    this.WEB_SERVICE_BASE = "/_WebServices/AjaxServices.asmx/DoMenuItemRating";
    this.UserLogin = userLogin;
    this.MaxRating = 10;
    this.RatingControls = [];
    
    this.StarImageActive = 'http://www.foodfinder.net/_Images/starActive.gif';
    this.StarImageFilled = 'http://www.foodfinder.net/_Images/starFilled.gif';
    this.StarImageUser = 'http://www.foodfinder.net/_Images/starActive.gif';
    this.StarImageEmpty = 'http://www.foodfinder.net/_Images/starEmpty.gif';
    this.StarImageHalf = 'http://www.foodfinder.net/_Images/starHalf.gif';
    
    this.WorkingImage = 'http://www.foodfinder.net/_Images/icon_anim_loadingSmallCircle.gif';
    
    this.DoSimpleRating = false;
    
    this.AddWidget = function(menuItemId, menuItemName, restaurantName, restaurantId, element, overallRating, userRating, ratingCount) {
                        this.RatingControls[menuItemId] = new MenuItemRatingWidget(menuItemId, menuItemName, restaurantName, restaurantId, this, element, overallRating, userRating, ratingCount)
                    }
                     
    this.RenderWidget = function(menuItemId, isEditable) {
    
                        if(this.DoSimpleRating)
                            this.RatingControls[menuItemId].RenderSimple();
                        else
                            this.RatingControls[menuItemId].Render(isEditable);
                    }
                     
    this.SetActiveRating = function(menuItemId, activeRating, toggleLock) {
                            
                        this.RatingControls[menuItemId].ActiveRating = activeRating;
                        this.RatingControls[menuItemId].Render(true);                       
                    }
    
    
    this.SubmitRating = function(menuItemId) {
                        
                        this.RatingControls[menuItemId].SubmitRating(this.UserLogin, this.WEB_SERVICE_BASE, menuItemId);
                        
                    }
                    
    this.ClearControls = function() {
                        this.RatingControls = new Array();
                    }
}


function MenuItemRatingWidget(menuItemId, menuItemName, restaurantName, restaurantId, container, element, overallRating, userRating, ratingCount) {
    this.MenuItemId = menuItemId;
    this.MenuItemName = menuItemName;
    this.RestaurantName = restaurantName;
    this.RestaurantId = restaurantId;
    this.Container = container;
    this.ElementToDrawInto = element;
    this.OverallRating = overallRating;
    this.RatingCount = ratingCount;
    this.UserRating = userRating;
    this.ActiveRating = 0;
    this.ActiveDescription = '';
    this.RatingLocked = false;
    
    this.Render = RenderMenuItemRatingWidget;    
    this.RenderSimple = RenderSimpleWidget;
    
    this.SetActiveRating = function (activeRating) { this.ActiveRating = activeRating; }
    
    this.SubmitRating = function (userLogin, serviceUrl, menuItemId) {
                        
            MessageBuffer.addMessage(
                serviceUrl,
                'DoRestaurantRating',
                'window.MenuItemRatingEngine.RatingControls[' + menuItemId + '].callback_SubmitRating',
                AJAX_REQTYPE_POST,
                AJAX_RETTYPE_JSON,
                'userLogin=' + userLogin + '&menuItemId=' + menuItemId + '&rating=' + this.ActiveRating + '&restaurantId=' + this.RestaurantId,
                false,
                false
            );
            
            document.getElementById(this.ElementToDrawInto).innerHTML = '<img src="' + this.Container.WorkingImage + '" title="Saving..." border="0" alt="Saving..." align="left" />&nbsp;&nbsp;Saving...';
        }
        
    this.callback_SubmitRating = function(msg) {
            var ratingResults = msg.ReturnValue;

            if(ratingResults && ratingResults.UpdateSuccessful) {
                this.UserRating = ratingResults.UserRating;
                this.OverallRating = ratingResults.OverallRating;
                this.RatingCount = ratingResults.TotalMenuItemRatings;
                
                this.Render(false, "<span style='color: green'>Thank you for your submission</span>");
            } else {
                this.Render(false, "<span style='color: maroon'>There was a problem with you submission</span>");
            }                
        }
}


function RenderMenuItemRatingWidget(isEditable, userMessage) {
    if(this.Container.DoSimpleRating) {
        this.RenderSimple();
        return;
    }
    
    var o = [];
    var isRated = (this.OverallRating > 0);
    var isRatedByUser = (this.UserRating > 0)
    var canDoRating = ((this.Container.UserLogin) && (this.Container.UserLogin.length > 0));

    var staticRatingIcon = isRatedByUser ? this.Container.StarImageUser : this.Container.StarImageFilled;
    var ratingToShow = isRatedByUser ? this.UserRating : this.OverallRating;
    
    o.push('<div class="ratingContainerLarge">');
    
    if(userMessage)
        o.push('<div style="margin-bottom: 6px; text-align: left;">' + userMessage + '<div>');
    
    if(isRated) {
        if(canDoRating) {
            o.push('<div style="margin-bottom: 11px;"><table cellpadding="2" border="0" cellspacing="0" width="230"><tr>');
            o.push('<td><b>Overall Rating</b></td>');
            o.push('<td>' + GetEditableMenuItemStarMarkup(this.Container.MaxRating, ratingToShow, this.ActiveRating, staticRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.MenuItemId) + '</td></tr></table>');
        } else {
            o.push('<div style="margin-bottom: 11px;"><table cellpadding="2" border="0" cellspacing="0" width="230"><tr>');
            o.push('<td><b>Overall Rating</b></td>');
            o.push('<td>' + GetStaticStarMarkup(this.Container.MaxRating, ratingToShow, staticRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf) + '</td></tr></table>');

            o.push('<div><a href="Login.aspx">Log in</a> or <a href="Register.aspx">Register</a> to rate</div>');
        }
    } else {
        if(canDoRating) {
            o.push('<div style="margin-bottom: 11px;"><table cellpadding="2" border="0" cellspacing="0" width="230"><tr>');
            o.push('<td><b>Overall Rating</b></td>');
            o.push('<td>' + GetEditableMenuItemStarMarkup(this.Container.MaxRating, ratingToShow, this.ActiveRating, staticRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.MenuItemId) + '</td></tr></table>');
            o.push("<div>Nobody has rated this dish<br /><b>Be the first!</b></div>");
        } else {   
            o.push("<div>Nobody has rated this dish<br /><b>Be the first!</b></div>");         
            o.push('<div><a href="Login.aspx">Log in</a> or <a href="Register.aspx">Register</a> to rate</div>');
        }        
    }
    
    if(isRated)
        o.push('<div>Average overall rating (' + this.RatingCount + ' ratings): ' + roundNumber(this.OverallRating, 2) + '</div>');        
    
    if(canDoRating) {
        if(isRatedByUser) {
            o.push('<div>Your rating: ' + this.UserRating + '</div>');
            o.push('<div>roll over to change rating</div>');            
        } else {    
            o.push('<div>roll over to rate</div>');
        }          
    }
        
    o.push('</div></div></div>');

    document.getElementById(this.ElementToDrawInto).innerHTML = o.join('');
}


function RenderSimpleWidget() {
    var o = [];
    var isRated = (this.OverallRating > 0);
    var isRatedByUser = (this.UserRating > 0)
    var canDoRating = ((this.Container.UserLogin) && (this.Container.UserLogin.length > 0));

    var staticRatingIcon = isRatedByUser ? this.Container.StarImageUser : this.Container.StarImageFilled;
    var ratingToShow = isRatedByUser ? this.UserRating : this.OverallRating;
    
    o.push('<div class="ratingContainerSimple">');
    
    if(isRated) {
        if(canDoRating) {
            o.push(GetEditableMenuItemStarMarkup(this.Container.MaxRating, ratingToShow, this.ActiveRating, staticRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.MenuItemId));
        } else {
            o.push(GetStaticStarMarkup(this.Container.MaxRating, ratingToShow, staticRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf));
        }
    } else {
        if(canDoRating) {
            o.push(GetEditableMenuItemStarMarkup(this.Container.MaxRating, ratingToShow, this.ActiveRating, staticRatingIcon, this.Container.StarImageEmpty, this.Container.StarImageHalf, this.Container.StarImageActive, this.MenuItemId));
        } else {           
            o.push('<a href="Login.aspx">Log in</a> or <a href="Register.aspx">Register</a> to rate');
        }        
    }
        
    o.push('</div>');

    document.getElementById(this.ElementToDrawInto).innerHTML = o.join('');
}


function GetEditableMenuItemStarMarkup(maxRating, rating, activeRating, filledStar, emptyStar, halfStar, activeStar, menuItemId) {
    var o = [];
    
    for(var i = 1; i <= maxRating; i++) {      
        if(i <= activeRating)
            o.push('<a href="javascript:void(0);" onclick="window.MenuItemRatingEngine.SubmitRating(' + menuItemId + ', ' + i + ', true);"><img src="' + activeStar + '" border="0" onmouseout="setTimeout(\'window.MenuItemRatingEngine.SetActiveRating(' + menuItemId + ', 0, false)\',100);"></a>');
        else if(i <= rating) {
            o.push('<a href="javascript:void(0);" onclick="window.MenuItemRatingEngine.SubmitRating(' + menuItemId + ', ' + i + ', true);"><img src="' + filledStar + '" border="0" onmouseover="window.MenuItemRatingEngine.SetActiveRating(' + menuItemId + ', ' + i + ', false);"></a>');
        } else if ((i > rating) && (Math.abs(i - rating) < 1)) {
            o.push('<a href="javascript:void(0);" onclick="window.MenuItemRatingEngine.SubmitRating(' + menuItemId + ', ' + i + ', true);"><img src="' + halfStar + '" border="0" onmouseover="window.MenuItemRatingEngine.SetActiveRating(' + menuItemId + ', ' + i + ', false);"></a>');
        } else {
            o.push('<a href="javascript:void(0);" onclick="window.MenuItemRatingEngine.SubmitRating(' + menuItemId + ', ' + i + ', true);"><img src="' + emptyStar + '" border="0" onmouseover="window.MenuItemRatingEngine.SetActiveRating(' + menuItemId + ', ' + i + ', false);"></a>'); 
        }      
    }
    
    return o.join('');
}
/*
----------------------------------------------------------------------------------------------|
*/




/*
----------------------------------------------------------------------------------------------|
there functions are shared between the two rating type
*/
function GetStaticStarMarkup(maxRating, rating, filledStar, emptyStar, halfStar) {
    var o = [];
    
    for(var i = 1; i <= maxRating; i++) {      
        if(i <= rating) {
            o.push('<img src="' + filledStar + '" border="0">');
        } else if ((i > rating) && (Math.abs(i - rating) < 1)) {
            o.push('<img src="' + halfStar + '" border="0">');
        } else {
            o.push('<img src="' + emptyStar + '" border="0">'); 
        }      
    }
    
    return o.join('');
}



function GetMenuItemResultMarkup(result) {
    var o = [];
    
    var priceText = (result.Price == 0) ? "Unknown" : formatCurrency(result.Price);
    
    o.push('<div style="border: solid 0px #BBBBBB; margin-bottom: 14px; margin-top: 11px;">');
    o.push('<table cellpadding="0" cellspacing="0" border="0" width="100%">');
    o.push('<tr class="resultHeadxx">');
    o.push('<td colspan="2" style="padding: 0px;">');
    o.push('<h3>' + result.MenuItemName + '</h3>');
    o.push('</td>');
    o.push('</tr><tr><td colspan="2" style="padding: 0px;">');
    
    /*
    if((result.UserLogin) && (result.UserLogin.length > 0) && (result.UserMenuItemRating == 0)) {
        if(result.MarkedForUserToTry)
            o.push('<div style="background-color: #99FF99; color: black; padding: 3px; border: solid 1px #999999;"><img src="http://www.foodfinder.net/_images/objects_032.gif" alt="You marked this item...eat up!" title="You marked this item...eat up!" /> You\'ve marked this item...eat up!</div>');
    } 
    */ 
    
    o.push('</td></tr><tr>');
    o.push('<td valign="top" style="padding: 4px;">');      
    o.push('<div>Restaurant: <a href="' + result.RestaurantUrl + '">' + result.RestaurantName + '</a></div>');
    o.push('<div>Distance: <span id="distanceDiv_' + result.MenuItemId + '" style="font-weight: bold;">' + roundNumber(result.Distance, 2) + '</span> miles</div>');
    o.push('<div>Price: <span id="itemPriceDiv_' + result.MenuItemId + '" style="font-weight: bold;">' + priceText + '</span></div>');
    o.push('<div id="descriptionDiv_' + result.MenuItemId + '" style="margin-top: 8px;">' + result.MenuItemDescription + '</div>');
    /*
    if((!result.MarkedForUserToTry) && (result.UserMenuItemRating == 0))
        o.push('<div style="padding: 3px; margin-top: 8px;" id="mark_' + result.MenuItemId + '"><a href="javascript:void(0);" onclick="setItemToTryForUser(\'' + result.UserLogin + '\', ' + result.MenuItemId + ', \'mark_' + result.MenuItemId + '\', ' + result.RestaurantId + ');"><img src="http://www.foodfinder.net/_images/objects_030.gif" alt="Mark this item to try" title="Mark this item to try" /> Mark this item to try!</a></div>');
    */    
    o.push('</td>');
    o.push('<td valign="top" align="right" width="300" style="padding: 0px 20px 0px 4px;">');
    o.push('<div id="resultRight_' + result.MenuItemId + '" style="width: 300px; padding: 0px"></div>');
    o.push('</td>');
    o.push('</tr>');
    o.push('</table>');
    o.push('</div>');
    
    window.MenuItemRatingEngine.AddWidget(
        result.MenuItemId, 
        result.MenuItemName, 
        result.RestaurantName, 
        result.RestaurantId,
        "resultRight_" + result.MenuItemId,
        result.MenuItemRating,
        result.UserMenuItemRating,
        result.RatingCount);
    
    return o.join('');    
}


function GetMenuItemResultMarkupForRestaurant(result, restaurantId, cssClass) {
    var o = [];
    
    var priceText = (result.Price == 0) ? "Unknown" : formatCurrency(result.Price);

    o.push('<tr class="' + cssClass + '">');
    o.push('<td valign="top"><span title="' + result.MenuItemDescription + '"><strong>' + result.MenuItemName + '</strong><div style="font-family: tahoma; font-size: 8pt; font-style: italic; padding-left: 14px;">' + result.MenuItemDescription + '</div></span></td>');
    o.push('<td valign="top"><strong>' + priceText + '</strong></td>');
    o.push('<td valign="top" align="right"><div id="resultRight_' + result.MenuItemId + '"></div></td>');
    o.push('</tr>');
    
    window.MenuItemRatingEngine.AddWidget(
        result.MenuItemId, 
        result.MenuItemName, 
        result.RestaurantName, 
        restaurantId,
        "resultRight_" + result.MenuItemId,
        result.MenuItemRating,
        result.UserMenuItemRating,
        result.RatingCount);
    
    return o.join('');    
}



function GetRestaurantResultMarkup(result) {
    var o = [];

    o.push('<div style="border-bottom: dotted 0px #BBBBBB; margin-bottom: 6px; margin-top: 5px;">');
    o.push('<table cellpadding="3" cellspacing="0" border="0" width="100%">');
    o.push('<tr class="resultHeadxx">');
    o.push('<td colspan="2" style="padding: 0px;">');
    o.push('<h3>');
    //o.push('<a href="Restaurant.aspx?rid=' + result.RestaurantId + '">' + result.RestaurantName + '</a>');
    o.push('<a href="' + result.RestaurantUrl + '">' + result.RestaurantName + '</a>');
    o.push('<span style="font-size: 9pt; font-family: arial helvetica;"> - (<b>' + roundNumber(result.Distance, 2) + '</b> miles)</span>');
    o.push('</h3>');
    o.push('</td>');
    o.push('</tr>');
    o.push('<tr><td valign="top" style="padding: 5px;">');
    
    o.push('<div style="font-style: italic; margin-bottom: 21px;">');
    
    if(result.OverallRating > 0)
        o.push(GetStaticStarMarkup(10, result.OverallRating, "http://www.foodfinder.net/_images/starLarge2.jpg", "http://www.foodfinder.net/_images/starLarge_empty2.jpg", "http://www.foodfinder.net/_images/starLarge_half2.jpg") + '<br />');
        
    o.push('<div>' + result.Address1 + '</div>');
    o.push('<div>' + result.City + ', ' + result.State + ' ' + result.Zip + '</div>');
    o.push('<div>' + result.Phone + '</div>');
    o.push('</div>');
    
    if(result.Menus && result.MenuItemCount > 0) {
    
        o.push('<div style="padding: 5px;">');
        o.push('<img src="http://www.foodfinder.net/_images/icon_menu.gif" alt="Menu items are available" title="Menu items are available" align="left" style="padding: 5px;" /> View <b>' + result.MenuItemCount + '</b> items in <b>' + result.Menus.length + '</b> menus<br />');
        
        for(var i = 0; i < result.Menus.length; i++) {
            o.push('<a href="javascript:void(0);" onclick="updateMenuResults(' + result.Menus[i].MenuId + ', \'menu_' + result.RestaurantId + '\', '  + result.RestaurantId + ');">');
            o.push(result.Menus[i].MenuName + '</a>');
            
            if(i < (result.Menus.length - 1))
                o.push(' | ');
        }  
        o.push('</div>');
    } else {
        o.push('<div style="padding: 5px;"><i>no menu items yet</i></div>');
    }

    o.push('</td>');
    o.push('<td valign="top" align="right">');
    o.push('<div id="resultRight_' + result.RestaurantId + '"></div>');
    o.push('</td></tr>');
    
    o.push('<tr><td colspan="2">');
    o.push('<div id="menu_' + result.RestaurantId + '" style="padding: 5px;"></div>');
    o.push('</td></tr></table>');   
    
    o.push('</div>');
    
    window.RestaurantRatingEngine.AddWidget (
        result.RestaurantId, 
        result.RestaurantName, 
        "resultRight_" + result.RestaurantId,
        result.OverallRating,
        result.ServiceRating,
        result.AtmosphereRating,
        result.UserServiceRating,
        result.UserAtmosphereRating,
        result.RestaurantRatingCount
    );
    
    return o.join('');  

}



function setItemToTryForUser(userLogin, menuItemId, linkContainer, restaurantId) {
        document.getElementById(linkContainer).innerHTML = '<img src="http://www.foodfinder.net/_Images/icon_anim_loadingSmallCircle.gif" title="Saving..." border="0" alt="Saving..." align="left" />&nbsp;&nbsp;Saving...';
        MessageBuffer.addMessage(
            '/_WebServices/AjaxServices.asmx/SetItemToTryForUser',
            'SetItemToTryForUser',
            'callback_SetItemToTryForUser',
            AJAX_REQTYPE_POST,
            AJAX_RETTYPE_JSON,
            'userLogin=' + userLogin + '&menuItemId=' + menuItemId + '&restaurantId=' + restaurantId,
            false,
            false
        );
    }
    
function callback_SetItemToTryForUser(message) {

    setTimeout('updateResults();', 50);
}




function encodeRestaurantCriteria(){

}


function saveUserSearchLocation(userLogin, searchQuery) {
        MessageBuffer.addMessage(
            '/_WebServices/AjaxServices.asmx/PersistSearchQuery',
            'PersistSearchQuery',
            'callback_PersistSearchQuery',
            AJAX_REQTYPE_POST,
            AJAX_RETTYPE_JSON,
            'userLogin=' + userLogin + '&searchQuery=' + searchQuery,
            false,
            false
        );
    }
    
function callback_PersistSearchQuery() { }



function getDidYouKnow(userLogin, page) {
    if(!$("didYouKnowDiv"))
        return;
    
    document.getElementById("didYouKnowDiv").innerHTML = '<img src="http://www.foodfinder.net/_Images/icon_anim_loadingSmallCircle.gif" title="Loading..." border="0" alt="Loading..." align="left" />&nbsp;&nbsp;Loading...';

    MessageBuffer.addMessage(
            '/_WebServices/AjaxServices.asmx/GetDidYouKnow',
            'GetDidYouKnow',
            'callback_GetDidYouKnow',
            AJAX_REQTYPE_POST,
            AJAX_RETTYPE_STRING,
            'userLogin=' + userLogin + '&url=' + page,
            false,
            false
        );
}


function callback_GetDidYouKnow(message) {
    document.getElementById("didYouKnowDiv").innerHTML = message.ReturnValue;    
}



function craftRestaurantPacket( userLogin, overallRatingLow, overallRatingHigh,
                                longitude, latitude, maxDistance,
                                keyword, ratingOption, startRecord, endRecord) {
    var o = [];
    
    o.push("userLogin=" + userLogin + "&");
    o.push("overallRatingLow=" + overallRatingLow + "&");
    o.push("overallRatingHigh=" + overallRatingHigh + "&");
    o.push("longitude=" + longitude + "&");
    o.push("latitude=" + latitude + "&");
    o.push("maxDistance=" + maxDistance + "&");
    o.push("keyword=" + keyword + "&");
    o.push("ratingOption=" + ratingOption + "&");
    o.push("startRecord=" + startRecord + "&");
    o.push("endRecord=" + endRecord);
    
    return o.join('');
}


function craftMenuItemPacket(   startRecord, endRecord, userLogin, 
                                ratingLow, ratingHigh, priceLow, 
                                priceHigh, longitude, latitude,
                                maxDistance, cuisineIdList, keyword,
                                ratingOption, restaurantId) {
    var o = [];
    
    o.push("userLogin=" + userLogin + "&");
    o.push("ratingLow=" + ratingLow + "&");
    o.push("ratingHigh=" + ratingHigh + "&");
    o.push("priceLow=" + priceLow + "&");
    o.push("priceHigh=" + priceHigh + "&");
    o.push("longitude=" + longitude + "&");
    o.push("latitude=" + latitude + "&");
    o.push("maxDistance=" + maxDistance + "&");
    o.push("cuisineIdList=" + cuisineIdList + "&");
    o.push("keyword=" + keyword + "&");
    o.push("ratingOption=" + ratingOption + "&");
    o.push("startRecord=" + startRecord + "&");
    o.push("endRecord=" + endRecord + "&");
    o.push("restaurantId=" + restaurantId);
    
    return o.join('');  
}


function craftMenuFilterPacket(menuId, restaurantId) {
    var o = [];
    
    o.push("menuId=" + menuId + "&restaurantId=" + restaurantId);
    
    return o.join('');           
}



//---jQuery rating widget rating functions
function submitMenuItemRating(mid,rating,rid) {
    //turnOnInterstitial();
    
    var userLogin = document.getElementById('ul').value;

    MessageBuffer.addMessage(
        '/_WebServices/AjaxServices.asmx/DoMenuItemRating',
        'DoRestaurantRating',
        'cb',
        AJAX_REQTYPE_POST,
        AJAX_RETTYPE_JSON,
        'userLogin=' + userLogin + '&menuItemId=' + mid + '&rating=' + rating + '&restaurantId=' + rid,
        false,
        false
    );    
}


function cb(msg) {
    //turnOffInterstitial();
}


function submitRestaurantRating(rid,service,atmosphere) {
    //turnOnInterstitial();
    
    var userLogin = document.getElementById('ul').value;

    MessageBuffer.addMessage(
                '/_WebServices/AjaxServices.asmx/DoRestaurantRating',
                'DoRestaurantRating',
                'cb',
                AJAX_REQTYPE_POST,
                AJAX_RETTYPE_JSON,
                'userLogin=' + userLogin + '&restaurantId=' + rid + '&serviceRating=' + service + '&atmosphereRating=' + atmosphere,
                false,
                false
            );    
}



