
function getExpiryDate(days, hours, minutes) {

  var expDate = new Date();

  if(typeof days == "number" && typeof hours == "number" && typeof minutes == "number") {
    expDate.setDate(expDate.getDate() + parseInt(days));
    expDate.setHours(expDate.getHours() + parseInt(hours));
    expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
    return expDate.toGMTString();
  }
}


function setCookie(name, value, expires, path, domain, secure) {

  document.cookie = name + "=" + escape(value) +
    ((expires)? "; expires=" + expires : "") + 
    ((path)? "; path=" + path : "") + 
    ((domain)? "; domain=" + domain : "") + 
    ((secure)? "; secure" : "");
}


function getCookieValue(offset) {

  var endstr = document.cookie.indexOf(";", offset);
  if(endstr == -1) {
    endstr = document.cookie.length;
  }
  return unescape(document.cookie.substring(offset, endstr));
}


function getCookie(name) {

  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i=0;

  while(i < clen) {
    var j = i + alen;
    if(document.cookie.substring(i, j) == arg) {
      return getCookieValue(j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if(i == 0) break;
  }
  
  return "";
}


function deleteCookie(name, path, domain) {
  if(getCookie(name)) {
    document.cookie = name + "=" + 
      ((path)? "; path=" + path : "") + 
      ((domain)? "; domain=" + domain : "") + 
      "; expires=Thu, 01-jan-70 00:00:01 GMT";
  }
}


/* My Favourites functions */

var cookieName = "clgFavourites";

function setFavourite() {
	
	var cookieValue = getCookie(cookieName);
	if(cookieValue.indexOf(this.location.pathname) < 0) {
		cookieValue += "@url@" + this.location.pathname;
		cookieValue += "@title@" + getShortTitle(document.title);
		setCookie(cookieName, cookieValue, getExpiryDate(365, 0, 0), "/");
	}
	
}


function deleteFavourite(index) {
	
	var cookieValue = getCookie(cookieName);
	var tempCookieValue = "";
	var valueArr = cookieValue.split("@url@");

	for(var i=1 ; i< valueArr.length; i++) 
	{
	  if(i != index) 
	  {
	  	tempCookieValue += "@url@" + valueArr[i];
	  }
	}
	setCookie(cookieName, tempCookieValue, getExpiryDate(365, 0, 0), "/");
}


function createFavouritesList(policySite) {

	var cookieValue = getCookie(cookieName);
	var valueArr = cookieValue.split("@url@");
  var temp = "<p>You currently have no favourites saved.</p>";
	
	if(valueArr.length > 0 && cookieValue.length > 0) {
		temp = "<ul>";
		var tempSplit;
		
		for(var i=1; i<valueArr.length; i++) {
			tempSplit = valueArr[i].split("@title@");
			temp += "<li><input type=\"image\" src=\"http://collection.europarchive.org/tna/20090106142604/http://communities.gov.uk/static/images/buttons/delete_"  + policySite + ".gif\" ";
			temp += "alt=\"Delete this item from the list\" style=\"float:right;\" onclick=\"deleteFavourite(" + i + ")\" />"
			temp += "<a href=\"" + tempSplit[0] + "\">" + tempSplit[1] + "</a></li>"
		}
		temp += "</ul>";
	}
	
	return temp;

}


function createFavouritesAddForm(policySite) {
  
	var cookieValue = getCookie(cookieName);
	var temp = "";
	if(cookieValue.indexOf(this.location.pathname) < 0) {
	  var temp = "<form action=\"#frmFavourites\" method=\"post\" id=\"frmSubmitFavourites\" onsubmit=\"setFavourite()\"><p style=\"text-align:right;\">" +
	    "<input type=\"image\" src=\"http://collection.europarchive.org/tna/20090106142604/http://communities.gov.uk/static/images/buttons/add_page_"  + policySite + ".gif\" alt=\"Add this page to the My Favourites list\" /></p></form>";
	}
	
	return temp;

}


function getShortTitle(longTitle)
{
	var shortTitle = longTitle;
	var ipos = longTitle.indexOf(" - ");
	if(ipos > 0)
	{
		shortTitle = shortTitle.substring(0, ipos);	
	}
	return shortTitle;
}


