var strFontColorCode = '#000000';
var strBrowser = getbrowser();

function getbrowser()
{
	var browser = "" ;
	window.env = new function()
	{
		this.Opera	= (window.opera && navigator.userAgent.match(/opera/gi)) ? true : false;
		this.IE		= (!this.Opera && document.all&&navigator.userAgent.match(/msie/gi)) ? true : false;
		this.Safari	= (!this.IE&&navigator.userAgent.match(/safari/gi)) ? true : false;
		this.Gecko	= (!this.IE&&navigator.userAgent.match(/gecko/gi)) ? true : false;
		this.Firefox= (!this.IE&&navigator.userAgent.match(/firefox/gi)) ? true : false;			
	};
	for (var i in env) {
		if (env[i])
		{
			browser = i;
		}
	}
	if (browser == "")
	{
		browser = "IE";
	}
	return browser;
}

function insertText(arg_objTextarea,arg_strCmd)
{
	var l_strNewUserInputText = "";
	var l_strUserInputText = "";
	var l_strUserInputColor = "";
	switch (arg_strCmd)
	{					
		case 'bold':
		{						
			l_strUserInputText = new String(prompt("請輸入字欲設成粗體的文字", ''))
			if (l_strUserInputText != "null")
			{
				l_strNewUserInputText = "<b>"+l_strUserInputText+"</b>";
			}
		}
		break;
		case 'italic':
		{
			l_strUserInputText = new String(prompt("請輸入字欲設成斜體的文字", ''))
			if (l_strUserInputText != "null")
			{
				l_strNewUserInputText = "<i>"+l_strUserInputText+"</i>";
			}
		}
		break;
		case 'underline':
		{
			l_strUserInputText = new String(prompt("請輸入字欲設成底線的文字", ''))
			if (l_strUserInputText != "null")
			{
				l_strNewUserInputText = "<u>"+l_strUserInputText+"</u>";
			}
		}
		break;
		case 'setcolor':
		{
			l_strUserInputText = new String(prompt("請輸入文字", ''))
			if (l_strUserInputText != "null")
			{
				l_strNewUserInputText = "<font color=\""+strFontColorCode+"\">"+l_strUserInputText+"</font>";				
			}
		}
		break;
		case 'hyperlink':
		{
			l_strUserInputText = new String(prompt("請輸入鏈結位置", 'http://'))
			if (l_strUserInputText != "null")
			{
				l_strNewUserInputText = "<a href=\""+l_strUserInputText+"\" target='_blank' >"+l_strUserInputText+"</a>";
			}
		}
		break;
		case 'image':
		{
			l_strUserInputText = new String(prompt("請輸入圖片鏈結位置", 'http://'))
			if (l_strUserInputText != "null")
			{
				l_strNewUserInputText = "<img src=\""+l_strUserInputText+"\">";
			}
		}
		break;
	}	
	fetch_object(arg_objTextarea).focus();
	if ((l_strUserInputText != "null") && (l_strUserInputText != "undefined"))
	{
		insertTextAtIndicator(l_strNewUserInputText);		
	}				
}

function insertEmoticon(arg_strCode)
{
	insertTextAtIndicator(arg_strCode);
	fetch_object('editor_textarea').focus();
}
	
function recordTheIndicator(arg_objTextarea)
{				
	if(strBrowser == "IE") //for ie
	{
		if (arg_objTextarea.createTextRange())
		{
			arg_objTextarea.caretPos = document.selection.createRange().duplicate();				
		}
	}
}

function insertTextAtIndicator(arg_strNewText)
{
	var objTextarea = fetch_object('editor_textarea')
	if(strBrowser == "IE") //for ie
	{	
		if (objTextarea.createTextRange() && objTextarea.caretPos) 
		{
			objTextarea.caretPos.text = objTextarea.caretPos.text.charAt(objTextarea.caretPos.text.length - 1) == '' ? arg_strNewText + '' : arg_strNewText;
		} 
		else 
		{
			objTextarea.value += arg_strNewText; 
		}
	}
	else  //for other browser ex. Firefox,Safari,Opera
	{	
		if (typeof(objTextarea.selectionStart) == "undefined")
		{
			objTextarea.value += arg_strNewText;
		} 
		else 
		{
			var strCaretPos = objTextarea.selectionStart;
			objTextarea.value = objTextarea.value.substr(0, strCaretPos) + arg_strNewText + objTextarea.value.substr(strCaretPos);
			objTextarea.selectionStart = strCaretPos + arg_strNewText.length;
			objTextarea.selectionEnd = objTextarea.selectionStart;
		}
	}
}

function fetch_object(strElementID)
{
	return document.getElementById(strElementID);
}
