/* ----------------------------------------------- 
Copyright (c) 2005-2009 Gennadii Varzugin
All rights reserved. You CAN NOT redistribute it and/or
modify it without permission granted.
-------------------------------------------------- */


if (typeof BASE_PATH=='undefined') {
	var BASE_PATH='';
};

if (typeof CSS_PATH=='undefined') {
	var CSS_PATH=BASE_PATH+'_styles/';
};

if (typeof JBIN_PATH=='undefined') {
	var JBIN_PATH=BASE_PATH+'jbin/';
};

function login() {
	window.location.href=BASE_PATH+'login.php';
};

function logout(evt) {
	if(evt) Event.cancel(evt);
	if(confirm('Are you sure you want to logout?')) {
	window.location.href=BASE_PATH+'logout.php';
	}
};

function extend (obj, srcobj) {
	for (var p in srcobj) obj[p]=srcobj[p];
};

function ImportProperties (obj, srcobj) {
	for (var p in srcobj) obj[p]=srcobj[p];
};

var Request=function (url,p,m) {
	this.url=url;
	if(p) this.params=p;
	if(m) this.method=m;
};

Request.prototype.onerror =function (st,str) {
	alert(str);
	return true;	
};

if (typeof XMLHttpRequest=='undefined') {
var XMLHttpRequest = function() {

var http=null;
var err=null;
try{
http = new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
err=e;
  try{
  http = new ActiveXObject('Microsoft.XMLHTTP');
  }catch(e){
  err=e;
  };
};
return http;
};
};

function UrlEncode (str) {
if (typeof str=='number') return str;

if (typeof CreateTransTable=='function') {
if (!TRANS_TABLE){CreateTransTable();};
var output='';
for (var i = 0; i < str.length; i++)
  {
    var n = str.charCodeAt(i);
    if (typeof TRANS_TABLE[n] == 'number') n = TRANS_TABLE[n];
    if (n <= 0xFF) output+=String.fromCharCode(n);
  }

	return escape(output);
} else {
	return escape(str);
}
};


if (typeof dHTML=="undefined") {

var dHTML={

widgets:[],

MOVING_OBJ:null,

iconSet:{},

addIcon: function (name,src,w,h) {
if (!this.iconSet[name]) {
	this.iconSet[name]={};
	this.iconSet[name].img=Element.img(src,w,h);
	this.iconSet[name].width=w;
	this.iconSet[name].height=h;
}	
},

isBrowser: function (str) {
	 var ub = navigator.userAgent.toLowerCase();
	 var isb= str.toLowerCase();
	 if ((ub.indexOf(isb) > -1)) {
	 	return true;
	 } else {
	 	return false;
	 }
},

getViewport: function () {
	var vp={};
	if (window.innerWidth) {
		vp.width=window.innerWidth;
		vp.height=window.innerHeight;
	};
	if (document.documentElement && document.documentElement.clientWidth) {
		vp.width=vp.width?Math.min(vp.width,document.documentElement.clientWidth):document.documentElement.clientWidth;
		vp.height=vp.height?Math.min(vp.height,document.documentElement.clientHeight):document.documentElement.clientHeight;
	};
	if(document.body && document.body.clientWidth) {
		vp.width=vp.width?Math.min(vp.width,document.body.clientWidth):document.body.clientWidth;
    	vp.height=vp.height?Math.min(vp.height,document.body.clientHeight):document.body.clientHeight;
	};

	if (typeof window.pageYOffset=='number') {
		vp.top=window.pageYOffset;
		vp.left=window.pageXOffset;
	} else {
    	vp.top = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
    	vp.left = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
	};
	return vp;
},

getIcon:function (name) {
	if (this.iconSet[name]) {
		var img=this.iconSet[name].img.cloneNode(true);
		if(this.iconSet[name].width) img.width=this.iconSet[name].width;
		if(this.iconSet[name].height) img.height=this.iconSet[name].height;
		return img;
	};
	return null;
},

startMove: function (o,evt) {
o.startX=evt.clientX;
o.startY=evt.clientY;
var wleft=o.container.style.left;
var wtop=o.container.style.top;

if (wleft && wtop) {
o.startLeft=parseInt(wleft.replace(/\D/g,""));
o.startTop=parseInt(wtop.replace(/\D/g,""));

dHTML.MOVING_OBJ=o;
if(document.body && document.body.setCapture) {
document.body.setCapture();
};
Event.bind(document,'mouseup',dHTML.stopMove,null,true);
Event.bind(document,'mousemove',trackMouseMove,null,true);	
}
},

stopMove: function () {
if(document.body && document.body.setCapture) {
document.body.releaseCapture();	
};
Event.remove(document,'mouseup',dHTML.stopMove,true);
Event.remove(document,'mousemove',trackMouseMove,true);	
if (dHTML.MOVING_OBJ.stopMove) {
	dHTML.MOVING_OBJ.stopMove();
};
	dHTML.MOVING_OBJ=null;
},

registerWidget: function (o) {
	this.widgets[this.widgets.length]=o;
	if(o.name) {
	this.widgets[o.name]=o;	
	} else if (o.id) {
	this.widgets[o.id]=o;	
	}
},
windowLoaded: false,

initWidgets: null,

createWidget: function (eid,type,name,o) {
	var ready=(this.windowLoaded)?1:((document.readyState=='complete')?1:0);
	if (ready) {
	var e=(typeof eid=="string")?document.getElementById(eid):eid;
	if (typeof window[type]=='function') {
	var w=new window[type](e);
	w.type=type;
	if (!w.container) w.container=e;
	
	w.name=name;
	if (w.Init) {
	w.Init(o);
	this.registerWidget(w);
	};
	return w;	
	}
	} else {
	var wo={id:eid,type:type,name:name,o:o};
	if (!this.initWidgets) this.initWidgets=[];
	this.initWidgets[this.initWidgets.length]=wo;
	}
	return null;
},

getObject: function (ro) {
	var str=dHTML.getRemote(ro);
 	var o=null;
 	if (typeof str!='undefined') {
 	try {
	eval('o='+str);
 	}catch (err){ 
 	alert('Error: server returns invalid response: '+str);
 	throw 'server returns invalid responce';
 	}
 	}
 	return o;
},

getRemote: function (robj, tobj, omethod) {
	if (robj && typeof robj.url=='string')
	{
		var http=new XMLHttpRequest();
		if (http) {
			var method='GET';
			var params='';
			var url=robj.url;
			if (typeof robj.params=='object') {
				var po=robj.params;
				for (var p in po) {
					if (params!='') params=params+'&';
					if(po[p]!==null) params+=UrlEncode(p)+'='+UrlEncode(po[p]);
				};
			};
			if (robj.method && robj.method=='POST'){method='POST';};

			if (tobj) {
			http.onreadystatechange=function() {
				if(!omethod) omethod='Update';
				if(http.readyState==4) {
					var rt=(robj.requestType)?robj.requestType:'json';
					robj.response=http;
					
					if(http.status && http.status==403) return login();
					var errevt=true;
					
					if (typeof tobj=='function') {
						if(errevt)tobj(http.responseText,robj);
					} else if (rt=='json' && typeof tobj=='object' && tobj[omethod]) {
			         var ret=null;
					 	try {
						eval('ret='+http.responseText);
					 	}catch (e){ 
                	 	if (robj.onerror)errevt=robj.onerror(-1,http.responseText);
					 	};
					 	if(errevt)tobj[omethod](ret,robj);
					} else if (rt=='text' && typeof tobj=='object' && tobj[omethod]) {
						if(errevt)tobj[omethod](http.responseText,robj);
					}
				}
			};
			};
			if (method=='GET') {
				if(params!=''){url=url+'?'+params;};
				params=null;
			}; 
			http.open(method,url,(tobj? true:false));
			if (method=='POST'){
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			};

			http.send(params);
			if (!tobj) {
					if(http.status && http.status==403) {
						login();
						throw "403 Access Forbidden";
					}
					return http.responseText;
			}
			return null;
		} else {
			return null;
		};
	} else {
		return robj;
	};
},


printObject: function (e, obj) {
if (e.nodeType==1 && obj) {
	
		if (typeof obj.tag=='string' && obj.tag!='') {
		var ne=document.createElement(obj.tag);
		this.setAttributes(ne, obj.attrs);
		ne=e.appendChild(ne);
		if (obj.text) {
			var txt=document.createTextNode(obj.text);
			ne.appendChild(txt);
		}
		var chs=obj.children;
		if (chs && chs.length) {
		var i;
		 for (i=0;i<chs.length ;i++) {
			var nobj=chs[i];
			this.printObject(ne,nobj);
		 }
		}
		return ne;
		} else if (typeof obj.text=='string' || typeof obj.text=='number'){
			var txt=document.createTextNode(obj.text);
			e.appendChild(txt);
		} else if (typeof obj.attrs!='undefined') {
            this.setAttributes(e, obj.attrs);
		};
}
return e;
},
WM: {
	wins:[],
	zIndex:1000,
	setzIndex: function (w,rm) {
		var nwl=[];
		if(!w || !w.container) return false;
		var wins_count=this.wins.length;
		var top_win=(wins_count>0)?this.wins[wins_count - 1]:null;
		for (var i=0;i<wins_count;i++) {
			if(w!=this.wins[i]) {
				var win=this.wins[i];
				if (win.container) {
				if(win.zindexdisabled!=true) {
					win.container.style.zIndex=this.zIndex+nwl.length;
				};
				nwl[nwl.length]=win;
				}
			}
		};
		var nzi;
		if (rm) {nzi=this.zIndex} else {nzi=this.zIndex+nwl.length;};
		if(w.zindexdisabled!=true) {
			w.container.style.zIndex=nzi;
		};
		this.wins=nwl;
		if(!rm) {
			nwl[nwl.length]=w;
			if (top_win!=w) {
			 var nd=false;
			 if (w.onwindowup) nd=w.onwindowup(top_win);
			 if (top_win && top_win.onwindowdown) {
			 if (nd || top_win.isForcedDown) top_win.onwindowdown();
			 }
			}
		} else {
			if (top_win && top_win==w) {
			top_win=(nwl.length>0)?nwl[nwl.length - 1]:null;
			if (top_win && top_win.onwindowup) top_win.onwindowup();	
			}
		};
		return true;
	}
},
Init: function () {
	var wa=dHTML.initWidgets;
	dHTML.windowLoaded=true;
	if (wa) {
	for (var i=0;i<wa.length;i++) {
		dHTML.createWidget(wa[i].id,wa[i].type,wa[i].name,wa[i].o);
	};
	};
	dHTML.initWidgets=null;
},

Unload: function () {
this.widgets=null;
this.iconSet=null;
dHTML=null;	
},

setAttributes: function (e, attr) {
		if (e && attr && attr.length) {
		var k=0;
		for (k=0;k<attr.length;k++)	{
			var aobj=attr[k];
			if (typeof aobj.name=='string' && typeof aobj.value!='undefined')
			{   if (aobj.name=='class' || aobj.name=='className'){
				e.className=aobj.value;
			    } else if(aobj.name=='style' && e.style) {
				e.style.cssText=aobj.value;
			    } else {
				e.setAttribute(aobj.name,aobj.value);
				};
			};
		};
		} else if (e && attr && typeof attr=='object') {
		for (a in attr) {
			if (a=='class' || a=='className') {
			e.className=attr[a];	
			} else if (a=='style') {
			e.style.cssText=attr[a];	
			} else if (typeof attr[a]=='function'){
			
			} else {
			e.setAttribute(a,attr[a]);	
			}
		}
		};
}	
};
//end dHTML

};

