//v1.0.0



//
//get text in selection range
//
function GetRange(toElement)
{
	var range;
	if(typeof(toElement) != 'object'){toElement = document.getElementById(toElement);}
	toElement.focus();
	
	//IE +------------------------------------
	if (document.selection)
	{
		range = document.selection.createRange().text;
	}
	
	//all others +----------------------------
	else if(toElement.selectionStart || toElement.selectionStart == 0)
	{
		range = toElement.value.substring(toElement.selectionStart, toElement.selectionEnd) + "";
	}
	
	//no luck +-------------------------------
	else
	{
		range = null;
	}
	return range;
}



//
//set text in selection range
//
function SetRange(toElement, txt)
{
	if(typeof(toElement) != 'object'){toElement = document.getElementById(toElement);}
	var sStart, sEnd;
	toElement.focus();
	
	//IE +------------------------------------
	if (document.selection)
	{
		var objRange = document.selection.createRange();
		objRange.text = txt;
	}
	
	//all others +----------------------------
	else if(toElement.selectionStart || toElement.selectionStart == 0)
	{
		//get start and end points
		sStart = toElement.selectionStart;
		sEnd = toElement.selectionEnd;
		//set new text and set selection
		toElement.value = toElement.value.substring(0, sStart) + txt + toElement.value.substr(sEnd);
		toElement.selectionStart = sEnd + (txt.length - (sEnd - sStart));//set selection to no length after updated content
		toElement.selectionEnd = sEnd + (txt.length - (sEnd - sStart));
	}
	toElement.focus();
}



//
//insert opening and closing tags around range
//
function InsertTag(toElement, tagOpen, tagClose)
{
	if(typeof(toElement) != 'object'){toElement = document.getElementById(toElement);}
	SetRange(toElement, tagOpen + GetRange(toElement) + tagClose);
}



function AddClientLink(toElement)
{
	var hr = prompt('Enter the full path (URL) for the link:', '');
	if(hr != null)
	{
		InsertTag(toElement, '\r\n[link]' + hr + '[/link]\r\n', '');
	}
}



function AddClientImage(toElement, img)
{
	if(typeof(toElement) != 'object'){toElement = document.getElementById(toElement);}
	InsertTag(toElement, '\r\n' + img + '\r\n', '');
}



//
//rate activity
//
function Rate(userId, activityId, rating)
{
	ById('gIframe').src = 'a_rate.php?usid=' + userId + '&atid=' + activityId + '&rating=' + rating;
}







