var globalCursorPos, str; // global variabe to keep track of where the cursor was
var flg = false;

function getEventTrigger(thisinput){
	document.getElementById("flag").value="true";
	str=document.selection.createRange().text;
	//alert("The id of the triggered element: " + thisinput.id);
}

function getSelectedText(set){

 var $tb = document.getElementById("PGCONTENT");
 var flag = document.getElementById("flag").value;
 var start = $tb.selectionStart;
 var end = $tb.selectionEnd;

   if (document.selection && (flag=="true" || flg==true)){
   	document.getElementById("flag").value="none";
	
	if(str!=""){
	 //alert(str);
      var sel=document.selection.createRange();
	  if (sel.text!="")
	  switch (set){
	  	case 'b':
			sel.text="<b>"+str+"</b>";
			break;
		case 'i':
			sel.text="<i>"+str+"</i>";
			break;
		case "table":
			sel.text="<table><tr><td>"+str+"</td></tr></table>";
			break;
		case "tr":
			sel.text="<tr><td>"+str+"</td></tr>";
			break;
		case "td":
			sel.text="<td>"+str+"</td>";
			break;
		case "center":
			sel.text="<center>"+str+"</center>";
			break;
		case "p":
			sel.text="<p>"+str+"</p>";
			break;
		case "br":
			sel.text=str+"</br>";
			break;
        default: 
            sel.text=str+set;
	  }
	  str="";
	 
	 }
	 else {
   	   insertString(set);
	 }
	 
   }
   
   else if (typeof $tb.selectionStart != 'undefined' /*&& start!=end*/){
      var $before, $after, $selection;
      $before= $tb.value.substring(0, $tb.selectionStart);
      $selection = $tb.value.substring($tb.selectionStart, $tb.selectionEnd);
      $after = $tb.value.substring($tb.selectionEnd, $tb.value.length);
 
 	 switch (set){
	  	case 'b':
			$tb.value= String.concat($before, "<b>", $selection, "</b>", $after);
			break;
		case 'i':
			$tb.value= String.concat($before, "<i>", $selection, "</i>", $after);
			break;
		case 'table':
			$tb.value= String.concat($before, "<table><tr><td>", $selection, "</td></tr></table>", $after);
			break;
		case 'tr':
			$tb.value= String.concat($before, "<tr><td>", $selection, "</td></tr>", $after);
			break;
		case 'td':
			$tb.value= String.concat($before, "<td>", $selection, "</td>", $after);
			break;
		case 'center':
			$tb.value= String.concat($before, "<center>", $selection, "</center>", $after);
			break;
		case 'p':
			$tb.value= String.concat($before, "<p>", $selection, "</p>", $after);
			break;
		case 'br':
			$tb.value= String.concat($before, $selection, "</br>", $after);
			break;
        default:
            $tb.value= String.concat($before, $selection, set, $after);
	  }
     
  }
  
  $tb.focus();
  
 }

//sets the global variable to keep track of the cursor position
function setCursorPos(thisform) {
 globalCursorPos = getCursorPos(thisform.PGCONTENT);
 flg = true;
}


function getCursorPos(textElement) {
 if(str==""){
 var sOldText = textElement.value;

//create a range object and save off it's text
 var objRange = document.selection.createRange();
 var sOldRange = objRange.text;

//set this string to a small string that will not normally be encountered
 var sWeirdString = '#%~';

//insert the weirdstring where the cursor is at
 objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

//save off the new string with the weirdstring in it
 var sNewText = textElement.value;

//set the actual text value back to how it was
 objRange.text = sOldRange;

//look through the new string we saved off and find the location of
//the weirdstring that was inserted and return that value
 for (i=0; i <= sNewText.length; i++) {
   var sTemp = sNewText.substring(i, i + sWeirdString.length);
   if (sTemp == sWeirdString) {
     var cursorPos = (i - sOldRange.length);
     return cursorPos;
   }
 }
 
}

}

//this function inserts the input string into the textarea
//where the cursor was at
function insertString(stringToInsert) {
	if(flg==true && typeof(globalCursorPos)!="undefined"){
		//alert(flg+" - "+globalCursorPos);
		var firstPart = document.getElementById("PGCONTENT").value.substring(0, globalCursorPos);
		var secondPart = document.getElementById("PGCONTENT").value.substring(globalCursorPos, document.getElementById("PGCONTENT").value.length);
		//alert(firstPart+" - "+secondPart);
		document.getElementById("PGCONTENT").value = firstPart + stringToInsert + secondPart;
	}
}