// Copyright Webigger(R) 2002-2004
// This file is part of the Webigger Software and may not be copied or modified.

// Implement the Array.push method if missing
if(!Array.prototype.push){
	Array.prototype.push=function(){
		var l=this.length;
		for(var i=0; i<arguments.length; i++)
			this[l+i]=arguments[i];
		return this.length;
	}
}
function sty(o){return (o.style?o.style:o);}
function elt(s){
	if(typeof(s)!='string')
		return s;
	if(document.getElementById)
		return document.getElementById(s);
	if(document.all)
		return document.all[s];
	return null;
}
function selectValue(sSel,val){
	var opt=elt(sSel).options;
	opt.selectedIndex=0;
	for(var i=0;i<opt.length;++i)
		if(opt[i].value==val)
			opt.selectedIndex=i;
}
function Tree(sTree,sImagesUrl,fnOnChildrenLoading){
	this.sImagesUrl=sImagesUrl;
	this.fnOnChildrenLoading=fnOnChildrenLoading;
	this.sBeg='<img align="absmiddle" src="'+sImagesUrl+'/editor/tree';
	this.sEnd='.gif" border="0">';
	this.sVia=this.sBeg+'via'+this.sEnd;
	this.sDoc=this.sBeg+'doc'+this.sEnd;
	this.sDir=this.sBeg+'dir'+this.sEnd;
	this.sLink=this.sBeg+'link'+this.sEnd;
	this.sNo=this.sBeg+'no'+this.sEnd;
	this.sHighlight='inactivecaption';
	this.sHighlightText='inactivecaptiontext';
	this.bHighlight=true;
	this.bEdit=false;

	this.sName=sTree;
	this.iNextId=0;
}
Tree.prototype.getNewId=function(){
	var sId=this.iNextId.toString();
	++this.iNextId;
	return sId;
}
Tree.prototype.buildHtml=function(){
	var s=[];
	this.oRoot.buildHtml(s);
	return s.join('');
}
Tree.prototype.bind=function(){
	elt(this.sName).oTree=this;
	this.oTree=elt(this.sName);
	this.oRoot.bind();
}
Tree.prototype.add=function(oDot){
	if(oDot.sName==null)
		oDot.sName=this.getNewId();
	oDot.oTree=this;
	this.oRoot=oDot;
	this.oCur=oDot;
	oDot.bOpen=true;//the root dot is always open
}
Tree.prototype.get=function(){
	return 'elt("'+this.sName+'").oTree';
}
Tree.prototype.getDot=function(tPath,fnOnFinish){
	var iLevel=0;
	var oDot=oRoot;
	while(iLevel<tPath.length){
		var iDot=0;
		if(oDot.bChild&&oDot.tSub.length==0&&!oDot.bChildrenLoaded){
			oDot.loadChildren(fnOnFinish);
			return null;
		}
		while(iDot<oDot.tSub.length&&oDot.tSub[iDot].sName!=tPath[iLevel])
			++iDot;
		if(iDot==oDot.tSub.length)
			return null;
		oDot=oDot.tSub[iDot];
		++iLevel;
	}
	return oDot;
}
Tree.prototype.treeFocus=function(){
	this.sHighlight='highlight';
	this.sHighlightText='highlighttext';
	this.oTree.style.backgroundColor='infobackground';
	this.oCur.highlight();
}
Tree.prototype.treeBlur=function(){
	this.sHighlight='inactivecaption';
	this.sHighlightText='inactivecaptiontext';
	this.oTree.style.backgroundColor='window';
	this.oCur.highlight();
}
Tree.prototype.lowlight=function(){
	with(sty(this.oCur.div('oTitle'))){
		backgroundColor='';
		color='';
	}
}
Tree.prototype.getState=function(){
	var state=[];
	state['oCur']=this.oCur.get();
	this.oRoot.getState(state);
	return state;
}
Tree.prototype.setState=function(state){
	this.bSettingState=true;
	this.lowlight();
	this.tmpState=state;
	var sFunc=this.get()+'.setState('+this.get()+'.tmpState)';
	this.oRoot.setState(state, new Function(sFunc))
	try{
		var newCur=elt(state['oCur']);
		newCur.highlight();
	}
	catch(e)
	{}
	this.bSettingState=false;
}
//------------------------------------------------------------------------------
function Dot(sName,sTitle,bChild,iKind,tInfo,bOpen){
	this.sName=sName;
	this.sTitle=sTitle;
	this.tSub=[];
	this.m_parent=null;
	this.id=sName;
	this.iKind=iKind;
	this.bChild=bChild;
	this.tInfo=tInfo;
	this.bOpen=!!bOpen;
}
Dot.prototype.add=function(oDot,bAsFirstChild){
	if(oDot.sName==null)
		oDot.sName=this.oTree.getNewId();
	oDot.oTree=this.oTree;
	oDot.m_parent=this;
	oDot.id=this.id+'_'+oDot.sName;
	if(bAsFirstChild==true)
		this.tSub.splice(0,0,oDot);
	else
		this.tSub[this.tSub.length]=oDot;
	this.bChild=true;
}
Dot.prototype.remove=function(){
	if(!this.m_parent)
		return;
	if(this==this.oTree.oCur)
		this.m_parent.highlight();
	this.m_parent.tSub.splice(this.pos(),1);
	if(this.m_parent.tSub.length==0){
		this.m_parent.bChild=false;
		this.m_parent.bChildrenLoaded=false;
	}
	this.m_parent.updateTreePics();
	this.div().outerHTML='';
}
Dot.prototype.parent=function(iLev){
	if(iLev==0)return this;
	var p=this.m_parent;
	while(--iLev>0&&p!=null){
		p=p.m_parent;
	}
	return p;
}
Dot.prototype.get=function(sElt){
	return this.divName(sElt)+'.oDot';
}
Dot.prototype.pos=function(){
	if(this.m_parent==null)return 0;
	var pos=0;
	while(pos<this.m_parent.tSub.length&&this.m_parent.tSub[pos]!=this)++pos;
	return pos;
}
Dot.prototype.isFirst=function(){
	return(this.pos()==0);
}
Dot.prototype.isLast=function(){
	if(this==this.oTree.oRoot)return true;
	return(this.pos()==this.m_parent.tSub.length-1);
}
Dot.prototype.divName=function(sElt){
	if(!sElt)
		sElt='oDiv';
	return this.oTree.sName+sElt+this.id;
}
Dot.prototype.div=function(sElt){
	return elt(this.divName(sElt));
}
Dot.prototype.icon=function(){
	var ic=(this.bChild?'rep':'pag');
	if(!this.isLast())ic+='s';
	if(this.bChild)ic+=(this.bOpen?'o':'f');
	return this.oTree.sBeg+ic+this.oTree.sEnd;
}
Dot.prototype.prev=function(){
	if(this.m_parent==null)return null;
	var oDot=this.oTree.oRoot;
	while(oDot!=null&&oDot.next()!=this)
		oDot=oDot.next();
	return oDot;
}
Dot.prototype.next=function(){
	if(this.bChild&&this.bOpen)return this.tSub[0];
	if(this.m_parent==null)return null;
	if(this.isLast()){
		var oDot=this;
		while(oDot.m_parent.nextSibling()==null){
			oDot=oDot.m_parent;
			if(oDot.m_parent==null)return null;
		}
		return oDot.m_parent.nextSibling();
	}else
		return this.m_parent.tSub[this.pos()+1];
}
Dot.prototype.prevSibling=function(){
	if(this.isFirst())return null;
	return this.m_parent.tSub[this.pos()-1];
}
Dot.prototype.nextSibling=function(){
	if(this.isLast())return null;
	return this.m_parent.tSub[this.pos()+1];
}
Dot.prototype.isParent=function(oDot){
	while(oDot!=null){
		if(oDot==this)
			return true;
		else
			oDot=oDot.m_parent;
	}
	return false;
}
Dot.prototype.show=function(){
	var oDot=this.m_parent;
	while(oDot!=null){
		if(!oDot.bOpen)oDot.toggle();
		oDot=oDot.m_parent;
	}
	this.highlight();
	this.oTree.oTree.focus();
}
Dot.prototype.loading=function(){
	this.unloadChildren();
	this.div('oSubs').innerHTML='<div>•••</div>';
}
Dot.prototype.unloadChildren=function(){
	if(this.isParent(this.oTree.oCur))
		this.oTree.oCur=this;
	this.tSub.length=0;
	this.bChildrenLoaded=false;
	this.bOpen=false;
}
Dot.prototype.loadChildren=function(fnOnFinish){
	this.loading();
	this.bChildrenLoaded=true;
	jsres.onreadystatechange=(fnOnFinish?fnOnFinish:'');
	if(this.oTree.fnOnChildrenLoading)
		this.oTree.fnOnChildrenLoading(this);
}
Dot.prototype.toggle=function(fnOnFinish){
	this.highlight();
	if(!this.bChild)return;
	if(this.tSub.length==0&&!this.bChildrenLoaded){
		this.loadChildren(fnOnFinish);
		return false;
	}
	this.bOpen=!this.bOpen;
	for(var i=0;i<this.tSub.length;++i){
		//var div=this.tSub[i].div();
		var sDivName=this.tSub[i].divName();
		var div=elt(sDivName);
		sty(div).display=(this.bOpen?'block':'none');
	}
	if(this!=this.oTree.oRoot)//the root Dot has no toggle icon
		this.div('oLink').innerHTML=this.icon();
	return true;
}
Dot.prototype.click=function(){
	if(this.oTree.fnOnDotClicked)
		this.oTree.fnOnDotClicked(this);
	if(this!=this.oTree.oCur)
		this.highlight();
	else
		this.edit();
}
Dot.prototype.dblclick=function(){
	this.highlight();
	this.edit();
}
Dot.prototype.keyDown=function(key){
	if(this.oTree.bEdit==true){
		if(key==27||key==13){
			this.unedit(key==27);
			event.cancelBubble=true;
			return false;
		}
		return true;
	}
	if((key<33||key>40)&&key!=13&&key!=45&&key!=46)return true;
	var oDot=this.oTree.oCur;
	if(key==38)oDot=oDot.prev();
	if(key==40)oDot=oDot.next();
	if(key==37){
		if(oDot.bOpen)oDot.toggle();
		else oDot=oDot.m_parent;
	}
	if(key==39&&oDot.bChild){
		if(oDot.bOpen)oDot=oDot.tSub[0];
		else oDot.toggle();
	}
	if(key==36)oDot=this.oTree.oRoot;
	if(key==35){
		while(oDot.next()!=null)
			oDot=oDot.next();
	}
	if(key==33){
		var i=5;
		while(i-->0&&oDot.prev()!=null)
			oDot=oDot.prev();
	}
	if(key==34){
		var i=5;
		while(i-->0&&oDot.next()!=null)
			oDot=oDot.next();
	}
	if(key==13){
		oDot.edit();
	}
	if(key==45){
		if(this.oTree.fnOnInsert)
			this.oTree.fnOnInsert(oDot);
	}
	if(key==46){
		if(this.oTree.fnOnDelete)
			this.oTree.fnOnDelete(oDot);
	}
	if(oDot!=null)
		oDot.highlight();
	return false;
}
Dot.prototype.highlight=function(){
	if(this.oTree.fnOnCanCurrentChange&&!this.oTree.fnOnCanCurrentChange())
		return;
	this.oTree.lowlight();
	if(!this.oTree.bEdit&&this.oTree.bHighlight)
		with(sty(this.div('oTitle'))){
			backgroundColor=this.oTree.sHighlight;
			color=this.oTree.sHighlightText;
		}
	if(this.oTree.oCur!=this){
		this.oTree.oCur=this;
		this.scrollIntoView();
		if(this.oTree.fnOnCurrentChanged)
			this.oTree.fnOnCurrentChanged(this);
	}
}
Dot.prototype.scrollIntoView=function(){
	var dotTop=this.div().offsetTop;
	var dotHeight=this.div('oTree').offsetHeight;
	var treeHeight=this.oTree.oTree.style.pixelHeight;
	var scrollTop=this.oTree.oTree.scrollTop;
	if(dotTop+dotHeight>scrollTop+treeHeight)
		this.div('oTree').scrollIntoView(false);
	else if(dotTop<scrollTop)
		this.div('oTree').scrollIntoView(true);
}
Dot.prototype.level=function(){
	var l=0;
	var oDot=this;
	while(oDot.m_parent!=null){
		oDot=oDot.m_parent;
		++l;
	}
	return l;
}
Dot.prototype.updateTreePics=function(){
        this.div('oTree').innerHTML=this.getTreePics();
        this.div('oIcon').innerHTML=this.typeIcon();
        for(var i=0;i<this.tSub.length;++i)
        	this.tSub[i].updateTreePics();
}
Dot.prototype.getTreePics=function(){
	var iLev=this.level();
	var s=[];
	for(var i=1;i<iLev;++i){//starting from 1 as the root dot has no toggle icon
		var p=this.parent(iLev-i);
		s.push(p.isLast()?this.oTree.sNo:this.oTree.sVia);
	}
	if(iLev>0){//the root dot has no toggle icon (+/-)
		if(this.bChild)
			s.push('<a id="',this.divName('oLink'),'" href="#" onclick="',this.get(),'.toggle();return false;" class="ToggleLink">',this.icon(),'</a>');
		else
			s.push(this.icon());
	}
	return s.join('');
}
Dot.prototype.typeIcon=function(){
	if(this.oTree.fnTypeIcon)
		return this.oTree.fnTypeIcon(this);
	return (this.iKind==0?(this.bChild?this.oTree.sDir:this.oTree.sDoc):this.oTree.sLink);
}
Dot.prototype.getCursor=function(){
	if(this.oTree.fnCursor)
		return this.oTree.fnCursor(this);
	return (this.iKind==0?'default':'pointer');
}
Dot.prototype.label=function(){
	if(this.oTree.fnLabel)
		return this.oTree.fnLabel(this);
	return this.sTitle.replace(/<br>/gi,' ');
}
Dot.prototype.buildHtml=function(s){
	s.push('<table id="',this.divName(),'" ',(this.m_parent==null||this.m_parent.bOpen?'':'style="display:none"'),' cellpadding="0" cellspacing="0" border="0" class="Text">');
	this.buildInnerHtml(s);
	s.push('</span></td></tr></table>');
}
Dot.prototype.buildInnerHtml=function(s){
	s.push('<tbody><tr><td nowrap>');
	s.push('<span id="',this.divName('oTree'),'">',this.getTreePics(),'</span><span id="',this.divName('oIcon'),'" onclick="',this.get(),'.click()" ondblclick="click()" oncontextmenu="',this.get(),'.contextMenu();return false;">');
	s.push(this.typeIcon(),'</span>');
	s.push('<span id="',this.divName('oTitle'),'" style="cursor:',this.getCursor(),'" onclick="',this.get(),'.click()" ondblclick="click()" oncontextmenu="',this.get(),'.contextMenu();return false;">&nbsp;',this.label(),'&nbsp;</span><span id="',this.divName('oSubs'),'">');
	for(var i=0;i<this.tSub.length;++i)
		this.tSub[i].buildHtml(s);
	s.push('</span></td></tr></tbody>');
}
Dot.prototype.bind=function(){
	this.oDiv=this.div();
	this.oDiv.oDot=this;
	for(var i=0;i<this.tSub.length;++i)
		this.tSub[i].bind();
}
Dot.prototype.redraw=function(){
	this.unloadChildren();
	var s=[];
	this.buildHtml(s);
	this.div().outerHTML=s.join('');
	this.bind();
}
Dot.prototype.contextMenu=function(){
	if(this.oTree.fnOnContextMenu)
		this.oTree.fnOnContextMenu(this);
}
Dot.prototype.addTemp=function(oNew,bAsFirstChild){
	if(!this.bOpen)
		this.toggle();
	this.add(oNew,bAsFirstChild);
	var s=[];
	oNew.buildHtml(s);
	if(bAsFirstChild==true)
		this.div('oSubs').innerHTML=s.join('')+this.div('oSubs').innerHTML;
	else
		this.div('oSubs').innerHTML+=s.join('');
	this.bind();
	this.updateTreePics();
}
Dot.prototype.editNode=function(){
	location=this.sUrl+'?Action=Edit&Url='+this.oTree.sCurrentUrl;
}
Dot.prototype.move=function(bUp){
	if((bUp&&this.isFirst())||(!bUp&&this.isLast()))return false;
	var pos=this.pos();
	var dPos=(bUp?-1:1);
	var subs=this.m_parent.tSub;
        subs[pos].div().swapNode(subs[pos+dPos].div());
        var tmp=subs[pos];subs[pos]=subs[pos+dPos];subs[pos+dPos]=tmp;
        subs[pos].updateTreePics();
        subs[pos+dPos].updateTreePics();
        return true;
}
Dot.prototype.edit=function(){
	if(this.oTree.bEdit==true)
		return;
	if(this.oTree.fnOnEdit)
		this.oTree.fnOnEdit(this);
	else
		this.startEditLabel();
}
Dot.prototype.unedit=function(bCancel){
	if(this.oTree.bEdit==false)
		return;
	if(bCancel){
		if(this.oTree.fnOnCancelEdit)
			this.oTree.fnOnCancelEdit(this,oEditing.innerHTML);
		else
			this.endEditLabel();
	}else{
		if(this.oTree.fnOnSubmitEdit)
			this.oTree.fnOnSubmitEdit(this,oEditing.innerHTML);
		else
			this.endEditLabel();
	}
}
Dot.prototype.startEditLabel=function(){
	this.oTree.bEdit=true;
	this.div('oTitle').innerHTML='<span style="border:thin inset;background-color:window" id="oEditing" onblur="'+this.get()+'.unedit(false)">'+this.sTitle+'</span>';
	oEditing.contentEditable=true;
	this.highlight();
	oEditing.focus();
	var range=document.body.createTextRange().moveToElementText(oEditing);
	if(range)
		range.select();
}
Dot.prototype.endEditLabel=function(){
	this.oTree.bEdit=false;
	this.div('oTitle').innerHTML='&nbsp;'+this.sTitle+'&nbsp;';
	this.highlight();
	this.oTree.oTree.focus();
}
Dot.prototype.getState=function(state){
	var s=new Object();
	s.bOpen=this.bOpen;
	state[this.get()]=s;
	for(var i=0;i<this.tSub.length;++i)
		this.tSub[i].getState(state);
}
Dot.prototype.setState=function(state,fnOnFinish){
	var s=state[this.get()];
	if(s!=null&&s.bOpen!=this.bOpen)
		if(!this.toggle(fnOnFinish))
			return false;
	if(this.bOpen)
		for(var i=0;i<this.tSub.length;++i)
			if(!this.tSub[i].setState(state,fnOnFinish))
				return false;
	return true;
}