//Events
if (!window.Event) {
  var Event = new Object();
};

Event.stop=function (evt) {
if (evt.stopPropagation) {evt.stopPropagation();} else {evt.cancelBubble=true;}; 
if (evt.preventDefault) {evt.preventDefault();} else {evt.returnValue=false;}; 
};

Event.cancel=function (evt) {
if (evt && evt.preventDefault) {evt.preventDefault();} else if (evt) {evt.returnValue=false;}; 
};

Event.setPageXY=function (evt) {
	if (!evt.pageX)	{
	var y=window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	var x=window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
	evt.pageX=evt.clientX + x;
	evt.pageY=evt.clientY + y;
	};
	return evt;
};

Event.bind=function (src,type,trg,tfunc,ec) {
	if(!src) return null;
	var he;
	var capture=(ec)?true:false;
	if(!tfunc) tfunc='on'+type;
	if (typeof trg=='function')	{
		he=trg;
	} else {
		he=function (evt){HandleEvent.call(trg,tfunc,evt);};
	};
	if (src.addEventListener){
		src.addEventListener(type, he, capture);
	} else if (src.attachEvent){
		src.attachEvent('on'+type,he);
	} else {src['on'+type]=he;};
	return he;
};

Event.remove=function (src,type,fn,ec) {
	var capture=(ec)?true:false;
	if (src.removeEventListener) {
		src.removeEventListener(type,fn,capture);
	} else if (src.detachEvent) {
		src.detachEvent('on'+type,fn);
	} else {
		src['on'+type]=null;
	}
};

function HandleEvent (tfunc,evt) {
	evt=(evt)? evt:((window.event)? event:null);
	if (evt) {
		var trg=this;
		if (!evt.target && evt.srcElement) evt.target=evt.srcElement;
		if (typeof trg[tfunc]=='function') trg[tfunc](evt);
	}
};

function trackMouseMove (evt) {
if (!evt) evt=window.event;
dHTML.MOVING_OBJ.move(evt);
};

//end events

//--- lib functions
if (!window.Element) {
  var Element = new Object();
};

String.prototype.camelize = function() {
    var str=this;
    var re=/\-([a-z])/;
	while(re.test(str)) {
		str=str.replace(re,RegExp.$1.toUpperCase());
	}
    return str;
};
String.prototype.trim=function () {
    var str=this;
    var re=/^\s+/;
	str=str.replace(re,'');
    re=/\s+$/;
	str=str.replace(re,'');
	return str;
};
String.prototype.n2br=function () {
	return this.replace(/\n/,'<br />');
};

Element.div=function (cl) {
var body=(document.body)? document.body : document.getElementsByTagName("body")[0];
var div=document.createElement('div');
if (cl) div.className=cl;
body.appendChild(div);
return div;	
};

Element.moveToViewport=function (e) {
	if(!e) return false;
	var ret=true;
	var vp=dHTML.getViewport();
	var ep=Element.getPosition(e);
	var s=ep.top;
	var d=ep.top+ep.height-vp.top-vp.height;
	if (ep.height>vp.height) {
		s=vp.top;
		ret=false;
	} else if (vp.top>ep.top) { 
		s=vp.top;
	} else if(d>0) {
		s=ep.top - d;
	}
	if(s!=ep.top) e.style.top=s+'px';
	s=ep.left;
	d=ep.left+ep.width-vp.left-vp.width;
	if (ep.width>vp.width) {
		s=vp.left;
		ret=false;
	} else if (vp.left>ep.left) { 
		s=vp.left;
	} else if(d>0) {
		s=ep.left - d;
	}
	if(s!=ep.left) e.style.left=s+'px';
	return ret;
};

Element.get=function (eid) {
if (eid) {
var e=(typeof eid=="string")?document.getElementById(eid):eid;
return e;
};
return null;
};

Element.img=function (src,w,h) {
	var img;
	if (typeof src!='string') return src;
	if (w && h) {
	img=new Image(w,h);
	} else {
	img=new Image();
	}
	img.src=src;
	return img;
};

Element.rm=function (e) {
	if (e){var p=e.parentNode;p.removeChild(e);}
};

Element.rmAll=function (e) {
if (e) {
var chs=e.childNodes;
var i;
	if (chs){for (i=0;i<chs.length;i++){Element.rm(chs[i]);};};
return e;
};
return null;
};

Element.getPosition=function (e) {
var h=e.offsetHeight;
var w=e.offsetWidth;

if (e.getBoundingClientRect) {
	var box=e.getBoundingClientRect();
    var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
    var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
	return {height: h, width: w, top: box.top-2+scrollTop, left: box.left-2+scrollLeft};
} else {
	var op=e.offsetParent;
	var l=e.offsetLeft;
	var t=e.offsetTop;
		while (op) {	
			l+=op.offsetLeft;
			t+=op.offsetTop;
			op=op.offsetParent;
		}
	return {height: h, width: w, top: t, left: l};
};
};

Element.getStyle = function(e,name) {
   	if (e.style[name.camelize()]) {return e.style[name.camelize()];};
	if (e.currentStyle){
		return e.currentStyle[name.camelize()];
	} else {
		var pname=name.replace(/([A-Z])/g, "-$1" ).toLowerCase();
		if (document.defaultView) {
			var cs=document.defaultView.getComputedStyle(e, null);
			if (cs){
			return cs.getPropertyValue(pname);
			}; 
		};
	};
	return null;
};

Element.setStyle = function(e, style) {
    for (name in style)e.style[name.camelize()] = style[name];
};

Element.getOpacity = function(e){  
  var opacity;
  if (opacity = Element.getStyle(e, 'opacity'))  
    return parseFloat(opacity);  
  if (opacity = (Element.getStyle(e, 'filter') || '').match(/alpha\(opacity=(.*)\)/))  
    if(opacity[1]) return parseFloat(opacity[1]) / 100;  
  return 1.0;  
};

