function insertImageLink(guid, textfield, title) {
	insertAtCursor(textfield, "[img=" + guid + ";align=left;title="+title+"]");
	return false;
}
function insertTypedLink(guid, textfield, dtype, title) {
	insertAtCursor(textfield, "[link=" + guid + ";type=" + dtype + ";title="+title+"]");
	return false;
}

String.prototype.endsWith = function(str) {
	return (this.match(str + "$") == str);
}

String.prototype.trim = function() {
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

function insertYoutube(textfield) {
	var my_string = prompt("Geef de URL van het filmpje");
	if (my_string) {
		//  now test if the url is ok.
		var pos = my_string.indexOf('v=');
		var extra = 2;
		if (pos == -1) {
			// Retry with youtu.be link
			pos = my_string.indexOf('youtu.be/');
			extra = 9;
		}		
		if (pos>0) {
			my_string = my_string.substring(pos+extra); 
			insertAtCursor(textfield, '[youtube='+my_string+']');
		} else {
			alert("URL niet herkend als youtube url.");
		}		
	}
}

function insertAtCursor(myField, myValue) {
	var element = document.getElementById(myField);

	// IE support
	if (document.selection) {
		element.focus();
		// in effect we are creating a text range with zero
		// length at the cursor location and replacing it
		// with myValue
		sel = document.selection.createRange();
		sel.text = myValue;

		// Mozilla/Firefox/Netscape 7+ support
	} else if (element.selectionStart || element.selectionStart == '0') {
		// Here we get the start and end points of the
		// selection. Then we create substrings up to the
		// start of the selection and from the end point
		// of the selection to the end of the field value.
		// Then we concatenate the first substring, myValue,
		// and the second substring to get the new value.
		var startPos = element.selectionStart;
		var endPos = element.selectionEnd;
		element.value = element.value.substring(0, startPos) + myValue
				+ element.value.substring(endPos, element.value.length);
		element.setSelectionRange(endPos + myValue.length, endPos
				+ myValue.length);
	} else {
		element.value += myValue;
	}
}

function surroundWithTags(elemId, openTag, closeTag) {
	var element = document.getElementById(elemId);

	if (document.selection) {
		// Internet exploder
		var range = document.selection.createRange();
		var stored_range = range.duplicate();
		stored_range.moveToElementText(element);
		stored_range.setEndPoint('EndToEnd', range);

		// Now we can calculate start and end points
		element.selectionStart = stored_range.text.length - range.text.length;
		element.selectionEnd = element.selectionStart + range.text.length;

		var selection = element.value.substr(element.selectionStart,
				element.selectionEnd - element.selectionStart);

		if (selection.endsWith(" ")) {
			selection = selection.trim();
			closeTag = closeTag + ' ';
		}
		range.text = openTag + selection + closeTag;

	} else {
		// Normal browsers
		if (element.selectionStart != undefined) {
			var begin = element.value.substr(0, element.selectionStart);
			var selection = element.value.substr(element.selectionStart,
					element.selectionEnd - element.selectionStart);
			var after = element.value.substr(element.selectionEnd);
			if (selection.endsWith(" ")) {
				selection = selection.trim();
				after = " " + after;
			}
			var scrollTop = element.scrollTop;
			element.value = begin + openTag + selection + closeTag + after;
			element.scrollTop = scrollTop;
		}
	}
	return false;
}

function resizeTextArea(id) {
	var elem = document.getElementById(id);
	if (elem) {
		if (elem.rows != 24) {
			elem.rows = 24;
		} else {
			elem.rows = 48;
		}
	}
}

