function loadFace(showid,target)
{
	var obj = $("#facebox");

	if( obj.css('display') == "none" )
	{	
		var faceul = document.createElement("ul");
		for(i=1; i<31; i++) {
			var faceli = document.createElement("li");
			faceli.innerHTML = '<img src="./_static/images/face/'+i+'.gif" onclick="insertFace(\''+showid+'\','+i+', \''+ target +'\')" style="cursor:pointer; position:relative;" />';
			faceul.appendChild(faceli);
		}

		obj.slideDown("fast",function(){obj.html(faceul);});
	}
	else
	{
		obj.slideUp("fast",function(){obj.html("");});
	}
}

function insertFace(showid, id, target) {
	var obj = $("#facebox");
	var faceText = '[em:'+id+':]';
	if($(target) != null) {
		insertContent(target, faceText);
	}
}

function insertContent(target, text) {
	var obj = document.getElementById(target);
	selection = document.selection;
	checkFocus(target);
	if(!isUndefined(obj.selectionStart)) {
		var opn = obj.selectionStart + 0;
		obj.value = obj.value.substr(0, obj.selectionStart) + text + obj.value.substr(obj.selectionEnd);
	} else if(selection && selection.createRange) {
		var sel = selection.createRange();
		sel.text = text;
	} else {
		obj.value += text;
	}
}

function checkFocus(target) {
	var obj = document.getElementById(target);
	if(!obj.hasfocus) {
		obj.focus();
	}
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