Element.setOpacity = function(element, value){  
  if (value == 1){
    Element.setStyle(element, { opacity: 
      (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 
      0.999999 : null });
    if(/MSIE/.test(navigator.userAgent))  
      Element.setStyle(element, {filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});  
  } else {  
    if(value < 0.00001) value = 0;  
    Element.setStyle(element, {opacity: value});
    if(/MSIE/.test(navigator.userAgent))  
     Element.setStyle(element, 
       { filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') +
                 'alpha(opacity='+value*100+')' });  
  }   
};  

Event.bind(window,'load',dHTML.Init);
Event.bind(window,'unload',dHTML.Unload);


//Node
var Node=function (parent) {
	this.parent=parent;
};

Node.newNode=function (o) {
	var n=null;
	if (o && o.type && typeof window[o.type]=='function'){
	n=new window[o.type]();
	};
	return n;	
};

Node.prototype.Ancestors=function () {
var n=this;
var a=[];
while (n.parent)
{
	n=n.parent;
	if (n.parent) a[a.length]=n;
}
return a;
};

Node.prototype.getUrl=function () {
if (this.url) return this.url; 
var n=this;
while (n.parent) {
	n=n.parent;
	if (n.url) return n.url;
};
return this.tree.url;
};

Node.prototype.getName=function () {
if (this.name) return this.name; 
var n=this;
while (n.parent) {
	n=n.parent;
	if (n.name) return n.name;
};
return this.tree.name;
};

Node.prototype.IsLocked=function () {
if (this.lock) return true;
var n=this;
var a=this.Ancestors();
var i=0;
while (a[i]) {
	if (a[i].lock) return true;
	i++;
}
return false;
};


Node.prototype.IsFirst=function () {
var chs=null;
if (this.parent) { 
chs=this.parent.children;
if (chs && chs[0]===this) {return true;} else {return false;};
} else {return true;};
};

Node.prototype.IsLast=function () {
var chs=null;
if (this.parent) { 
chs=this.parent.children;
if (chs && chs[chs.length-1]===this){return true;} else {return false;};
} else {return true;};
};

Node.prototype.Position=function () {
	if (!this.parent) return -1;
	var chs=this.parent.children;
	var i=0;
		while (chs[i]) {
			if (chs[i]==this) return i;
			i++;
	    };
    return null;
};

Node.prototype.ExpandAll=function () {
this.expand=true;
if (this.children){
var i;
for (i=0;i<this.children.length;i++) {this.children[i].ExpandAll();};
};
};

Node.prototype.beforeInit=function (o) {};

Node.prototype.Init=function (o,tree) {
this.beforeInit(o);
extend(this,o);
this.tree=tree;
if (o) {
var chs=o.children;
if (chs && chs.length)
{var i;
 this.children=[];
   for (i=0;i<chs.length;i++ )
   {
	   var ch=chs[i];
	   var n;
	   if (ch.type && typeof window[ch.type]=='function'){
		   n=new window[ch.type](this);
	   } else {
	   	   if (typeof window['TreeNode']=='function') {
	   	   n=new TreeNode(this);
	   	   } else {
	   	   n=new Node(this);	
	   	   }
	   };
       n.parent=this;
       this.children[this.children.length]=n;
       n.Init(ch,tree);
   };
if (!this.children[0]) this.children=null;
};
this.onInit(o);
};
};

Node.prototype.onInit=function (o) {};

Node.prototype.appendChild=function (o) {
	var n=Node.newNode(o);
	if (n){
	n.parent=this;
	n.Init(o,this.tree);
	if (!this.children) this.children=[];
	this.children[this.children.length]=n;
	};
	return n;
};

Node.prototype.insertfirstChild=function (o) {
	var n=Node.newNode(o);
	if (n){
	n.parent=this;
	n.Init(o,this.tree);
	if (!this.children) this.children=[];
	this.children.unshift(n);
	};
	return n;
};

Node.prototype.removeNode=function (n) {
	var chs=this.children;
	if (n && chs){
	var nchs=[];
	var i;
	for (i=0;i<chs.length;i++) {
		var ch=chs[i];
		if (ch!==n)	{
			nchs[nchs.length]=ch;
		} else {
			n.Clean(true);
	        n.parent=null;
		};
	}
	if (nchs.length>0){this.children=nchs;} else {this.children=null;};
	return n;
	}
	return null;
};


Node.prototype.removeChildren=function () {
	var chs=this.children;
	if (chs) {   
		var i;
		for (i=0;i<chs.length;i++) chs[i].Destroy();
	}
    this.children=null;	
};

Node.prototype.onDestroy=function () {};

Node.prototype.Destroy=function () {
	this.onDestroy();
	this.Clean(true);
	this.removeChildren();
	this.parent=null;
	this.tree=null;
};

Node.prototype.Clean=function () {};

Node.prototype.onClean=function () {};

function dForm (eid) {
this.container=Element.get(eid);
};

dForm.prototype.disableFields=function(t,name) {
var inputs=this.getElements(t,name);
for (var i=0;i<inputs.length;i++) {
inputs[i].disabled=true;
}	
};

dForm.prototype.enableFields=function(t,name) {
var inputs=this.getElements(t,name);
for (var i=0;i<inputs.length;i++) {
inputs[i].disabled=false;
}	
};

dForm.prototype.disable=function() {
if (!this.isDisabled) {
var inputs=this.getElements('input');
for (var i=0;i<inputs.length;i++) {
if (inputs[i].type=='submit') {inputs[i].disabled=true;};
};	
this.isDisabled=true;
}
};

dForm.prototype.enable=function() {
if (this.isDisabled) {
var inputs=this.getElements('input');
for (var i=0;i<inputs.length;i++) {
if (inputs[i].type=='submit') {inputs[i].disabled=false;};
};	
this.isDisabled=false;
}
};

dForm.prototype.getElements=function(t,nm) {
var inputs=this.container.getElementsByTagName(t);
if(!nm) return inputs; 	
var inm=[];
for (var i=0;i<inputs.length;i++) {
if (inputs[i].name==nm) {inm[inm.length]=inputs[i];}
};
return inm;
};


dForm.prototype.getParams=function() {
var prms={};
var etypes=['input','textarea','select'];
for (k=0;k<3;k++) {
var inputs=this.container.getElementsByTagName(etypes[k]);
if (inputs && inputs.length) {
for (var i=0;i<inputs.length;i++) {
	var nm=inputs[i].name;
	if (nm) {
	var v=inputs[i].value;
	var t=inputs[i].type;
	if (etypes[k]=='input' && t=='checkbox') {
		if (typeof prms[nm]=='undefined' || prms[nm]===null) {
			if (inputs[i].checked){prms[nm]=v;} else {prms[nm]=null;};
		} else if (typeof prms[nm]=='object' && inputs[i].checked) {
			prms[nm][prms[nm].length]=v;
		} else if (inputs[i].checked) {
			var v1=prms[nm];
			prms[nm]=[v1,v];
		}
	} else if(etypes[k]=='input' && t=='radio') { 
		if (inputs[i].checked) prms[nm]=v;
	} else {
		prms[nm]=v;
	}
	}
}
}
};
this.updateParams(prms);
return this.params;
};

dForm.prototype.setDefaultValues=function (o) {
	if (o) {
		this.defaultvalues=o;
	} else { 
		this.defaultvalues={};
		extend(this.defaultvalues,this.getParams());
	}
};

dForm.prototype.restoreDefault=function () {
	if (this.defaultvalues) {
		this.setParams(this.defaultvalues);
	}
};

dForm.prototype.Init=function(o) {
extend(this,o);
if (this.params)this.params=null;
var inputs=this.container.getElementsByTagName('input');
if (inputs && inputs.length) { 
for (var i=0;i<inputs.length;i++) {
	var nm=inputs[i].name;
	var v=inputs[i].value;
	var t=inputs[i].type;
	if (t=='button' || t=='submit') {
		if (nm && typeof this[nm]=='function') {
			Event.bind(inputs[i],'click',this,nm);
		} else {
			if (typeof this.onclickexecaction=='function') {
			Event.bind(inputs[i],'click',this,'onclickexecaction');	
			}
		}
	}
}
};

if (o && o.params) this.setParams(o.params);
};

dForm.prototype.updateParams=function (o) {
	this.params={};
	extend(this.params,o);
};

dForm.prototype.onclickexecaction=function (evt) {
	Event.cancel(evt);
	if(!this.isDisabled) {
	var w=(typeof this.targetwidget=='object')?this.targetwidget:(this.targetwidget?dHTML.widgets[this.targetwidget]:null);
	if (w) {
		var src=evt.target;
		this.actionname=src.name;
		if (w.objectselected && typeof w.objectselected.execAction=='function') {
			w.objectselected.execAction(this);
		} else if (typeof w.execAction=='function') {
			w.execAction(this);
		};
	}
	}
};

dForm.prototype.setParams=function(o) {
this.updateParams(o);
var etypes=['input','textarea','select'];
for (k=0;k<3;k++) {
var inputs=this.container.getElementsByTagName(etypes[k]);
if (inputs && inputs.length) { 
for (var i=0;i<inputs.length;i++) {
var nm=inputs[i].name;
if (typeof this.params[nm]!='undefined' && typeof this.params[nm]!='function') {
	var v=inputs[i].value;
	var t=inputs[i].type;
	if (etypes[k]=='input' && t=='checkbox') {
		if (typeof this.params[nm]=='object' && this.params[nm]!==null) {
			var ap=this.params[nm];
			var chk=false;
			for (var j=0;j<ap.length;j++) {
				if (v==ap[j]) {
					chk=true;
					break;
				}
			};
			inputs[i].checked=chk;
		} else { 
		  if (this.params[nm]==v) {
		  inputs[i].checked=true;
		  } else {
		  inputs[i].checked=false;	
		  }	
		}
	} else if (etypes[k]=='input' && t=='radio') { 
		  if (this.params[nm]==v) {
		  inputs[i].checked=true;
		  } else {
		  inputs[i].checked=false;	
		  }	
	} else {
		inputs[i].value=this.params[nm];
	}
}
}
}
}
};

function dWindow (e) {
this.container=Element.get(e);
if (this.container)this.container.style.display='none';
};

dWindow.prototype.move=function (evt) {
var wtop=this.startTop+ evt.clientY - this.startY;
wtop=(wtop>=0)?wtop:0;
var wleft=this.startLeft + evt.clientX - this.startX;
this.container.style.top=wtop+'px';
this.container.style.left=wleft+'px';  	
};

dWindow.prototype.getPosition=function () {
	var pos={};
	pos.top=parseInt(this.container.style.top.replace(/\D/g,""));
	pos.left=parseInt(this.container.style.left.replace(/\D/g,""));
	return pos;
};

dWindow.prototype.dragStart=function (evt) {
dHTML.startMove(this,evt);	
};
dWindow.prototype.Destroy=function () {this.destroy();alert('remove destroy');};
dWindow.prototype.destroy=function () {
Element.rm(this.container);
this.container=null;
if (this.name && dHTML.widgets[this.name]) dHTML.widgets[this.name]=null;	
};

dWindow.prototype.movable=true;

dWindow.prototype.setChildWindow=function(name,win) {
if(!this.childWindows) this.childWindows={};
this.childWindows[name]=win;
};

dWindow.prototype.setTitle=function(str) {
	this.title.innerHTML=str;
};
dWindow.prototype.putOnTop=function (evt) {
dHTML.WM.setzIndex(this);
};

dWindow.prototype.Init=function(o) {
extend(this,o);
if(!this.container) this.container=Element.div('window');
var div=this.container;
var pdiv=dHTML.printObject(div,{tag:'div', attrs:[{name:'class', value:'winpanel'}]});
if(this.movable){
	Event.bind(pdiv,'mousedown',this,'dragStart');
	Event.bind(div,'mousedown',this,'putOnTop');
};
var cl=dHTML.printObject(pdiv,{tag:'span', attrs:[{name:'class', value:'closelabel'}]});
Event.bind(cl,'click',this,'close');
if (o && o.icon) {
	this.icon=Element.img(o.icon);
};
if (this.icon) {
	this.icon.className='winicon';
    pdiv.appendChild(this.icon);
};
if (o && o.title) {
	cl=dHTML.printObject(pdiv,{tag:'h3', attrs:[{name:'class', value:'wintitle'}]});
    cl.innerHTML=o.title;
    this.title=cl;
};
this.content=dHTML.printObject(div,{tag:'div', attrs:{'class':'wincontent'}});
if(this.defaultposition) this.setPosition(this.defaultposition);
if (typeof this.onInit=='function') this.onInit();
};

dWindow.prototype.addContent=function (e) {
return this.content.appendChild(e);	
};

dWindow.prototype.setPosition=function (pos) {
if (pos) {
	if (pos.top) this.container.style.top=pos.top+'px';
	if (pos.left) this.container.style.left=pos.left+'px';
	if (pos.width) this.container.style.width=pos.width+'px';
	if (pos.height) this.container.style.height=pos.height+'px';
}	
};
dWindow.prototype.open=function (pos) {
this.setPosition(pos);
if (typeof this.onopen=='function') this.onopen();
if (this.container) { 
this.container.style.display='block';
dHTML.WM.setzIndex(this);
this.isOpen=true;
if (this.movable) Element.moveToViewport(this.container);
}
};

dWindow.prototype.close=function (evt) {
if (evt) Event.stop(evt);
var windows=this.childWindows;
if (windows) {
for (var p in windows) {
windows[p].close();
}
};
if (this.container) { 
this.container.style.display='none';
dHTML.WM.setzIndex(this,true);
this.isOpen=false;
}
if (typeof this.onclose=='function') this.onclose();
};
//table
function TCell (p) {
this.parent=p;
};

TCell.prototype=new Node();

TCell.prototype.drawNode=function(td) {
	td.innerHTML=this.value;
};

TCell.prototype.setValue=function(v) {
	this.value=v;
	this.container.innerHTML=v;
};

function TControl (p) {this.parent=p;}
TControl.prototype=new TCell();
TControl.prototype.drawNode=function(td) {
	var img=null;
	if (this.iconName) {
		img=dHTML.getIcon(this.iconName);
	} else if (this.icon) {
		img=Element.img(this.icon);
	} else {
		img=new Image();
	};
	img.style.cursor='pointer';
	td.appendChild(img);
	var o=this.parent.getAccocObject();
	if (o && this.action && o[this.action])Event.bind(img,'click',o,this.action);
};

function TRow (p) {this.parent=p;};
TRow.prototype=new Node();

TRow.prototype.getValue=function(name) {
var cell=this.getCell(name);
if (cell) return cell.value;	
return null;
}; 

TRow.prototype.refresh=function () {
	if(!this.hidden) {
	var n=this.getAccocObject();
	var c=this.tree.createRow(n);
	this.setRow(c);
	}
};

TRow.prototype.setRow=function(o) {
	for (p in o) {
		var cell=this.getCell(p);
		if (cell && cell.setValue) cell.setValue(o[p]);
	}
};

TRow.prototype.getObject=function () {
var chs=this.children;
var p={};
for (var i=0;i<chs.length;i++) {
if(chs[i].name)p[chs[i].name]=chs[i].value; 
};
return p;
};

TRow.prototype.getAccocObject=function() {
	if (typeof this.objectindex!='undefined') {
		return this.tree.objectsList[this.objectindex];
	};
	return null;
};

TRow.prototype.getCell=function(name) {
var chs=this.children;
for (var i=0;i<chs.length;i++) {
if(chs[i].name==name)return chs[i]; 
};
return null;	
};

TRow.prototype.drawNode=function (ist) {
if (this.children && this.tree.tbody && !this.hidden) {
var c=this.tree.pushTR(ist);
this.container=c;
var chs=this.children;
for (var i=0;i<chs.length;i++) {
var td=dHTML.printObject(c,{tag:'td'});
chs[i].container=td;
chs[i].drawNode(td);
};	
};
};

function ListWidget (eid) {
this.container=Element.get(eid);
};

ListWidget.prototype.getById=function (id,idname) {
if (this.objectsList) {
	var idn=idname?idname:'id';
	for (var j=0;j<this.objectsList.length;j++) {
		if (this.objectsList[j].data[idn]==id) {
			return this.objectsList[j];
		};
	}
};
return null;
};

ListWidget.prototype.createList=function (a) {
	this.objectsList=[];
	if (a) {
	for (var i=0;i<a.length;i++) {
	var t=(a[i].type)?a[i].type:this.objectclass;
	   if (t && typeof window[t]=='function') {
		  var n=new window[t]();
		  n.type=t;
		  n.data=a[i];
		  this.objectsList[this.objectsList.length]=n;
		  if (n.Init) n.Init(this);  
	   }
	}
	}
};

ListWidget.prototype.Init=function (a) {
this.clearList();
this.createList(a);	
};

ListWidget.prototype.get=function (ro) {
if (!ro.url)ro.url=this.url;	
try {
var fa=dHTML.getObject(ro);
} catch (err){return false;};
this.Init(fa);
return true;
};


ListWidget.prototype.clearList=function () {
	this.objectselected=null;
	if (this.objectsList) {
	for (var i=0;i<this.objectsList.length;i++) {
	if (this.objectsList[i].onclear) this.objectsList[i].onclear(this);
		if (this.objectsList[i].container) {
		Element.rm(this.objectsList[i].container);
		this.objectsList[i].container=null;
		}
	}
	};
	this.isDrawn=false;
};

ListWidget.prototype.deleteObject=function (n) {
	if (this.objectsList) {
	var newlist=[];
	for (var i=0;i<this.objectsList.length;i++) {
		if (this.objectsList[i]!=n) {
			newlist[newlist.length]=this.objectsList[i];
		} else {
			if (this.objectsList[i].container) {
			Element.rm(this.objectsList[i].container);
			this.objectsList[i].container=null;
			}
		}	
	};
	this.objectsList=newlist;	
	};
	return n;	
};

ListWidget.prototype.clear=function () {
this.clearList();
};

ListWidget.prototype.draw=function (eid) {
	c=Element.get(eid);
	if (c) this.container=c;
	if (this.ondraw) this.ondraw();
	for (var i=0;i<this.objectsList.length;i++) {
	this.objectsList[i].drawItem(this);
	};
	this.isDrawn=true;	
};

ListWidget.prototype.execAction=function(f) {
var p=f.getParams();
p.action=f.actionname;
var ro=new Request(this.url,p,'POST');
ro.form=f;
ro.actionname=p.action;
if (this.onexecaction && this.onexecaction(ro)) {
if (f.disable) f.disable();
dHTML.getRemote(ro,this,'execInsert');
}
};

ListWidget.prototype.execInsert=function(o,ro) {
var n=null;
var ievt=true;
if(ro && ro.form && ro.form.enable) ro.form.enable();
if(typeof this.oncompleteinsert=='function') ievt=this.oncompleteinsert(o,ro);
if(ievt) {
var a=ro.actionname;
n=this.insertObject(o);
if (n)this.countinsert=this.countinsert?this.countinsert+1:1;
if (typeof this.oncompleteaction=='function') this.oncompleteaction(a,n);
};
return n;
};

ListWidget.prototype.insertObject=function(o) {
var n=null;
if (o) {
	var t=(o.type)?o.type:this.objectclass;
	if (t && typeof window[t]=='function') {
		if (!this.objectsList) this.objectsList=[];
		n=new window[t]();
		n.type=t;
		n.data=o;
		if(n.Init) n.Init(this);
		if (!this.insertontop) {
		this.objectsList[this.objectsList.length]=n;
		n.drawItem(this);  
		} else {
		this.objectsList.unshift(n);	
		n.drawItem(this,true);  
		}
	}	

};
return n;
};

//to delete
ListWidget.prototype.Draw=function () {
this.draw();alert('rename Draw');
};

ListWidget.prototype.Clear=function () {
this.clear();alert('rename Clear');
};
//end to delete

function TableWidget (eid) {
this.container=Element.get(eid);
};
TableWidget.prototype=new ListWidget();
TableWidget.prototype.pushTR=function (ist) {
	var tr=null;
	if (ist) {
	if (this.tbody) {
	var trs=this.tbody.childNodes;
		if (trs[0]) {
		var tr1=trs[0];
		tr=document.createElement('tr');
		this.tbody.insertBefore(tr,tr1);
		return tr;
		}
	}
	};
	var tr=document.createElement('tr');
	var cl=(this.isoddtr)?this.zebraclass:this.rowclass;
	if (cl) tr.className=cl;
	this.isoddtr=(this.isoddtr)?0:1;
	this.tbody.appendChild(tr);
	return tr;
};

TableWidget.prototype.removeRow=function(r) {
	var n=this.root.removeNode(r);
	if (n && n.container) {
		Element.rm(n.container);
		n.container=null;
	}
};

TableWidget.prototype.createRow=function (n) {
	var t=this.template;
	var c={};
		for (var j=0;j<t.length;j++) {
			if (t[j].name) {
				var pn=t[j].name;
				if (typeof n[pn]=='function') {
					c[pn]=n[pn]();
				} else {
					c[pn]=n.data[pn];
				}
			}
		};
	return c;
};

TableWidget.prototype.createHead=function (cf) {
	var conf=(cf)?cf:this.configure;
	this.configure=conf || {};
	var t=this.template;
	var tb=document.createElement("table");
	tb.cellSpacing = (conf && conf.cellSpacing)?conf.cellSpacing:0;
	if (conf && conf.cellPadding) tb.cellPadding = conf.cellPadding;
	if (conf && conf.className) tb.className=conf.className;
	if (conf && conf.id) tb.setAttribute('id',conf.id);

	if (!this.configure.omithead) {
	var thead=document.createElement("thead");
	var headRow=document.createElement("tr");
	for (var i=0;i<t.length;i++) {
	var headerCell = document.createElement("th");
	if (t[i].className) headerCell.className=t[i].className;
	if (t[i].headicon) {
	headerCell.appendChild(Element.img(headicon));	
	} else if(t[i].headiconName) {
	headerCell.appendChild(dHTML.getIcon(t[i].headiconName));	
	} else {
	headerCell.appendChild(document.createTextNode(t[i].title));
	}	
	
	headRow.appendChild(headerCell);
	}
	thead.appendChild(headRow);
	tb.appendChild(thead);
	};
	
	var tbody=document.createElement("tbody");
	tb.appendChild(tbody);
	if (this.container) this.container.appendChild(tb);
	this.tbody=tbody;
	return tb;
};

TableWidget.prototype.createTable=function (a) {
	var t=this.template;
	if(a)this.createList(a);
	var rows=[];
	for (var i=0;i<this.objectsList.length;i++) {
		var n=this.objectsList[i];
		var cells=[];
		for (var j=0;j<t.length;j++) {
			var c={};
			extend(c,t[j]);
			if (c.name) {
				var pn=c.name;
				if (typeof n[pn]=='function') {
					c.value=n[pn]();
				} else {
					c.value=n.data[pn];
				}
			};
		cells[cells.length]=c;	
		};
		var row={type:'TRow',children:cells};
		rows[rows.length]=row;
	};
	var o={children:rows};
	this.root=new Node(null);
	this.root.Init(o,this);
	this.setObjectIndex();
	return o;
};

TableWidget.prototype.setObjectIndex=function () {
	if (this.objectsList && this.root) {
	for (var i=0;i<this.objectsList.length;i++) {
		this.objectsList[i].row=this.root.children[i];
		this.root.children[i].objectindex=i;
	}
	}	
};

TableWidget.prototype.removeAccocObject=function (n){
var newlist=[];
for (var i=0;i<this.objectsList.length;i++) {
	if (this.objectsList[i]!=n) {
		var o=this.objectsList[i];
		var p=newlist.length;
		newlist[p]=o;
		o.row.objectindex=p;
	} else {
		this.removeRow(this.objectsList[i].row);
	}
};
this.objectsList=newlist;
};

TableWidget.prototype.insertontop=true;
TableWidget.prototype.insertObject=function(o) {
var n=null;
if (o) {
	var t=(o.type)?o.type:this.objectclass;
	if (t && typeof window[t]=='function') {
		if (!this.objectsList) this.objectsList=[];
		n=new window[t]();
		n.type=t;
		n.data=o;
		var c=this.createRow(n);
		if (!this.insertontop) {
		this.objectsList[this.objectsList.length]=n;  
		if (this.root) {
		var row=this.addRow(c);
		n.row=row;
		row.objectindex=this.objectsList.length - 1;
		row.drawNode();
		}
		} else {
		this.objectsList.unshift(n);	
		if (this.root) {
		var row=this.addRow(c,null,true);
		n.row=row;
		this.setObjectIndex();
		row.drawNode(true);
		}
		}
	}	
};
return n;	
};



TableWidget.prototype.CleanRows=function () {
this.clearRows();alert('remove clean rows');
};

TableWidget.prototype.cleanRows=function () {
this.clearRows();alert('remove clean rows');
};

TableWidget.prototype.clearRows=function () {
if (this.tbody && this.root.children) {
	for (var i=0;i<this.root.children.length;i++) {
		var ch=this.root.children[i];
		if (ch.children) {
			for (var j=0;j<ch.children.length;j++) {
			ch.children[j].container=null;	
			}
		};
		Element.rm(ch.container);
		ch.container=null;
	}
}
this.isDrawn=false;	
};

TableWidget.prototype.Clear=function () {
this.clear();alert('Remove Clear');
};

TableWidget.prototype.clear=function () {
	if (this.container) {
	this.clearRows();
	this.container.innerHTML='';
	this.tbody=null;
	}
};

TableWidget.prototype.Draw=function (tc,conf) {
if (!this.isDrawn) {
if (tc) {
	if (this.container) this.clear();
	this.container=Element.get(tc);
	if (this.container) this.createHead(conf);
};
if (!this.tbody && this.container) {
var c=(this.container)?this.container.getElementsByTagName('tbody'):null;
if (c && c[0]) {
this.tbody=c[0];
} else {
	this.createHead(conf);
}
};
if (this.tbody) {
if (this.beforeDraw) this.beforeDraw();
	for (var i=0;i<this.root.children.length;i++) {
		this.root.children[i].drawNode();
	};
this.isDrawn=true;
if (this.onDraw) this.onDraw();
}
}
};

TableWidget.prototype.Create=function (ob) {
var o=ob;
if (o) {
if (ob && typeof ob.length!='undefined') {
	o=this.createTable(ob);
} else {
extend(this,o);
this.root=new Node(null);
this.root.Init(o,this);
if (this.root.template) this.root.template=null;
if (this.children) this.children=null;
}
}
this.countinsert=0;
};

TableWidget.prototype.Init=function (ob) {
this.clearRows();
this.Create(ob);
this.Draw();
if (this.onInit) this.onInit();
};

TableWidget.prototype.createTemplate=function () {
if (this.root && this.root.children) {
var chs=this.root.children[0].children;
var t=[];
for (var i=0;i<chs.length;i++) {
var co={};
co.name=chs[i].name;
co.type=(chs[i].type)?chs[i].type:'TCell';
t[t.length]=co;
}	
this.template=t;
} 	
};

TableWidget.prototype.addRow=function (o,rtype,ist) {
if (!this.template) this.createTemplate();

if (this.template && o && this.root) {
var chs=this.template;
var root=this.root;
var rowtype=(rtype)?rtype:'TRow';
var r=(!ist)?root.appendChild({type:rowtype}):root.insertfirstChild({type:rowtype});
var cells={};
for (var i=0;i<chs.length;i++) {
   var cello={};
   extend(cello,chs[i]);
   var nm=chs[i].name;
   cello.type=(chs[i].type)?chs[i].type:'TCell';
   if (nm) {
   if (typeof o[nm]!='undefined') {
   	cello.value=o[nm];
   	cells[nm]=1;
   }
   };
   r.appendChild(cello);
};	
for (p in o) {
	if (!cells[p]) {
		r[p]=o[p];
	}
};
return r;
};
return null;
};

function aObject () {};
aObject.prototype.execAction=function(f) {
if(!this.lock) {
var pf=f.getParams();
var p={};
extend(p,this.data);
extend(p,pf);
p.action=f.actionname;
var purl=this.url?this.url:this.row.tree.url;
var ro=new Request (purl,p,'POST');
ro.form=f;
ro.actionname=p.action;
if (this.onexecaction && this.onexecaction(ro)) {
this.lock=true;
if (f.disable) f.disable();
dHTML.getRemote(ro,this,'updateData');
}
}
};

aObject.prototype.execDelete=function () {
if(!this.lock) {
var p={};
extend(p,this.data);
var purl=this.url?this.url:this.row.tree.url;
var ro=new Request (purl,p,'POST');
p.action='delete';
var ro=new Request (purl,p,'POST');
if (this.onexecdelete && this.onexecdelete(ro)) {
this.lock=true;
dHTML.getRemote(ro,this,'completeDelete');
}
}	
};

aObject.prototype.completeDelete=function(o,ro) {
this.lock=false;
var exec=(o)?true:false;
if (this.oncompletedelete) exec=this.oncompletedelete(o,ro);
if(exec) {
if(this.row)this.row.tree.removeAccocObject(this);
}
};

aObject.prototype.updateData=function(o,ro) {
this.lock=false;
if (ro && ro.form && ro.form.enable) ro.form.enable();
var uevt=(o)?true:false;
	if (this.onupdatedata) uevt=this.onupdatedata(o,ro);
	if (uevt) {
	extend(this.data,o);	
	if (this.row)this.row.refresh();
	if(this.oncompleteupdate) this.oncompleteupdate(ro.actionname);
	}		
};

var TabsMenu=function (eid) {
this.container=Element.get(eid);
};

TabsMenu.prototype.getItem=function(ipos) {
return this.root.children[ipos];	
};

TabsMenu.prototype.getByTitle=function(ttl) {
var chs=this.root.children;
if (chs) {
	for(var i=0;i<chs.length;i++) {
		if (chs[i].title==ttl) {
			return chs[i];
		}
	}
};
return null;	
};

TabsMenu.prototype.getById=function(id) {
var chs=this.root.children;
if (chs) {
	for(var i=0;i<chs.length;i++) {
		if (chs[i].id==id) {
			return chs[i];
		}
	}
};
return null;	
};

TabsMenu.prototype.InitMenu=function (eid,ipos) {
if (eid) this.container=this.container=Element.get(eid);
if (this.container) {
	this.root=new Node(null);
	this.root.Init(null,this);
	this.id=this.container.getAttribute('id');
	this.className=this.container.className;
	var lis=this.container.getElementsByTagName('li');
	var icl=this.className+'Item';
	var pos=1;
	if (!ipos) ipos=1;
	for (var i=0;i<lis.length;i++) {
		if (lis[i].className==icl) {
			var id=this.id+'_'+pos;
			var n=this.root.appendChild({type:'TabItem'});
			n.container=lis[i];
			n.content=document.getElementById(id);
			Event.bind(n.container,'click',n,'activate');
			Event.bind(n.container,'mouseover',n);
			Event.bind(n.container,'mouseout',n);
			if (n.content) {
				if (ipos==pos) {
				n.activate();	
				} else {
				n.content.style.display='none';
				}
			}
			pos++;
		}
	}
}
};

TabsMenu.prototype.Init=function (o) {
	this.root=new Node(null);
	this.root.Init(o,this);
	this.id=(this.root.id)?this.root.id:null;
	this.className=(this.root.className)?this.root.className:'winmenu';

    var c=document.createElement('ul');
    var div=document.createElement('div');
    div.className=this.className+'content';
    if (this.id) c.setAttribute('id',id);
    c.className=this.className;
    for (var i=0;i<this.root.children.length;i++) {
    var n=this.root.children[i];
    var li=document.createElement('li');	
    li.className=this.className+'Item';
    li.appendChild(document.createTextNode(n.title));
    if (n.id) li.setAttribute('id',n.id);
    var idiv=document.createElement('div');
    if (this.id)idiv.setAttribute('id',this.id + '_' +(i+1));
    idiv.style.display='none';
    c.appendChild(li);
    n.container=li;
    div.appendChild(idiv);
	n.content=idiv;
	Event.bind(n.container,'click',n,'activate');
	Event.bind(n.container,'mouseover',n);
	Event.bind(n.container,'mouseout',n);
    }
    if (this.container) {
    	this.container.appendChild(c);
    	this.container.appendChild(div);
    }
    if(this.onInit) this.onInit();
};

function TabItem () {};

TabItem.prototype=new Node();

TabItem.prototype.activate=function(evt) {
if (this.tree.active!=this) {
if (this.tree.active) this.tree.active.deactivate();
this.tree.active=this;
this.container.className=this.tree.className+'Item'+'Active';
if (this.content)this.content.style.display='block';
if(this.tree.onactivateitem) this.tree.onactivateitem(this);
}
};

TabItem.prototype.deactivate=function() {
if (this.content)this.content.style.display='none';
this.container.className=this.tree.className+'Item';
if(this.tree.ondeactivateitem) this.tree.ondeactivateitem(this);
};

TabItem.prototype.onmouseover=function (evt) {
if (this.tree.active!=this) {
this.container.className=this.tree.className+'Item'+'Hover';
}
};

TabItem.prototype.onmouseout=function (evt) {
if (this.tree.active!=this) {
this.container.className=this.tree.className+'Item';
}
};

/* Calendar */
var Calendar=function (eid) {
this.container=Element.get(eid);
this.dates={};
};

Calendar.setYMD=function (pn,p) {
	if (typeof p[pn]!='undefined') {
		var str=p[pn];
		if (str) {
		var mdy=str.split('/');
		p[pn+'_year']=mdy[2];
		p[pn+'_month']=mdy[0];
		p[pn+'_date']=mdy[1];
		} else {
		p[pn+'_year']='yyyy';
		p[pn+'_month']='mm';
		p[pn+'_date']='dd';
		}
	}
};
Calendar.date2sqlDate=function (dt) {
	var sql=dt.getFullYear();
	sql+='-';
	var m=dt.getMonth();
	m++;
	sql+=m;
	sql+='-'+dt.getDate();
	return sql;	
};

Calendar.date2str=function (dt) {
	var m=dt.getMonth();
	m++;
	var str=m;
	str+='/'+dt.getDate();
	str+='/'+dt.getFullYear();
	return str;	
};

Calendar.setsqlDate=function (pn,p) {
	
	var year=p[pn+'_year'];
	year=year.trim();
	var month=p[pn+'_month'];
	var date=p[pn+'_date'];
	var re=/^\d+$/;
	p[pn]=null;
	if (re.test(year)) {
		var y=parseInt(year,10);
		var d=new Date();
		if (y && y<100 && y>0) {
			if (2000 + y>d.getFullYear()) {
				y=1900 + y;
			} else {
				y=2000 + y;
			}
		};
		if (y>0) {
			var m=parseInt(month,10);
			var d=parseInt(date,10);
			if (typeof m=='number' && m>=1 && m<=12 && typeof d=='number' && d>=1 && d<=31) {
			p[pn]=y+'-'+Calendar.add_zero(m)+'-'+Calendar.add_zero(d);	
			} else {
			alert('Error: incorrect month ('+month+') or date ('+date+')');
			return false;
			}
		} else {
			alert('Error: incorrect year');
			return false;
		}
	};
	return true;
};

Calendar.prototype.closeicon=true;

Calendar.prototype.isForcedDown=true;

Calendar.prototype.onwindowdown=function () {
this.close();	
};


Calendar.prototype.Init=function (o) {
	extend(this,o);
	if (this.iconid) {
		this.initCalendarIcon(this.iconid);
	}
};

Calendar.prototype.trackinput=function (evt) {
var inp=evt.target;
var np=inp.name;
np=np.replace(/\_(year|month|date)$/,'');
if (np!=this.currentPName) {
	if (this.isOpen) this.close();
	this.currentPName=np;
};
var sd=this.parseInput();
if (sd) {
this.setSelectedDate(sd);
} else if (this.currentPName) {
var di=this.dates[this.currentPName];
for (var i=0;i<3;i++) {
	var dp=di.order[i];
	if (inp.name==this.currentPName+'_'+dp) {
		if (i!=2 && Calendar['test_'+dp](inp.value)) {
			var dpnext=di.order[i + 1];
			var inext=di[dpnext];
			if (inext && !Calendar['test_'+dpnext](inext.value)) {
				inext.value='';
				inext.focus();
			}	
		};
		break;
	}
};
var	y=Calendar.parse_year(di.year.value);
var	m=Calendar.parse_month(di.month.value);

if (typeof y=='number') {
	this.setMonth(y,m);
}
}	
};

Calendar.prototype.parseInput=function () {
var tests=0;
var sd=new Date();
var di=(this.currentPName)?this.dates[this.currentPName]:null;
if (di) {
		var y=Calendar.parse_year(di.year.value);
		if (typeof y=='number') {
		sd.setFullYear(y);
		tests++;
		};	

		var m=Calendar.parse_month(di.month.value);
		if (typeof m=='number') {
		sd.setMonth(m);
		tests++;
		};	
	
		var d=Calendar.parse_date(di.date.value);
		if (typeof d=='number') {
		sd.setDate(d);
		tests++;
		}	
};
if (tests==3) return sd;
return null;	
};

Calendar.prototype.initinput=function (evt) {
var inp=evt.target;
var re=/^\d+$/;
if (inp && !re.test(inp.value)) {
	inp.value='';
}	
};

Calendar.prototype.setParaSqlDate=function (p) {
var dpms=this.dates;
var st=true;
	for (var pn in dpms) {
	if(!Calendar.setsqlDate(pn,p)) st=false;
	}	
return st;
};

Calendar.prototype.setParaYMD=function (p) {
var dpms=this.dates;
	for (var pn in dpms) {
	Calendar.setYMD(pn,p);
	}	
};

Calendar.prototype.setInput=function () {
var di=(this.currentPName)?this.dates[this.currentPName]:null;

if (di && this.selectedDay) {
	var y=this.selectedDay.getFullYear();
	if (Calendar.parse_year(di.year.value)!=y)di.year.value=y;
	var m=this.selectedDay.getMonth();
	m++;
	if (parseInt(di.month.value,10)!=m)di.month.value=Calendar.add_zero(m);
	var d=this.selectedDay.getDate();
	if (parseInt(di.date.value,10)!=d)di.date.value=d;
}	
};

Calendar.prototype.open=function (pos) {
	var sd=this.parseInput();
	if (sd) {
		this.setSelectedDate(sd);
	} else {
		this.selectedDay=null;
		this.current=null;
		this.setMonth();		
	};
	if (pos && pos.left) {
		this.container.style.left=pos.left+'px';
	};
	if (pos && pos.top) {
		this.container.style.top=pos.top+'px';
	};
	
	dHTML.WM.setzIndex(this);
	this.isOpen=true;
	this.container.style.display='block';
};

Calendar.prototype.getPName=function (e) {
var id=e.getAttribute('id');
id=id.split('-');
return 	id[0];
};

Calendar.prototype.oncalendariconclick=function (evt) {
var pn=this.getPName(evt.target);
var di=this.dates[pn];
this.currentPName=pn;
	if (di && di.year && !di.year.disabled) {
		var pos=Element.getPosition(di[di.order[0]]);
		pos.top=pos.top + pos.height + 4;
		pos.left=pos.left + 2;
		this.open(pos);
	}
};

Calendar.prototype.setCalendarIcons=function (eid) {
var e=(typeof eid=="string")?document.getElementById(eid):eid;
if (e) {
	var imgs=e.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++) {
		var id=imgs[i].getAttribute('id');
		if (!id) id=imgs[i].getAttribute('name');
		if (/\-calendar$/.test(id))this.initCalendarIcon(imgs[i]);
	}
}	
};

