// JScript source code for everything to do with handling properyt bookmarks

var asCurrentBookmarkItems = new Array();
var lBookmarkCounter = 0
var objUserCookie 

function ConfigureCookies()
	{

		//DebugCookieInfo();
		objUserCookie = ReadUserBookmarks('BookmarkedProperties');	
		//DebugOuputBookmarks();
	}

function ReadUserBookmarks(CookieName)
	{
		var bFoundCookieInfo = false
		
		if (document.cookie)
			{
				var asCookieInfo = document.cookie.split('; ');
				
				for (var iLoop=0; iLoop<asCookieInfo.length; iLoop++)
					{
						var asCrumbInfo = asCookieInfo[iLoop].split('=');
						if (asCrumbInfo[0] == CookieName)
							{
								if (asCrumbInfo[1])
									{
										var sAllValueString = unescape(asCrumbInfo[1]);
										var asAllValues = sAllValueString.split(',')

										for (var iAddItem = 0; iAddItem < asAllValues.length; iAddItem++)
											{
												asCurrentBookmarkItems[lBookmarkCounter] = asAllValues[iAddItem];
												lBookmarkCounter++;
												bFoundCookieInfo = true;

											}
									}
							}
					}
			}

		if (!bFoundCookieInfo) 
			{
				asCurrentBookmarkItems[0] = '';
				lBookmarkCounter = 0;
			}
		else
			{
				CheckBookmarkedProperties();
			}
			
	}

function CheckBookmarkedProperties()
	{
	
		for (var iCheckIt = 0; iCheckIt < asCurrentBookmarkItems.length; iCheckIt++)
			{
				if (document.getElementById('bmLink_' + asCurrentBookmarkItems[iCheckIt]))
					{
						document.getElementById('bmLink_' + asCurrentBookmarkItems[iCheckIt]).innerHTML = 'Drop this saved listing';
					}
			}
	}
function IsBookmarked(PropertyID)
	{
		var bRetVal = false;
		var lCheckLoop;
		
		// Look through our array to see if the item exists
		for (lCheckLoop = 0; lCheckLoop < lBookmarkCounter; lCheckLoop++)
			{
				if (parseInt(asCurrentBookmarkItems[lCheckLoop]) == parseInt(PropertyID))
					{
						bRetVal = true;
						break;
					}
			}
			
		return bRetVal;
	}

function GetBookmarkSubscript(PropertyID)
	{
		var lRetVal = -1;
		var lCheckLoop;
		
		// Look through our array to see if the item exists
		for (lCheckLoop = 0; lCheckLoop < lBookmarkCounter; lCheckLoop++)
			{
				if (parseInt(asCurrentBookmarkItems[lCheckLoop]) == parseInt(PropertyID))
					{
						lRetVal = lCheckLoop;
						break;
					}
			}
			
		return lRetVal;
	}
	
function SetBookmarkItem(PropertyID)
	{
	
		// Check to see if this item is already in the list
		if (!IsBookmarked(PropertyID))
			{
				// Does not already exist, so, tack it onto the end of the array
				asCurrentBookmarkItems[lBookmarkCounter] = PropertyID;
				lBookmarkCounter++;
				
				// Write the cookie away again to be safe
				WriteUserBookmarks();
			}
	}

function RemoveBookmarkItem(PropertyID)
	{
	
		// Does this item exist in our list of bookmarked items?
		if (IsBookmarked(PropertyID))
			{
				// Get the index of the item
				var lItemSubscript = GetBookmarkSubscript(PropertyID)
				
				// Blank this item out
				asCurrentBookmarkItems[lItemSubscript] = '';
				
				// Shuffle all of the following items up one subscript
				var iShuffle;
				
				for (iShuffle = lItemSubscript + 1; iShuffle < lBookmarkCounter; iShuffle++)
					{
						if (asCurrentBookmarkItems[iShuffle])
							{
								asCurrentBookmarkItems[iShuffle - 1] = asCurrentBookmarkItems[iShuffle];
							}
					}
					
				// Remove the final subscript
				asCurrentBookmarkItems.length--;
				lBookmarkCounter--;

				// Write away the new details
				WriteUserBookmarks();
			}
	}

