function TextEditor(ta){
	this.ta=ta;

	this.addurl=function(){
		var u=prompt("Pls enter the url:", 'http://');
		if(u=='' || u==null) return 0;
		var t=prompt("pls enter the link text:", 'Link');
		if(t!='' && t!=null) this.insertstr("[url="+u+"]"+t+"[/url]");
	}
	this.addimg=function(){
		var u=prompt("Pls enter the url:", 'http://');
		if(u=='' || u==null) return 0;
		this.insertstr("[img]"+u+"[/img]");
	}
	this.addtag=function(t){
		var l, r;
		l='['+t;
		r='[/'+t+']';
		if(arguments[1])l+="="+arguments[1]
		l+=']';
		this.insertstr(l, r);
	}

	this.matchnum=function(exp, st){
		var reg=new RegExp(exp, 'gi');
		var r=st.match(reg);
		return (r && r.length) ? r.length : 0;
	}

	this.closealltag=function(){
		var c=this.ta.value;
		var a=['u', 'i', 'b', 'font=(?:\\w*?)', 'size=(?:\\w*?)', 'color=(?:\\w*?)'];
		var endt, d, d1;

		for(var i in a){
			endt=a[i].split('=', 2);
			d=matchnum('\\['+a[i]+'\\]', c)-matchnum('\\[/'+endt[0]+'\\]', c);
			if(d>0){
				for(d1=d; d1>0; d1--)c+='[/'+endt[0]+']';
			}
		}
		this.ta.value=c;
		this.insertstr('');
	}

	this.insertstr=function(str){
		this.ta.focus();
		var str1='';
		if(arguments[1])str1=arguments[1];
		if(document.selection){
			var range=document.selection.createRange();
			range.text=str+str1;
			if(str1){
				range.moveStart('character', -str1.length);
				range.moveEnd('character', -str1.length);
			}
			range.select();
		}
		else if(this.ta.selectionStart!=null){
			var sstart=this.ta.selectionStart;
			this.ta.value=this.ta.value.substr(0, this.ta.selectionStart)+str+str1+this.ta.value.substr(this.ta.selectionEnd);
			this.ta.selectionStart=this.ta.selectionEnd=sstart+str.length;

		}
		else{
			this.ta.value+=str+str1;
		}
	}
}

function DropMenu(box, link, menu){
	this.box=box;
	this.link=link;
	this.menu=menu;

	this.postshowhide=function(){}

	this.showhide=function(){
		var offset=getoffset(this.box);
		this.menu.style.left=offset[0];
		this.menu.style.top=offset[3];
		this.menu.sh=(arguments[0]!==undefined ? arguments[0] : (this.menu.sh ? 0 : 1));
		showhide(this.menu, this.menu.sh);
		this.postshowhide();
	}

}
function DropMenu_create(vname, box, link, menu){
	menu=document.getElementById(menu);
	menu.style.visibility='hidden';
	menu.style.display='none';
	menu.style.zIndex=3;
	menu.style.position='absolute';
	box=document.getElementById(box);
	link=document.getElementById(link);

	eval(vname+"=new DropMenu(box, link, menu);");
	eval("link.onclick=function(){ this.focus();}");
	eval("link.href='javascript:"+vname+".showhide();';");
	eval("link.onblur=function(){ if(!"+vname+".menu.mouseon)"+vname+".showhide(0);}");
	menu.onmouseover=function(){this.mouseon=1;}
	menu.onmouseout=function(){this.mouseon=0;}
	eval("menu.onclick=function(){"+vname+".link.focus();}");

}

