// 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);
	getElem('id', id, null).style.left = "235px";
	if(getScrollXY()[1] < 85) {
		getElem('id', id, null).style.top = "85px";
	} else {
		getElem('id', id, null).style.top = getScrollXY()[1] + 85 + "px";
	}
	getElem('id', id, null).innerHTML = "<a href=\"javascript:hideLayer('" + id + "');\" onClick=\"hideLayer('" + id + "');hideBusyLayer();\"><span class=\"image\"><img src=" + src + " width=" + width + " height=" + height + " alt=\"\" /></span><span class=\"close\">&nbsp;</span></a>";
}

function showLargeVideo(count, src, width, height) {
	getElem('id', 'image_' + count, null).innerHTML = "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0\" width=\"" + width + "\" height=\"" + height + "\"><param name=\"src\" value=\"" + src + "\" /><param name=\"autoplay\" value=\"true\" /><param name=\"controller\" value=\"true\" /><param name=\"target\" value=\"myself\" /><param name=\"cache\" value=\"true\" /><param name=\"scale\" value=\"aspect\" /><embed src=\"" + src + "\" align=\"left\" scale=\"aspect\" width=\"" + width + "\" height=\"" + height + "\" target=\"myself\" pluginspage=\"http://www.apple.com/quicktime/download\" autoplay=\"true\" controller=\"true\" cache=\"true\" /></object>";
	getElem('id', 'caption_enlarge_' + count, null).innerHTML = "";
}

function showEmail(receipient, domain) {
	document.write(receipient + "@" + domain);
}

function showEmailLink(receipient, domain) {
	parent.location = "m" + "a" + "i" + "l" + "t" + "o" + ":" + receipient + "@" + domain;
}

function getX(oElement) {
	var iReturnValue = 0;
	while(oElement != null) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getY(oElement) {
	var iReturnValue = 0;
	while(oElement != null) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}