Calendar.prototype.initCalendarIcon=function(eid) {
var e=(typeof eid=="string")?document.getElementById(eid):eid;
var pn=this.getPName(e);
if (!this.dates[pn]) {
Event.bind(e,'click',this,'oncalendariconclick');
var p=e.parentNode;
var inps=p.getElementsByTagName('input');
var di={};
di.order=[];
var odr=di.order;
var re=/^\d+$/;
for (i=0;i<inps.length;i++) {
	if (inps[i].name==pn+'_year') {
		di.year=inps[i];
		odr[odr.length]='year';
		Event.bind(inps[i],'keyup',this,'trackinput');
		if(inps[i].value && !re.test(inps[i].value)) Event.bind(inps[i],'click',this,'initinput');
	} else if (inps[i].name==pn+'_month') {
		di.month=inps[i];
		odr[odr.length]='month';
		Event.bind(inps[i],'keyup',this,'trackinput');
		if(inps[i].value && !re.test(inps[i].value)) Event.bind(inps[i],'click',this,'initinput');
	} else if (inps[i].name==pn+'_date') {
		di.date=inps[i];
		odr[odr.length]='date';
		Event.bind(inps[i],'keyup',this,'trackinput');
		if(inps[i].value && !re.test(inps[i].value)) Event.bind(inps[i],'click',this,'initinput');
	}
};
this.dates[pn]=di;
}	
};