function xHttpRequest() // object prototype
{
  // Private Properties
  var _i = this; // instance object
  var _r = null; // XMLHttpRequest object
  var _t = null; // timer
  var _f = null; // callback function
  var _x = false; // XML response pending
  var _o = null; // user data object passed to _f
  var _c = false; // self-clean after send() completed?
  // Public Properties
  _i.OK = 0;
  _i.NOXMLOBJ = 1;
  _i.REQERR = 2;
  _i.TIMEOUT = 4;
  _i.RSPERR = 8;
  _i.NOXMLCT = 16;
  _i.ABORTED = 32;
  _i.status = _i.OK;
  _i.error = null;
  _i.busy = false;
  // Private Methods
  function _clean()
  {
    _i = null;
    _r = null;
    _t = null;
    _f = null;
    _x = false;
    _o = null;
    _c = false;
  }
  function _clrTimer()
  {
    if (_t) {
      clearTimeout(_t);
    }
    _t = null;
  }
  function _endCall()
  {
    if (_f) {
      _f(_r, _i.status, _o);
    }
    _f = null; _x = false; _o = null;
    _i.busy = false;
    if (_c) {
      _clean();
    }
  }
  function _abort(s)
  {
    _clrTimer();
    try {
      _r.onreadystatechange = function(){};
      _r.abort();
    }
    catch (e) {
      _i.status |= _i.RSPERR;
      _i.error = e;
    }
    _i.status |= s;
    _endCall();
  }
  function _newXHR()
  {
    try { _r = new XMLHttpRequest(); }
    catch (e) { try { _r = new ActiveXObject('Msxml2.XMLHTTP'); }
    catch (e) { try { _r = new ActiveXObject('Microsoft.XMLHTTP'); }
    catch (e) { _r = null; _i.error = e; }}}
    if (!_r) { _i.status |= _i.NOXMLOBJ; }
  }
  // Private Event Listeners
  function _oc() // onReadyStateChange
  {
    var ct;
    if (_r.readyState == 4) {
      _clrTimer();
      try {
        if (_r.status != 200) _i.status |= _i.RSPERR;
        if (_x) {
          ct = _r.getResponseHeader('Content-Type');
          if (ct && ct.indexOf('xml') == -1) { _i.status |= _i.NOXMLCT; }
        }
        delete _r['onreadystatechange']; // _r.onreadystatechange = null;
      }
      catch (e) {
        _i.status |= _i.RSPERR;
        _i.error = e;
      }
      _endCall();
    }
  }
  function _ot() // onTimeout
  {
    _t = null;
    _abort(_i.TIMEOUT);
  }
  // Public Methods
  this.send = function(m, u, d, t, r, x, o, f, c)
  {
    if (!_r || _i.busy) { return false; }
    _c = (c ? true : false);
    m = m.toUpperCase();
    if (m != 'POST') {
      if (d) {
        d = '?' + d;
        if (r) { d += '&' + r + '=' + Math.round(10000*Math.random()); }
      }
      else { d = ''; }
    }
    _x = (x ? true : false);
    _o = o;
    _f = f;
    _i.busy = true;
    _i.status = _i.OK;
    _i.error = null;
    if (t) { _t = setTimeout(_ot, t); }
    try {
      if (m == 'GET') {
        _r.open(m, u + d, true);
        d = null;
        _r.setRequestHeader('Cache-Control', 'no-cache');
        var ct = 'text/' + (_x ? 'xml':'plain');
        if (_r.overrideMimeType) {_r.overrideMimeType(ct);}
        _r.setRequestHeader('Content-Type', ct);
      }
      else if (m == 'POST') {
        _r.open(m, u, true);
        _r.setRequestHeader('Method', 'POST ' + u + ' HTTP/1.1');
        _r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      }
      else {
        _r.open(m, u + d, true);
        d = null;
      }
      _r.onreadystatechange = _oc;
      _r.send(d);
    }
    catch(e) {
      _clrTimer();
      _f = null; _x = false; _o = null;
      _i.busy = false;
      _i.status |= _i.REQERR;
      _i.error = e;
      if (_c) {
        _clean();
      }
      return false;
    }
    return true;
  };
  this.abort = function()
  {
    if (!_r || !_i.busy) { return false; }
    _abort(_i.ABORTED);
    return true;
  };
  this.reinit = function()
  {
    // Halt any HTTP request that may be in progress.
    this.abort();
    // Set all private vars to initial state.
    _clean();
    _i = this;
    // Set all (non-constant) public properties to initial state.
    _i.status = _i.OK;
    _i.error = null;
    _i.busy = false;
    // Create the private XMLHttpRequest object.
    _newXHR();
    return true;
  };
  // Constructor Code
  _newXHR();
}