function writeLinks()
{
	var sitePrefix = "CW";
	var erightsHost = "login.idg.com";
	var erights = readCookie("ERIGHTS");
	var links = "";
	var erightsLoginLink = "<a class='storylink' href='http://" + erightsHost + "/idg/" + sitePrefix + "Login.do?url=" + document.location.href + "'>Login</a>";
	var erightsProfileLink = "<a class='storylink' href='http://" + erightsHost + "/idg/" + sitePrefix + "Profile.do?call=viewProfile&url=" + document.location.href + "'>Profile</a>";
	var erightsRegisterLink = "<a class='storylink' href='http://www.computerworld.com/register'>Register</a>";
	var erightsLogoutLink = "<a class='storylink' href='http://" + erightsHost + "/idg/" + sitePrefix + "Logout.do' onClick='deleteCookie();'>Logout</a>";
	if (erights == null || erights.length < 1) {
		links = erightsLoginLink + "&nbsp;&nbsp;&nbsp;" + erightsRegisterLink;
	}
	else {
	var erightsUser = erights.slice(erights.indexOf("_")+1);
	var erightsHello = "Welcome <a class='storylink' href='http://" + erightsHost + "/idg/" + sitePrefix + "Profile.do?call=viewProfile'>" + erightsUser + "</a>";
	links = erightsHello + "&nbsp;&nbsp;&nbsp;" + erightsLogoutLink;
	}
	return links;
}

function readCookie(name)
{
	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;
}

function retrieveCookie( cookieName ) {
	/* retrieved in the format
	cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
	only cookies for this domain and path will be retrieved */
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
	}
	return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
    if( !cookieName ) { return false; }
    if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
    /* This next line sets the cookie but does not overwrite other cookies.
    syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]] Because of the way that document.cookie behaves, writing this here is equivalent to writing
    document.cookie = whatIAmWritingNow + "; " + document.cookie; */
    document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
    ( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
    ( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") +
    ( isSecure ? ";secure" : "");
    //check if the cookie has been set/deleted as required
    if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
    if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;
}

function deleteCookie() {
	setCookie('PCOOKIE','','delete');
	setCookie('ERIGHTS','','delete');
}
