
//Section to add the Review Text

function addReview(elementId)
{
   var recipeId    = document.getElementById("reviewrecipeid").value; 
   var ratingValue = document.getElementById("ratingvalue").value; 
   var notesType   = document.getElementById("notestype").value;
   var notesText   = document.getElementById(elementId).value;
   var userId      = document.getElementById("userid").value;
   
   GiantEagle.Applications.InternetSites.Web.Services.RecipeService.AddReview(recipeId,userId,notesText,ratingValue,notesType,callbackAddReview,undefined);
}

function callbackAddReview(result)
{  
    
   if(result.OperationSucceeded)
   {
      parent.location.href = _geApplicationRoot+'/Recipes/RecipeDetails.aspx?recipeID='+result.ClientData;
   }
   else
   {
     alert("There was an error while adding the review.Please try again..");
   }

} 
//End adding review section

//Start adding recipe to folder section

function addRecipeToFolder()
{  
  var recipeId    = document.getElementById(_recipereviewidElementID).value;
  var userId      = document.getElementById(_userElementID).value;
  var folderId    = document.getElementById(_recipeFolderElementID).value;
  
  GiantEagle.Applications.InternetSites.Web.Services.RecipeService.AddRecipeToFolder(userId,folderId,recipeId,callbackaddRecipeToFolder,undefined);
    
}
 
function callbackaddRecipeToFolder(result)
{
   if(result.OperationSucceeded)
   {
     
      var recipeAdded =  document.getElementById(_recipeAddedMsgElementID);
      recipeAdded.innerHTML = result.ClientData;
      setTimeout("addFunction('"+_recipeAddedMsgElementID+"','"+result.ClientData+"')",3000); 
      if(result.ClientData == 'Recipe Added') {
          window.location = window.location;
        __doPostBack('MyRecipesTest$LoadRecipeLink','');
      }
   }
   else
   {
     alert("There was an error while adding the recipe to the folder.Please try again..");
   }
} 

//End adding recipe to folder section

//Start adding ingredients to list section
function addIngredientsToList()
{
    var recipeId    = document.getElementById(_recipereviewidElementID).value;
    var userId      = document.getElementById(_userElementID).value;
 
    GiantEagle.Applications.InternetSites.Web.Services.RecipeService.AddRecipeToShoppingList(userId,recipeId,callbackaddIngredientsToList,undefined);

}
 
 function callbackaddIngredientsToList(result)
 {
    if(result.OperationSucceeded)
    {
           var ingredientAdded =  document.getElementById("ingredientsadded");
           ingredientAdded.innerHTML = 'Ingredients Added';
           //vendjxv added window.location = window.location; to force reload of page so shopping list updates its visuals
           //no longer need to clear out "Add Ingredients" or use __doPostBack()
           window.location = window.location;
           //setTimeout("addFunction('ingredientsadded','Ingredients Added')", 3000);
           //__doPostBack('dashboard$ManageShopList1$updateLink','');
    }
    else
    {
        alert("There was an error while adding the ingredients to the shopping list.Please try again..");
    }

}
 
 
function addFunction(elementId,message)
{
    var productsAdded =  document.getElementById(elementId);
    
    if(productsAdded.innerHTML == message)
    {
         productsAdded.innerHTML ='';
    }
    else
    {
         productsAdded.innerHTML = message;
    }
}

//End adding ingredients to list section

// Start the rating system section
   var _rating = 0;
   var _starImages = new Array("Images/rating0.0.png","Images/rating1.0.png",
                                "Images/rating2.0.png","Images/rating3.0.png",
                                "Images/rating4.0.png","Images/rating5.0.png");
                                
   var _starMessages = new Array("Images/0.png","Images/1.png",
                                "Images/2.png","Images/3.png",
                                "Images/4.png","Images/5.png","Images/saved.gif");
	
   var _starMap = new Array('0,0,0,20',
	'1,0,14,20',
	'15,0,28,20',
	'29,0,42,20',
	'43,0,56,20',
	'57,0,75,20');
		
   var _nullStarMessage = "Images/0.png"	

    var _savedRatings = new Array();
	var _changedRatings = new Array();
	var _starTwinkler = new Array();
	var _msgTwinkler = new Array();
	var _isRatingsBarChanged = false;
	var _delayTime = 500;
	var _allImages = new Array();
	
	
