function addList(form) {
	if(validateList()) {
		new Ajax.Updater('tasks', '/list/add', {
			onComplete: createSortable,
			onLoading: function(transport) {
				$('list-submit').disabled = true;
				$('list-submit').value = 'Saving...';
			},
			parameters: Form.serialize(form)
		});
	}
}

function addNote(form) {
	if(validateNote(0)) {
		$('note-created-at').value = Date();
		new Ajax.Updater('notes-list', '/note/add', {
			insertion: Insertion.Top,
			onComplete: function(transport) {
				$('note-submit').disabled = false;
				$('note-submit').value = 'Add note';
				$('note-text-0').value = '';
				new Effect.Highlight($('notes-list').firstChild, {duration: 1, endcolor: '#FFFFFF', startcolor: '#FFFFCC'});
			},
			onLoading: function(transport) {
				$('note-submit').disabled = true;
				$('note-submit').value = 'Saving...';
			},
			parameters: Form.serialize(form)
		});
	}
}

function addTask(form) {
	if(validateTask(0)) {
		new Ajax.Updater('tasks', '/task/add', {
			onComplete: function(transport) {
				$('task-action-0').focus();
				$('task-action-0').value = '';
				$('task-submit').disabled = false;
				$('task-submit').value = 'Add task';
				createSortable();
				hideTaskDetails();
				new Effect.Highlight($('tasks-list').childNodes[0], {duration: 1, endcolor: '#FFFFFF', startcolor: '#FFFFCC'});
			},
			onLoading: function(transport) {
				$('task-submit').disabled = true;
				$('task-submit').value = 'Saving...';
			},
			parameters: Form.serialize(form)
		});
	}
}

function checkTask(id) {
	new Ajax.Updater('tasks', '/task/check/' + id, {encoding: 'ISO-8859-1', onComplete: createSortable});
	return false;
}

function clearTasks() {
	new Ajax.Updater('tasks', '/task/clear', {encoding: 'ISO-8859-1', onComplete: createSortable});	
}

function confirmDeleteKeyword() {
	return confirm('Are you sure you want to delete this keyword?');
}

function confirmTerminate(element) {
	if(element.checked) {
		element.checked = confirm('Terminating your account will destroy all your personal data. Do you want to continue?');
	}
}

function createSortable() {
	Position.includeScrollOffsets = true;
	Sortable.create('tasks-list', {
		handle: 'handle',
		onUpdate: function() {
			new Ajax.Request('/task/order', {
				parameters: Sortable.serialize('tasks-list')
			})
		}
	});
}

function deleteList(id) {
	if(confirm('Are you sure you want to delete this list and all tasks in it?')) {
		new Ajax.Updater('tasks', '/list/delete/' + id, {encoding: 'ISO-8859-1', onComplete: createSortable});
	}
}

function deleteNote(id) {
	if(confirm('Are you sure you want to delete this note?')) {
		new Ajax.Request('/note/delete/' + id, {
			onSuccess: function(transport) {
				new Effect.Fade('note-' + id, {duration: 0.5});
			}
		});
	}
}

function editNote(form, id) {
	if(validateNote(id)) {
		new Ajax.Updater('notes', '/note/save', {
			onLoading: function(transport) {
				$('note-submit-' + id).disabled = true;
				$('note-submit-' + id).value = 'Saving...';
			},
			parameters: Form.serialize(form)}
		);
	}
}

function editTask(form, id) {
	if(validateTask(id)) {
		new Ajax.Updater('tasks', '/task/save', {
			onComplete: createSortable,
			onLoading: function(transport) {
				$('task-submit-' + id).disabled = true;
				$('task-submit-' + id).value = 'Saving...';
			},
			parameters: Form.serialize(form)}
		);
	}
}

function findUser(form) {
	new Ajax.Updater('user', '/admin/find_user', {
		onComplete: function(transport) {
			$('email').focus();
			$('email').value = '';
			$('user-submit').disabled = false;
			$('user-submit').value = 'Search';	
		},
		onLoading: function(transport) {
			$('user-submit').disabled = true;
			$('user-submit').value = 'Searching...';
		},
		parameters: Form.serialize(form)}
	);
}

function getKeyword(element) {
	$('submit').value = 'Search ' + default_keyword;
	
	if(match = /^([^\s]+)/.exec(element.value)) {
		if(keywords[match[1]] != undefined) {
			$('submit').value = 'Search ' + keywords[match[1]];
		}
	}
}

function hideListForm() {
	$('list-form').hide();
	$('list-select').innerHTML = selected_list;
}

function hideTaskDetails() {
	$('task-description').value = '';
	$('task-enter-details').show();
	$('task-is-high-priority').checked = false;
	$('task-cancel').hide();
	$('task-details').hide();
}

function highlightRow(element, state) {
	if(!document.getElementsByTagName) return; 
	var cells = element.getElementsByTagName('td');
	
	for(var i = 0; i < cells.length; i++) {
		cells[i].style.backgroundColor = state ? '#FFD' : '#FFF';
	} 
}

function isEmpty(id, message) {
	if(!$F(id)) {
		alert(message);
		$(id).focus();
		return true;
	}
	
	return false;
}

function setExternalLinks() {
	if(!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName('a');
	
	for(var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if(anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') anchor.target = '_blank';
	} 
}

function setList(id) {
	new Ajax.Updater('tasks', '/list/set/' + id, {
		onComplete: createSortable,
		onLoading: function(transport) {
			$('list-select').innerHTML = 'Loading...';
			$('lists-list').hide();
		}
	});
}

function showListForm() {
	$('list-form').show();
	$('list-name').focus();
	$('list-select').innerHTML = 'Enter below&hellip;'
}

function showTaskDetails() {
	$('task-cancel').show();
	$('task-details').show();
	$('task-description').focus();
	$('task-enter-details').hide();
}

function toggleEditNote(id) {
	$('note-form-' + id).toggle();
	$('note-show-' + id).toggle();
	
	if($('note-form-' + id).style.display == 'block') {
		$('note-text-' + id).focus();
	}
}

function toggleEditTask(id) {
	createSortable();
	$('task-form-' + id).toggle();
	$('task-show-' + id).toggle();
	
	if($('task-form-' + id).style.display == 'block') {
		$('task-action-' + id).focus();
	}
}

function toggleListSelect() {
	var duration = 0.2;
	var element = $('lists-list');
	
	if(element.style.display == 'none') {
		new Effect.BlindDown(element, {duration: 0.2});
	} else {
		new Effect.BlindUp(element, {duration: 0.2});
	}
}

function validateList() {
	if(isEmpty('list-name', 'Please enter a list name')) {
		return false;
	}
	
	return true;
}

function validateNote(id) {
	if(isEmpty('note-text-' + id, 'Please enter a note.')) {
		return false;
	}
	
	return true;
}

function validateTask(id) {
	if(isEmpty('task-action-' + id, 'Please enter a task.')) {
		return false;
	}
	
	return true;
}