/* COOKIES */

function getCookieValue(cookie) {
        var allCookies = document.cookie;
        var key = cookie + "=";

        var pos = allCookies.indexOf(key);
        var start = pos + key.length;
        var end = allCookies.indexOf(";", start);
        if(end == -1) end = allCookies.length;

        var value = allCookies.substring(start, end);
        return decodeURIComponent(value);
}

function hasCookie(cookie) {
        var allCookies = document.cookie;
        var pos = allCookies.indexOf(cookie);

        if(pos == -1) 
                return false;
        else 
                return true;        
}

// if expireDays <= 0, expire at end of session
function saveCookie(cookie, value, expireDays) {
        if (expireDays <= 0) {
           document.cookie = cookie + "=" + value;        
        } else {
           var today = new Date();
           var expire = new Date();
           expire.setTime(today.getTime() + 3600000*24*365*4);
           document.cookie = cookie + "=" + value+";expires="+expire.toGMTString();
        }
}

//DEPRECATED: below methods replaced in favor of 'named' cookies using methods above

// 0 - show resources toggle checkbox
// 1 - show landmarks toggle checkbox
// 2 - show pois toggle checkbox (if applicable)


// searchs for and returns the value of the cookie 'cookiesave'
// default value is null
function getCookiesave() {
	var cookieArray = document.cookie.split(';')
	for(var i = 0; i < cookieArray.length; i++) {
		var cookiePairs = cookieArray[i].split('=');
		if (cookiePairs[0] == 'cookiesave') {
			return cookiePairs[1];
		}
	}
	return null;
}

// given an index, returns the value of the cookie 'cookiesave' at that index
// default value is 0;
function getCookiesaveAt(index) {
	if (index < 0) return 0;
	var cookiesave =  getCookiesave();
	if (cookiesave == null) return 0;
	if (cookiesave.length < index) return 0;
	return cookiesave.charAt(index);
}

// given an index and a value, sets the value of the cookie 'cookiesave' at that index.
// value can only be a single char or a boolean.  No special chars please.
// 0 = false
// 1 = true
function setCookiesaveAt(index, value) {
	if (index >= 0 && (value.length > 1) || value == true || value == false) {
		if (value == false) value = 0;
		if (value == true) value = 1;
		var cookiesave =  getCookiesave();
		if (cookiesave == null) {
			cookiesave = '';
			for (var i = 0; i < index; i++) {
				cookiesave = cookiesave + '0';
			}
			cookiesave = cookiesave + value;
		}
		else {
			if (index == cookiesave.length - 1) {
				cookiesave = cookiesave.substring(0, index) + value
			}
			else {
				cookiesave = cookiesave.substring(0, index) + value + cookiesave.substring(index+1, cookiesave.length);
			}
		}
		document.cookie = 'cookiesave='+cookiesave;
	}
}
	
