﻿var clearMsgTimer = 0;

function CloseBannerMsg()
{
	var msgDiv = top.NavBannerRight.document.getElementById('miscmsgs');
	 if(msgDiv != null)
	 {	
			msgDiv.style.display = 'none'
	 }
}
function ClearMsgBlock()
{ //clears the msg div in the banner
	with(top.NavBannerRight.document)	
				{
					getElementById('miscmsgs').innerHTML = '';	
					getElementById('msgtoshow').innerHTML = '';
					getElementById('msgsusername').innerHTML = '';
				}				
}

function HandleFavoritesforSearch(obj,femaleAccount,isFavorite,isAdmirer,isMatch,userName)
{
	try
	{	
		//determines if fav, admirer or match and show appropriate msg
		//depending on	 adding or removing
		otherFunction = true; //set this to true so the profile does not get opened
		var userID = GetCookieValue('ASPSessionID'); //make sure they are logged in (in getdata.js)
		clearTimeout(clearMsgTimer);	 //cnx any timer that is set to close
		try
		{
			eval("top.NavMain.clearTimeout(clearMsgTimer)");
		}
		catch(e){}	
		CloseBannerMsg();	 //go agead and close any msg that is open
		if(userID != null)
		{	//logged in?
			var msgDiv = top.NavBannerRight.document.getElementById('miscmsgs'); //get div to hold msg
			if(msgDiv != null)
			{
				var msg = '';
				var newSrc = '';	
				var newTitle = '';	
				var favsAction	 = 0;  //see enums (enums.vb)
				var currentIcon = new String(obj.src); //get the current icon

				if(isAdmirer == 0 && isMatch == 0)
				{// if nothing or in favorites...	
					if(currentIcon.indexOf('favadd') > -1)
					{		//they added, chage icons and msg to remove
						newSrc = '../images/i_favdelete.jpg';
						newTitle = 'Remove ' + userName + ' from your favorites';			
						msg = ' has been added to your <a href="../blackbook/default.asp?searchtype=favorites&toid=' + femaleAccount + '" target="_top" id="bbHref">Favorites</a>!';
						favsAction = 1;	
					}
					else	
					{	//they removed, set it back to add
						newSrc = '../images/i_favadd.jpg';
						newTitle = 'Add ' + userName + ' to your favorites';			
						msg = ' has been removed from your <a href="../blackbook/default.asp?searchtype=favorites&toid=' + femaleAccount + '" target="_top" id="bbHref">Favorites</a>!';
						favsAction = 2;	
					}		
				}	
				
				if(isAdmirer == 1)
				{ //they are an admirer
					if(currentIcon.indexOf('matchadd') > -1)
					{		//add to matches - change to remove from
						newSrc = '../images/i_matchdelete.jpg';
						newTitle = 'Remove ' + userName + ' from your matches';			
						msg = ' has been added to your <a href="../blackbook/default.asp?searchtype=matches&toid=' + femaleAccount + '" target="_top" id="bbHref">Matches</a>!';
						favsAction = 3;
					}
					if(currentIcon.indexOf('matchdelete') > -1)
					{ //remove from matches - set to add
						var ask = confirm('By removing ' + userName + ' from your matches, \nthis action will also remove her from your favorites.\n\nWould you like to Proceed?');
						if (ask)	
						{		//if they remove someone from matches, they get removed from favs too - confirm it
							newSrc = '../images/i_favadd.jpg';
							newTitle = 'Add ' + userName + ' to your matches';			
							msg = ' has been removed from your Matches.';
							favsAction = 4;
						}	
					}			
				}	
				
				if(newSrc != '' && msg != '')
				{
					SaveFavorite(femaleAccount,favsAction);	//xmlhttp - save the action (add or remove)
					obj.src = newSrc; //change the icon
					obj.title = newTitle;		 //change the title
					ClearMsgBlock	(); //clear current message (caching issue?)
					with(top.NavBannerRight.document)	
					{ //show the msg in the banner
						getElementById('msgthumb').src = '../lb_photos/' + femaleAccount + '/' + femaleAccount + '_tn.jpg'; //insert thumb
						getElementById('msgtoshow').innerHTML = msg; //insert msg
						getElementById('msgsusername').innerHTML = userName; //insert usrname
						var favsTable = getElementById('msgstable').innerHTML;	 //grab new msg to show
					}				
					msgDiv.innerHTML = 	favsTable; //insert new msg into div to show
					
					msgDiv.style.display = 'block'
					t = 10000;
					//make it automatically disapear	 after t msecs
					clearMsgTimer = setTimeout("CloseBannerMsg()", t);
				
					//try to change the gif on the profile card	
					var navMainLocal = '';
					try
					{
						navMainLocal = new String(top.NavMain.document.location)
					}
					catch(e){}

					if(navMainLocal.indexOf('bbemail') > -1)
					{
						try
						{	
							navMainLocal = new String(top.NavMain.right_sub.document.location);
							navMainLocal = navMainLocal.toLowerCase();
						}
						catch(e){}								
					}

					if(navMainLocal != null && navMainLocal != '')
					{
						var imageID = 'fav' + femaleAccount; //create element name
						var imageItem = '';
					 try
						{											
							if(navMainLocal.indexOf('profile_detail.asp') > -1 && navMainLocal.indexOf(femaleAccount) > -1)
							{							
								imageItem = 	top.NavMain.document.getElementById(imageID); //get element					
							}
							if((navMainLocal.indexOf('profile.aspx') > -1 || navMainLocal.indexOf('featuredlady.aspx') > -1) && navMainLocal.indexOf(femaleAccount) > -1)
							{							
								imageItem = 	top.NavMain.document.getElementById('ucProfileBar_uxFavorites'); //get element					
							}
							else if (navMainLocal.indexOf('email/emailmsg.aspx') > -1 && top.NavMain.right_sub.document.getElementById('toid').value == femaleAccount)
							{
							if(top.NavMain.right_sub.document.getElementById('ucProfileBar_uxFavorites') != null)	
								{
									imageItem = 	top.NavMain.right_sub.document.getElementById('ucProfileBar_uxFavorites');	
								}
							}
						}catch(e){}
			
						if(imageItem != null && imageItem != '')
						{
							var newProfileDetailImage = ''	
							//get the current imagename
							var profileImage = 	new String(imageItem.src);
							profileImage = profileImage.toLowerCase();
							
							if(profileImage.indexOf('removefavorite') > -1)
							{//was a favorite
								newProfileDetailImage = profileImage.replace('remove','');								
							}
							if(profileImage.indexOf('b_fav') > -1)
							{//was not a favorite
								newProfileDetailImage = profileImage.replace('b_fav','b_removefav');
							}
							if(profileImage.indexOf('rem_admirers') > -1)
							{//was an admirer
								newProfileDetailImage = profileImage.replace('rem_admirers','rem_matches');
							}
							if(profileImage.indexOf('rem_matches') > -1)
							{//was a match
								//newProfileDetailImage = profileImage.replace('rem_matches','b_favorites');
								newProfileDetailImage = profileImage.replace('rem_matches','rem_admirers');
								newTitle = newTitle.replace('Add','Remove');	
								newTitle = newTitle.replace('to','trom');	
								newTitle = newTitle.replace('matches','admirers');		
							}							
							// set new image and title
							imageItem.src = 	newProfileDetailImage;
							imageItem.title = newTitle;						
						}
					}
				}				
			}
		}
		else
		{
			ShowLoginPrompt(); //not logged in
		}
	}
	catch(e){}	
}

