function checkCookie(propId) {
  wishlist=getCookie('wishlist');
  if (wishlist!=null && wishlist!=""){
    addProperty(propId, 'wishlist');
  } else {
    createCookie('wishlist',propId,365);
    alert ('The property ' + propId + ' has been added into your wishlist');
    document.location.reload();
  }
}

function removeProperty (propId) {

  var name = 'wishlist';
  
  var remstart = document.cookie.indexOf(name);
  remstart = remstart + name.length + 1;
  var remend = document.cookie.indexOf(';', remstart);
  if (remend = -1) {
    remend = document.cookie.length;
  }
  var remstr = document.cookie.substring(remstart, remend);

  var pos1 = remstr.indexOf(propId); // -1 is to delete the '.' which is a delimiter.
  var pos2 = pos1+6;
  var str1 = remstr.substring (0, pos1);
  var str2 = remstr.substring (pos2);
  var newremstr = str1 + str2;
  createCookie ('wishlist', str1+str2, 365);
  alert ('The property ' + propId + ' has been removed from your wishlist');
  document.location.reload();

  return false;
}

function addProperty (propId, name) {
  var wishstart = document.cookie.indexOf(name);
  wishstart = wishstart + name.length + 1;
  var wishend = document.cookie.indexOf(';', wishstart);
  if (wishend == -1) { wishend = document.cookie.length;}
  var wishstr = document.cookie.substring(wishstart, wishend);
  newwishstr = wishstr + '.' + propId;
  
  if(document.cookie.indexOf(propId) ==-1) {
    createCookie (name, newwishstr, 365);
    alert ('The property ' + propId + ' has been added into your wishlist');
  } else {  
    alert ('The property ' + propId + ' already exists in your wishlist');
  }
  document.location.reload();
  
  return false;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name + "=" + escape(value) + ';' + expires + "; path=/";  
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(',');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",365);
  document.location.reload();
  
  return false;
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1 
      c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return unescape(document.cookie.substring(c_start,c_end))
    }
  }
  return "";
}




