var kWishListCookieName = "FLACARWish";
var kWishList = null;
var kWishListDictionary = null;

var expDays = 180;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function ListToDoItems() {
	// Clean-up old cookies:
	/*var vMaxLoop = GetCookie('PT_NumToDoList');
	if(vMaxLoop != null) {
		for(var vLoop = 1; vLoop <= vMaxLoop; vLoop++) {
			DeleteCookie('PT_ToDoItem'+vLoop);
		}
		DeleteCookie('PT_NumToDoList');
	}*/
	
	loadCookies();
	paintCookies();
}
function DeleteItem(pEventID) {
	if((kWishListDictionary != null) && (kWishListDictionary[pEventID] != null)) {
		// Remove from arrays
		kWishList.splice(kWishListDictionary[pEventID], 1);
		eventSort();
		
		// Save cookies
		saveCookies();
		
		// Refresh display
		paintCookies();
	}
}

function loadCookies() {
	kWishListDictionary = new Array();
	kWishList = new Array();
	
	var vCookie = GetCookie(kWishListCookieName);
	
	if(vCookie != null) {
		var vEvents = vCookie.match(/.*?[^\\]\|/g);
		if(vEvents != null) {
			for(var vLoop = 0 ; vLoop < vEvents.length; vLoop++) {
				var vEvent = new Event("", "", "", "", "");
				if(vEvent.readCookieValue(vEvents[vLoop].replace(/\|$/, "").replace(/\\\|/g, "|"))) {
					var vIndex = kWishList.length;
					kWishListDictionary[vEvent.identifier] = vIndex;
					kWishList[vIndex] = vEvent;
				}
			}
			eventSort();
		}
	}
}
function saveCookies() {
	if(kWishList != null) {
		var vCookie = "";
		for(var vLoop = 0; vLoop < kWishList.length; vLoop++) {
			vCookie += kWishList[vLoop].cookieValue().replace(/([^\\])\|/g, "$1\\|") + "|";
		}
		
		SetCookie(kWishListCookieName, vCookie, exp);
	}
}
function paintCookies() {
	var vListDiv = document.getElementById("wish_list");
	if(vListDiv != null) {
		
		emptyNode(vListDiv);
		
		if((kWishList == null) || (kWishList.length <= 0)) return;
		
		var vListTable = document.createElement("table");
		var vList = document.createElement("tbody");
		
		var vRow = document.createElement("tr");
		
		var vCell = document.createElement("th");
		vCell.className = "date_pair";
		vCell.appendChild(document.createTextNode("Date"));
		vRow.appendChild(vCell);
		
		vCell = document.createElement("th");
		vCell.className = "name_pair";
		vCell.appendChild(document.createTextNode("Event"));
		vRow.appendChild(vCell);
		
		vCell = document.createElement("th");
		vCell.className = "location_pair";
		vCell.appendChild(document.createTextNode("City"));
		vRow.appendChild(vCell);
		
		vCell = document.createElement("th");
		vCell.className = "button_pair";
		vCell.appendChild(document.createTextNode("-"));
		vRow.appendChild(vCell);
		
		vList.appendChild(vRow);
		for(var vLoop = 0; vLoop < kWishList.length; vLoop++) {
			
			vRow = document.createElement("tr");
			if((vLoop % 2) != 0) vRow.className = "pair";
			
			vCell = document.createElement("td");
			vCell.className = "date" + (((vLoop % 2) != 0) ? "_pair" : "");
			vCell.appendChild(document.createTextNode(kWishList[vLoop].date));
			vRow.appendChild(vCell);
			
			vCell = document.createElement("td");
			vCell.className = "name" + (((vLoop % 2) != 0) ? "_pair" : "");
			vCell.appendChild(document.createTextNode(kWishList[vLoop].name));
			vRow.appendChild(vCell);
			
			vCell = document.createElement("td");
			vCell.className = "location" + (((vLoop % 2) != 0) ? "_pair" : "");
			vCell.appendChild(document.createTextNode(kWishList[vLoop].location));
			vRow.appendChild(vCell);
			
			vCell = document.createElement("td");
			vCell.className = "button" + (((vLoop % 2) != 0) ? "_pair" : "");
			var vLink = document.createElement("a");
			vLink.href = "javascript:DeleteItem(\"" + kWishList[vLoop].identifier + "\")";
			vLink.appendChild(document.createTextNode("Delete"));
			vCell.appendChild(vLink);
			vRow.appendChild(vCell);
			
			vList.appendChild(vRow);
			
		}
		
		vListTable.appendChild(vList);
		vListDiv.appendChild(vListTable);
	}
}
function eventObjectSorting(pEvent1, pEvent2) {
	if(pEvent1.date < pEvent2.date) return -1;
	if(pEvent1.date == pEvent2.date) return 0;
	return 1;
}
function eventSort() {
	kWishList.sort(eventObjectSorting);
	kWishListDictionary = new Array();
	
	for(var vLoop = 0; vLoop < kWishList.length; vLoop++) {
		kWishListDictionary[kWishList[vLoop].identifier] = vLoop;
	}
}

