// 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);
			email_details = link.split("@");
			tag_open = "<a href=\"javascript:showEmailLink('" + email_details[0] + "','" + email_details[1] + "');\"><script type='text/javascript'>showEmail('" + email_details[0] + "','" + email_details[1] + "');</script>";
			tag_close = "</a>";
			textfield.value = textfield.value.substring(0, textfield.selectionStart) +
				tag_open + 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;
}

function showLargeImage(id, src, width, height) {
	showLayer(id);
	if(window.pageYOffset < 85) {
		getElem('id', id, null).style.top = "85px";
	} else {
		getElem('id', id, null).style.top = window.pageYOffset + 85 + "px";
	}
	getElem('id', id, null).innerHTML = "<div class=\"image\"><img src=" + src + " width=" + width + " height=" + height + " alt=\"\" /></div><a href=\"javascript:hideLayer('" + id + "');\" onClick=\"hideBusyLayer();\"><div class=\"close\">&nbsp;</div></a>";
}

function sineInOut(pos) {
	// EASE IN AND OUT by position
	// pos = 0 ... 1
	// returns float(0 ... 1)
	return (1 - Math.cos(pos * Math.PI)) / 2;
}

function showEmail(receipient, domain) {
	document.write(receipient + "@" + domain);
}

function showEmailLink(receipient, domain) {
	parent.location = "m" + "a" + "i" + "l" + "t" + "o" + ":" + receipient + "@" + domain;
}