function HandleFavoritesforOther(obj,femaleAccount,userName)
{
	try
	{
		//determines if fav, admirer or match and show appropriate msg
		//depending on	 adding or removing		
		var userID = GetCookieValue('ASPSessionID'); //make sure they are logged in (in getdata.js)
		var currentLocation = new String(top.location);
		currentLocation = currentLocation.toLowerCase();
		try
		{
			if(currentLocation.indexOf('email') > -1)
			{
				eval("top.MailMain.clearTimeout(clearMsgTimer)");
			}
			else
			{
				eval("top.NavContent.clearTimeout(clearMsgTimer)"); //cnx any timer that is set to close
			}			
		}
		catch(e){}
		clearTimeout(clearMsgTimer)		
		CloseBannerMsg();	 //go agead and close any msg that is open
		if(userID != null)
		{	//logged in?
			var msgDiv = top.NavBannerRight.document.getElementById('miscmsgs'); //get div to hold msg
			if(msgDiv != null)
			{
				var msg = '';
				var newIconSrc = '';	
				var newButtonSrc = '';		
				var newTitle = '';	
				var favsAction	 = 0;  //see enums (enums.vb)
				//var currentIcon = new String(obj.src); //get the current icon
				var currentButton = new String(obj.src);
				currentButton = currentButton.toLowerCase();
				
				if(currentButton.indexOf('b_favorites') > -1)
				{
					var curTitle = 	new String(obj.title);
					if(curTitle.indexOf('matches') > -1)
					{	
						newIconSrc = '../images/i_matchdelete.jpg';
						newButtonSrc = '../images/rem_matches.gif';	
						newTitle = 'Remove ' + userName + ' from your matches';			
						msg = ' has been added to your <a href="../blackbook/default.asp?searchtype=matches&toid=' + femaleAccount + '" target="_top" id="bbHref">Matches</a>!';
						favsAction = 1;		
					}
					else
					{
						newIconSrc = '../images/i_favdelete.jpg';
						newButtonSrc = '../images/b_removefavorites.gif';	
						newTitle = 'Remove ' + userName + ' from your favorites';			
						msg = ' has been added to your <a href="../blackbook/default.asp?searchtype=favorites&toid=' + femaleAccount + '" target="_top" id="bbHref">Favorites</a>!';
						favsAction = 1;		
					}	
				}		
				else if(currentButton.indexOf('b_removefavorites') > -1)
				{				
					newIconSrc = '../images/i_favadd.jpg';
					newButtonSrc = '../images/b_favorites.gif';		
					newTitle = 'Add ' + userName + ' to your favorites';			
					msg = ' has been removed from your <a href="../blackbook/default.asp?searchtype=favorites&toid=' + femaleAccount + '" target="_top" id="bbHref">Favorites</a>!';
					favsAction = 2;
				}
				else if(currentButton.indexOf('rem_admirers') > -1)
				{
					newIconSrc = '../images/i_favadd.jpg';
					newButtonSrc = '../images/b_favorites.gif';		
					newTitle = 'Add ' + userName + ' to your matches';
					msg = ' has been removed from your <a href="../blackbook/default.asp?searchtype=admirers&toid=' + femaleAccount + '" target="_top" id="bbHref">Admirers</a>!';
					favsAction = 5;
				}	
				else if(currentButton.indexOf('rem_matches') > -1)
				{
					var ask = confirm('By removing ' + userName + ' from your matches, \nthis action will also remove her from your favorites.\n\nWould you like to Proceed?');
					if (ask)	
					{		//if they remove someone from matches, they get removed from favs too - confirm it
						newIconSrc = '../images/i_favadd.jpg';
						newButtonSrc = '../images/rem_admirers.gif';	
						newTitle = 'Remove ' + userName + ' from your admirers';
						msg = ' has been removed from your Matches.';
						favsAction = 4;
					}
					else
					{return}
				}
			
				obj.title = newTitle;		 //change the title
					
				if(newButtonSrc != '' && msg != '')
				{
					SaveFavorite(femaleAccount,favsAction);	//xmlhttp - save the action (add or remove)
					obj.src = newButtonSrc; //change the icon
					
					var searchFavIconID = GetFavIDFromSearch(femaleAccount);
					
					if(searchFavIconID != null)
					{
						try
						{
						
							if(newButtonSrc == '../images/rem_admirers.gif')
							{
								newTitle = 'Add ' + userName + ' to your matches';
							}
							
							with(top.NavContent.document)
							{
								getElementById(searchFavIconID).src = newIconSrc;
								getElementById(searchFavIconID).title = newTitle;									
							}
						}
						catch(e){}	
					}					
					
					ClearMsgBlock	(); //clear current message (caching issue?)
					
					with(top.NavBannerRight.document)	
					{ //show the msg in the banner
						getElementById('msgthumb').src = '../lb_photos/' + femaleAccount + '/' + femaleAccount + '_tn.jpg'; //insert thumb
						getElementById('msgtoshow').innerHTML = msg; //insert msg
						getElementById('msgsusername').innerHTML = userName; //insert usrname
						var favsTable = getElementById('msgstable').innerHTML;	 //grab new msg to show
					}				
					msgDiv.innerHTML = 	favsTable; //insert new msg into div to show
					
					msgDiv.style.display = 'block'
					t = 10000;
					//make it automatically disapear	 after t msecs
					clearMsgTimer = setTimeout("CloseBannerMsg()", t);
				}				
			}
		}		
	}
	catch(e){}	
}

