var arrRecords = new Array();

expireDate = new Date;
oldDate    = new Date;
expireDate.setDate(expireDate.getDate()+60);

/*
if (navigator.cookieEnabled == 0) {
  alert("You need to enable cookies for this site to load properly!");
}
*/

//para borrar una actividad desde la agenda
function borraact(idr, idsr, ide, ed, srubro, lan)
{
	window.location = "agen_borra_act.asp?e="+ed+"&a=0&idr="+idr+"&idsr="+idsr+"&ide="+ide+"&nsr="+srubro+"&lan="+lan;
}

//para agregar o borrar segun sea el caso del parametro 'accion' desde el programa
function manda(accion, idr, idsr, ide, ed, lan, srubro, fec, hr) 
{
	//alert(document.cookie)
	if(document.cookie != "") {
		retStr = cookieVal("FIL") ;
		//alert ("fec=" + fec );
		window.location = "prog_agrega.asp?idu="+retStr+"&e="+ed+"&lan="+lan+"&a="+accion+"&idr="+idr+"&idsr="+idsr+"&ide="+ide+"&nsr="+srubro+"&f="+fec+"&hr="+hr;
		/*if (retStr != null) {
			arrRecords = retStr.split(",");
			//alert(arrRecords);
		}*/
    }
}


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() 
{
	//alert(document.cookie)
	if(document.cookie != "") {
		retStr = cookieVal("Fil") ;
		//alert ("retStr=" + retStr );
		if (retStr != null) {
			arrRecords = retStr.split(",");
			//alert(arrRecords);
		}
    }
}

/*
function setCookie (name, value, expires) {
if (!expires) expires = new Date();
document.cookie=name+"="+escape (value)+"; expires="+expires.toGMTString()+"; path=/";
}
*/

function addEvent(idr, idsr, ide) 
{
	var i = 0;
	var recCount = getNumEvents();
	var found = false;
	var rEvent;

	/*alert(idr);
	alert(idsr);
	alert(ide);
	alert(recCount);*/
	// check it is not already there
    for(i = 0; i < recCount; i++) {
		rEvent = arrRecords[i];
		if (rEvent == idr+'.'+idsr+'.'+ide) {
			found=true;
			break;
		}
	}
	
	if (found==false)
	{
		lastitem = arrRecords.length;
		arrRecords[lastitem] = idr+'.'+idsr+'.'+ide;
		
		document.cookie = "Fil="+arrRecords+";expires=" + expireDate.toGMTString()+"; path=/";
		parent.location.reload();
	}
	else
	{
		alert('This event has already been added to your schedule.');
	}
}

function inarray(idr,idsr,ide){
	var i = 0;
	var recCount = getNumEvents();
	var found = false;
	for(i = 0; i < recCount; i++) {
		rEvent = arrRecords[i];
		if (rEvent == idr+'.'+idsr+'.'+ide) {
			found=true;
			break;
		}
	}
	return found;
}

function showEvents(){
	//alert(document.cookie)
	if(document.cookie != "") {
		retStr = cookieVal("Fil") ;
		//alert ("retStr=" + retStr );
		if (retStr != null) {
			arrRecords = retStr.split(",");
			return(arrRecords);
		}else return(0);
    }
}

function getNumEvents()
{
	if (arrRecords.length != "" ) 	{
		return arrRecords.length ;
	} else {
		return 0;
	}
}

function deleteEvent(idr,idsr,ide) 
{
	var arrTemp = new Array();
	var i = 0;
	var j = 0;
	var recCount = getNumEvents() ;
	var rEvent = null;

	if (recCount == 1)
	{
		nullStr = "";
		document.cookie = "Fil="+nullStr+";expires=" + oldDate.toGMTString()+"; path=/";
		parent.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 != idr+'.'+idsr+'.'+ide) {
			arrTemp[j++] = arrRecords[i];
		} 
		//else 
		//{
		//	alert('found item to delete : ' + rEvent);
		//}
	}

    arrRecords = arrTemp ;
	document.cookie = "Fil="+arrRecords+";expires=" + expireDate.toGMTString()+"; path=/";
	parent.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 the entire schedule?')) {
		nullStr = "";
		document.cookie = "Fil="+nullStr+";expires=" + oldDate.toGMTString()+"; path=/";
		parent.location.reload();
	}
}

function ClearEvents() {
    document.write ('<a href="javascript:delEvents();">Click here to Clear All Events from schedule.</a>');
}			


loadCookie();