var isSelected
function markSelection ( txtObj ) {
 if ( txtObj.createTextRange ) {
   txtObj.caretPos = document.selection.createRange().duplicate();
   isSelected = true;
 }
}

function insertTag ( txtName, tag, enclose ) {
 var closeTag = tag;
 if ( enclose ) {
   var attribSplit = tag.indexOf ( ' ' );
   if ( tag.indexOf ( ' ' ) > -1 )
     closeTag = tag.substring ( 0, attribSplit );
 }
 if ( isSelected ) {
   var txtObj = eval ( "document.form1." + txtName );
   if (txtObj.createTextRange && txtObj.caretPos) {
     var caretPos = txtObj.caretPos;
     caretPos.text = ( ( enclose ) ? "<"+tag+">"+caretPos.text+"</"+closeTag+">" : "<"+tag+">"+caretPos.text );
     markSelection ( txtObj );
     if ( txtObj.caretPos.text=='' ) {
       isSelected=false;
    txtObj.focus();
     }
   }
 } else {
   // placeholder for loss of focus handler
 }
}

function insertTagHRef ( txtName) {
 if ( isSelected ) {
   var txtObj = eval ( "document.form1." + txtName );
   if (txtObj.createTextRange && txtObj.caretPos) {
     var caretPos = txtObj.caretPos;
     caretPos.text = "<a target=_New class=under href="+caretPos.text+">"+caretPos.text+"</a>";
     markSelection ( txtObj );
     if ( txtObj.caretPos.text=='' ) {
       isSelected=false;
    txtObj.focus();
     }
   }
 } else {
   // placeholder for loss of focus handler
 }
}