//on dom ready run checkTick()
document.observe('dom:loaded', function(){checkTick();});

//Look at the state of the tick and judge whether to check it and hide or divs accordingly
function checkTick()
{
    //Start check of email tick box
	var tckBox = $("chbxRecEm");
	var tckState = readIt("tckBoxTick");
	
	if (!tckState)
	{
		saveIt("tckBoxTick", "false");
	}
	
	if (tckState == "true")
	{
		tckBox.checked = true;
		$('search-email').toggle(); $('search-prem-top').toggle();
	}
	else
	{
		tckBox.checked = false;
	}
	
	Event.observe(tckBox, "click", function()
	{
		var tckState = readIt("tckBoxTick");
		if (tckState == "true")
		{
			$('search-email').toggle(); $('search-prem-top').toggle();
			saveIt("tckBoxTick", "false");
		}
		else
		{
			$('search-email').toggle(); $('search-prem-top').toggle();
			saveIt("tckBoxTick", "true");
		}
	});      
    //End check of email tick box
}



//Save, read and delete cookie
function saveIt(name, value) 
{
	if (!value)
	{
		alert('Please provide cookie value.');
	}		
	else 
	{
		Cookies.create(name,value,7);
	}
}

function readIt(name) 
{
	return Cookies[name];
}

function eraseIt(name) 
{
	Cookies.erase(name);
}

var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (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=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();