Calendar.prototype.close=function(evt) {
	if (this.container){
	this.container.style.display='none';
	};	
	dHTML.WM.setzIndex(this,true);
	this.isOpen=false;
};

Calendar.prototype.onselecteddateupdate=function () {
this.setInput();	
};

Calendar.prototype.ondayclick=function (evt) {
var e=evt.target;
if (e && e.className!='celldisabled' && e.className!='selected') {
var d=parseInt(e.innerHTML);
this.setSelectedDate(this.current.getFullYear(),this.current.getMonth(),d);
if (this.closeauto) this.close();
}
};

Calendar.prototype.incYear=function () {
this.setMonth(this.current.getFullYear() + 1,this.current.getMonth());	
};


Calendar.prototype.decYear=function () {
this.setMonth(this.current.getFullYear() - 1,this.current.getMonth());	
};

Calendar.prototype.incMonth=function () {
var m=this.current.getMonth();
m++;
if (m>11) m=0;
this.setMonth(this.current.getFullYear(),m);	
};

Calendar.prototype.decMonth=function () {
var m=this.current.getMonth();
m--;
if (m<0) m=11;
this.setMonth(this.current.getFullYear(),m);	
};

Calendar.prototype.setDate=function (pn,y,m,d) {
	var nd=(typeof y=='object')?y:null;
	if (pn && this.dates[pn]) {
		if (!nd && typeof y=='number') { 
			nd=new Date();
			nd.setFullYear(y);
			if (typeof m=='number') nd.setMonth(m);	
			if (typeof d=='number' && d) nd.setDate(d);	
		};	
	var di=this.dates[pn];
	if (nd) {	
		di.year.value=nd.getFullYear();
		var m=nd.getMonth();
		m++;
		di.month.value=Calendar.add_zero(m);
		di.date.value=nd.getDate();
	} else {
		di.year.value=typeof y=='string'?y:'yyyy';
		di.month.value=typeof m=='string'?m:'mm';
		di.date.value=typeof d=='string'?d:'yy';
	}
	}
};

