
// Takes three args: element, open tag (string), and close tag (string). Wraps the selected text in  the open and close tags (anything that formats as (openTag)Text(closeTag))

function wrapText(el, openTag, closeTag) {
	if (el.setSelectionRange) {
 		// W3C/Mozilla
 		el.value = el.value.substring(0,el.selectionStart) + openTag + el.value.substring(el.selectionStart,el.selectionEnd) + closeTag + el.value.substring(el.selectionEnd,el.value.length);
		document.el.closeTag.focus();
 	}
 	else if (document.selection && document.selection.createRange) {
 		// IE code goes here
		el.focus(); //or else text is added to the activating control
		var range = document.selection.createRange();
		range.text = openTag + range.text + closeTag;
		document.el.closeTag.focus();
 	}
}

// Uses the function wrapText to wrap selected text in a given element with a link tag. Prompts the user to type or paste in the URL which is used as the href attribute.
function make_link(el) {
	url = prompt("Type the URL or address of the page you would like to link to.","");
	
	openTag = '<a href="' + url + '">';
	
	closeTag = '</a>';
	
	wrapText(el,openTag,closeTag);
}

// Uses the function wrapText to wrap selected text in a given element with a link tag. Prompts the user to type or paste in the URL which is used as the href attribute.
function make_video(el) {
	url = prompt("Type the URL or address of the page you would like to link to.","");
	
	openTag = '<video href="' + url + '">';
	
	closeTag = '</video>';
	
	wrapText(el,openTag,closeTag);
}

/*
	----- ABSTRACTED TEXT EDITOR FUNCTIONS -----
	
	These functions make use of wrapText to format selected text in an editable page element with the given tags.
*/

function make_bold(el) {
	wrapText(el,"<b>","</b>");
}

function make_italic(el) {
	wrapText(el,"<i>","</i>");
}

function make_underline(el) {
	wrapText(el,"<u>","</u>");
}

function make_strikethrough(el) {
	wrapText(el,"<del>","</del>");
}

function make_superscript(el) {
	wrapText(el,"<sup>","</sup>");
}

function make_subscript(el) {
	wrapText(el,"<sub>","</sub>");
}
