var shortlist = new Array();

function isShortlisted (propid)
{
    for(var i = 0; i < shortlist.length; i++)
	{
	    if(shortlist[i] == propid )
		{
		    return (i+1);
		}
	}
    return 0;
}



function clearShortlist()
{
    shortlist = new Array();
    delCookie('shortlist');
    writeShortlist(0);
}

function writeShortlist(startIndex)
{

    createShortlistCookie();
   // ajax magic
     if ( window.ActiveXObject ) // if it's ie
    {
       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // xmlhttp is global
       xmlhttp.onreadystatechange = function ()
       {
          if(xmlhttp.readyState == 4) writeShorlistContent(xmlhttp.responseText);
       };
    }
    else
    {
       if (window.XMLHttpRequest) // if it's firefox etc.
       {
         var xmlhttp        = new XMLHttpRequest() // xmlhttp is global
         xmlhttp.onload = function () { writeShorlistContent(xmlhttp.responseText) };
       }
       else return; //
    }

     if(!startIndex)
     {
	     startIndex = 0;
     }



     if(Get_Cookie('shortlist_rental'))
     {
			var url = "http://" + document.location.hostname  + "/renting/write_shortlist.cgi?startindex=" + startIndex + "&uniq=" + new Date().getTime();
			xmlhttp.open("GET", url , true);
			xmlhttp.send(null);
			document.getElementById('shortlistContent').style.display = 'block';
     }
     else
     {
	     document.getElementById('shortlistContent').style.display = 'none';
     }

}

function writeShorlistContent(html)
{
  var targ = document.getElementById('shortlistContent');
  if(targ) { targ.innerHTML = html; }
}

function storeToShortList(propid)
{
    // we can only store a max of six
    if(shortlist && shortlist.length >= 6)
	{
	    alert('Sorry the maximum you can shortlist is 6.');
	}
    else //add to the end of the list
	{
	    // if we have this already stored alert
	    for(var i = 0; i < shortlist.length; i++)
		{
		    if(shortlist[i] == propid)
			{
			    alert("You have already stored this property.");
			    return;
			}
		}
	    shortlist[shortlist.length]  = propid;
	}
    // lets call ajax now to write the html and leave it at this index so they can see it has been added
    var index = 0;
    if(shortlist.length > 3 ) { index = 3; }
    writeShortlist(index);
}

function createShorlistArrayFromCookie()
{

  shortlist = new Array();
  var cookiestring = Get_Cookie('shortlist_rental');
  if(cookiestring)
  {
	  var separatedValues = cookiestring.split("/");
	  for( var i = 0; i < separatedValues.length; i++)
	  {
		var tmp = separatedValues[i].split("-");
		shortlist[tmp[0]] = tmp[1];
	  }
  }
}



// let stick the shortlist in a cookie so it hangs around for a while
function createShortlistCookie()
 {
  var ExpireDate = new Date ();
  var tmp = new Date();
  ExpireDate.setTime(ExpireDate.getTime() + 100000000);
  var valuestring = '';
  if(shortlist)
      {
	  for(var i = 0; i < shortlist.length; i++)
	      {
		  valuestring = valuestring + '/' + i + '-' + shortlist[i];
	      }
	  var curCookie = 'shortlist_rental=' + valuestring + "; expires=" + ExpireDate.toGMTString() + "; path=/";
	  document.cookie = curCookie;
      }
 }

function removeFromShortlist(startIndex, propid)
{
  var tmplist = new Array();
  var count = 0;
  for(var i = 0; i < shortlist.length; i++)
  {
    if(shortlist[i] != propid)
	{
	  tmplist[count] = shortlist[i];
	  count++;
	}
  }
  shortlist = new Array();
  for(var i = 0; i < tmplist.length; i++)
      {
	  shortlist[i] = tmplist[i];
      }
  writeShortlist(startIndex);
}

function onloadfuncs()
{
    createShorlistArrayFromCookie();
    writeShortlist(0);
}

addEvent( window, 'load', onloadfuncs );