Calendar.prototype.setSelectedDate=function (y,m,d) {
	var err=false;
	
	if (typeof y=='object') {
	this.selectedDay=y;
	} else {
	if(!this.selectedDay) this.selectedDay=new Date();
	if (typeof y=='number') {
	this.selectedDay.setFullYear(y);
	} else {err=true};
	if (typeof m=='number') {
	this.selectedDay.setMonth(m);	
	} else {err=true};
	if (typeof d=='number' && d) {
	this.selectedDay.setDate(d);	
	}else {err=true};
	};
	
	if (err) {this.selectedDay=null;return false;};
	this.setMonth(this.selectedDay.getFullYear(),this.selectedDay.getMonth());
	this.onselecteddateupdate();
	return true;
};

Calendar.prototype.initCurrent=function () {
	this.current=new Date();
	if (this.selectedDay) {
		this.current.setFullYear(this.selectedDay.getFullYear());
		this.current.setMonth(this.selectedDay.getMonth());
	}
	this.current.setDate(1);
};

Calendar.prototype.setMonth=function (y,m) {
	if (!this.current) this.initCurrent();
	if (typeof y=='number') {
	this.current.setFullYear(y);
	};
	if (typeof m=='number') {
	this.current.setMonth(m);	
	};

	if (!this.table) {
		this.createTable();
	} else {
		var today=new Date();
		var cd=Calendar.firstcellDay(this.current.getFullYear(),this.current.getMonth());
		var date=this.current;
        this.yearmonth.innerHTML=date.getFullYear() + ' ' + Calendar.MONTHS_LONG[date.getMonth()];

		var tbody=this.table.getElementsByTagName('tbody');
		tbody=tbody[0];
		var days=tbody.getElementsByTagName('td');
		var count=0;
		for (j=0;j<6;j++) {
			for (k=0;k<7;k++) {
			var td=days[count];
			td.innerHTML=cd.getDate();
			td.className='';
			if (cd.getMonth()!=date.getMonth()) {
			td.className='celldisabled';	
			} else if (this.selectedDay && cd.getMonth()==this.selectedDay.getMonth() && cd.getDate()==this.selectedDay.getDate() && cd.getFullYear()==this.selectedDay.getFullYear()) {
			td.className='selected';
			} else if (cd.getMonth()==today.getMonth() && cd.getDate()==today.getDate() && cd.getFullYear()==today.getFullYear()) {
			td.className='today';
			};
			cd.setDate(cd.getDate() + 1);
			count++;	
			}
		}
	}
};

