﻿var setStartPageCookieName = "startPageScanSe";
var startPageStr = "/sitebase/scan.aspx";

//alert(getCookie(setStartPageCookieName) + "\n" + location.href);

function checkStartCookie() {
	if(document.getElementById('setCookieLink')) {
		checkCookie();
	}
}

function setStartPageCookie(location) {
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + 31536000000);
	if (location.indexOf(startPageStr) == -1) {
		//sidan är inte startsida
		setCookie(setStartPageCookieName, location, expiration, '/');
	} else {
		setCookie(setStartPageCookieName, "", expiration, '/');
		document.getElementById('cookieLink').innerHTML="";
        document.getElementById('cookieLink').style.display = "none";
        document.getElementById('cookieLink').style.height = "0px";
	}
	alert("Denna sida är nu din personliga startsida på scan.se");
	checkCookie();
}

function deleteStartPageCookie() {
	//alert('delete!');
	var expiration = new Date();
	expiration.setTime(expiration.getTime() - 31536000000);
	//alert(expiration);
	setCookie(setStartPageCookieName, '', expiration, '/');
	deleteCookie(setStartPageCookieName, '/');
	//alert(setStartPageCookieName);
	checkCookie();
}

function checkCookie() {
	//alert(getCookie(setStartPageCookieName) + "\n" + unescape(location.href));
	if(document.getElementById('cookieLinkArrow') != null) {
		document.getElementById('cookieLinkArrow').style.display='inline';
	}
	if (top.location.href.indexOf(startPageStr) != -1 && (getCookie(setStartPageCookieName) == '' || getCookie(setStartPageCookieName) == null)) {
		//om det är startsidan och om kakan är tom eller null, visa inte länken
	} else {
		if (getCookie(setStartPageCookieName)==unescape(top.location.href)) {
			document.getElementById('setCookieLink').onclick = function () {
				deleteStartPageCookie();
			};
			document.getElementById('setCookieLink').innerHTML='&Aring;terst&auml;ll startsidan p&aring; scan.se';
		} else {
			if(document.getElementById('setCookieLink')) {
				document.getElementById('setCookieLink').onclick = function () {
					setStartPageCookie(top.location.href);
				};
				document.getElementById('setCookieLink').innerHTML='S&auml;tt din startsida p&aring; scan.se';
			}
		}
	}
}

function setCookie(name, value, expires, path, domain, secure) {
	//alert(name + "-" + path  + "-" + domain);
	if(!expires) { expires = new Date(); expires.setDate(expires.getDate()+365); }
	document.cookie = name + "=" + escape(value) +
      "; expires=" + expires.toGMTString() +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}

function deleteCookie(name, path, domain) {
	//alert(name + "-" + path  + "-" + domain);
	if (getCookie(name)) {
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function getCookie(name) {
	var cookie_start = document.cookie.indexOf(name + "=");
	if(cookie_start == -1) return null;
	var cookie_end = document.cookie.indexOf("; ", cookie_start);
	if(cookie_end == -1) cookie_end = document.cookie.length;
	return unescape(document.cookie.substring(cookie_start + name.length + 1, cookie_end));
}
