function jump_page(offset)
{
	if (document.page_form.action.match(/offset=\d*/)) {
		document.page_form.action = document.page_form.action.replace(/offset=\d*/, "offset=" + offset);
	} else if (document.page_form.action.match(/\?/)) {
		document.page_form.action += "&offset=" + offset;
	} else {
		document.page_form.action += "?offset=" + offset;
	}
	document.page_form.submit();
}

function add_entity(type, all)
{
	document.add_entity.action = '/contacts/add_' + type + '.chtml';
	document.add_entity.method = all ? 'get' : 'post';
	document.add_entity._doctype.value = type;
	document.add_entity.submit();
}

function add_link(doctype, docid, name, sub_name)
{
	document.new_link.target_docid.value = docid;
	document.new_link.target_doctype.value = doctype;
	if (sub_name) {
		name = name + ' in ' + sub_name;
	}
	document.new_link.target_name.value = name;
	document.new_link.submit();
}

function delete_person()
{
	return delete_entity();
}

function delete_organisation()
{
	return delete_entity();
}

function delete_entity()
{
	if (confirm("Are you sure you want to delete " + document.deleteform.name.value)) {
		if (document.deleteform._redirect) {
			document.deleteform._redirect.value = '';
		}
		document.deleteform.submit();
	}
}

function delete_department(orgid)
{
	if (confirm("Are you sure you want to delete " + document.deleteform.name.value)) {
		document.deleteform._redirect.value = "/contacts/view_organisation.chtml?docid="+orgid + "&ref_command=unlink_department&removed_name=" + document.deleteform.name.value;
		document.deleteform.submit();
	}
}

function delete_mailinglist(docid, name)
{
	if (docid && name) {
		docid = "mailinglist=" + docid;
		document.deleteform.remove.value = docid;
	}
	name = name || document.deleteform.name.value;
	if (confirm("Are you sure you want to delete " + name)) {
		document.deleteform.submit();
	}
}

function unlink_entity(link_docid, docid, name)
{
	if (confirm("Are you sure you want to delete " + name + " from " + document.deleteform.name.value)) {
		document.deleteform.remove.value = "link=" + link_docid;
		if (document.deleteform._redirect) {
			document.deleteform._redirect.value += "&removed_docid=" + docid + "&removed_name=" + name;
		}
		document.deleteform.submit();
	}
}

function unlink_department(docid, name)
{
	if (confirm("Are you sure you want to delete " + name
		+ " from " + document.deleteform.name.value
		+ ".\nAll staff will be moved to the organisation")) {
		document.deleteform.remove.value = "department=" + docid;
		document.deleteform._redirect.value += "&removed_docid=" + docid + "&removed_name=" + name;
		document.deleteform.submit();
	}
}

function unsubscribe(list_name, list, link)
{
	if (confirm("Are you sure you want to unsubscribe " + document.deleteform.name.value + " from " + list_name)) {
		document.unsubscribeform.unsubscribe.value = "list=" + list + ";link=" + link;
		if (document.unsubscribeform._redirect) {
			document.unsubscribeform._redirect.value += "&removed_docid=" + list + "&removed_name=" + list_name;
		}
		document.unsubscribeform.submit();
	}
}

function delete_iss()
{
	if (confirm("Are you sure you want to unlink " + document.deleteform.name.value + " from ISS? Contact information will not be updated automatically")) {
		document.delete_iss.submit();
	}
}


function set_now()
{
	if (!document.getElementById('text_time')) {
		return;
	}
	var d = new Date();
	document.getElementById('text_time').innerHTML =
	document.log_form.contactlog__time.value =
		sprintf(
			'%02d:%02d %s',
			d.getHours()>12 ? d.getHours() - 12 : (d.getHours() || 12),
			d.getMinutes(),
			d.getHours()>=12 ? 'PM' : 'AM'
		);
	document.getElementById('text_date').innerHTML =
	document.log_form.contactlog__date.value =
		sprintf(
			'%02d/%02d/%04d',
			d.getDate(),
			d.getMonth()+1,
			d.getFullYear()
		);
	click_now();
}

function click_now()
{
	if (document.log_form.change_now.checked) {
		document.log_form.contactlog__time.style.display = "none";
		document.log_form.contactlog__time.value =
			document.getElementById('text_time').innerHTML;
		document.log_form.contactlog__date.style.display = "none";
		document.log_form.contactlog__date.value =
			document.getElementById('text_date').innerHTML;
		document.getElementById('text_time').style.display = "block";
		document.getElementById('text_date').style.display = "block";
	} else {
		if(!document.log_form.contactlog__time.value) {
			set_now();
		}
		document.log_form.contactlog__time.style.display = "block";
		document.log_form.contactlog__date.style.display = "block";
		document.getElementById('text_time').style.display = "none";
		document.getElementById('text_date').style.display = "none";
	}
}

function check_person_name(form)
{
	if (!form.person__given_names.value && !form.person__family_name.value) {
		alert("Please enter a name");
		return false;
	}
	return true;
}

function check_organisation_name(form)
{
	if (!form.organisation__name.value) {
		alert("Please enter an organisation name");
		return false;
	}
	ucfirst(form.organisation__name);
	return true;
}