function addToEventList(pID, pDateString, pEvent, pLocation) {
	if(kWishList == null) loadCookies();
	
	if(kWishListDictionary[pID] == null) {
		var vIndex = kWishList.length;
		
		kWishList[vIndex] = new Event(pID, pDateString, pEvent, pLocation);
		kWishListDictionary[pID] = vIndex;
		
		eventSort();
		
		saveCookies();
		
		alert("The '" + pEvent +"' has been added to your events.");
	} else {
		alert("The '" + pEvent +"' is already in your events.");
	}
	
	return false;
}

function Event(pID, pDateString, pName, pLocation) {
	this.identifier = pID;
	this.name = pName;
	this.date = pDateString;
	this.location = pLocation;
	
	this.encodeCookieValue = function (pValue) {
		return pValue.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
	};
	this.decodeCookieValue = function (pValue) {
		return pValue.replace(/\\\[/g, "[").replace(/\\\]/g, "]");
	};
	this.cookieValue = function () {
		return "[" + this.encodeCookieValue(this.identifier) + "][" + this.encodeCookieValue(this.name) + "][" + this.encodeCookieValue(this.date) + "][" + this.encodeCookieValue(this.location) + "]";
	};
	this.readCookieValue = function (pCookie) {
		if((pCookie == null) || (pCookie.length <= 2)) return false;
		
		var vInfo = pCookie.match(/[^\\]?\[.*?[^\\]\]/g);
		if(vInfo == null) return false;
		
		if(vInfo.length > 0) {
			this.identifier = this.decodeCookieValue(vInfo[0].replace(/\[(.*)\]/, "$1"));
			if(vInfo.length > 1) {
				this.name = this.decodeCookieValue(vInfo[1].replace(/\[(.*)\]/, "$1"));
				if(vInfo.length > 2) {
					this.date = this.decodeCookieValue(vInfo[2].replace(/\[(.*)\]/, "$1"));
					if(vInfo.length > 3) {
						this.location = this.decodeCookieValue(vInfo[3].replace(/\[(.*)\]/, "$1"));
					}
				}
			}
		}
		
		return true;
	};
}


function emptyNode(pNode) {
	if(pNode == null) return;
	
	for(var vChild = (pNode.childNodes.length - 1); vChild >= 0; vChild--) {
		if((pNode.childNodes[vChild].nodeType) == 1 || (pNode.childNodes[vChild].nodeType == 3))
			pNode.removeChild(pNode.childNodes[vChild]);
	}
}
function set(){
VisitorName = prompt("Who are you?");
SetCookie ('VisitorName', VisitorName, exp);
SetCookie ('WWHCount', 0, exp);
SetCookie ('WWhenH', 0, exp);
}
function getCookieVal (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 getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}
function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}