function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	var nameEQ = c_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 loadPrefs() {
	window.prefs = document.getElementById('prefs');
	window.inputs = prefs.getElementsByTagName('input');
}

function showPrefs() {
	if (window.prefs.style.display != 'block') {
		var excerpt_input = document.getElementById('excerpt');
		if ((excerpt = getCookie('excerpt')) && !excerpt_input.disabled) excerpt_input.value = excerpt;	
		window.prefs.style.display = 'block';
		window.prefs_state = '';
		for (var i=0; i<window.inputs.length; i++) window.prefs_state += window.inputs[i].value + window.inputs[i].checked;
	}
}

function submitForm() {
	var submit_state = '';
	for (var i=0; i<window.inputs.length; i++) submit_state += window.inputs[i].value + window.inputs[i].checked;
	(submit_state == window.prefs_state)
		? window.prefs.style.display = 'none'
		: document.getElementById('prefs_form').submit();
}

function replaceButton() {
	var submit = document.getElementById('submit');
	submit.childNodes[0].style.display = 'none';
	var doneButton = document.createElement('span');
	doneButton.setAttribute('onclick','submitForm()');
	doneButton.appendChild(document.createTextNode('Done'));
	submit.appendChild(doneButton);
}

var newtab = 0;

function toggleNewTab() {
	var toggle = document.getElementById('toggle').childNodes[0];
	var items = document.getElementsByTagName('a');
	if (newtab == 0) {
		for (var x = 1; x < items.length; x++) items[x].setAttribute('target','_blank');
		toggle.style.backgroundPosition = 'bottom';
		newtab = 1;
		setCookie('newtab','yes',365);
	} else {
		for (var x = 1; x < items.length; x++) items[x].removeAttribute('target');
		toggle.style.backgroundPosition = 'top';
		newtab = 0;
		setCookie('newtab','no',365);
	}
}

function checkNewTab() {
	if (getCookie('newtab') == 'yes') toggleNewTab();
}