function textInput(id, textid, container, insbef, text, lang, savescript, minheight, absoluteElement, prefix, allowhtml, imghandler, colorhandler, linkhandler, help) {
	var base = document.createElement("div");
	var ie = (document.all)?true:false;
	base.id = id;
	base.lang = lang;
	base.textid = textid;
	base.container = container;
	base.savescript = savescript;
	base.minheight = minheight;
	//base.prefix = prefix;
	base.allowhtml = allowhtml;
	base.imghandler = (imghandler)?imghandler:defImgHandler;
	base.colorhandler = (colorhandler)?colorhandler:defColorHandler;
	base.linkhandler = (linkhandler)?linkhandler:defLinkHandler;
	base.htmlmode = false;
	base.absel = absoluteElement;
	if(help) {
		base.tid = "helptext_" + base.textid;
	} else {
		base.tid = "d_text_" + base.textid;
	}
	base.setEdit = setEdit;
	base.resetText = resetText;
	base.saveText = saveText;
	base.isAltered = isAltered;
	base.destroy = destroy;
	base.toggleHtml = toggleHtml;
	base.setTextImage = setTextImage;
	base.setTextLink = setTextLink;
	base.setColor = setColor;

	if(insbef) {
		base.container.insertBefore(base, insbev);
	} else {
		base.container.appendChild(base);
	}
	base.style.marginBottom = "10px";

	// controls bar

	base.ctable = document.createElement("table");
	base.appendChild(base.ctable);
	base.ctable.className = "textinput_control_table";
	base.ctable.style.width = "100%";

	base.tbod = document.createElement("tbody");
	base.ctable.appendChild(base.tbod);

	base.topr = document.createElement("tr");
	base.tbod.appendChild(base.topr);

	base.cbar = document.createElement("td");
	base.topr.appendChild(base.cbar);
	base.cbar.style.textAlign = "left";
	base.cbar.style.verticalAlign = "top";
	


	// cbar (1)

	//controls
	base.cbar.flg = document.createElement("img");
	base.cbar.appendChild(base.cbar.flg);
	base.cbar.flg.src = prefix + "/images/flags/" + lang + ".gif";
	base.cbar.flg.style.height = "16px";
	base.cbar.flg.style.margin = "2px";
	base.cbar.flg.style.marginRight = "15px";
	base.cbar.flg.align = "left";

	// bold
	base.cbar.ibld = document.createElement("img");
	base.cbar.appendChild(base.cbar.ibld);
	base.cbar.ibld.className = "textinput_cpic";
	base.cbar.ibld.src = prefix + "/images/textinput/bold_s.png";
	base.cbar.ibld.style.height = "16px";
	base.cbar.ibld.style.margin = "2px";
	base.cbar.ibld.align = "left";
	base.cbar.ibld.onclick = function() {
		base.cframe.contentWindow.document.execCommand("bold", false, null);
	}

	//italic
	base.cbar.iitl = document.createElement("img");
	base.cbar.appendChild(base.cbar.iitl);
	base.cbar.iitl.className = "textinput_cpic";
	base.cbar.iitl.src = prefix + "/images/textinput/italic_s.png";
	base.cbar.iitl.style.height = "16px";
	base.cbar.iitl.style.margin = "2px";
	base.cbar.iitl.align = "left";
	base.cbar.iitl.onclick = function() {
		base.cframe.contentWindow.document.execCommand("italic", false, null);
	}

	//underlined
	base.cbar.iudl = document.createElement("img");
	base.cbar.appendChild(base.cbar.iudl);
	base.cbar.iudl.className = "textinput_cpic";
	base.cbar.iudl.src = prefix + "/images/textinput/und_s.png";
	base.cbar.iudl.style.height = "16px";
	base.cbar.iudl.style.margin = "2px";
	base.cbar.iudl.align = "left";
	base.cbar.iudl.onclick = function() {
		base.cframe.contentWindow.document.execCommand("underline", false, null);
	}

	//strike
	base.cbar.istr = document.createElement("img");
	base.cbar.appendChild(base.cbar.istr);
	base.cbar.istr.className = "textinput_cpic";
	base.cbar.istr.src = prefix + "/images/textinput/strike.png";
	base.cbar.istr.style.height = "16px";
	base.cbar.istr.style.margin = "2px";
	base.cbar.istr.style.marginRight = "10px";
	base.cbar.istr.align = "left";
	base.cbar.istr.onclick = function() {
		base.cframe.contentWindow.document.execCommand("strikethrough", false, null);
	}

	//align left
	base.cbar.ialt = document.createElement("img");
	base.cbar.appendChild(base.cbar.ialt);
	base.cbar.ialt.className = "textinput_cpic";
	base.cbar.ialt.src = prefix + "/images/textinput/aleft_s.png";
	base.cbar.ialt.style.height = "16px";
	base.cbar.ialt.style.margin = "2px";
	base.cbar.ialt.align = "left";
	base.cbar.ialt.onclick = function() {
		base.cframe.contentWindow.document.execCommand("justifyleft", false, null);
	}

	//centered
	base.cbar.icnt = document.createElement("img");
	base.cbar.appendChild(base.cbar.icnt);
	base.cbar.icnt.className = "textinput_cpic";
	base.cbar.icnt.src = prefix + "/images/textinput/cent_s.png";
	base.cbar.icnt.style.height = "16px";
	base.cbar.icnt.style.margin = "2px";
	base.cbar.icnt.align = "left";
	base.cbar.icnt.onclick = function() {
		base.cframe.contentWindow.document.execCommand("justifycenter", false, null);
	}

	//align right
	base.cbar.iart = document.createElement("img");
	base.cbar.appendChild(base.cbar.iart);
	base.cbar.iart.className = "textinput_cpic";
	base.cbar.iart.src = prefix + "/images/textinput/aright_s.png";
	base.cbar.iart.style.height = "16px";
	base.cbar.iart.style.margin = "2px";
	base.cbar.iart.style.marginRight = "10px";
	base.cbar.iart.align = "left";
	base.cbar.iart.onclick = function() {
		base.cframe.contentWindow.document.execCommand("justifyright", false, null);
	}



	// controls right top

	base.cbar_r = document.createElement("td");
	base.topr.appendChild(base.cbar_r);
	base.cbar_r.style.textAlign = "right";
	base.cbar_r.style.verticalAlign = "top";
	base.cbar_r.style.width = "240px";

	//status gif
	base.cbar_r.stgf = document.createElement("img");
	base.cbar_r.appendChild(base.cbar_r.stgf);
	base.cbar_r.stgf.src = prefix + "/images/textinput/static.gif";
	base.cbar_r.stgf.style.margin = "5px";
	base.cbar_r.stgf.align = "left";

	
	// reset
	base.cbar_r.rsbut = document.createElement("button");
	base.cbar_r.rsbut.className = "textinput_button";
	base.cbar_r.appendChild(base.cbar_r.rsbut);
	base.cbar_r.rsbut.innerHTML = 'reset';
	base.cbar_r.rsbut.onclick = function () {
		base.resetText();
	}

	function resetText(mode) {
		if(base.htmlmode) {
			base.hwind.value = base.otext;
		} else {
			document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML = base.otext;
		}
		if(document.getElementById(base.tid)) {
			document.getElementById(base.tid).innerHTML = base.ostext;
		}
		if(mode == "closeall") {
			checkTextSave(base.textid);
		}
	}

	// preview
	base.cbar_r.pvbut = document.createElement("button");
	base.cbar_r.pvbut.className = "textinput_button";
	base.cbar_r.appendChild(base.cbar_r.pvbut);
	base.cbar_r.pvbut.innerHTML = 'preview';
	base.cbar_r.pvbut.onclick = function () {
		if(document.getElementById(base.tid)) {
			/*
			if(!base.ostext) { 
				document.getElementById(base.tid).innerHTML;
			}
			*/
			if(base.htmlmode) {
				var ntxt = base.hwind.value;
			} else {
				var ntxt = document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML;
			}
			document.getElementById(base.tid).innerHTML = ntxt;
		}
	}

	// save
	base.cbar_r.svbut = document.createElement("button");
	base.cbar_r.svbut.className = "textinput_button";
	base.cbar_r.appendChild(base.cbar_r.svbut);
	base.cbar_r.svbut.innerHTML = 'save';
	base.cbar_r.svbut.onclick = function () {
		base.saveText();
	}

	function saveText(mode) {
		base.exit = (mode)?mode:false;
		base.cbar_r.stgf.src = prefix + "/images/textinput/loading.gif";
		if(base.htmlmode) {
			var nwtxt = base.hwind.value;
		} else {
			var nwtxt = document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML;
		}
		nwtxt = (nwtxt == "<br>\t")?"":nwtxt;
		var nwtxt = encodeURIComponent(nwtxt);

		var content = "content="+nwtxt;

		var xmlHttp;
		try {
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4) {
				if(xmlHttp.responseText === 'true') {
					base.cbar_r.stgf.src = prefix + "/images/textinput/static.gif";
					base.otext = decodeURIComponent(nwtxt);
					base.ostext = decodeURIComponent(nwtxt);
					if(base.exit) {
						if(base.exit == "closeall") {
							checkTextSave(base.textid);
						} else {
							base.parentNode.removeChild(base);
						}
					} else if(document.getElementById(base.tid)) {
						document.getElementById(base.tid).innerHTML = decodeURIComponent(nwtxt);
					}
				}
			}
		}
		xmlHttp.open("POST", base.savescript, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
		//xmlHttp.setRequestHeader("Content-length", content.length);
		//xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(content);

	}

	// cbar 2
	
	base.secr = document.createElement("tr");
	base.tbod.appendChild(base.secr);

	base.cbar2 = document.createElement("td");
	base.secr.appendChild(base.cbar2);
	base.cbar2.colSpan = "2";
	base.cbar2.style.textAlign = "center";
	base.cbar2.style.verticalAlign = "top";

	// font type
	base.cbar2.tinp = document.createElement("select");
	base.cbar2.appendChild(base.cbar2.tinp);
	base.cbar2.tinp.options[0] = new Option("Arial", "Arial");
	base.cbar2.tinp.options[0].style.fontFamily = "Arial";
	base.cbar2.tinp.options[1] = new Option("Arial Black", "Arial Black");
	base.cbar2.tinp.options[1].style.fontFamily = "Arial Black";
	base.cbar2.tinp.options[2] = new Option("Comic Sans MS", "Comic Sans MS");
	base.cbar2.tinp.options[2].style.fontFamily = "Comic Sans MS";
	base.cbar2.tinp.options[3] = new Option("Tahoma", "Tahoma");
	base.cbar2.tinp.options[3].style.fontFamily = "Tahoma";
	base.cbar2.tinp.options[4] = new Option("Times New Roman", "Times New Roman");
	base.cbar2.tinp.options[4].style.fontFamily = "Times New Roman";
	base.cbar2.tinp.options[5] = new Option("Verdana", "Verdana");
	base.cbar2.tinp.options[5].style.fontFamily = "Verdana";
	base.cbar2.tinp.options[6] = new Option("Courier New", "Courier New");
	base.cbar2.tinp.options[6].style.fontFamily = "Courier New";
	base.cbar2.tinp.options[7] = new Option("Impact", "Impact");
	base.cbar2.tinp.options[7].style.fontFamily = "Impact";

	base.cbar2.tbut = document.createElement("button");
	base.cbar2.tbut.className = "textinput_button";
	base.cbar2.appendChild(base.cbar2.tbut);
	base.cbar2.tbut.style.width = "40px";
	base.cbar2.tbut.innerHTML = "font";
	base.cbar2.tbut.onclick = function() {
		base.cframe.contentWindow.document.execCommand("FontName", false, base.cbar2.tinp.value);
	}

	// font size
	base.cbar2.sinp = document.createElement("input");
	base.cbar2.appendChild(base.cbar2.sinp);
	base.cbar2.sinp.value = "3";
	base.cbar2.sinp.style.height = "12px";
	base.cbar2.sinp.style.width = "30px";
	base.cbar2.sinp.style.fontSize = "11px";
	base.cbar2.sinp.style.marginLeft = "8px";

	base.cbar2.sbut = document.createElement("button");
	base.cbar2.sbut.className = "textinput_button";
	base.cbar2.appendChild(base.cbar2.sbut);
	base.cbar2.sbut.style.width = "40px";
	base.cbar2.sbut.innerHTML = "size";
	base.cbar2.sbut.onclick = function() {
		base.cframe.contentWindow.document.execCommand("FontSize", false, base.cbar2.sinp.value);
	}

	// font color
	base.cbar2.cbut = document.createElement("button");
	base.cbar2.cbut.className = "textinput_button";
	base.cbar2.appendChild(base.cbar2.cbut);
	base.cbar2.cbut.style.width = "40px";
	base.cbar2.cbut.innerHTML = "color";
	base.cbar2.cbut.onclick = function() {
		base.colorhandler(textid, lang, base.cbar2.cbut);
	}

	function defColorHandler() {
		alert('No color handler found');
	}

	function setColor(color) {
		base.cframe.contentWindow.document.execCommand("ForeColor", false, color);
	}

	// image
	base.cbar2.ibut = document.createElement("button");
	base.cbar2.ibut.className = "textinput_button";
	base.cbar2.appendChild(base.cbar2.ibut);
	base.cbar2.ibut.style.width = "30px";
	base.cbar2.ibut.innerHTML = "img";
	base.cbar2.ibut.onclick = function() {
		base.imghandler(textid, lang);
	}

	function defImgHandler() {
		alert("No image handler found");
	}

	function setTextImage(imgsrc, imgheight, imgwidth, imgalign, i_onclick, i_onmouseover, i_onmouseout) {
		if(base.htmlmode) {
			var insat = base.hwind.selectionStart;
			var imgtag = '<img src="'+imgsrc+'" ';
			imgtag += 'style="width: '+imgwidth+'px; height: '+imgheight+'px;" ';
			if(imgalign) {
				imgtag += 'align="'+imgalign+'" ';
			}
			if(i_onclick) {
				imgtag += 'onclick="javascript: '+i_onclick+';" ';
			}
			if(i_onmouseover) {
				imgtag += 'onmouseover="javascript: '+i_onmouseover+';" ';
			}
			if(i_onmouseout) {
				imgtag += 'onmouseout="javascript: '+i_onmouseout+';" ';
			}

			imgtag += '/>';
			base.hwind.value = base.hwind.value.substring(0, insat) + imgtag + base.hwind.value.substr(insat);
		} else {
			var cursel = base.cframe.contentWindow.getSelection();
			var insRange = cursel.getRangeAt(0);
			insRange.collapse(false);
			var imgNode = document.createElement("img");
			imgNode.src = imgsrc;
			imgNode.setAttribute("style", 'width: '+imgwidth+'px; height: '+imgheight+'px;');
			if(imgalign) {
				imgNode.setAttribute("align", imgalign);
			}
			if(i_onclick) {
				imgNode.setAttribute("onclick", "javascript: "+i_onclick+";");
			}
			if(i_onmouseover) {
				imgNode.setAttribute("onmouseover", "javascript: "+i_onmouseover+";");
			}
			if(i_onmouseout) {
				imgNode.setAttribute("onmouseout", "javascript: "+i_onmouseout+";");
			}
			insRange.insertNode(imgNode);
		}
	}
	
	// hmtl

	if(base.allowhtml) {
		base.cbar2.hbut = document.createElement("button");
		base.cbar2.hbut.className = "textinput_button";
		base.cbar2.appendChild(base.cbar2.hbut);
		base.cbar2.hbut.innerHTML = "html";
		base.cbar2.hbut.onclick = function() {
			base.toggleHtml();		
		}
	}



	function toggleHtml() {
		if(base.htmlmode) {
			base.cbar2.hbut.innerHTML = "html";
			base.htmlmode = false;
			base.cframe.style.height = base.hwind.offsetHeight + "px";
			document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML = base.hwind.value;
			base.cframe.style.display = "block";
			base.hwind.style.display = "none";
		} else {
			base.cbar2.hbut.innerHTML = "normal";
			base.htmlmode = true;
			base.hwind.style.height = base.cframe.offsetHeight + "px";
			var nwtxt = document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML;
			base.hwind.value = nwtxt;
			base.hwind.style.display = "block";
			base.cframe.style.display = "none";
		}
	}

	//link
	base.cbar2.lbut = document.createElement("button");
	base.cbar2.lbut.className = "textinput_button";
	base.cbar2.appendChild(base.cbar2.lbut);
	base.cbar2.lbut.innerHTML = "link";
	base.cbar2.lbut.onclick = function() {
		base.linkhandler(textid, lang);	
	}

	function defLinkHandler() {
		alert("No Link handler found");
	}

	function setTextLink(ahref, atext, atarget) {

		atarget = '_'+atarget;
		
		if(base.htmlmode) {
			var insat = base.hwind.selectionStart;
			var atag = '<a href="'+ahref+'" ';
			if(atarget) {
				atag += 'target="'+atarget+'"';
			}
			atag += '>'+atext+'</a>';
			base.hwind.value = base.hwind.value.substring(0, insat) + atag + base.hwind.value.substr(insat);
		} else {
			var cursel = base.cframe.contentWindow.getSelection();
			var insRange = cursel.getRangeAt(0);
			insRange.collapse(false);
			var aNode = document.createElement("a");
			aNode.href = ahref;
			if(atarget) {
				aNode.setAttribute('target', atarget);
			}
			aNode.appendChild(document.createTextNode(atext));
			insRange.insertNode(aNode);
		}
	}

	// window

	base.frdiv = document.createElement("div");
	base.appendChild(base.frdiv);
	base.frdiv.style.textAlign = "center";
	base.frdiv.style.paddingTop = "5px";

	base.cframe = document.createElement("iframe");
	base.frdiv.appendChild(base.cframe);
	base.cframe.id = "textinput_editw_" + lang;
	base.cframe.style.width = "99%";
	base.cframe.style.margin = "auto";
	base.cframe.style.height = "100%";
	base.cframe.src = text;

	if(document.getElementById(base.tid)) {
		base.ostext = document.getElementById(base.tid).innerHTML;
	} else {
		base.ostext = false;
	}

	function setEdit() {
		/*************************
		Gets called by ajax.textcanvas.php as soon as it's done loading.
		Slow server makes a nerd's head ache...
		*************************/
		if(ie) {
			setTimeout( function() {
				base.otext = document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML
				frames[base.cframe.id].document.designMode = "on";
			}, 10 );
		} else {
			setTimeout( function() { 
				base.otext = document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML
				base.cframe.contentDocument.designMode = "on";
			}, 10 );
		}
	}

	// html window

	if(base.allowhtml) {
		base.hwind = document.createElement("textarea");
		base.frdiv.appendChild(base.hwind);
		base.hwind.style.display = "none";
		base.hwind.style.width = "99%";
		base.hwind.style.margin = "auto";
		base.hwind.id = "textinput_htmlw_" + lang;
	}

	// pullme

	base.pullbot = document.createElement("div");
	base.appendChild(base.pullbot);
	base.pullbot.style.textAlign = "center";
	base.pullbot.pullme = document.createElement("span");
	base.pullbot.appendChild(base.pullbot.pullme);
	base.pullbot.pullme.className = "textinput_pullbot";
	base.pullbot.pullme.innerHTML = "###";
	base.pullbot.pullme.onmousedown = function (e) {
		var elem = (base.htmlmode)?base.hwind:base.cframe;
		base.shield = document.createElement("div");
		base.appendChild(base.shield);
		document.onmouseup = function() {
			base.removeChild(base.shield);
			document.onmouseup = function() { //nop
			}
			selectStart();
		}
		selectStop();
		var tempY = 0;
		var ytop = findPosY(elem);
		var yleft = findPosX(elem);
		var ymin = ytop + base.minheight;
		var ymax = getWindowHeight() - 30;
		var mouseNow = getMouseY(e);
		var corry = mouseNow - (ytop + elem.offsetHeight);
		base.shield.style.position = "absolute";
		base.shield.style.top = "0px";
		base.shield.style.zIndex = "9";
		if(base.absel) {
			base.shield.style.left = (findPosX(elem) - findPosX(base.absel)) + "px";
		} else {
			base.shield.style.left = yleft + "px";
		}
		base.shield.style.width = elem.offsetWidth + "px";
		var shieldtop = findPosY(base.shield);
		base.shield.style.height = ((mouseNow - shieldtop) + 100) + 'px';
		//base.shield.style.backgroundColor = "blue";
		document.onmousemove = grabIt;
		function grabIt(e) {
			if(ie) {
				tempY = event.clientY + document.body.scrollTop;
			} else {
				tempY = e.pageY;
			}  
			if(tempY < 0) {
				tempY = 0;
			}
			if(tempY < ymax) {
				if((tempY - corry) > ymin) {
					elem.style.height = ((tempY - ytop) - corry) + 'px';
					base.shield.style.height = ((tempY - shieldtop) + 100) + 'px';
				} else {
					elem.style.height = (ymin - ytop) + 'px';
				}
			} else {
				base.removeChild(base.shield);
				document.onmouseup = function() { //nop
				}
				selectStart();
			}
		}
	}

	function isAltered() {
		if(document.getElementById(base.tid)) {
			if(base.htmlmode) {
				var ntxt = base.hwind.value;
			} else {
				var ntxt = document.getElementById("textinput_editw_" + base.lang).contentWindow.document.body.innerHTML;
			}
			ntxt = (ntxt == "<br>\t")?"":ntxt;
			ntxt = (ntxt == "\t")?"":ntxt;
			otxt = (base.otext == "\t")?"":base.otext;
			if(ntxt != otxt) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}


	function destroy() {
		if(base.isAltered()) {
			var csdiv = '<div style="text-align: center; margin: 10px;"><img src="'+prefix+'/images/flags/'+lang+'.gif" />&nbsp;Save changes?<br /><br />';
			csdiv += '<button type="button" id="texinput_sc_cancel_'+textid+'_'+lang+'" class="textinput_button">Cancel</button>';
			csdiv += '<button type="button" id="texinput_sc_nosave_'+textid+'_'+lang+'" class="textinput_button" style="width: 80px;">Dont\'t Save</button>';
			csdiv += '<button type="button" id="texinput_sc_save_'+textid+'_'+lang+'" class="textinput_button">Save</button>';
			csdiv += '</div>';

			ePop = standardPop("texinput_save_changes_"+base.textid, "adpopbase_r", "adpopbar_r", "Save changes?", true);
			ePop.setWidth(250);
			ePop.fill(csdiv);
			var cancelbut = document.getElementById('texinput_sc_cancel_'+textid+'_'+lang);
			cancelbut.onclick = function() {
				ePop.destroy();
			}
			var nosavebut = document.getElementById('texinput_sc_nosave_'+textid+'_'+lang);
			nosavebut.onclick = function() {
				ePop.destroy();
				base.parentNode.removeChild(base);
			}
			var savebut = document.getElementById('texinput_sc_save_'+textid+'_'+lang);
			savebut.onclick = function() {
				ePop.destroy();
				base.saveText("true");
			}
			ePop.center();
			ePop.show();
		} else {
			base.parentNode.removeChild(base);
		}
	}

}
