function createForm(name,view){
	var base=document.getElementsByName(name)[0];

	var ctrl=document.createElement('A');
	ctrl.base=base;
	ctrl.style.display="block";
	ctrl.toForm=function(){
		var base=this.base;
		base.createForm();
		base.style.display="none";
		this.innerHTML="Классический вид";
		this.href="#";
		this.onclick=function(){this.toText();return false;};
	};
	ctrl.toText=function(){
		var base=this.base;
		base.eraseForm();
		base.style.display="block";
		this.innerHTML="Развернутый вид";
		this.href="#";
		this.onclick=function(){this.toForm();return false;};
	};

	base.createForm=function(){
		function getFields(text){
			var a=[];
			text=text.replace(/<!-- +\/field +-->/gi,"\x60");
			var s=text.split("\x60"),m;
			for(var j=0;j<s.length-1;j++){
				m=s[j].match(/<!-- +field +(\w+) +"([^"]+)" +(\d+) +-->([^\x60]*)/i);
				a[j]={id:m[1],caption:m[2],height:m[3],value:m[4]};
			}
			return a;
		}
		if(this.fields)this.eraseForm();

		var f=getFields(this.value);

		var ta,lbl,plus,minus;
		var div=document.createElement("DIV");
		var b;
		this.fields=[];

		for(j=0;j<f.length;j++){
			lbl=document.createElement("DIV");
			lbl.appendChild(document.createTextNode(f[j].caption+' '));

			ta=document.createElement("TEXTAREA");
			ta.style.width=this.style.width;
			ta.style.height=f[j].height+'px';
			ta.style.display='block';
			ta.id=f[j].id;
			ta.value=f[j].value;
			ta.caption=f[j].caption;
			ta.base=this;
			ta.label=lbl;

			ta.remove=function(){
				var par=this.parentNode;
				par.removeChild(this.label);
				par.removeChild(this);
			}
			ta.onchange=function(){
				var code=this.base.value;
				code=code.replace(/<!-- +\/field +-->/gi,"\x60");
				code=code.replace(new RegExp('<!-- +field +'+this.id+' +"[^"]+" +\\d+ +-->([^\\x60]*)',"i"), '<!-- field '+this.id+' "'+this.caption +'" '+parseInt(this.style.height)+' -'+'->'+this.value);
				code=code.replace(/\x60/gi,"<!-- /field -->");
				this.base.value=code;
				
			};
			ta.heightPlus=function(number){
				this.style.height=(parseInt(this.style.height)+number)+'px';
				this.onchange();
			};

			plus=document.createElement('A');
			plus.textarea=ta;
			plus.href="#";
			plus.appendChild(document.createTextNode('+'));
			plus.onclick=function(){this.textarea.heightPlus(50);return false;};

			minus=document.createElement('A');
			minus.textarea=ta;
			minus.href="#";
			minus.appendChild(document.createTextNode('-'));
			minus.onclick=function(){this.textarea.heightPlus(-50);return false;};

			lbl.appendChild(plus);
			lbl.appendChild(document.createTextNode(' '));
			lbl.appendChild(minus);			

			this.fields[j]=ta;
			this.parentNode.appendChild(lbl);
			this.parentNode.appendChild(ta);
		}

	};
	base.eraseForm=function(){
		if(this.fields){
			for(var i=0; i<this.fields.length;i++){
				this.fields[i].remove();
			}
			this.fields=undefined;
		}
	};
	base.parentNode.insertBefore(ctrl,base);
	base.ctrl=ctrl;
	base.code=function(){
		var t=this.value.replace(/<!-- +\/field +-->/gi,cTag);
		return t.replace(/<!-- +field +/gi,oTag);
	};

	switch(view){
		case "text":
			ctrl.toText();
			break;
		case "form":
			ctrl.toForm();
			break;
	}
}