	var accessStatusInfoCookie = "HS_DIGI_ACCESS_INFO";
	var archiveAccessStatusInfoCookie = "HS_ARCHIVE_ACCESS_INFO";
	var userNameCookie = "HS_DIGI_USER_NAME";

	var sessionCookie = "SMSESSION";
	var loginSiteCookie = "HSLOGINSITE";

	var userCookie = "CK_VL_INTERFACE";
	var adminCompanyUserCookie = "CK_VL_COMP_ADMIN";
	var combinationUserCookie = "CK_VL_COMBINATION_SUBSCRIPTION";

	var privateUserCookieValue = "PRIVATE";
	var companyUserCookieValue = "COMPANY";
	
	var validAccessIndentifier = "voimassa:";

	function writeUserInformation()
	{
		if (isSignedIn()) {
			var name = getName();
			if (name != '')
			{
				document.write('Tervetuloa, <span class="person">' + name + '</span>');
				document.write('<br/>');
			}
			
			var accessInfo = getAccessInfo(unescape(getCookieValue(accessStatusInfoCookie)));
			
			if (accessInfo  != '')
			{
				document.write(accessInfo);
			}
			else if (name != '')
			{
				document.write('Ei voimassa olevaa lukuoikeutta. <span class="buy"><a href="/valitsetuote/">Osta &raquo;</a></span>');
			}
		}
	}

	function writeArchiveUserInformation()
	{
		if (isSignedIn()) {
			var name = getName();
			if (name != '')
			{
				document.write('Tervetuloa, <span class="person">' + name + '</span>');
				document.write('<br/>');	
			}

			var accessInfo = unescape(getCookieValue(archiveAccessStatusInfoCookie));
			if (accessInfo  != '')
			{
				document.write(accessInfo);
			}
			else if (name != '')
			{
				document.write('Ei voimassa olevaa lukuoikeutta Arkistoon. <span class="buy"><a href="/valitsetuote/">Osta &raquo;</a></span>');
			}
		}
	}

	function writeUserLinks(domainName)
	{
		if (isSignedIn()) {
			if (isPrivateUser()) {
				document.write('<a href="https://' + domainName + '/omattiedot/">Omat tiedot</a> <span class="toolTube">|</span> ');
			} else if (isAdminCompanyUser()) {
				document.write('<a href="https://' + domainName + '/yritykset/hallinta/arkisto/kayttajalista.do">Hallintaliittym&auml;</a> <span class="toolTube">|</span> ');
			}
			document.write('<a href="http://' + domainName + '/kirjautuminen/poistu/">Kirjaudu ulos</a> <span class="toolTube">|</span> ');
		}
		document.write('<a href="http://' + domainName + '/digilehti/apua/#HSDigilehti">Apua</a>');
	}
	
	function writeHSUserLinks(domainName)
	{
		if (isSignedIn()) {
			if (isPrivateUser()) {
				document.write('<a href="https://' + domainName + '/omattiedot/">Omat tiedot</a> <span class="toolTube">|</span> ');
			} else if (isAdminCompanyUser()) {
				document.write('<a href="https://' + domainName + '/yritykset/hallinta/arkisto/kayttajalista.do">Hallintaliittym&auml;</a> <span class="toolTube">|</span> ');
			}
			document.write('<a href="http://' + domainName + '/kirjautuminen/poistu/">Kirjaudu ulos</a> <span class="toolTube">|</span> ');
		} else {
			document.write('<a href="https://' + domainName + '/kirjautuminen/kirjaudu/">Kirjaudu sis&auml;&auml;n</a> <span class="toolTube">|</span> ');
		}
		document.write('<a href="http://' + domainName + '/apua/">Apua</a>');
	}
	
	function writeArchiveUserLinks(domainName)
	{
		if (isSignedIn()) {
			if (isPrivateUser()) {
				document.write('<a href="https://' + domainName + '/omattiedot/luetutartikkelit/">Luetut artikkelit</a> <span class="toolTube">|</span> <a href="https://' + domainName + '/omattiedot/">Omat tiedot</a> <span class="toolTube">|</span> ');
			} else if (isAdminCompanyUser()) {
				document.write('<a href="https://' + domainName + '/yritykset/hallinta/arkisto/kayttajalista.do">Hallintaliittym&auml;</a> <span>|</span> ');
			}
			document.write('<a href="http://' + domainName + '/kirjautuminen/poistu/">Kirjaudu ulos</a> <span>|</span> ');
		}
		document.write('<a href="http://' + domainName + '/arkisto/apua/#HSArkisto">Apua</a>');
	}

	function writeOldDigiPapersLink()
	{
		if (isSignedIn() && (isCompanyUser() || isCombinationUser())) {
			document.write('<span class="infoDigilehdet">');
			document.write('<strong>UUTTA!</strong><br/>');
			document.write('Voit selailla vuoden 2006 alusta l&auml;htien HS Digilehtien n&auml;k&ouml;isversioita.<br>');
			document.write('<a href="/digilehti/selailedigilehtia/">Selaile &raquo;</a>');
			document.write('</span>');
		}
	}

	function getAccessInfo(accessInfo)
	{
		var startOfValidAccessIndentifier = accessInfo.indexOf(validAccessIndentifier);
		if (startOfValidAccessIndentifier > -1)
		{
			accessInfo = accessInfo.substring(validAccessIndentifier.length);
		}
		return accessInfo;
	}

	function hasAccessToDigiPaper()
	{
		if (isSignedIn())
		{
			var accessStatusInfoCookieValue = unescape(getCookieValue(accessStatusInfoCookie));
			var startOfValidAccessIndentifier = accessStatusInfoCookieValue.indexOf(validAccessIndentifier);
			if (startOfValidAccessIndentifier > -1)
			{
				return true;
			}
		}
		return false;
	}
	
	function writeDigiPaperLink(address)
	{
		document.write('<a href="' + address + '"');
		if (hasAccessToDigiPaper())
		{
			document.write(' target="_blank"');
		}
		document.write('>');
	}

	function writeDigiPaperLinkWithOldPaperStyle(address)
	{
		document.write('<a class="digip" href="' + address + '"');
		if (hasAccessToDigiPaper())
		{
			document.write(' target="_blank"');
		}
		document.write('>');
	}
	
	function openDigiPaperWindow(address)
	{
		if (hasAccessToDigiPaper())
		{
			openWindow(address);
		}
		else
		{
			document.location.href = address;
		}
	}
	
	function getName()
	{
		var name = '';
		if (isPrivateUser()) 
		{
			name = unescape(getCookieValue(userNameCookie));
		}
		
		return name;
	}
	
	function isPrivateUser()
	{
		if (getCookieValue(userCookie) == privateUserCookieValue)
		{
			return true;
		}
		return false;
	}

	function isCompanyUser()
	{	
		if (getCookieValue(userCookie) == companyUserCookieValue)
		{
			return true;
		}
		return false;
	}

	function isCombinationUser()
	{	
		if (getCookieValue(combinationUserCookie) == 'Y')
		{
			return true;
		}
		return false;
	}

	function isAdminCompanyUser()
	{	
		if (isCompanyUser() && getCookieValue(adminCompanyUserCookie) == 'Y')
		{
			return true;
		}
		return false;
	}
		
	function isSignedIn()
	{
		var CookieString = document.cookie;
		var CookieSet = CookieString.split(';');
		var SetSize = CookieSet.length;
		var CookiePieces;
		var CookieValue = "";
		var x = 0;
		var SessionExists = "FALSE";
		var SiteIsVerkkoliite = "FALSE";

		for (x = 0; x < SetSize; x++) {

			CookiePieces = CookieSet[x].split ('=');

			if (CookiePieces[0].substring(0,1) == ' ') {
				CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
			}

			if (CookiePieces[0] == sessionCookie) {
				CookieValue = CookiePieces[1];

				if ((CookieValue.length > 10) && (CookieValue != "LOGGEDOFF")) {
					SessionExists = "TRUE";
				}
			}

			if (CookiePieces[0] == loginSiteCookie) {
				CookieValue = CookiePieces[1];

				if (CookieValue == "Verkkoliite") {
					SiteIsVerkkoliite = "TRUE";
				}
			}
		}

		if ( (SiteIsVerkkoliite == "TRUE") && (SessionExists == "TRUE") ) {
			return true;
		}

		return false;
	}

	function getCookieValue(cookieName)
	{
		var cookieValue = "";
		var cookie = document.cookie;
		var prefix = cookieName + "=";
		var index = cookie.indexOf(prefix);

		// if index was found
		if (index != -1)
		{
			// try to find end index
			var endIndex = cookie.indexOf(";", index);
	
			// if end index was not found, use string end
			if (endIndex == -1)
			{
				endIndex = cookie.length;
			}
	
			// get value substring
			cookieValue = cookie.substring(index + prefix.length, endIndex);
		}
	
		return cookieValue;
	}

	function getUserNameFromCookie() {
		var myCookies = document.cookie;
		var startOfVLCookie = myCookies.indexOf("vlUserName=");
		if (startOfVLCookie > -1) {
			myCookies = myCookies.substring(startOfVLCookie + 11);
			var endOfVLCookie = myCookies.indexOf(";");
			var userName = "";
			if (endOfVLCookie > -1) {
				userName = unescape(myCookies.substring(0, endOfVLCookie));
			}
			else {
				userName = unescape(myCookies);
			}
			document.myForm.USER.value = userName;
			if (userName.length == 0) {
				document.myForm.remember.checked = false;
				document.myForm.USER.focus();
			}
			else {
				document.myForm.remember.checked = true;
				document.myForm.PASSWORD.focus();
			}
		}
		else {
			document.myForm.remember.checked = false;
			document.myForm.USER.focus();
		}
	}

	function setUserNameInCookie() {
		setTemporaryLoginUserNameInCookie();
		var currentDate = new Date();
		var expirationDate = new Date(currentDate.getTime() + 1000 * 60 *60 * 24 * 365 * 20);
		if (document.myForm.remember.checked == true) {
			document.cookie = "vlUserName=" + escape(document.forms["myForm"].USER.value) + "; expires=" + expirationDate.toGMTString() + "; path=/";
		}
		else {
			document.cookie = "vlUserName=; expires=" + expirationDate.toGMTString() + "; path=/";
		}

	}
	
	function setTemporaryLoginUserNameInCookie() {
		var currentDate = new Date();
		var expirationDate = new Date(currentDate.getTime() + 1000 * 60 * 10);
		document.cookie = "vlTemporaryLoginUserName=" + escape(document.forms["myForm"].USER.value) + "; expires=" + expirationDate.toGMTString() + "; path=/";
	}
