	function Trim(strText) { 
		// this will get rid of leading spaces 
		while (strText.substring(0,1) == ' ') 
			strText = strText.substring(1, strText.length);
		// this will get rid of trailing spaces 
		while (strText.substring(strText.length-1,strText.length) == ' ')
			strText = strText.substring(0, strText.length-1);
		return strText;
	} 
	function storeCaret(text)
	{ // voided
	}

	function InsertText(doc,startTag,defaultText,endTag) {
		if (doc.createTextRange) {
			var text;
			doc.focus(doc.caretPos);
			doc.caretPos = document.selection.createRange().duplicate();
			if(doc.caretPos.text.length>0) {
				var strText=doc.caretPos.text;
				if (strText.substring(strText.length-1,strText.length) == ' ') endTag+=" ";
				if (defaultText) doc.caretPos.text = startTag+defaultText+endTag;
				else doc.caretPos.text = startTag + Trim(doc.caretPos.text) + endTag;
			} else {
				doc.caretPos.text = startTag+defaultText+endTag;
			}
		} else if (doc.setSelectionRange) {
			var start=doc.selectionStart;
			var end=doc.selectionEnd;
			var instext;
			if (end>start) {
				var strText=doc.value.substr(start,end-start);
				if (strText.substring(strText.length-1,strText.length) == ' ') endTag+=" ";
				if (defaultText) instext=startTag+defaultText+endTag;
				else instext=startTag+Trim(strText)+endTag;
			} else {
				instext=startTag+defaultText+endTag;
			}
			doc.value=doc.value.substr(0,start)+instext+doc.value.substr(end);
			var newpos=start+instext.length;
			doc.setSelectionRange(newpos,newpos);
		}
		else doc.value += startTag+defaultText+endTag;
        doc.focus();
	}

	function ReplaceText(doc,replacement,all) {
		
	}

	function GetMarkedText(doc) {
		if (doc.createTextRange) {
			var text;
			doc.focus(doc.caretPos);
			doc.caretPos = document.selection.createRange().duplicate();
			if(doc.caretPos.text.length>0) return doc.caretPos.text;
		} else if (doc.setSelectionRange) {
			var start=doc.selectionStart;
			var end=doc.selectionEnd;
			var text;
			if (end==start) return '';
			return doc.value.substr(start,end-start);
		}
		return '';
	}

    function PromptKeyword(field) {
        var keyword = Trim(GetMarkedText(field));
		if (!keyword) keyword=prompt('Bitte Keywort eingeben', '');
        if (keyword) InsertText(field,'<stichwort '+keyword+'>',keyword,'</stichwort>');
        field.focus();
    }
    function PromptFileDB(field) {
        var value = Trim(GetMarkedText(field));
        var keyword   = prompt('Bitte ID der Datei angeben:', '');
        if (keyword) InsertText(field,'<FILEDB '+keyword+'>',value,'</FILEDB>');
        field.focus();
    }
    function PromptArtikel(field) {
        var value = Trim(GetMarkedText(field));
        var keyword   = prompt('Bitte ID des Artikels angeben:', '');
        if (keyword) InsertText(field,'<ARTLINK '+keyword+'>',value,'</ARTLINK>');
        field.focus();
    }
    function PromptExtlink(field) {
        var value = Trim(GetMarkedText(field));
        var keyword   = prompt('Bitte URL eingeben', 'http://');
        if (keyword) InsertText(field,'<EXTLINK '+keyword+'>',value,'</EXTLINK>');
        field.focus();
    }
    function AddText(field,text) {
        field.value+=text;
        field.focus();
    }

    function AddImage(field,alignment) {
        var small   = prompt('URL Thumbnail:', '');
        var big   = prompt('URL Grosse Grafik (optional):', '');
        if (small || big)  {
			var strText;
            strText='<cimg';
            if (alignment) strText+=' align='+alignment;
            if (small) strText+=' small=\"'+small+'\"';
            if (big) strText+=' big=\"'+big+'\"';
            strText+='>';
			InsertText(field,strText,'','');
        }
        field.focus();
    }