function preloadImages()
{
	for (i=0; i < preloadImages.length ;i++)
	{
	    _allImages[i] = new Image();
	    _allImages[i].src = preloadImages.arguments[i];
	}
}
preloadImages(_starImages);
preloadImages(_starMessages);

function SaveRatingAjax(id, ratingType, ratingValue)
{
	_rating = ratingValue;
	_isRatingsBarChanged = true;
	document.images["messages." + id]=null;	
}

function SwapStars(id, rating)
{
	if (rating == undefined)
	{
		rating = 0;
	}
	document.images["stars." + id].src = _starImages[rating];
}
function SwapStarMsg(id, rating)
{
	if (rating == undefined)
	{
		if(_changedRatings[id] ) 
		{
			document.images["messages." + id].src = _starMessages[6];
		} 
		else 
		{
		    document.images["messages." + id].src = _nullStarMessage;
		}
	} 
	else 
	{
		document.images["messages." + id].src = _starMessages[rating];
	}
}

function changeStarsAndMsg()
{
   var rating = document.getElementById(_ratingValueID).value
   document.getElementById("stars.0").src  =  _geApplicationRoot + 'Images/rating'+ rating + '.0.png';
   document.getElementById("messages.0").src  =  _geApplicationRoot + 'Images/'+rating+'.png';
}

function updaterating()
{
     var rating = document.getElementById(_ratingSelectionId).value;
     if(rating == "-1")
     {
        rating = 0;
     }
     SaveStarsAjax(1,rating);
}


function SaveStarsAjax(id, rating)
{
   
	if ((rating==1)||(rating==2)||(rating==3)||(rating==4)||(rating==5))
	{

	  	if(id == 0)
	  	{	  	   
	  	    var recipeId    = document.getElementById(_recipereviewidElementID).value;
            var userId      = document.getElementById(_userElementID).value;
	  	    GiantEagle.Applications.InternetSites.Web.Services.RecipeService.AddReview(recipeId,userId,"",rating,"reviewnote",callbackAddRating,undefined,rating);
	  	}
	  	
	  	if(id==1)
	  	{   
	  	     document.getElementById("stars.0").src  =  _geApplicationRoot + 'Images/rating'+ rating + '.0.png';
		     document.getElementById("messages.0").src  =  _geApplicationRoot + 'Images/'+rating+'.png';
      	     document.getElementById(_ratingValueID).value = rating;
      	     document.getElementById(_ratingSelectionId).value = rating;   
	  	}
   	}
   	else if(rating ==0)
   	{
   	    	 document.getElementById("stars.0").src  =  _geApplicationRoot + 'Images/rating'+ rating + '.0.png';
		     document.getElementById("messages.0").src  =  _geApplicationRoot + 'Images/'+rating+'.png';
      	     document.getElementById(_ratingValueID).value = rating;
      	     document.getElementById(_ratingSelectionId).value = rating;   
   	
   	}   	
	else
	{
	    alert("Rating Value out of the bound, Values can only be 1/2/3/4/5. Current rating value: " + rating);
	}
}

function callbackAddRating(result,rating)
{
    if(result.OperationSucceeded)
    {
        document.getElementById("stars.0").src  =  _geApplicationRoot + 'Images/rating'+ rating + '.0.png';
		document.getElementById("messages.0").src  =  _geApplicationRoot + 'Images/'+rating+'.png';
      	document.getElementById(_ratingValueID).value = rating;
      	document.getElementById(_ratingSelectionId).value = rating;
    }
    else
    {
        
        alert("There was an error while rating the recipe.Please try again..");
       
    }
}