function ChangeBookmarkState(PropertyID)
	{
	
		// MJS mod - 23Mar2005. Check for cookies enabled in the browser
		if (document.cookie)
			{
				if (!IsBookmarked(PropertyID))
					{
						SetBookmarkItem(PropertyID);
						document.getElementById('bmLink_'+PropertyID).innerHTML = 'Remove from shortlist';
					}
				else
					{
						RemoveBookmarkItem(PropertyID);
						document.getElementById('bmLink_'+PropertyID).innerHTML = 'Add to shortlist';
					}

				// DebugOuputBookmarks();
			}
		else
			{
				// No cookies enabled.
				var sCookieErrorText
				
				sCookieErrorText = 'Attention.\n\======\nThis function requires cookies to be enabled. We have been unable to confirm that cookies are enabled in your web browser.';
				sCookieErrorText += '\n\nPlease enable cookies before attempting to use this function.\n\nFor information on enabling cookies, please refer to your browser\'s help material or contact your system administrator.';
				sCookieErrorText += '\n\nWe apologise for any inconvenience this may cause.';
				
				alert(sCookieErrorText);
				
				// Undo the check/uncheck that we have just done
				objCheckbox.checked = !objCheckbox.checked;
			}
	}
	
function ChangeSingleBookmark(p_PropertyID)
{
		if (document.cookie)
			{
				if (!IsBookmarked(p_PropertyID))
					{
						SetBookmarkItem(p_PropertyID);
						document.getElementById('singleSave').innerHTML = 'Remove from shortlist';
					}
				else
					{
						RemoveBookmarkItem(p_PropertyID);
						document.getElementById('singleSave').innerHTML = 'Add to shortlist';
						
					}
				
				// DebugOuputBookmarks();
			}
		else
			{
				// No cookies enabled.
				var sCookieErrorText
				
				sCookieErrorText = 'Attention.\n\======\nThis function requires cookies to be enabled. We have been unable to confirm that cookies are enabled in your web browser.';
				sCookieErrorText += '\n\nPlease enable cookies before attempting to use this function.\n\nFor information on enabling cookies, please refer to your browser\'s help material or contact your system administrator.';
				sCookieErrorText += '\n\nWe apologise for any inconvenience this may cause.';
				
				alert(sCookieErrorText);
				
				// Undo the check/uncheck that we have just done
				objCheckbox.checked = !objCheckbox.checked;
			}
}
	
function WriteUserBookmarks()
	{
	
		var sBookmarkList
		
		if (lBookmarkCounter > 0)
			{
				sBookmarkList = asCurrentBookmarkItems.join(',');
			}
		else
			{
				sBookmarkList = ''; // No items in the list
			}
			
		var dCookieExpiry = new Date();
		
		dCookieExpiry.setTime(dCookieExpiry.getTime() + (1000*60*60*24*365)) // One year from today
		document.cookie = 'BookmarkedProperties=' + escape(sBookmarkList) + ';expires=' + dCookieExpiry.toGMTString() + ';path=/;';
	
		// DebugCookieInfo();	
	}
	
function DebugOuputBookmarks()
	{
	
		var iDebug = 0;
		var sMsgText = '';
		
		for (iDebug = 0; iDebug < asCurrentBookmarkItems.length; iDebug++)
			{
				sMsgText += iDebug + '=' + asCurrentBookmarkItems[iDebug] + '\n';
			}
		
		alert('Bookmark items:\n\n' + sMsgText);
		
	}	
	
function DebugCookieInfo()
	{
	
		var asCookieInfo = document.cookie.split(';');
		for (var iLoop=0; iLoop<asCookieInfo.length; iLoop++)
			{
				var asCrumbInfo = asCookieInfo[iLoop].split('=');
				alert(asCrumbInfo[0] + '=' + unescape(asCrumbInfo[1]));
			}
	}
