//<script type="text/javascript" src="/www/common/client_scripts/favorite.js"></script>

var favToggleHiddenFieldID = 'favToggle';
var onFileKey = "_on";
var offFileKey = "_off";
var ajaxLoaderFavMealFileName = 'fav_meal_loader.gif';

function ToggleFavorite(divID, vars, doDBUpdate)
{
    try{
        // Set the appropriate type (food or meal favorite)
        var favType;
        var removeDesc;
        var addDesc;
        if (divID.indexOf('divFavInst') > -1)
        {
            favType = 'inst';
            removeDesc = "Remove meal from favorites";
            addDesc = "Add meal to favorites";
        }
        else
        {
            favType = 'food';
            removeDesc = "Remove food from favorites";
            addDesc = "Add food to favorites";
        }  
          
        //2 ways to swap- by key or filename, so try both    
        var newState = '';               
        var allTags = document.getElementsByTagName('div');
        for(i=0; i<allTags.length; i++){
            if(allTags[i].id == divID) // if id == 'divFavFood32'
            {
                var thisInnerHTML =  allTags[i].innerHTML;
                if (thisInnerHTML.indexOf(this.onFileKey) > -1) // iF THIS HAD THE ON STATE
                {
                    newState = 'toggleoff';
                    allTags[i].innerHTML = thisInnerHTML.replace(this.onFileKey, this.offFileKey).replace(removeDesc, addDesc);
                }
                else if (thisInnerHTML.indexOf(this.offFileKey) > -1 )// IF it has the OFF State
                {
                    newState = 'toggleon'; 
                    allTags[i].innerHTML = thisInnerHTML.replace(this.offFileKey, this.onFileKey).replace(addDesc, removeDesc);
                    if (doDBUpdate && favType == 'inst' && divID.indexOf('_') > -1) // if divID has no instID assigned to it
                    {
                        // This is the case where we are saving a brand new inst.. so we want to show the loader image:  because it may take a bit.
                        var m = thisInnerHTML.match(/(.*)[\/\\]([^\/\\]+\.\w+)(['"].*)$/);
                        if (m != null)
                           allTags[i].innerHTML = thisInnerHTML.replace(m[2], this.ajaxLoaderFavMealFileName);                  
                    }
                } 
            }           
        }
        
        if (newState == '')
            newState = 'toggleoff';
        
        newVars = favType+'|'+newState+'|'+doDBUpdate+'|'+vars;  
       __doPostBack(favToggleHiddenFieldID, newVars); 
        
        
    }catch(ex){throw ex;}
}
