// utility functions ///////////////////////////////////////////////////////////
function checkEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else {
		return false;
	}
}

function spamBlock(user,domain) {
	string = "mailto:" + user + "@" + domain;
	window.location = string;
}

function insertTags(textfield_id, tag_open, tag_close) {
	textfield = getElem("id", textfield_id, null);
	textfield.value = textfield.value.substring(0, textfield.selectionStart) +
		tag_open + textfield.value.substring(textfield.selectionStart,textfield.selectionEnd) +
		tag_close + textfield.value.substring(textfield.selectionEnd,textfield.value.length);
}

function insertEmail(textfield_id) {
	link = prompt("Email address:", "");
	if(link != null) {
		if(checkEmail(link)) {
			textfield = getElem("id", textfield_id, null);
			tag_open = "<a href=\"mailto:" + link + "\">";
			tag_close = "</a>";
			textfield.value = textfield.value.substring(0, textfield.selectionStart) +
				tag_open + textfield.value.substring(textfield.selectionStart,textfield.selectionEnd) +
				tag_close + textfield.value.substring(textfield.selectionEnd,textfield.value.length);
		} else {
			alert("Email address not valid");
		}
	}
}

function insertURL(textfield_id) {
	link = prompt("URL:", "http://");
	if(link != null) {
		if(link.substring(0, 7) == "http://") {
			textfield = getElem("id", textfield_id, null);
			tag_open = "<a href=\"" + link + "\">";
			tag_close = "</a>";
			textfield.value = textfield.value.substring(0, textfield.selectionStart) +
				tag_open + textfield.value.substring(textfield.selectionStart,textfield.selectionEnd) +
				tag_close + textfield.value.substring(textfield.selectionEnd,textfield.value.length);
		} else {
			alert("Link needs to start with 'http://'");
		}
	}
}

function insertPageLink(textfield_id) {
	link = prompt("Page Link ID:");
	if(link != null) {
		textfield = getElem("id", textfield_id, null);
		tag_open = "<pagelink id=\"" + link + "\">";
		tag_close = "</pagelink>";
		textfield.value = textfield.value.substring(0, textfield.selectionStart) +
			tag_open + textfield.value.substring(textfield.selectionStart,textfield.selectionEnd) +
			tag_close + textfield.value.substring(textfield.selectionEnd,textfield.value.length);
	}
}

function insertFileLink(textfield_id) {
	link = prompt("File Link ID:");
	if(link != null) {
		textfield = getElem("id", textfield_id, null);
		tag_open = "<filelink id=\"" + link + "\">";
		tag_close = "</filelink>";
		textfield.value = textfield.value.substring(0, textfield.selectionStart) +
			tag_open + textfield.value.substring(textfield.selectionStart,textfield.selectionEnd) +
			tag_close + textfield.value.substring(textfield.selectionEnd,textfield.value.length);
	}
}

function resetTextfield(textfield_id1, textfield_id2) {
	textfield1 = getElem("id", textfield_id1, null);
	textfield2 = getElem("id", textfield_id2, null);
	textfield1.value = textfield2.value;
}