function GetFavIDFromSearch(favToFind)
{	
	try
	{	
		favIDToFind = 'Favorite' + favToFind;
		var currentLocation = new String(top.location);
		currentLocation = currentLocation.toLowerCase();
		if(currentLocation.indexOf('email') == -1)
		{			
			if(top.NavContent.document.getElementsByTagName)
			{
				var images = top.NavContent.document.getElementsByTagName('IMG');
				for(var i=0; i<images.length; i++)
				{
					var favImgID = 	new String(images[i].id);
					if(favImgID.indexOf(favIDToFind) > -1)
					{			
						return favImgID;
					}	
				}
			}	
		}
	}
	catch(e){}	
}

function SaveFavorite(femaleAccount,favsAction)
{ //save the favorite action to db
	try
 {  
  var url = '../favorites/Favorites_Manage.aspx?femaleaccount=' + femaleAccount + '&action=' + favsAction+ '&id=' + Math.random();
  if(window.XMLHttpRequest)
  {
   xmlhttpFavs = new XMLHttpRequest();
   with(xmlhttpFavs)
   {  
    onreadystatechange = Finito;
    open("GET",url,true);
    send(null);
   } 
  }
  else if (window.ActiveXObject)
  {
   xmlhttpFavs = new ActiveXObject("Microsoft.XMLHTTP");
   if (xmlhttpFavs)
   {
    with(xmlhttpFavs)
    {  
     onreadystatechange = Finito;
     open("GET",url,true);
     send();
    } 
   }
  }
 }
 catch(e)
 {
 } 
}

function Finito()
{
 try
 { 
  if (xmlhttpFavs.readyState == 4)
  {
   if (xmlhttpFavs.status == 200) 
   {//nothing to do , firefox requires this tho
   }
  }
 }
 catch(e)
 {
 }
}