function Menupair(button, dropdown){
	var bname=button, dname=dropdown;
	var position={'left':'offset[0]', 'top':'offset[3]'};
	if(arguments[2] && arguments[2].posleft) position.left=arguments[2].posleft;
	if(arguments[2] && arguments[2].postop) position.top=arguments[2].postop;

	if(!(button && dropdown && (button=document.getElementById(button)) && (dropdown=document.getElementById(dropdown)))) return;
	this.dropdown=dropdown;
	this.button=button;
	dropdown.mp_button=button;
	button.mp_name=bname;
	dropdown.mp_name=dname;
	button.mp_dropdown=dropdown;
	var o={position:'absolute', 'z-index':300, 'visibility':'hidden', 'display':'none'};
	for(var i in o) dropdown.style[i]=o[i];
	dropdown.onmouseover=function(){
		this.mover=1;
		this.showhide();
	}
	dropdown.onmouseout=function(){
		this.mover=0;
//		this.showhide();
		setTimeout("document.getElementById('"+this.mp_name+"').showhide();", 100);

	}
	dropdown.posleft=position.left;
	dropdown.postop=position.top;
	dropdown.showhide=function(){
		var offset=getoffset(this.mp_button);
		eval('this.style.left='+this.posleft+';');
		eval('this.style.top='+this.postop+';');
		showhide(this, this.mover || this.mp_button.mover);
	}
	button.links=button.getElementsByTagName('a');
	button.onmouseover=function(){
		this.mover=1;
		this.style.cursor='pointer';
		if(this.links){
			for(var i=0;i<this.links.length;i++){
				if(this.links[i].style.color!='#ccc'){
					this.links[i].style.color_o=this.links[i].style.color;
					this.links[i].style.color='#ccc';
				}
			}
		}
		this.mp_dropdown.showhide();
	}
	button.onmouseout=function(){
		this.mover=0;
		this.style.cursor='default';
		if(this.links){
			for(var i=0;i<this.links.length;i++){
				if(this.links[i].style.color_o || 1){
					this.links[i].style.color=this.links[i].style.color_o;
				}
			}
		}
//		this.mp_dropdown.showhide();
		setTimeout("document.getElementById('"+this.mp_dropdown.mp_name+"').showhide();", 100);
	}

}
function SortTb(obj){
	this.tb=obj;
	this.rows=[];
	this.vname='tsobj';
	this.rowoffset=1;
	this.getcmptxt=function (o){
		if(o.comparetxt)return o.comparetxt;
		var a=o.getAttribute('comparetxt');
		if(a!==null && a!==undefined) return o.comparetxt=a;
		else o.comparetxt=o.innerHTML.replace(/<.*?>/g, '');
		return o.comparetxt;
	}
	this.docompare=function (a, b){
		var st=a.sortobj;
		var ca, cb;
		if(st.headcol.getcmptxt){
			ca=st.headcol.getcmptxt(a.cells[st.colindex]);
			cb=st.headcol.getcmptxt(b.cells[st.colindex]);
		}
		else{
			ca=st.getcmptxt(a.cells[st.colindex]);
			cb=st.getcmptxt(b.cells[st.colindex]);
		}
		return ca>cb ? 1 : (ca<cb ? -1 : 0);
	}
	this.dosort=function (o){

		if(this.headcol.arrowspan) this.headcol.arrowspan.innerHTML='';
		for(var i=0; o && o.previousSibling && (o.tagName==o.previousSibling.tagName); i++, o=o.previousSibling);
		this.setcolindex(i);
		if(!this.rows || !this.rows.length){
			for(i=this.rowoffset;i<this.tb.rows.length;i++) this.rows[i-this.rowoffset]=this.tb.rows[i];
		}
		for(i=0;i<this.rows.length;i++) this.rows[i].sortobj=this;
		this.rows.sort(this.docompare);
		if(this.headcol.sdir=='u')this.rows.reverse();
		for(i=0;i<this.rows.length;i++){
			this.tb.rows[0].parentNode.appendChild(this.rows[i]);
		}
		if(!this.headcol.arrowspan){
			var e=document.createElement('span');
			this.headcol.appendChild(e);
			this.headcol.arrowspan=e;
		}
		if(this.headcol.sdir!='u') this.headcol.sdir='u';
		else this.headcol.sdir='d';
		this.headcol.arrowspan.innerHTML='&nbsp;&'+this.headcol.sdir+'arr;';
	}
	this.setcolindex=function (i){
		this.colindex=i;
		this.headcol=this.tb.rows[0].cells[i];
	}
	this.setcolindex(0);
	for(i=0;i<this.tb.rows[0].cells.length;i++){
		this.tb.rows[0].cells[i].style.cursor='pointer';
	}
}
function SortTb_create(vname, obj){
	eval(vname+'=new SortTb(obj);'+vname+'.vname=vname;');
	for(i=0;i<obj.rows[0].cells.length;i++){
		eval('obj.rows[0].cells[i].onclick=function(){'+vname+'.dosort(this);}');
	}
}