Calendar.prototype.createTable =function () {
if (!this.current) this.initCurrent();
var date=this.current;
var today=new Date();
var tb=document.createElement("table");
tb.cellSpacing = 0;
tb.className='calendar';
var thead=document.createElement("thead");
var headRow=document.createElement("tr");
var headerCell = document.createElement("th");
headerCell.className='calheader';
headerCell.colSpan=7;

var e=document.createElement("span");
e.className='yearmonth';
e.innerHTML=date.getFullYear() + ' ' + Calendar.MONTHS_LONG[date.getMonth()];
headerCell.appendChild(e);
this.yearmonth=e;

if (this.closeicon) {
e=document.createElement("span");
e.className='calclose';
headerCell.appendChild(e);
Event.bind(e,'click',this,'close');
};

e=document.createElement("span");
e.className='right2arrnav';
e.innerHTML='&raquo;&raquo;';
headerCell.appendChild(e);
Event.bind(e,'click',this,'incYear');

e=document.createElement("span");
e.className='right1arrnav';
e.innerHTML='&raquo;';
headerCell.appendChild(e);
Event.bind(e,'click',this,'incMonth');

e=document.createElement("span");
e.className='left2arrnav';
e.innerHTML='&laquo;&laquo;';
headerCell.appendChild(e);
Event.bind(e,'click',this,'decYear');

e=document.createElement("span");
e.className='left1arrnav';
e.innerHTML='&laquo;';
headerCell.appendChild(e);
Event.bind(e,'click',this,'decMonth');

headRow.appendChild(headerCell);
thead.appendChild(headRow);

headRow=document.createElement("tr");
headRow.className='weekdays';
for(i=0;i<Calendar.WEEKDAYS_SHORT.length;i++) {
	var th=document.createElement("th");
	th.appendChild(document.createTextNode(Calendar.WEEKDAYS_SHORT[i]));
	headRow.appendChild(th);
};
thead.appendChild(headRow);
var cd=Calendar.firstcellDay(date.getFullYear(),date.getMonth());
var tbody=document.createElement("tbody");
for (j=0;j<6;j++) {
	var tr=document.createElement("tr");
	tr.className='daysrow';
	for (k=0;k<7;k++) {
	var td=document.createElement("td");
	td.appendChild(document.createTextNode(cd.getDate()));
	if (cd.getMonth()!=date.getMonth()) {
	td.className='celldisabled';	
	} else if (this.selectedDay && cd.getMonth()==this.selectedDay.getMonth() && cd.getDate()==this.selectedDay.getDate() && cd.getFullYear()==this.selectedDay.getFullYear()) {
	td.className='selected';
	} else if (cd.getMonth()==today.getMonth() && cd.getDate()==today.getDate() && cd.getFullYear()==today.getFullYear()) {
	td.className='today';
	}
	cd.setDate(cd.getDate() + 1);
	Event.bind(td,'click',this,'ondayclick');
	Event.bind(td,'mouseover',Calendar.mouseover);
	Event.bind(td,'mouseout',Calendar.mouseout);
	
	tr.appendChild(td);	
	};
	tbody.appendChild(tr);
};
tb.appendChild(thead);
tb.appendChild(tbody);
if (!this.container) this.container=Element.div('calcontainer');
this.container.appendChild(tb);
this.table=tb;
};

