﻿function ListingSaver() {

	var SavedListings = new Array();

	this.Load = function(cookie)
	{		
	    var savedListings = cookie;
	    if(!savedListings || savedListings == '')
	        return;
	    if(savedListings.indexOf('&n=')!=-1) savedListings = this.reformat(savedListings);
		SavedListings = savedListings.split('|');
	};
	
	this.reformat = function(cookie)
	{
	    var oldItems = cookie.split('|');
        var newItems = new Array();
        for(i=0;i<oldItems.length;i++)
        {
           newItems[i] = oldItems[i].split('&')[0].split('=')[1];
        }
        return newItems.join('|');
    }

	this.Add = function(listingId)
	{
		for(var i=0;i<SavedListings.length;i++)
		{
			if(SavedListings[i] == listingId)
			{
			    return;
			}
		}
		SavedListings.push(listingId);
		this.Save();
	};

	this.Delete = function(listingId)
	{
	    
	    for(var i=0;i<SavedListings.length;i++)
		{
			if(SavedListings[i] == listingId)
			{
			    SavedListings.splice(i,1);
			    break;
			}
		}
		this.Save();
	};

	this.Save = function()
	{
		setCookie('SavedListings', SavedListings.join('|'), new Date(9999, 12, 31), '/');
	};
	
	this.ToQueryString = function()
	{
	    var n = '';
	    for(var i=0;i<SavedListings.length;i++)
		{
		     n += '&ListingID=' + SavedListings[i];
		}
		return n;
	}
}