var arrRecords = new Array();

expireDate = new Date;
oldDate    = new Date;
expireDate.setDate(expireDate.getDate()+60);

function cookieVal(cookieName) 
{
	thisCookie = document.cookie.split("; ")
	for (i = 0; i < thisCookie.length; i++) {
		if (cookieName == thisCookie[i].split("=")[0]) {
			return thisCookie[i].split("=")[1];
		}
	}
	return null;
}

function loadCookie() 
{
	if(document.cookie != "") {
		retStr = cookieVal("Events") ;
		// alert ("retStr=" + retStr );
		if (retStr != null) {
			arrRecords = retStr.split(",");
		}
    }
}

/*
function setCookie (name, value, expires) {
if (!expires) expires = new Date();
document.cookie=name+"="+escape (value)+"; expires="+expires.toGMTString()+"; path=/";
}
*/

function addEvent(id) 
{
	var i = 0;
	var recCount = getNumEvents() ;
	var found = false;
	var rEvent;
	var newurl = document.location +'#Event'+id;

	// check it is not already there
    for(i = 0; i < recCount; i++) {
		rEvent = arrRecords[i];
		if (rEvent == id) {
			found=true;
			break;
		}
	}

	if (found==false)
	{
		lastitem = arrRecords.length;
		arrRecords[lastitem] = id;
		
		document.cookie = "Events="+arrRecords+";expires=" + expireDate.toGMTString()+"; path=/";
		// alert(newurl);
		// document.location.href=newurl;
		document.getElementById("Event"+id).innerHTML = "<span class='ev_in_shortlist'>Shortlisted</span>";
		// location.reload();
	}
	//else
	//{
	//	alert('This event has already been added to your schedule.');
	//}
}

function getNumEvents()
{
	if (arrRecords.length != "" ) 	{
		return arrRecords.length ;
	} else {
		return 0;
	}
}

function deleteEvent(id, rl) 
{
	var arrTemp = new Array();
	var i = 0;
	var j = 0;
	var recCount = getNumEvents() ;
	var rEvent = null;

	if (recCount == 1)
	{
		nullStr = "";
		document.cookie = "Events="+nullStr+";expires=" + oldDate.toGMTString()+"; path=/";
		//if (rl) {
			location.reload();
		//}
		return ;
	}

	// copy active array into temp array
	// skip the item to be deleted
    for (i = 0, j = 0; i < recCount; i++) {
		rEvent = arrRecords[i];
		if (rEvent != id) {
			arrTemp[j++] = arrRecords[i];
		} 
		//else 
		//{
		//	alert('found item to delete : ' + rEvent);
		//}
	}

    arrRecords = arrTemp ;
	document.cookie = "Events="+arrRecords+";expires=" + expireDate.toGMTString()+"; path=/";
	//if (rl) {
		location.reload();
	//}
}

function getEvent(index)
{
	if (arrRecords.length != "" && index < arrRecords.length) 	{
		return arrRecords[index] ;
	} else {
		return 0;
	}
}

function delEvents() {
	if (window.confirm('Sure you want to clear your entire Shortlist?')) {
		nullStr = "";
		document.cookie = "Events="+nullStr+";expires=" + oldDate.toGMTString()+"; path=/";
		location.reload();
	}
}

function ClearEvents() {
    document.write ('<a href="javascript:delEvents();">Clear All Talks from Shortlist</a>');
}			


loadCookie();