function StarMouseOver(id, rating)
{
	if (_starTwinkler[id] != 0)
	{
	    window.clearTimeout(_starTwinkler[id]);
	    _starTwinkler[id] = 0;
	}
	if (_msgTwinkler[id] != 0)
	{
	    window.clearTimeout(_msgTwinkler[id]);
	    _msgTwinkler[id] = 0;
	}
  SwapStars(id, rating);
  SwapStarMsg(id, rating); 
}

function StarMouseOut(id)
{
	_starTwinkler[id] = window.setTimeout("SwapStars('"+id+"')", _delayTime);
	_msgTwinkler[id] = window.setTimeout("SwapStarMsg('"+id+"')",_delayTime);
}
function DisplayStarsAjax (id, rating)
{
	var starID = "stars." + id;
	_starTwinkler[id] = 0;
	_msgTwinkler[id] = 0;
	
	document.write("<map name='starmap" + id +"'>");
	var i = 0;
	for (i = 1; i < 6; i++) 
	{
	      document.write("<area shape=rect " + 
	        "coords='" + _starMap[i] + "' " +
	        "onMouseOver=\"StarMouseOver('" + id + "'," + i + ");\" " +
	        "onClick=\"SaveStarsAjax('" + id + "'," + i + ");" +
	        "\" >");
	}
	
	//"onMouseOut=\"StarMouseOut('" + id + "');\" " +	
	document.write("</map>");
	document.write("<img vspace=2 width='74' height='12' title = 'Rate Recipe' src='" + _starImages[rating] + "'");
	document.write(" border=0 usemap='#starmap" + id);
	document.write("' id='" + starID + "'>");
}

function DisplayMsgAjax (id, rating)
{
	var msgID = "messages." + id;
	
	if (rating == undefined ) 
	{
		document.write("<img vspace=2 width='63' height='11' src='" + _nullStarMessage + "'");
	}
	else 
	{
	    document.write("<img vspace=2 width='63' height='11' src='" +_starMessages[rating] + "'"); 
	}
	document.write("' id='" + msgID + "'");
}

//End rating system section

//Section to print the recipe
function openPrintRecipe()
{
    var recipeId = document.getElementById(_printrecipeid).value;
    var url = _geApplicationRoot + "/Recipes/PrintRecipe.aspx?recipeID="+recipeId ;
    var _contentwindow =  window.open(url,'printrecipe','location=no,toolbar=no,resizable=yes,menubar=no,status=yes,scrollbars=yes,width=650,height=650');    
}

function printRecipe()
{
    document.getElementById("printbutton").style.display = "none";
    window.print();
    window.close();
}

//End section to print the recipe

//Start review and personal notes section
function showPersonalNotes(containerId)
{
    showRecipeToolItem(containerId, _personalNoteTextId);
}

function hidePersonalNote(containerId)
{
    hideRecipeToolItem(containerId, _personalNoteTextId);
}

function showReview(containerId)
{
    showRecipeToolItem(containerId, _reviewTextId);
}

function hideReview(containerId)
{
    hideRecipeToolItem(containerId, _reviewTextId);
}


function showRecipeToolItem(containerId, textId)
{
    document.getElementById(containerId).style.display = 'inline';
    document.getElementById(textId).style.fontWeight = 'bold';
    document.getElementById(textId).style.color = '#627cc7';    
}

function hideRecipeToolItem(containerId, textId)
{
    document.getElementById(containerId).style.display='none';
    document.getElementById(textId).style.fontWeight = 'normal';
    document.getElementById(textId).style.color = '#97989D';    
}


function selectAddText(el)
{
    if (el.innerHTML == "Type your note here" || el.innerHTML == "Type your review here")
    {
        el.select();
    }
}

function UpdatePageSpecificLabel(sNewListName)
{
    var oListNameText = document.getElementById("listnametext");
    if(document.all)
    {
        oListNameText.innerText = sNewListName;
    }
    else
    {
        oListNameText.textContent = sNewListName;
    }    
}

//End review and personal notes section