

function addhtml(object,tag,directory)
{

	var html = "";
	// Get Selected text (if any)
	html = getSelected(document.getElementById(object));
	var begin = "";
	var end = "";
	var url = "";
	var alttag = "";
	var default_text = "Your text goes here";
	var temp = "";
	
	if(!directory) directory = '';
	
	switch (tag) {
	case "ol":
	case "ul":
		if(html)
		{
			var listHTML = "";
			var list = html.split('\n');
			for (x in list)
			{
				if(list[x].length>0) listHTML += "<li>"+list[x]+"<\/li>\n";
			}
			html = "<"+tag+">\n" + listHTML + "<\/"+tag+">\n";
		}
		if(!html)
		{
			var num = prompt("Please enter the number of list items you want to create.","1")
			if ( isNaN(num) || num < 1 ) { return false; }
			var list = new Array()
			for ( var i = 1; i <= num; i++ ) {
				list[i] = prompt("Please enter a list item:",default_text)
				html = html + "\t<li>" + list[i] + "<\/li>\n"
			}
			html = "\n<"+tag+">\n" + html + "<\/"+tag+">\n"
		}
	break
	case "img":
		url = prompt("Please enter the full URL of this image, including the http:// for best results:","http://");
		if(!url) return false;
		alttag = prompt("Please enter a SHORT description of this image:",default_text);
		if(!alttag) return false;
		html = " <img src=\"" + directory + url + "\" alt=\"" + alttag + "\" /> " ;
	break
	case "a":
		clicktext = html;
		url = prompt("Please enter the URL for this link:\n (http://www.example.com)","http://");
		if(!url) return false;
		if(!clicktext)
		{
			clicktext = prompt("Please enter the CLICKABLE text for this link:","");
			if(!clicktext) return false;
		}
		alttag = prompt("Please enter a SHORT description of this link:",default_text);
		if( url && clicktext ) html = " <a href=\"" + url + "\" title=\"" + alttag + "\">" + clicktext + "<\/a> ";
	break
	case "p":
	case "pc":
	case "pr":
	case "h2":
	case "h3":
	case "h4":
	case "strong":
		if(!html) {
			html = prompt("Please enter some text:",default_text)
			if(!html) return false;
		}
		if(tag=="em") { html = "<em>" + html + "<\/em>"; }
		if(tag=="strong") { html = "<strong>" + html + "<\/strong>"; }
		if(tag=="p") { html = "<p>" + html + "<\/p>"; }
		if(tag=="h2") { html = "<h2>" + html + "<\/h2>"; }
		if(tag=="h3") { html = "<h3>" + html + "<\/h3>"; }
		if(tag=="h4") { html = "<h4>" + html + "<\/h4>"; }
		if(tag=="pc") { html = "<p style='text-align:center;'>" + html + "<\/p>"; }
		if(tag=="pr") { html = "<p style='text-align:right;'>" + html + "<\/p>"; }
	break
	case "br":
		html += "<br \/>"
	break
	case "lorem":
		html = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin cursus congue diam. Donec aliquet purus ut felis. Nullam ultrices condimentum nisi. Aenean tincidunt. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin cursus congue diam. Donec aliquet purus ut felis. Nullam ultrices condimentum nisi. Aenean tincidunt. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin cursus congue diam. Donec aliquet purus ut felis. Nullam ultrices condimentum nisi. Aenean tincidunt.\n"
	break
	}	
	
	insertAtCursor( document.getElementById(object) , html , begin , end )
	return false;
	
}


function getSelected(myField) 
{
	var myText = '';
	//IE support
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		myText = sel.text;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myText = myField.value.substring(startPos,endPos);
	}
	return myText;
}


function insertAtCursor(myField, myValue) 
{
	//IE support
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

function addTags(id)
{
	var object = document.getElementById(id);
	var html = object.value;
	var temp = html.split('\n');
	var newtext = "";
	for (x in temp)
	{
		if( temp[x].length>0 ) {
			if( temp[x].indexOf('<p>')==-1 ) {
				newtext += "<p>"+temp[x]+"<\/p>\n\n";
			} else {
				newtext += temp[x]+"\n\n";
			}
		}
	}
	object.value = newtext;
	return false;
}

function removeTags(id)
{
	var object = document.getElementById(id);
	var html = object.value;
	html = html.replace(/\<p\>/g, '' );
	html = html.replace(/\<\/p\>/g, '' );
	object.value = html;
	return false;
}