function ActSelect(select, act, opt){

	this.loadOptions1=function(ops){
		var o=this.select.options;
		for(var i=o.length; i>0; i--)o[i]=null;
		if(ops!=null)addOptions(this.select, ops);
		else {
			if(this.predecessor==null) addOptions(this.select, this.opt);
			else {
				var v=this.predecessor.select.value;
				if(v!='') addOptions(this.select, this.opt[v]);
			}
		}
		this.selectChanged();
	}
	this.loadOptions=function(ops){this.loadOptions1(ops);}

	this.setSelect1=function(v){
		setSelect(this.select, v);
		this.selectChanged();
	}
	this.setSelect=function(v){this.setSelect1(v);}

	this.refreshSelect1=function(id, name){
		var a=this.select.value;
		refreshSelect(this.select, id, name);
		if(this.select.value!=a) this.selectChanged();
	}
	this.refreshSelect=function(id, name){this.refreshSelect1(id, name);}

	this.editSelect1=function(){
		if(this.act==null) return;
		var v=this.select.value, a=this.act.value;
		var rv;

		switch(a){
			case 'new':
				var extra=this.predecessor==null ? '' : "&"+this.predecessor.select.name+"="+this.predecessor.select.value;
				rv=showModalDialog('?w=modal&act=form&o='+this.select.name+extra, window,'status:0;resizable:1;');
				break;
			case 'edit':
				rv=showModalDialog('?w=modal&act=form&o='+this.select.name+'&id='+v,window,'status:0;resizable:1;');
				break;
			case 'delete':
				if(confirm("Are you sure to delete \""+this.select.options[this.select.selectedIndex].text+"\" ?")) rv=showModalDialog('?w=modal&act=delete&id='+v+'&o='+this.select.name,window,'status:0;resizable:1;dialogWidth:250px;dialogHeight:150px;');
				break;
			default:
				return;
		}
		if(rv!=null) {
			this.modalvalue=rv;
			this.selectEdited(rv);
		}
		this.act.selectedIndex=0;
		this.act.blur();
	}
	this.editSelect=function(){this.editSelect1();}

	this.selectEdited1=function(obj){
		if(this.act==null)return;
		var i=this.select.value, iname=this.select.options[this.select.selectedIndex].text, j=this.act.value;
		if(this.loadframe!='')document.frames(this.loadframe).history.go(0);
		var opt;
		if(this.predecessor==null) opt=this.opt;
		else{
			if(this.opt[this.predecessor.select.value]==null) this.opt[this.predecessor.select.value]=new Array();
			opt=this.opt[this.predecessor.select.value];
		}

		if(j=='delete'){
			var k=keySearch(opt, i);
			if(k>=0)opt.splice(k, 2);
			this.select.options[this.select.selectedIndex]=null;
		}
		else{
			if(obj.id==null)return;

			if(j=='new')opt.push(obj.id, obj.name);
			else if(obj.name!=iname){
				var k=keySearch(opt, i);
				if(k>=0)opt[k+1]=obj.name;
			}
			this.refreshSelect(obj.id, obj.name);
		}
		if(i!=this.select.value)this.selectChanged();
	}
	this.selectEdited=function(obj){this.selectEdited1(obj);}

	this.refreshAct1=function(){
		if(this.act==null)return;
		var o=this.act.options, i=this.select.selectedIndex;
		if(this.select.value==''){
			o[3]=null;
			o[2]=null;
		}
		else{
			if(o[2]==null)o[2]=new Option('Edit', 'edit');
			if(o[3]==null)o[3]=new Option('Delete', 'delete');
		}
		if(this.predecessor!=null){
			if(this.predecessor.select.value=='')this.act.options[1]=null;
			else if(this.act.options[1]==null)this.act.options[1]=new Option('New', 'new');
		}
	}
	this.refreshAct=function(){this.refreshAct1();}

	this.selectChanged1=function(){
		this.preChange();
		this.refreshAct();
		if(this.successor!=null) this.successor.loadOptions();
		this.postChange();
	}
	this.selectChanged=function(){this.selectChanged1();}

	this.preChange=function(){};
	this.postChange=function(){};

	this.select=select;
	this.act=act;
	this.opt=opt;
	this.predecessor=null;
	this.successor=null;
	this.vname='';
	this.handler='general.php';
	this.loadframe='';
	if(this.act!=null){
		this.act.options[0]=new Option('(Action)', '');
		this.act.options[1]=new Option('New', 'new');
	}
	if(this.select.options.length<1)this.select.options[0]=new Option('', '');
	this.modalvalue=null;
}
function ActSelect_create(vn, select, act, opt){
	eval(vn+'=new ActSelect(select, act, opt);'+vn+'.vname=vn;');
	eval("select.onchange=function(){"+vn+'.selectChanged();}');
	if(act!=null)eval("act.onchange=function(){"+vn+'.editSelect();}');
	if(arguments[4]!=null) eval(vn+".handler=arguments[4];");
}
function ActSelect_link(pre, suc){
	pre.successor=suc;
	suc.predecessor=pre;
}
loopcount=0;
function ActionSelect(select, act, dt){
	this.dt=dt;
	this.select=select;
	this.act=act;
	this.predecessor=null;
	this.successor=[];
	this.loadframe='';
	this.linkextra='';

	this.optionmargin=this.select.options.length;
	this.o=this.select.name;

	this.optiontxt=function(key){
		return this.dt[key].name;
	}
	this.addoption=function(key){
		var a=[], select=this.select, i;
		if(!key){
			for(i in this.dt){
				if(!(this.dt[i].parent>0)) a.push(i);
			}
		}
		else{
			if(this.dt[key].child==null){
				this.dt[key].child=[];
				for(i in this.dt){
					if(this.dt[i].parent==key) this.dt[key].child.push(i);
				}
			}
			a=this.dt[key].child;
		}
		for(i=0;i<a.length;i++){
			select.options[select.options.length]=new Option(this.optiontxt(a[i]), this.dt[a[i]].id);
		}
	}
	this.loadoption=function(){
		var o=this.select.options;
		for(var i=o.length-1; i>=this.optionmargin; i--)o[i]=null;
		if(this.predecessor==null) this.addoption(arguments[0]);
		else {
			var v=this.predecessor.select.value;
			if(v!='') this.addoption(v);
		}
		this.selectchanged();
	}
	this.setselect=function(v){
		setSelect(this.select, v);
		this.selectchanged();
	}

	this.setvalue=function(v){
		if(!(loopcount>0)) loopcount=1;
		if(loopcount++>50000) {alert(this.select.name+''+loopcount);  return;}
		if(v && this.predecessor && this.dt[v] &&  (p=this.dt[v].parent)) this.predecessor.setvalue(p);
		this.setselect(v);
	}
	this.refreshselect=function(id, name){
		var a=this.select.value;
		refreshSelect(this.select, id, name);
		if(this.select.value!=a) this.selectchanged();
	}
	this.editlink=function(a, v){
		switch(a){
			case 'new':
				var extra=this.predecessor==null ? '' : "&parent="+this.predecessor.select.value;
				return '?w=modal'+this.linkextra+'&act=getform&parentfixed=1&o='+this.o+extra;
			case 'edit':
				return '?w=modal'+this.linkextra+'&act=getform&parentfixed=1&o='+this.o+'&id='+v;
			case 'delete':
				return '?w=modal'+this.linkextra+'&act=tbdelete&id='+v+'&o='+this.o;
		}
	}
	this.editselect=function(){
		if(!this.act) return;
		var v=this.select.value, a=this.act.value;
		var rv;

		var link=this.editlink(a, v);
		if(!link)return;
		if(a=='delete'){
			if(confirm("Are you sure to delete \""+this.select.options[this.select.selectedIndex].text+"\" ?")) rv=showModalDialog(link,window,'status:0;resizable:1;dialogWidth:250px;dialogHeight:150px;');
		}
		else rv=showModalDialog(link, window,'status:0;resizable:1;');
		if(rv!=null) {
			this.modalvalue=rv;
			this.selectedited(rv);
		}
		this.act.selectedIndex=0;
		this.act.blur();
	}
	this.selectedited=function(obj){
		if(!this.act)return;
		var i=this.select.value, iname='', j=this.act.value;
		if(this.select.options && this.select.options.length>0 &&  this.select.options[this.select.selectedIndex])iname=this.select.options[this.select.selectedIndex].text;

		if(this.loadframe!='')document.frames(this.loadframe).history.go(0);

		if(j!='edit' && i && this.dt && this.dt[i] && (p=this.dt[i].parent) && this.dt[p]){
	 		this.dt[p].child=null;
		}
		if(j=='delete'){
			delete(this.dt[i]);
			this.loadoption();
		}
		else{
			if(obj.id==null)return;
			if(!this.dt[i])this.dt[i]={};
			if(j=='new') {
				if(this.predecessor && !obj.parent) obj.parent=this.predecessor.select.value;
				this.dt[obj.id]=obj;
			}
			else if(obj.name!=iname){
				this.dt[i].name=obj.name;
			}
			this.refreshselect(obj.id, obj.name);
		}
		if(i!=this.select.value)	this.selectchanged();
	}
	this.refreshact=function(){
		if(!this.act)return;
		var o=this.act.options, i=this.select.selectedIndex;
		if(this.select.value==''){
			o[3]=null;
			o[2]=null;
		}
		else{
			if(o[2]==null)o[2]=new Option('Edit', 'edit');
			if(o[3]==null)o[3]=new Option('Delete', 'delete');
		}
		if(this.predecessor!=null){
			if(this.predecessor.select.value=='')this.act.options[1]=null;
			else if(this.act.options[1]==null)this.act.options[1]=new Option('New', 'new');
		}
	}
	this.selectchanged1=function(){
		this.refreshact();
		if(this.successor){
			for(var i=0;i<this.successor.length;i++) this.successor[i].loadoption();
		}
	}
	this.selectchanged=function(){this.selectchanged1();}

	if(this.act){
		this.act.options[0]=new Option('(Action)', '');
		this.act.options[1]=new Option('New', 'new');
	}
}
function ActionSelect_create(vn, select, act, dt){
	if(!select)return;
	eval(vn+'=new ActionSelect(select, act, dt);'+vn+'.vname=vn;');
	eval("select.onchange=function(){"+vn+'.selectchanged();}');
	if(act!=null)eval("act.onchange=function(){"+vn+'.editselect();}');
}
function ActionSelect_link(pre, suc){
	pre.successor.push(suc);
	suc.predecessor=pre;
}
function ActionSelect_createchain(par, dt){
	for(var i=0;i<par.length;i++){
		ActionSelect_create(par[i].vname, par[i].select, par[i].act, dt);
		if(i==0 && par[i].loadoption!=null) eval(par[i].vname+'.loadoption(par[i].loadoption);');
		if(i>0) eval('ActionSelect_link('+par[i-1].vname+', '+par[i].vname+');');
		if(par[i].setselect) eval(par[i].vname+'.setselect(par[i].setselect);');
	}
	if((i=par.length-1) && par[i].setvalue) eval(par[i].vname+'.setvalue(par[i].setvalue);');
}

