// basic get and set functions for javascript cookies
function setCookie (cookieName, cookieValue, expires, path, domain, secure) {
document.cookie = 
escape(cookieName) + '=' + escape(cookieValue) 
+ (expires ? '; EXPIRES=' + expires.toGMTString() : '')
+ (path ? '; PATH=' + path : '')
+ (domain ? '; DOMAIN=' + domain : '')
+ (secure ? '; SECURE' : '');
}

function getCookie (cookieName) {
var cookieValue = null;
var posName = document.cookie.indexOf(escape(cookieName) + '=');
if (posName != -1) {
var posValue = posName + (escape(cookieName) + '=').length;
var endPos = document.cookie.indexOf(';', posValue);
if (endPos != -1) {
cookieValue = unescape(document.cookie.substring(posValue, endPos));
} else {
cookieValue = unescape(document.cookie.substring(posValue));
}
}
return cookieValue;
}

// custom code to store and remove links from the cookies

function storePage(){
thisPage = document.location.href;
var pageList = getCookie("pageList");
// check to see whether this url is already in the cookie's links list
var linkFound = false;
if (pageList != "" && pageList != null) {
pSplit = pageList.split(";");
for (a=0;a<pSplit.length;a++) {
if (pSplit[a] == thisPage) linkFound = true;
}
}

// if url not already in cookie, then add it here
if (!linkFound) {
if (pageList == null || pageList == ";") {
pageList = thisPage
} else {
pageList += ";" + thisPage;
}
}
// make the cookie expire in 1 years time:
var now = new Date();
var nextYear = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365);
setCookie("pageList",pageList,nextYear);

//refresh screen after link has been added

confirm('Are you sure you want to add this city to your Favorites List?');

window.location.reload();
}

function removePage(url){
if (confirm('Are you sure to delete this City?')) {
	var pageList = getCookie("pageList");
	var linkList = ""
	// add each link to the linksList string and skip the one you want to remove
	if (pageList != "" && pageList != null) {
	pSplit = pageList.split(";");
	for (a=0;a<pSplit.length;a++) {
	if (pSplit[a] != url && pSplit[a] != '') linkList += ";" + pSplit[a];
	}
	}

	// get the cookie expiry date
	var now = new Date();
	var nextYear = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 365);
	setCookie("pageList",linkList,nextYear);
	// refresh screen after link has been removed
	window.location.reload();
	}
}

// code to write the list of urls to the page, and extra link to remove the url from the list.
function writeLinks(){
var pageList = getCookie("pageList")
if (pageList != "" && pageList != null) {
pSplit = pageList.split(";")
for (a=0;a<pSplit.length;a++) {
if (pSplit[a] != '' && pSplit[a] != 'null') {

s = new String('<li><a href="' + pSplit[a] + '"><img src="images/bookmark_blue.png" border="0" class="bookmark">');
s = s.replace(/#/g,"");
s = s.replace(/-/g,"");
document.write(s);


sText = new String('' + pSplit[a] + '');
sText = sText.replace(/\//g,"");
sText = sText.replace(/\./g,"");
sText = sText.replace(/\:/g,"");
sText = sText.replace(/\#/g,"");
sText = sText.replace(/www/g,"");
sText = sText.replace(/httpiweathrcom/g,"");
sText = sText.replace(/php/g,"");
sText = sText.replace(/_/g," ");
sText = sText.replace(/cities/g,"");
sText = sText.replace(/-/g,"");

document.write(sText);

document.write("</a>");

sLink = new String('<a class="del_btn" href="#"  onClick="javascript:removePage(\'' + pSplit[a] + '\')" target="_self"></a></li>');

document.write(sLink);


}
}
} else {
document.write("<li>Favorites List is Empty</li>");
}
}

