function getElem(id){
	return document.getElementById(id);
}

function showElem(id, display){
	element = getElem(id);
	if(!display)
		display = 'block';
	if(element)
		element.style.display = display;
}

function hideElem(id){
	element = getElem(id);
	if(element)
		element.style.display = 'none';
}

function restricttextarea(textarea, maxlength){
	if(textarea.value.length > maxlength)
		textarea.value = textarea.value.substring(0, maxlength);
}

function getTextareaCursorPosition(textarea) {
	var cursorPos = 0;
	if(document.selection && document.selection.createRange){
    var range = document.selection.createRange();
		range.collapse(false);
    var rangeOver = range.duplicate();
    rangeOver.moveToElementText(textarea);
    range.text = '#RANGETAG#';
    cursorPos = rangeOver.text.indexOf('#RANGETAG#');
    range.text = '';
    textarea.value = textarea.value.split('#RANGETAG#').join('');
    if(cursorPos > 0)
			setTextareaCursorPosition(textarea, cursorPos);
	}else if(textarea.selectionEnd)
		cursorPos = textarea.selectionEnd;
	if(cursorPos < 0 || cursorPos > textarea.value.length)
		cursorPos = 0;
	return cursorPos;
}

function setTextareaCursorPosition(textarea, cursorPos) {
	if(textarea.createTextRange){
		var range = textarea.createTextRange();
		range.collapse(true);
		cursorPos = cursorPos - textarea.value.substring(0, cursorPos).split('\r').length + 1;
		range.moveEnd('character', cursorPos);
		range.moveStart('character', cursorPos);
		range.select();
	}else if (textarea.selectionEnd){
		textarea.selectionStart = cursorPos;
		textarea.selectionEnd = cursorPos;
	}
	textarea.focus();
}

function insertTextareaAtCursorPosition(textarea, text){
	var textareaScrollTop = textarea.scrollTop;
	var cursorPos = getTextareaCursorPosition(textarea);
	if(cursorPos == textarea.value.length)
		textareaScrollTop = textarea.scrollHeight;
	var textareaValue = textarea.value;
	textarea.value = textareaValue.substring(0, cursorPos) + text + textareaValue.substring(cursorPos);
	textarea.focus();
	setTextareaCursorPosition(textarea, cursorPos + text.length);
	textarea.scrollTop = textareaScrollTop;
}