// http://www.quirksmode.org/js/cookies.html

COOKIE_PREFIX = "pf_search_";
COOKIE_LIFE = 365;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getNextCookieId() {
	var index = 1;
	while (readCookie(COOKIE_PREFIX + index)) {
		index++;
	}
	console.log("next cookie id: " + index);
	return index;
}

function saveSearch(name, form) {
	var id = getNextCookieId();
	var value = "searchName:" + name + "|searchId:" + id;

	var no_spaces = name.replace(/ /g,"");
	alert("Your search results have been saved as: " + no_spaces + "\n Access these results from the main Search page.");
	//alert(name.length);
	if(name.length>0 && no_spaces.length>0){
	for (i = 0; i < form.elements.length; i++) {
		value += "|" + form.elements[i].name + ":" + form.elements[i].value;
	}
	console.log("called save search. got value = \"" + value + "\"");
	createCookie(COOKIE_PREFIX + id, value, COOKIE_LIFE);
}
	else{
		saveSearch(prompt('The search name may not be blank. \nPlease enter a name to save this search under:'), form);
	}
}

function getAllSavedSearches() {
	var index = 1;
	var max_id = 30;
	var searches = new Array();

	// loop through each cookie
	while (index < max_id) {
		value = readCookie(COOKIE_PREFIX + index);
		if (value) {
			console.log("got cookie with value: " + value);
			search = getSearchFromCookieValue(value);
			// save all search objects in array
			searches[searches.length] = search;
			console.log("put search in searches[" + (searches.length) + "] with name " + search['name']);
		}
		index++;
		
	}

	// return array of search objects
	return searches;
}

function getSearchFromCookieValue(value) {
	var pairs = value.split('|');
	var search = {};

	// loop through each key/value pair in the cokie
	for (i = 0; i < pairs.length; i++) {
		pair = pairs[i].split(":");
		search[pair[0]] = pair[1];
		//console.log('set ' + pair[0] + ' to ' + pair[1]);
	}
	return search;
}

function loadSearch(id) {
	cookie = readCookie(COOKIE_PREFIX + id);
	search = getSearchFromCookieValue(cookie);

	for (prop in search) {
		var formElement = document.forms[0][prop];
		if (formElement) {
			formElement.value = search[prop];
		}
	}
	document.getElementById('find').onclick();

}

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 forgetSearch(id) {
	eraseCookie(COOKIE_PREFIX + id);
	document.getElementById('search_' + id).style.display = 'none';
	
	var savedSearches = getAllSavedSearches();
	if( savedSearches.length <= 0)
	{
		var savedDiv = document.getElementById('saved_searches_container');
		savedDiv.innerHTML = "";
	}
}

function eraseCookie(name) {
	console.log("erasing cookie with name " + name);
	createCookie(name,"",-1);
}


function saveMapPrefs(closed) {
	var id = "pf_map_prefs";
	var value = "mapClosed:" + closed;

	console.log("called save Map Prefs. got value = \"" + value + "\"");
	createCookie(id, value, COOKIE_LIFE);
}

function saveInfoPrefs(closed) {
	var id = "pf_info_prefs";
	var value = "infoClosed:" + closed;

	console.log("called save info Prefs. got value = \"" + value + "\"");
	createCookie(id, value, COOKIE_LIFE);
}

function getMapPrefs() {
	var mapPrefs = "";

	//get map prefs (if they exist)
	value = readCookie("pf_map_prefs");
	if(value)
		mapPrefs = value;
	console.log(" map Prefs: \"" + mapPrefs + "\"" );
	
	return mapPrefs;
}

function getInfoPrefs() {
	var infoPrefs = "";
	
	//get info box prefs (if they exist)
	value = readCookie("pf_info_prefs");
	if(value)
		infoPrefs = value;
	console.log(" info Prefs: \"" + infoPrefs + "\"" );
	
	return infoPrefs;
}

