﻿var locationCookieName = "favLocId";

String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
}

function SetLocationCookie(locId) {

	// Expire the cookie after 365 days.
	var numdays = 365;
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + (numdays * 24 * 60 * 60 * 1000));

	// set cookie domain to .domain.com to allow acces to cookie by subdomains
    var host = location.hostname.toLowerCase();
	if (host.startsWith("www"))
	    host = host.replace("www", "");
	else
	    host = "." + host;

	document.cookie = locationCookieName + "=" + locId + "; expires=" + expdate.toGMTString() + "; path=/; domain=" + host;    

	//alert("cookie set for: " + locKey);
}

// returns saved location id
function GetLocationCookie() {
	var cookieValue = null;
	if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = jQuery.trim(cookies[i]);
			// Does this cookie string begin with the name we want?
			if (cookie.substring(0, locationCookieName.length + 1) == (locationCookieName + '=')) {
				cookieValue = decodeURIComponent(cookie.substring(locationCookieName.length + 1));
				break;
			}
		}
	}
	return cookieValue;
}