function SetallTb(obj){
	this.tb=obj;
	this.rows=[];
	this.vname='tsobj';
	this.rowoffset=1;

	this.doset=function(o){
		var i,i2;
		var v=prompt('please input the value the this column', '');
		if(v===null)return;
		for(var i=0; o && o.previousSibling && (o.tagName==o.previousSibling.tagName); i++, o=o.previousSibling);
		this.setcolindex(i);
		if(!this.rows || !this.rows.length){
			for(i=this.rowoffset;i<this.tb.rows.length;i++) this.rows[i-this.rowoffset]=this.tb.rows[i];
		}
		for(i=0;i<this.rows.length;i++) {
			var e=this.rows[i].cells[this.colindex];
			if(!(e && (e=e.getElementsByTagName('input'))))continue;
			if(!e)continue;
			for(i2=0;i2<e.length;i2++){
				if(e[i2].getAttribute('type')=='text') e[i2].value=v;
			}
		}
	}
	this.setcolindex=function (i){
		this.colindex=i;
		this.headcol=this.tb.rows[0].cells[i];
	}
	this.setcolindex(0);
	for(i=0;i<this.tb.rows[0].cells.length;i++){
		this.tb.rows[0].cells[i].style.cursor='pointer';
	}
}
function SetallTb_create(vname, obj){
	eval(vname+'=new SetallTb(obj);'+vname+'.vname=vname;');
	for(i=0;i<obj.rows[0].cells.length;i++){
		eval('obj.rows[0].cells[i].onclick=function(){'+vname+'.doset(this);}');
	}
}

function SetallTr(obj){
	this.tb=obj;
	this.rows=[];
	this.vname='trsobj';
	this.rowoffset=1;

	this.doset=function(o){
		var i,i2;
		var v=prompt('please input the value the this row', '');
		if(v===null || v===undefined)return;

		for(o=o.nextSibling;o;o=o.nextSibling){
			var e=o;
			if(!(e && (e=e.getElementsByTagName('input'))))continue;
			if(!e)continue;
			for(i2=0;i2<e.length;i2++){
				if(e[i2].getAttribute('type')=='text') e[i2].value=v;
			}
		}
	}
	for(i=this.rowoffset;i<this.tb.rows.length;i++){
		this.tb.rows[i].cells[0].style.cursor='pointer';
	}
}
function SetallTr_create(vname, obj){
	eval(vname+'=new SetallTr(obj);'+vname+'.vname=vname;');
	for(i=1;i<obj.rows.length;i++){
		eval('obj.rows[i].cells[0].onclick=function(){'+vname+'.doset(this);}');
	}
}