function markSelection ( txtObj ) {
	if ( txtObj.createTextRange ) {
	  txtObj.caretPos = document.selection.createRange().duplicate();
	  isSelected = true;
	}
}

function insertTag ( myField, myStartTag, check ) {
    myField = eval ( "document.forms['form']." + myField );
	var myEndTag = myStartTag;
	
	var myStartTag = '<' + myStartTag + '>';
	var myEndTag = '</' + myEndTag + '>';
	
	if(document.selection) {
		// IE/OPERA support
		myField.focus();
		sel = document.selection.createRange();
		
		// Check if there are tags to be removed again
		if(check) {
			if(sel.text.substring(0, myStartTag.length) == myStartTag && sel.text.substring(sel.text.length-myEndTag.length, sel.text.length) == myEndTag) {
				sel.text = sel.text.substring(myStartTag.length, sel.text.length - myEndTag.length);
			} else {
				check = false;
			}
		}
		
		if(!check)
			sel.text = myStartTag + sel.text + myEndTag;

		sel.select();
	} else if(myField.selectionStart || myField.selectionStart == '0') {
		// MOZILLA/NETSCAPE support
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;

		// Check if there are tags to be removed again
		if(check) {
			if(myField.value.substring(startPos, myStartTag.length) == myStartTag && myField.value.substring(endPos - myEndTag.length, endPos) == myEndTag) {
				myField.value = myField.value.substring(0, startPos) + myField.value.substring(startPos+myStartTag.length, endPos-myEndTag.length) + myField.value.substring(endPos, myField.value.length);
				myField.focus();
				myField.selectionStart=startPos;
				myField.selectionEnd=endPos-myStartTag.length-myEndTag.length;
			} else {
				check = false;
			}
		}
		
		if(!check) {
			myField.value = myField.value.substring(0, startPos) + myStartTag + myField.value.substring(startPos, endPos) + myEndTag + myField.value.substring(endPos, myField.value.length);

			myField.focus();
			myField.selectionStart=startPos;
			myField.selectionEnd=endPos+myStartTag.length+myEndTag.length;
		}
	} else {
		// IF BOTH FAIL, GIVE OUT ERROR
		alert('Sorry, this feature is not supported by your browser =(');
	}
	
}

function insertEmoticon ( txtName, emoticon ) {
	if ( isSelected ) {
	  var txtObj = eval ( "document.forms['form']." + txtName );
	  if (txtObj.createTextRange && txtObj.caretPos) {
	    var caretPos = txtObj.caretPos;
	    caretPos.text = " "+emoticon+" "+caretPos.text;
    	markSelection ( txtObj );
    	if ( txtObj.caretPos.text=='' ) {
      		isSelected=false;
   			txtObj.focus();
    	}
  	  }
	} else {
  		// placeholder for loss of focus handler
	}
}

function refreshEmoticons() {
	var i;
	var d=document;
	var reg = /smiles.icon_/;
	for (i=0; i<d.images.length; i++) { 
		s = d.images[i].src; 
		if (reg.exec(s)) {
			d.images[i].src = s;
		}
	} 
}

function insertImage ( txtName ) {
	if ( isSelected ) {
	  var txtObj = eval ( "document.forms['form']." + txtName );
	  if (txtObj.createTextRange && txtObj.caretPos) {
	    var caretPos = txtObj.caretPos;
	    caretPos.text = '<img src="'+caretPos.text+'" border="0">';
    	markSelection ( txtObj );
    	if ( txtObj.caretPos.text=='' ) {
      		isSelected=false;
   			txtObj.focus();
    	}
  	  }
	} else {
  		// placeholder for loss of focus handler
	}
}

function changeInfotxt ( infotxt ) {
	document.forms['form'].info_txt.value = infotxt;
}

function resetInfotxt () {
	document.forms['form'].info_txt.value = '';
}

// Popup
function openWindow(theURL,winName,features) {
  open(theURL,winName,features);
}
