//http://www.quirksmode.org/js/cookies.html

/*
	When calling createCookie() you have to give it three bits of information: 
the name and value of the cookie and the number of days it is to remain active. 
In this case the name-value pair should become ppkcookie=testcookie and it should 
be active for 7 days.

				createCookie('ppkcookie','testcookie',7)

	If you set the number of days to 0 the cookie is trashed when the user closes 
the browser. If you set the days to a negative number the cookie is trashed immediately.
*/
function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}	
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


/*
	To read out a cookie, call this function and pass the name of the cookie. Put the name 
in a variable. First check if this variable has a value (if the cookie does not exist the 
variable becomes null, which might upset the rest of your function), then do whatever is 
necessary.

				var x = readCookie('ppkcookie1')
				if (x) 
				{
					[do something with x]
				}
*/
function readCookie(name) 
{
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		var dc = document.cookie; 
		var cname = name + "="; 
		if (dc.length > 0) 
		{ 
			begin = dc.indexOf(cname); 
			if (begin != -1) 
			{ 
				begin += cname.length; 
				end = dc.indexOf(";", begin);
				if (end == -1) 
					end = dc.length;
				return unescape(dc.substring(begin, end));
			} 
		} 
		return null; 
	}
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') 
			c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*
	Erasing is extremely simple.

	eraseCookie('ppkcookie')
*/
function eraseCookie(name) 
{
	/*
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/"; 
	}
	else
		*/
	{
		createCookie(name,"",-1);	
	}
}

function adaugaObiectiv(id, regiune)
{
	//verific daca exista cookie
	var x = readCookie("traseu");
	if (x)
	{
		//daca exista verific daca in continut gasesc id-ul iar daca nu exista il adaug la sfarsit		
		if ( x.indexOf("" + id) == -1)
		{
			//eraseCookie("traseu");
			createCookie("traseu", (x+id+"$"), 0);			
			if (regiune !== "EMPTY_REGION")
				createCookie("regiune", regiune, 0);
		}
	}
	else
	{
		//daca nu exista il fac si adaug id-u
		createCookie("traseu", (id+"$"), 0);
		if (regiune !== "EMPTY_REGION")
			createCookie("regiune", regiune, 0);
	}
}
