function get_selection(obj) 
{
	var str = new String("");
	if (window.getSelection)
	{
		var startPos = obj.selectionStart;
		var endPos = obj.selectionEnd;
		str = obj.value.substring(startPos,endPos);
	} 
	else if (document.selection && document.selection.createRange)
	{ 
		var range = document.selection.createRange();
		str = range.text.toString();
	} 
	else
	{ 
		//alert("Sorry, event capture is not possible with your browser.");
	}
	// remove non-alphanumeric stuff from the string
	str = str.replace(/[^a-zA-Z 0-9]+/g,'');
	return str;
}

function createlink()
{
	var ta = document.getElementById("ta");
	
	var startPos = ta.selectionStart;
	var endPos = ta.selectionEnd;
	
	var text = get_selection(ta);
	text = text.replace( /\n|\r|<br \/>/g,'');
	
	var url = prompt("Please enter the address for this link.",'http://');
	if(!url || url == null || url == 'http://')
	{
		return false;
	}
	text=text?text:prompt("Please enter a title for this link.","");
	if(!text || text==null)
		return false;
	
	insertAtCursor(ta," <a href=\""+url+"\">"+text+"</a> ");
}

function insertAtCursor(myField, myValue)
{
	if(document.selection)
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	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 popup(mylink, windowname)
{
	if(!window.focus)return true;
	var href;
	if(typeof(mylink) == 'string')
	   href = mylink;
	else
	   href = mylink.href;
	window.open(href, windowname, 'width=500,height=300,scrollbars=yes');
	return false;
}