Calendar.WEEKDAYS_SHORT=["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
Calendar.MONTHS_SHORT=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
Calendar.MONTHS_LONG=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],

Calendar.firstcellDay=function(y,m) {
var fd=new Date(y,m,1);
var day=fd.getDay();
fd.setDate(fd.getDate() - day);
return fd;
};

Calendar.mouseover=function(evt) {
var e=(evt.target)?evt.target:evt.srcElement;
if (e.className!='celldisabled') {
	if (!e.className) {
	e.className='cellhover';	
	} else {
	e.className+=' cellhover';	
	}
}
};

Calendar.mouseout=function(evt) {
var e=(evt.target)?evt.target:evt.srcElement;

if (e.className!='celldisabled') {
	e.className=e.className.replace(/\s*cellhover$/,'');
}
};
Calendar.test_year=function (str) {
if (/^\s*\d\d\d\d\s*$/.test(str)) return true;
return false;
};
Calendar.test_month=function (str) {
if (/^\s*\d\d\s*$/.test(str)) return true;
return false;
};
Calendar.test_date=function (str) {
if (/^\s*\d\d\s*$/.test(str)) return true;
return false;
};

Calendar.parse_year=function (str) {
		if (/^\s*\d\d\d\d\s*$/.test(str)) {
		var y=parseInt(str,10);
		return y;
		}	
return null;
};

Calendar.parse_month=function (str) {
		if (/^\s*\d(\d)?s*$/.test(str)) {
		var m=parseInt(str,10);
		if (m<=0 || m>12) return null;
		m--;
		return m;
		}	
return null;
};

Calendar.parse_date=function (str) {
		if (/^\s*\d(\d)?s*$/.test(str)) {
		var d=parseInt(str,10);
		if(d>0)return d;
		}	
return null;
};

Calendar.add_zero=function (m) {
	if (m>=10) {
		return m;
	} else {
		return '0' + m;
	}
};
/* Calendar end */

TableWidget.prototype.zebraclass='zebra';

if (dHTML.isBrowser('MSIE 6')) {
dHTML.hideSelectBoxes=function (e) {
	if (e) {
	var selects = e.getElementsByTagName("select");
	for (var i = 0; i < selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
	}
};

dHTML.showSelectBoxes=function (e) {
	if (e) {
	var selects = e.getElementsByTagName("select");
	for (var i = 0; i < selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	}
};

dWindow.prototype.onwindowup=function (dw) {
dHTML.showSelectBoxes(this.container);
return true;	
};

dWindow.prototype.onwindowdown=function () {
dHTML.hideSelectBoxes(this.container);
};
};//end if

dHTML.showcontentIcon=BASE_PATH+'_images/icons/show.png';
dHTML.hidecontentIcon=BASE_PATH+'_images/icons/hide.png';

dHTML.showhideContent=function (evt) {
	evt=(evt)? evt:((window.event)? event:null);
	Event.cancel(evt);
	var ic=(evt.target)?evt.target:evt.srcElement;
	var id=ic.getAttribute('id');
	var isshown=ic.getAttribute('status');
	if (/^use_id\-(.+)/.test(id)) {
		var eid=RegExp.$1;
		var e=Element.get(eid);
		var img=(ic.nodeName && ic.nodeName.toLowerCase()=='img')?ic:null;
		if (!img) {
			var imgs=ic.getElementsByTagName('img');
			if(imgs && imgs[0]) img=imgs[0];
		};
		if (!isshown) {
		ic.setAttribute('status',1);
		e.style.display='';
		if(img)img.src=dHTML.hidecontentIcon;	
		} else {
		ic.setAttribute('status','');
		e.style.display='none';
		if(img)img.src=dHTML.showcontentIcon;	
		}
	} else if (/^use_name\-(.+)/.test(id)) {
		
	}
};	