function opacityState(state) {
	return 1-Math.sin(Math.PI*(5-state)/10);
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
};
window.dirties = [];
window.addDirty = function(obj) {
	if (typeof obj.isDirty != 'function') return;
	var size = this.dirties.length;
	this.dirties[size] = obj;
}
window.isDirty = function() {
	var size = this.dirties.length;
	for (var i=0; i<size; i++)
		if (this.dirties[i].isDirty()) return true;
	return false;
}
window.resetUpdate = function() {
	var size = this.dirties.length;
	for (var i=0; i<size; i++)
		this.dirties[i].resetUpdate();
}
if (typeof(HTMLElement)!="undefined") {
	HTMLElement.prototype.contains = function(obj) {
		do {
			if (obj == this) return true;
		}while (obj = obj.parentNode);
		return false;
	};
	HTMLElement.prototype.__defineGetter__("innerText",
		function() {
			var anyString = "";
			var childS = this.childNodes;
			for(var i=0; i<childS.length; i++) {
				if (childS[i].nodeType==1)
					anyString += childS[i].tagName=="BR"?'\n':childS[i].innerText;
				else if(childS[i].nodeType==3)
					anyString += childS[i].nodeValue;
			}
			return anyString.trim();
		}
	);
	HTMLElement.prototype.__defineSetter__("innerText",
		function(sText) {
			this.textContent=sText;
		}
	);
	HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr) {
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		this.appendChild(r.createContextualFragment(htmlStr));
	}
}
var pointer = document.all?'hand':'pointer';
function $(id) {
	if (typeof id == 'object') return id;
	return document.getElementById(id);
}
function addHandle(name, func, obj) {
	if (!obj) obj = document;
	if (obj.attachEvent)
		obj.attachEvent('on'+name, func);
	else if (obj.addEventListener)
		obj.addEventListener(name, func, true);
}
function cleanChild(obj) {
	var child = obj.lastChild;
	while (child) {
		var prev = child.previousSibling;
		if (child.nodeType != 1)
			obj.removeChild(child);
		child = prev;
	}
}
function removeHandle(name, func, obj) {
	if (!obj) obj = document;
	if (obj.detachEvent)
		obj.detachEvent('on'+name, func);
	else if (obj.removeEventListener)
		obj.removeEventListener(name, func, true);
}
function autoWidget() {
	var re = new RegExp("\\bmarquee\\b");
	var all = document.getElementsByTagName('div');
	for (var i=0; i<all.length; i++)
		if (re.test(all[i].className)) Marquee(all[i], true);
}
function addClass(e, cls) {
	var re = new RegExp("\\b"+cls+"\\b");
	if (!re.test(e.className))
		e.className += " "+cls;
}
function removeClass(e, cls) {
	var re = new RegExp("\\b"+cls+"\\b");
	e.className = e.className.replace(re, ' ');
}
function getPosition(el) {
	var offset = {x:0, y: 0};
	if (el.getBoundingClientRect) {
		var rect = el.getBoundingClientRect();
		if (document.documentElement) {
			offset.x = document.documentElement.scrollLeft;
			offset.y = document.documentElement.scrollTop;
		}else {
			offset.x = document.body.scrollLeft;
			offset.y = document.body.scrollTop;
		}
		offset.x += rect.left;
		offset.y += rect.top;
		return offset;
	}
	offset.x = el.offsetLeft;
	offset.y = el.offsetTop;
	if (el.scrollLeft)
		offset.x -= el.scrollLeft;
	if (el.scrollTop)
		offset.y -= el.scrollTop;
	var par = el.offsetParent;
	while (par) {
		offset.x += par.offsetLeft;
		offset.y += par.offsetTop;
		par = par.offsetParent;
	}
	offset.x -= document.body.scrollLeft;
	offset.y -= document.body.scrollTop;
	return offset;
}
function getCursor(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }else if (document.documentElement) {
        var d = document.documentElement;
        cursor.x = e.clientX + d.scrollLeft - d.clientLeft;
        cursor.y = e.clientY + d.scrollTop - d.clientTop;
    }else {
        var b = document.body;
        cursor.x = e.clientX + b.scrollLeft;
        cursor.y = e.clientY + b.scrollTop;
    }return cursor;
}
function getStyle(elem, style) {
    if (document.all) {
        style = style.replace(/-([a-z])/g, function(s, a){return a.toUpperCase();});
        value = elem.currentStyle[style];
    } else {
        var css = document.defaultView.getComputedStyle(elem, null);
        value = css ? css.getPropertyValue(style) : null;
    }
    return value == 'auto' ? null : value;
}
function windowSize() {
	if (typeof(window.innerWidth) == 'number')
		return {'width':window.innerWidth, 'height':window.innerHeight};
	var base = null;
	if (!document.compatMode || document.compatMode == 'BackCompat')
		base = document.body;
	else
		base = document.documentElement;
	return {'width':base.clientWidth, 'height':base.clientHeight};
}
function windowScroll() {
	if (typeof(window.pageYOffset) == 'number')
		return {'x':window.pageXOffset, 'y':window.pageYOffset};
	var base = null;
	if (!document.compatMode || document.compatMode == 'BackCompat')
		base = document.body;
	else
		base = document.documentElement;
	return {'x':base.scrollLeft, 'y':base.scrollTop};
}
function checkAll(obj) {
	var form = obj.form;
	var pre = (arguments.length>1)?arguments[1]:'';
	for (var i=1; i<form.elements.length; i++) {
		var e = form.elements[i];
		if (pre && e.name.indexOf(pre)!=0)
			continue;
		if (e.type == 'checkbox')
			e.checked = obj.checked;
	}
}
function createModal() {
	var modal = $('modal');
	if (modal) return null;
	modal = document.createElement("div");
	modal.id = "modal";
	with (modal.style) {
		position = 'absolute';
		backgroundImage = 'url(.png)';
		left = '0px';
		top = '0px';
		width = '100%';
		height = '100%';
		zIndex = 9999;
	}
	document.body.appendChild(modal);
	return modal;
}
function destroyModal(modal) {
	if (!modal) return;
	document.body.removeChild(modal);
}
function openWindow(url, width, height, modal) {
	var popup = $('popup');
	if (!popup) {
		popup = document.createElement('iframe');
		popup.id = 'popup';
		popup.name = 'popup';
		popup.style.display = 'none';
		popup.style.position = 'absolute';
		popup.setAttribute('frameborder', '0');
		document.body.appendChild(popup);
	}
	popup.src = url;
	popup.width = width;
	popup.height = height;
	if (modal)
		popup.modal = createModal();
	else
		popup.modal = null;
	var size = windowSize();
	popup.style.left = (size.width-width)/2+"px";
	var value = size.height - height;
	value = value*0.4 + windowScroll().y;
	popup.style.top = parseInt(value) + "px";
	popup.style.zIndex = 1000;
}
function showWindow() {
	var popup = $('popup');
	if (!popup) return;
	with (popup.style) {
		if (display != 'none') return;
		opacity = 1;
		filter = "alpha(opacity=100)";
		display = 'block';
	}
	popup.tick = 5;
	popup.focus();
}
function closeWindow() {
	var popup = $('popup');
	if (!popup) return;
	if (popup.style.display=='none') return;
	if (--popup.tick > 0) {
		var opacity = opacityState(popup.tick);
		popup.style.opacity = opacity;
		opacity = opacity * 100;
		popup.style.filter = "alpha(opacity="+opacity+")";
		setTimeout('closeWindow()', 50);
	}else {
		destroyModal(popup.modal);
		popup.modal = null;
		if (window.reload) {
			window.location = location.href;
			window.reload = false;
		}else popup.style.display = 'none';
	}
}
function titleDialog(title) {
	$('dlg_title').innerHTML = title;
}
var imagePath = '';
function showDialog(text) {
	var btnOk = $('dlg_ok');
	var btnNo = $('dlg_no');
	var btnClose = $('dlg_close');
	var img = $('dlg_icon');
	var popup = $('dialog');
	if (arguments.length > 2) {
		var yes_callback = arguments[1];
		img.src = "question.gif";
		btnOk.innerHTML = '是(<u>Y</u>)';
		btnOk.style.display = '';
		btnOk.onclick = function() {
			yes_callback();
			if (!btnClose.disabled)
				closeDialog(true);
		}
		btnNo.style.display = '';
		var no_callback = arguments[2];
		btnNo.onclick = function() {
			no_callback();
			closeDialog(true);
		}
		btnClose.disabled = false;
		btnClose.style.display = '';
		btnClose.onclick = closeDialog;
	}else if (arguments.length < 2 ||
		typeof arguments[1] == 'number') {
		img.src = imagePath + "info.gif";
		btnNo.style.display = 'none';
		btnClose.style.display = 'none';
		btnOk.innerHTML = '确定(<u>O</u>)';
		btnOk.style.display = '';
		btnOk.onclick = closeDialog;
		if (arguments.length > 1)
			setTimeout('closeDialog()', arguments[1]);
	}else if (typeof arguments[1] == 'string') {
		if (arguments[1] == 'warn')
			img.src = imagePath + "warning.gif";
		else if (arguments[1] == 'error')
			img.src = imagePath + "error.gif";
		else if (arguments[1] == 'wait')
			img.src = imagePath + "waiting.gif";
		else
			img.src = imagePath + "info.gif";
		window.reload = false;
		btnNo.style.display = 'none';
		if (arguments[1] == 'wait') {
			btnClose.style.display = '';
			btnClose.disabled = true;
			btnOk.style.display = 'none';
		}else {
			btnClose.style.display = 'none';
			btnOk.innerHTML = '确定(<u>O</u>)';
			btnOk.style.display = '';
			btnOk.onclick = closeDialog;
		}
	}else {
		img.src = imagePath + "question.gif";
		btnOk.innerHTML = '确定(<u>O</u>)';
		btnOk.style.display = '';
		var callback = arguments[1];
		btnOk.onclick = function() {
			callback();
			if (!btnClose.disabled)
				closeDialog(true);
		}
		btnClose.disabled = false;
		btnClose.style.display = '';
		window.reload = false;
		btnClose.innerHTML = '取消(<u>C</u>)';
		btnClose.onclick = closeDialog;
		btnNo.style.display = 'none';
	}
	$('dlg_info').innerHTML = text;
	if (popup.style.display == 'none') {
		popup.modal = createModal();
		var size = windowSize();
		popup.style.left = (size.width-400)/2+"px";
		var value = size.height - 160;
		value = value*0.4 + windowScroll().y;
		popup.style.top = parseInt(value) + "px";
		with (popup.style) {
			zIndex = 10001;
			opacity = 1;
			filter = "alpha(opacity=100)";
			display = 'block';
		}
	}
	popup.tick = 5;
	popup.focus();
}
function closeDialog(fast) {
	var popup = $('dialog');
	if (typeof fast == 'number') {
		setTimeout('closeDialog()', fast);
	}else if (fast) {
		popup.style.display = 'none';
		destroyModal(popup.modal);
		popup.modal = null;
	}else if (--popup.tick > 0) {
		var opacity = opacityState(popup.tick);
		popup.style.opacity = opacity;
		opacity = opacity * 100;
		popup.style.filter = "alpha(opacity="+opacity+")";
		setTimeout('closeDialog()', 50);
	}else {
		destroyModal(popup.modal);
		popup.modal = null;
		if (!window.reload) {
			popup.style.display = 'none';
		}else if (typeof window.reload == 'function') {
			window.reload();
			window.reload = false;
		}else {
			window.location = location.href;
			window.reload = false;
		}
	}
}
function getSelected(form) {
	var ids = new Array();
	if (!form) form = document.forms[0];
	var items = 0, index = 0;
	for (var i=0; i<form.elements.length; i++) {
		var e = form.elements[i];
		if (e.type!='checkbox') continue;
		if (++items == 1) continue;
		if (!e.checked||!e.value) continue;
		ids[index++] = e.value;
	}return ids;
}
function firstSelected(form) {
	if (!form) form = document.forms[0];
	for (var i=1; i<form.elements.length; i++) {
		var e = form.elements[i];
		if (e.type!='checkbox') continue;
		if (!e.checked||!e.value) continue;
		return e.value;
	}return '';
}
function strSelected(form) {
	var items=0, name='', ids='';
	if (arguments.length > 1)
		name = arguments[1];
	if (!form) form = document.forms[0];
	for (var i=0; i<form.elements.length; i++) {
		var e = form.elements[i];
		if (e.type!='checkbox') continue;
		if (++items == 1) continue;
		if (!e.checked||!e.value) continue;
		if (name&&e.name!=name) continue;
		if (ids) ids += ';';
		ids += e.value;
	}return ids;
}
function isSelected(form) {
	var name = arguments[1];
	if (!form) form = document.forms[0];
	for (var i=1; i<form.elements.length; i++) {
		var e = form.elements[i];
		if (e.type!='checkbox') continue;
		if (name && e.name!=name) continue;
		if (e.checked) return e;
	}return false;
}
function selectValue(id, value, defindex) {
	var obj = $(id);
	if (!obj) return;
	if (value == '' && defindex) {
		obj.options[defindex].selected = true;
		return;
	}
	for (var i=0; i<obj.options.length; i++) {
		if (obj.options[i].value == value) {
			obj.options[i].selected = true;
			return;
		}
	}
}
function XmlHttp() {
	if(window.XMLHttpRequest)
		this.objXmlHttp = new XMLHttpRequest();
	else if(window.ActiveXObject) {
		var success = false;
		try {
			this.objXmlHttp = new ActiveXObject('MSXML2.XMLHTTP');
			success = true;
		}catch(e) {}
		if (!success)
			this.objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else {
		this.objXmlHttp = null;
		throw "XmlHttp Error!";
	}
	this.async = true;
	this.typeXml = false;
	this.onsuccess = null;
	this.doGet = function(url, callback) {
		var self = this.objXmlHttp;
		this.objXmlHttp.onreadystatechange = function() {
			if (self.readyState != 4 || !callback) return;
			var text = self.responseText;
			if (self.status != 200)
				callback(false, text);
			else if (text=='' || text.substr(0,3)=='ok:')
				callback(true, text.substr(3));
			else {
				if (text.substr(0,9) == "<!DOCTYPE")
					text = '';
				callback(false, text);
			}
		}
		this.objXmlHttp.open("GET", url, callback?true:false);
		this.objXmlHttp.setRequestHeader("If-Modified-Since","0");
		this.objXmlHttp.send("");
		return callback?"":self.responseText;
	}
	this.doPost = function(callback, url, form) {
		var self = this.objXmlHttp;
		var ajax = this;
        this.objXmlHttp.onreadystatechange = function() {
			if (self.readyState != 4) return;
			var text = self.responseText;
			if (self.status != 200)
				callback(false, text);
			else if (text=='') {
				if (ajax.onsuccess)
					ajax.onsuccess('');
				callback(true, '');
			}else if (text.substr(0,3)=='ok:') {
				var str = text.substr(3);
				if (ajax.onsuccess)
					ajax.onsuccess(str);
				callback(true, str);
			}else
				callback(false, text);
		}
        this.objXmlHttp.open("POST", url, this.async);
		if (this.typeXml)
			this.objXmlHttp.setRequestHeader("Content-Type", "text/xml");
		else
			this.objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.objXmlHttp.setRequestHeader("If-Modified-Since","0");
		if (typeof form != 'string')
			this.objXmlHttp.send(this.strForm(form));
		else if (this.typeXml)
			this.objXmlHttp.send("<?xml version='1.0' encoding='UTF-8'?>" + form);
		else
			this.objXmlHttp.send(form);
		if (!this.async && this.onsuccess) {
			var text = self.responseText;
			if (text=='') {
				callback(true, '');
				this.onsuccess(text);
			}else if (text.substr(0,3)=='ok:') {
				var str = text.substr(3);
				if (this.onsuccess)
					this.onsuccess(str);
				callback(true, str);
			}else
				callback(false, text);
		}
	}
	this.postForm = function(url, str) {
		var form = document.createElement("FORM");
		form.method = "post";
		form.action = url;
		str = str.replace(/\+/g, ' ');
		var args = str.split('&');
		for (var i=0; i<args.length; i++) {
			var pair = args[i].split('=');
			if (pair.length < 2) pair[1] = '';
			var input = document.createElement("INPUT");
			input.type = 'hidden';
			input.name = pair[0];
			input.value = pair[1];
			form.appendChild(input);
		}
		document.body.appendChild(form);
		form.submit();
	}
    this.strForm = function(form) {
        var el, type, params = '';
        for (var i=0; i<form.length; i++) {
			el = form[i];
			if (!el.name) continue;
			type = el.type.toLowerCase();
			if ((type=="radio"||type=="checkbox")
				&& !el.checked) continue;
			if (params != '') params += "&";
			params += el.name + "=" + encodeURIComponent(el.value);
		}
		return params;
	}
}
function LineEdit(textarea) {
	var input = null;
	if (textarea) {
		input = document.createElement('TEXTAREA');
	}else {
		input = document.createElement('INPUT');
		input.type = "text";
		input.filter = '';
		input.onkeypress = function(e) {
			var key = e?e.which:event.keyCode;
			if (!this.filter) return true;
			if (key==8||key==37||key==39) return true;
			if (key>=48&&key<=57) return true;
			if (this.filter=='number'&&key==46) return true;
			return (this.filter=='char' && key<127);
		}
	}
	input.className = 'lineedit';
	input.style.width = '100%';
	input.setValue = function(value) {
		if (value == this.value) return;
		if (this.owner)
			this.owner.setValue(value);
		else {
			this.value = value;
			if (!input.form) return;
			input.form.dirty = true;
			try{this.onchange();}catch(e){}
		}
	}
	input.owner = null;
	input.autocomplete = "off";
	return input;
}
var Dropdowns = new Array();
function ComboBox(id) {
	var combo = $(id);
	if (!combo) {
		combo = document.createElement("DIV");
		combo.id = id;
		combo.className = "combobox";
		combo.style.display = "none";
		combo.style.position = "absolute";
		combo.style.zIndex = "9999";
		document.body.appendChild(combo);
	}
	if (combo.message) return combo;
	combo.input = null;
	combo.current = null;
	combo.value = "";
	combo.fixed = true;
	combo.message = new Array();
	combo.dropdown = function(input, data, filter) {
		this.filter = filter;
		this.input = input;
		if (!data);
		else if (typeof data != 'string')
			this.message = data;
		else {
			var dropdown = Dropdowns[data];
			if (typeof dropdown == 'function')
				dropdown();
			this.message = Dropdowns[data];
		}
		input.combo = this;
		input.onkeydown = function(e) {
			var key = e?e.which:event.keyCode;
			setTimeout(function(){this.combo.keydown(key);},0);
		}
		var pt = getPosition(input);
		pt.y += input.offsetHeight;
		this.style.left = pt.x + 'px';
		this.style.top = pt.y + 'px';
		this.style.width = input.offsetWidth + 'px';
		if (input.tagName == 'INPUT') {
			if (!input.setValue) {
				input.setValue = function(value) {
					if (value == this.value) return;
					this.value = value;
					if (input.form)
						input.form.dirty = true;
					try{this.onchange();}catch(e){}
				}
				input.autocomplete = "off";
			}
			this.populate(input.value);
		}else
			this.populate(input.innerText);
		if (pt.y + this.offsetHeight > document.body.offsetHeight) {
			pt.y -= this.offsetHeight;
			pt.y -= input.offsetHeight;
			this.style.top = pt.y + 'px';
		}
		setTimeout(function(){input.focus();},0);
	}
	addHandle('mousedown', function(e) {
		var obj = e.target?e.target:event.srcElement;
		if (obj!=combo.input && !combo.contains(obj))
			combo.style.display = 'none';
	});
	combo.keydown = function(key) {
		if (key == 13)
			this.style.display = 'none';
		else if (key == 39)
			this.style.display = 'block';
		else if (key == 40 || key == 38) {
			if (this.current) {
				this.current.className = "";
				if (key == 40)
					this.current = this.current.nextSibling;
				else
					this.current = this.current.previousSibling;
			}
			if (this.current);
			else if (key == 40)
				this.current = this.firstChild;
			else
				this.current = this.lastChild;
			this.current.className = "hover";
			this.itemClicked(this.current);
			this.style.display = 'block';
		}
		else if (this.input.tagName == 'INPUT' &&
			this.value != this.input.value)
			this.populate(this.input.value);
	}
	combo.itemClicked = function(item) {
		if (!this.input) return;
		this.input.setValue(item.innerText);
	}
	combo.populate = function(value) {
		this.innerHTML = "";
		this.value = value;
		if (!this.fixed && !value) return;
		this.current = null;
		var re = new RegExp("^" + value, "i");
		for (var i=0; i<this.message.length; i++) {
			if (this.filter && !re.test(this.message[i])) continue;
			var div = document.createElement("DIV");
			div.innerHTML = this.message[i];
			if (this.message[i] == value) {
				div.className = "hover";
				this.current = div;
			}
			div.combo = this;
			div.onmouseover = function() {
				if (this.combo.current)
					this.combo.current.className = "";
				this.combo.current = this;
				this.className = "hover";
			}
			div.onmouseout = function() {
				this.className = "";
			}
			div.onmousedown = function() {
				this.combo.itemClicked(this);
				this.combo.style.display = "none";
			}
			this.appendChild(div);
		}
		var items = this.childNodes.length;
		if (items < 1)
			this.style.display = "none";
		else {
			this.style.height = (items>15)?"240px":"";
			this.style.display = "block";
		}
	}
	return combo;
}
var SORTED = 0;
function sort_integer(a, b) {
	var aa = parseInt(a.cells[SORTED].innerText)||0;
	var bb = parseInt(b.cells[SORTED].innerText)||0;
	return aa - bb;
}
function sort_numeric(a, b) {
	var aa = parseFloat(a.cells[SORTED].innerText)||0;
	var bb = parseFloat(b.cells[SORTED].innerText)||0;
	return aa - bb;
}
function sort_default(a, b) {
	var aa = a.cells[SORTED].innerText;
	var bb = b.cells[SORTED].innerText;
	if (aa==bb) return 0;
	if (aa<bb) return -1;
	return 1;
}
function sort_insensitive(a, b) {
	var aa = a.cells[SORTED].innerText.toLowerCase();
	var bb = b.cells[SORTED].innerText.toLowerCase();
	if (aa==bb) return 0;
	if (aa<bb) return -1;
	return 1;
}
function sort_locale(a, b) {
	var aa = a.cells[SORTED].innerText;
	var bb = b.cells[SORTED].innerText;
	return aa.localeCompare(bb);
}
function GridEdit(id) {
	var table = $(id);
	if (!table || !table.rows ||
		typeof table.activeCell == 'object')
		return table;
	table.header = 1;
	if (arguments.length == 1)
		table.primary = new Array();
	else if (typeof arguments[1] == 'number') {
		table.header = 0;
		table.columnItems = arguments[1];
		table.primary = new Array();
	}else if (arguments[1].indexOf(",") > 0)
		table.primary = arguments[1].split(",");
	else
		table.primary = new Array(arguments[1]);
	if (arguments.length > 2)
		table.editable = arguments[2];
	else
		table.editable = true;
	table.activeCell = null;
	table.activeInput = null;
	table.activeCombo = null;
	table.lastSort = null;
	table.deleted = new Array();
	table.columns = new Array();
	var item, row = table.rows[0];
	if (row) {
		row.table = table;
		row.selected = 0;
		item = row.cells[0];
		if (item.firstChild.tagName &&
			item.firstChild.tagName == 'INPUT') {
			table.checkbox = true;
			item = item.firstChild;
		}else
			table.checkbox = false;
		item.style.cursor = 'pointer';
	}else {
		table.header = 0;
		table.checkbox = true;
	}
	if (table.header) {
		table.columnItems = row.cells.length;
		var hidden = (row.cells[0].className == 'hidden');
		table.columns[0] = {'hidden': hidden};
		if (!hidden) item.className = 'select';
		item.onclick = function() {
			var row = this.parentNode;
			if (!row.table)
				row = row.parentNode;
			row.select(!row.selected);
		};item=null;
		row.select = function(state) {
			var size = this.table.rows.length;
			for (var i=this.table.header; i<size; i++)
				this.table.rows[i].select(state);
			this.selected = state?1:0;
		}
		for (var j=1; j<table.columnItems; j++) {
			var cell = row.cells[j];
			var subtype = '';
			var type = cell.getAttribute('type');
			if (type == null) type = '';
			var pos = type.indexOf(":");
			if (pos > 0) {
				subtype = type.substr(pos + 1);
				type = type.substr(0, pos);
				if (!Dropdowns[subtype]) type = '';
			}
			var realValue = false;
			if (type.charAt(0) == '!') {
				realValue = true;
				type = type.substr(1);
			}
			hidden = (cell.className == 'hidden');
			var initial = cell.getAttribute('initial');
			var width = cell.getAttribute('width');
			if (width) {
				if (width.indexOf('%')>0)
					width = table.offsetWidth * parseFloat(width) / 100;
				else width = parseInt(width);
			}
			if (initial == null) initial = '';
			var name, dbname = cell.getAttribute('name');
			if (dbname) {
				pos = dbname.indexOf('.') + 1;
				if (pos > 0)
					name = dbname.substr(pos);
				else
					name = dbname;
			}else name = dbname = '';
			table.columns[j] = {'title':cell.innerText,
				'name': name,'dbname': dbname,'textarea': false,
				'hidden': hidden, 'initial': initial,
				'type': type, 'dropdown': subtype,
				'width': width||cell.offsetWidth,
				'realValue': realValue,
				'transform': cell.style.textTransform,
				'key': cell.getAttribute('key')};
			if (!cell.innerText) continue;
			cell.style.cursor = 'pointer';
			cell.onclick = function() {table.sort(this);}
		}
		for (var j=1; j<table.columnItems; j++)
			row.cells[j].setAttribute('width', table.columns[j].width);
	}else
		table.columns[0] = {'hidden': false};
	if (table.getAttribute('dataid')) {
		table.header = 2;
		row = table.insertRow(1);
		row.className = 'filter';
		row.style.backgroundColor = '#E9F4FF';
		row.selected = 0;
		row.dirty = false;
		row.isnew = false;
		var cell = row.insertCell(-1);
		var img = document.createElement('img');
		img.src = 'search.gif';
		img.title = '快速筛选';
		img.style.border = 'medium none';
		img.style.cursor = 'pointer';
		img.onclick = function() {
			var str = '';
			var cell = this.parentNode.nextSibling;
			for (; cell; cell=cell.nextSibling) {
				if (cell.className == 'hidden') continue;
				var val = cell.firstChild.value;
				if (val.length < 1) continue;
				if (str) str += ";";
				str += cell.firstChild.dbname;
				if (cell.firstChild.coltype == 'number' ||
					cell.firstChild.coltype == 'integer')
					str += ' EQ ' + val;
				else
					str += ' IN ' + val;
			}
			if (typeof table.onfiltering != 'function') {
				var ajax = new XmlHttp;
				var dataid = table.getAttribute('dataid');
				var filter = 'filter=' + str;
				filter += '&dataid=' + dataid;
				ajax.doPost(function(succ, info) {
					if (succ) location = location.href;
					else showDialog(info, 'error');
				}, 'info.htm?cmd=filter', filter);
			}else table.onfiltering(str);
		}
		cell.appendChild(img);
		var filters = [];
		var filter = table.getAttribute('filter');
		if (filter.length) {
			var all = filter.split(';');
			for (var i=0; i<all.length; i++) {
				var item = all[i].split(' ');
				if (item.length != 3) continue;
				filters[item[0]] = item[2];
			}
		}
		for (var j=1; j<table.columnItems; j++) {
			cell = row.insertCell(-1);
			var colinfo = table.columns[j];
			if (colinfo.hidden)
				cell.className = 'hidden';
			else {
				var input = document.createElement('input');
				input.type = 'text';
				input.name = colinfo.name;
				input.dbname = colinfo.dbname;
				var value = filters[colinfo.dbname];
				if (typeof value != 'undefined')
					input.value = value;
				input.className = 'search';
				var width = colinfo.width - 7;
				input.style.width = width + 'px';
				input.coltype = colinfo.type;
				if (input.coltype == 'date') {
					input.onclick = function() {Calendar('cal1').dropdown(this);};
				}else if (colinfo.dropdown) {
					input.dropdown = colinfo.dropdown;
					if (colinfo.type == 'listbox')
						input.readOnly = true;
					input.onclick = function() {ComboBox('combo').dropdown(this,this.dropdown);};
				}
				input.onkeydown = function(e) {
					if (!e) e = window.event;
					if (e.keyCode==13) img.onclick(e)
				}
				cell.appendChild(input);
			}
		}
	}
	addHandle('mousedown', function(e) {
		if (!table.editable || !table.activeInput) return;
		var el = e.target?e.target:e.srcElement;
		if (el == table || el == table.activeInput ||
			el == table.activeCombo) return;
		if (!table.contains(el) && !table.activeInput.contains(el))
			table.onblur();
	});
	table.isDirty = function() {
		if (!this.editable) return false;
		if (this.deleted.length > 0) return true;
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++)
			if (this.rows[i].dirty) return true;
		return false;
	}
	table.mapValue = function(col) {
		if (typeof col == 'string')
			col = this.columnIndex(col);
		var colinfo = this.columns[col];
		if (!colinfo.dropdown) return;
		var array = Dropdowns[colinfo.dropdown];
		var rows = this.rows;
		var value, number = false;
		if (colinfo.type == 'integer' ||
			colinfo.type == 'number')
			number = true;
		for (var i=this.header; i<rows.length; i++) {
			var cell = rows[i].cells[col];
			if (number)
				value = parseInt(cell.innerText);
			else
				value = cell.innerText;
			cell.innerHTML = array[value];
			cell.realValue = value;
		}
	}
	table.onblur = function() {
		if (!this.editable) return;
		if (!this.activeCell) return;
		this.activeCell.save();
		this.activeCell = null;
		this.activeInput = null;
	}
	table.resetUpdate = function() {
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++) {
			this.rows[i].dirty = false;
			this.rows[i].isnew = false;
		}
		this.deleted = new Array();
	}
	table.resetAsnew = function() {
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++) {
			this.rows[i].dirty = true;
			this.rows[i].isnew = true;
		}
		this.deleted = new Array();
	}
	table.reset = function() {
		this.deleted = new Array();
		var size = this.rows.length;
		while (--size >= this.header)
			this.deleteRow(size);
		this.activeCell = null;
	}
	table.sort = function(cell) {
		var cells = cell.parentNode.cells;
		SORTED = cells.length;
		while (SORTED--)
			if (cells[SORTED] == cell) break;
		if (SORTED < 1 ||
			this.rows.length <= this.header+1) return;
		var rows = new Array();
		if (!this.header)
			rows = this.rows;
		else {
			var offset = this.header;
			for (var i=offset; i<this.rows.length; i++)
				rows[i-offset] = this.rows[i];
		}
		var colinfo = this.columns[SORTED];
		if (colinfo.type == 'integer')
			rows.sort(sort_integer);
		else if (colinfo.type == 'number')
			rows.sort(sort_numeric);
		else if (colinfo.transform.indexOf('case') > 0)
			rows.sort(sort_insensitive);
		else if (colinfo.type.indexOf('char') < 0)
			rows.sort(sort_locale);
		else
			rows.sort(sort_default);
		if (!this.lastSort) {
			var img = document.createElement('img');
			img.src = 'asc.gif';
			cell.appendChild(img);
		}else if (this.lastSort != cell) {
			var img = this.lastSort.lastChild;
			img.src = 'asc.gif';
			cell.appendChild(img);
		}else {
			var img = cell.lastChild;
			if (img.src.indexOf('asc')<0)
				img.src = 'asc.gif';
			else {
				rows.reverse();
				img.src = 'desc.gif';
			}
		}
		var i, tbody = this.tBodies[0];
		for (i=0; i<rows.length; i++)
			tbody.appendChild(rows[i]);
		this.lastSort = cell;
	}
	table.insert = function(force) {
		if (!this.editable && !force) return null;
		var index = this.firstSelected();
		if (index < 1 || table.addtail) index = -1;
		var row = this.insertRow(index);
		row.height = 20;
		index = row.rowIndex;
		row.isnew = true;
		this._initrow(row);
		var cell = row.insertCell(-1);
		if (this.columns[0].hidden)
			cell.className = 'hidden';
		else
			cell.className = 'select';
		if (this.checkbox) {
			var box = document.createElement('INPUT');
			box.type = 'checkbox';
			cell.appendChild(box);
			cell = box;
		}else
			cell.innerText = index;
		cell.style.cursor = pointer;
		cell.onclick = function(e) {
			var row = this.parentNode;
			if (!row.table)
				row = row.parentNode;
			row.select(!row.selected);
			if (!e) e = event;
			e.cancelBubble = true;
			if (e.stopPropagation)
				e.stopPropagation();
		}
		for (var j=1; j<this.columnItems; j++) {
			cell = row.insertCell(-1);
			if (!this.header)
				cell.innerHTML = "<br>";
			else if (this.columns[j].hidden) {
				cell.style.display = 'none';
				this._initcell(cell, index, j);
			}else {
				if (this.columns[j].initial != '')
					cell.innerHTML = this.columns[j].initial;
				else cell.innerHTML = "<br>";
				if (this.columns[j].type == 'readonly')
					cell.className = 'readonly';
				this._initcell(cell, index, j);
			}
		}
		if (index == this.rows.length - 1)
			row.scrollIntoView();
		return row;
	}
	table.remove = function(force) {
		if (!this.editable && !force) return;
		var dels = this.deleted.length;
		var primary = this.primary.length;
		var i = this.rows.length;
		var fails = 0;
		while (i--) {
			if (i < this.header) break;
			if (this.rows[i].selected < 1) continue;
			if (!this.rowDeleting(this.rows[i]))
				fails++;
			else {
				if (primary && !this.rows[i].isnew)
					this.deleted[dels++] = this.rows[i].getPrimary();
				this.deleteRow(i);
			}
		}
		size = this.rows.length;
		for (++i; i<size; i++) {
			var cells = this.rows[i].cells;
			if (!this.checkbox)
				cells[0].innerText = i;
			for (var j=0; j<cells.length; j++)
				cells[j].row = i;
		}
		if (fails > 0) this.rowDeleting(fails);
	}
	table.firstSelected = function() {
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++)
			if (this.rows[i].selected > 0) return i;
		return 0;
	}
	table.firstSelectedId = function() {
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++)
			if (this.rows[i].selected > 0)
				return this.rows[i].getId();
		return null;
	}
	table.getRowId = function(row) {
		if (row < 1 || row >= this.rows.length) return null;
		return this.rows[row].getId();
	}
	table.getSelected = function() {
		if (this.primary.length < 1) return '';
		var xml = "<array name='" + this.id + "'>";
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++) {
			if (this.rows[i].selected < 1) continue;
			xml += '<item>' + this.rows[i].getPrimary() + '</item>';
		}xml += "</array>";
		return xml;
	}
	table.getSelectedIds = function() {
		if (this.primary.length < 1) return '';
		var ids = "";
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++) {
			if (this.rows[i].selected < 1) continue;
			if (ids) ids += ';';
			ids += this.rows[i].getId();
		}
		return ids;
	}
	table.getFull = function() {
		var xml = "<array name='" + this.id + "'>";
		var i = this.header;
		var size = this.rows.length;
		for (; i<size; i++) {
			var row = this.rows[i];
			if (row.selected < 0) continue;
			xml += '<item>';
			for (var j=0; j<row.cells.length; j++) {
				if (!this.columns[j].name) continue;
				xml += "<" + this.columns[j].name + ">";
				if (this.columns[j].realValue)
					xml += row.cells[j].realValue;
				else if (this.columns[j].textarea)
					xml += row.cells[j].innerHTML;
				else
					xml += row.cells[j].innerText;
				xml += "</" + this.columns[j].name + ">";
			}xml += "</item>";
		}xml += "</array>";
		return xml;
	}
	table.getUpdate = function() {
		var xml = "<array name='" + this.id + "'>";
		var primary = (this.primary.length > 0);
		var i, size = 0;
		if (primary) {
			size = this.deleted.length;
			for (i=0; i<size; i++)
				xml += '<item type="delete">' + this.deleted[i] + '</item>';
		}
		size = this.rows.length;
		for (i=this.header; i<size; i++) {
			var row = this.rows[i];
			if (primary) {
				if (!row.dirty) continue;
				if (row.isnew)
					xml += '<item type="create">';
				else
					xml += '<item type="update">';
			}else xml += '<item>';
			for (var j=0; j<row.cells.length; j++) {
				if (!this.columns[j].name) continue;
				xml += "<" + this.columns[j].name + ">";
				if (this.columns[j].realValue)
					xml += row.cells[j].realValue;
				else if (this.columns[j].textarea)
					xml += row.cells[j].innerHTML;
				else
					xml += row.cells[j].innerText;
				xml += "</" + this.columns[j].name + ">";
			}xml += "</item>";
		}xml += "</array>";
		return xml;
	}
	table.hasCreate = function() {
		var size = this.rows.length;
		var i = this.header;
		for (; i<size; i++)
			if (this.rows[i].isnew) return true;
		return false;
	}
	table._initrow = function(row) {
		row.table = this;
		row.selected = 0;
		row.dirty = false;
		row.getPrimary = function() {
			var primary = this.table.primary;
			var result = '';
			for (var i=0; i<primary.length; i++) {
				result += '<'+primary[i]+'>';
				result += this.getColumn(primary[i]);
				result += '</'+primary[i]+'>';
			}return result;
		}
		row.getId = function() {
			var primary = this.table.primary;
			var result = '';
			for (var i=0; i<primary.length; i++) {
				if (result) result += ',';
				result += this.getColumn(primary[i]);
			}return result;
		}
		row.getColumn = function(col) {
			if (typeof col != 'string')
				return this.cells[col].innerText;
			var columns = this.table.columns;
			for (var j=0; j<this.cells.length; j++) {
				if (columns[j].name == col)
					return this.cells[j].innerText;
			}return null;
		}
		row.setColumn = function(col, value) {
			if (typeof col != 'string')
				this.cells[col].innerHTML = value;
			else {
				var columns = this.table.columns;
				for (var j=0; j<this.cells.length; j++) {
					if (columns[j].name == col) {
						this.cells[j].innerHTML = value;
						break;
					}
				};
			}
		}
		row.select = function(state) {
			if (this.selected < 0) return;
			this.selected = state?1:0;
			this.style.backgroundColor = state?'#D9F3FC':'';
			if (!this.table.header) return;
			this.table.rows[0].selected = this.table.firstSelected() > 0;
			if (this.table.checkbox) {
				var box = this.cells[0].lastChild;
				box.checked = state;
				box = this.table.rows[0];
				box.cells[0].lastChild.checked = box.selected;
			}
		}
	}
	table.setColumn = function(col, value) {
		if (typeof col == 'string')
			col = this.columnIndex(col);
		for (var i=1; i<this.rows.length; i++)
			this.rows[i].cells[col].innerHTML = value;
	}
	table.setTextarea = function (col) {
		if (typeof col == 'string')
			col = this.columnIndex(col);
		var size = this.columns.length;
		if (col < 0 || col >= size) return;
		this.columns[col].textarea = true;
	}
	table.isPrimary = function (name) {
		var len = this.primary.length;
		if (len < 1) return false;
		for (var i=0; i<len; i++)
			if (this.primary[i] == name) return true;
		return false;
	}
	table.rowDeleting = function(row) {
		return true;
	}
	table.cellEditing = function(cell) {
		return true;
	}
	table.cellChanged = function(cell, value) {
		return true;
	}
	table.oncelledit = null;
	table._initcell = function(cell, i, j) {
		cell.table = this;
		cell.row = i;
		cell.column = j;
		if (!this.header) return;
		var transform = this.columns[j].transform;
		cell.style.textTransform = transform;
		cell.oldText = cell.innerText;
		if (!cell.oldText && !cell.innerHTML.match(/<img/i))
			cell.innerHTML = '<br>';
		cell.setValue = function(value) {
			var table = this.table;
			if (table.cellChanged(this, value)) {
				var transform = this.style.textTransform.toLowerCase();
				if (transform == 'uppercase')
					value = value.toUpperCase();
				else if (transform == 'lowercase')
					value = value.toLowerCase();
				this.innerHTML = value;
				if (this.oldText != this.innerText) {
					this.oldText = this.innerText;
					table.rows[this.row].dirty = true;
					var name = table.columns[this.column].name;
					table.onrowchanged(this.parentNode, name);
				}
			}else
				this.innerText = this.oldText;
			table.activeInput = null;
		}
		cell.getValue = function() {
			var val = this.innerText;
			var type = this.table.columns[this.column].type;
			if (type == 'integer') {
				val = parseInt(val)||0;
			}else if (type == 'number') {
				val = parseFloat(val)||0;
			}return val;
		}
		cell.save = function() {
			var table = this.table;
			var type = table.columns[this.column].type;
			var row = table.rows[this.row];
			if (type.charAt(0) == '*') {
				if (!row.isnew) return;
				type = type.substr(1);
			}
			if (!row.isnew && table.primary.length) {
				var name = table.columns[this.column].name;
				if (table.isPrimary(name)) return;
			}			
			if (type == 'readonly') {
			}else if (type == 'date') {
				var cal = Calendar('cal1');
				cal.style.display = 'none';
				table.activeInput = null;
			}else if (type == 'listbox') {
				var combo = ComboBox('combo');
				combo.style.display = 'none';
				table.activeInput = null;
			}else {
				var input = table.activeInput;
				if (type == 'combo') {
					var combo = ComboBox('combo');
					combo.style.display = 'none';
					if (table.columns[this.column].realValue)
						this.realValue = input.realValue;
					table.activeCombo = null;
				}
				if (!input) return;
				if (!input.value)
					this.setValue("<br>");
				else
					this.setValue(input.value);
			}
		}
		cell.edit = function() {
			var table = this.table;
			if (!table.editable) return false;
			if (!table.columns[this.column].name) {
				table.activeCell = null;
				return false;
			}
			var type = table.columns[this.column].type;
			if (table.activeCell) {
				if (table.activeCell != this)
					table.activeCell.save();
				else if (type!='date' && type!='listbox' &&
					type!='combo') return false;
			}
			table.activeCell = this;
			var row = table.rows[this.row];
			if (type.charAt(0) == '*') {
				if (!row.isnew) return;
				type = type.substr(1);
			}
			if (type == 'readonly') return false;
			var column = table.columns[this.column];
			if (!row.isnew && table.primary.length) {
				var name = column.name;
				if (table.isPrimary(name)) return false;
			}
			if (!table.cellEditing(this)) return false;
			if (type == 'event') {
				table.activeCell = null;
				if (table.oncelledit)
					table.oncelledit(this, row)
			}else if (type == 'date') {
				var cal = Calendar('cal1');
				table.activeInput = cal;
				cal.dropdown(this);
			}else if (type == 'listbox') {
				var combo = ComboBox('combo');
				combo.style.textTransform = column.transform;
				table.activeInput = combo;
				combo.dropdown(this, column.dropdown, 0);
			}else {
				var input = LineEdit(column.textarea);
				input.style.textTransform = column.transform;
				if (column.textarea) {
					input.rows = 6;
					if (this.innerText.length == 0)
						input.value = '';
					else
						input.value = this.innerHTML;
				}else input.value = this.innerText;
				input.owner = this;
				this.innerHTML = '';
				this.appendChild(input);
				input.select();
				table.activeInput = input;
				if (type == 'combo') {
					input.filter = '';
					input.style.imeMode = 'auto';
					var combo = ComboBox('combo');
					table.activeCombo = combo;
					combo.dropdown(input, column.dropdown, 1);
				}else {
					input.filter = type;
					input.style.imeMode = type?'disabled':'auto';
					setTimeout(function(){input.focus();},0);
				}
			}
			return true;
		}
		cell.getColumnInfo = function() {
			return this.table.columns[this.column];
		}
		cell._nextCell = function() {
			var table = this.table;
			if (this.column < table.columns.length-1)
				return table.rows[this.row].cells[this.column+1];
			if (this.row < table.rows.length-1)
				return table.rows[this.row+1].cells[1];
			return table.rows[1].cells[1];
		}
		cell._nextRow = function() {
			var table = this.table;
			var row = this.row + 1;
			if (row == table.rows.length) row = 1;
			return table.rows[row].cells[this.column];
		}
		cell._priorRow = function() {
			var table = this.table;
			var row = this.row - 1;
			if (row < 1) row = table.rows.length - 1;
			return table.rows[row].cells[this.column];
		}
		cell.onclick = function(e) {
			if (!this.edit()) return;
			if (!e) e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation)
				e.stopPropagation();
		}
		cell.onkeydown = function(e) {
			if (!e) e = window.event;
			if (e.keyCode==9 || e.keyCode==13)
				this._nextCell().edit();
			else if (e.keyCode == 38)
				this._priorRow().edit();
			else if (e.keyCode == 40)
				this._nextRow().edit();
			else return;
			return false;
		}
	}
	table.getValue = function(rowIndex, colIndex) {
		if (rowIndex<1 || colIndex<1) return null;
		return this.rows[rowIndex].cells[colIndex].getValue();
	}
	table.columnIndex = function(colname) {
		for (var j=1; j<this.columns.length; j++) {
			if (this.columns[j].name == colname) return j;
		}return 0;
	}
	table.filterDialog = function(html) {
		document.activeGrid = this;
		if (!html) html = 'filter.html';
		openWindow(html, 400, 280);
	}
	table.sortDialog = function(html) {
		document.activeGrid = this;
		if (!html) html = 'sort.html';
		openWindow(html, 400, 280);
	}
	table.onrowchanged = function(row, colname) {
	}
	table.updateView = function() {
		var i = this.header;
		for (; i<this.rows.length; i++) {
			var row = this.rows[i];
			for (var j=1; j<row.cells.length; j++) {
				if (this.columns[j].hidden) continue;
				var cell = row.cells[j];
				if (cell.className == 'readonly') continue;
				if (!this.cellEditing(cell))
					cell.className = 'readonly';
			}
		}
	}
	table.onsorting = null;
	table.onfiltering = null;
	var i = table.header;
	for (; i<table.rows.length; i++) {
		row = table.rows[i];
		if (row.onclick)
			row.style.cursor = 'pointer';
		row.isnew = false;
		table._initrow(row);
		var item = row.cells[0];
		if (item.colSpan > 1) continue;
		if (table.checkbox) {
			if (item.firstChild) {
				var name = item.firstChild.tagName;
				if (!name || name != 'INPUT') {
					if (i != 1 || name != 'BR')
						item.removeChild(item.firstChild);
					else row.selected = -1;
				}
			}
			if (!item.firstChild) {
				var box = document.createElement('INPUT');
				box.type = 'checkbox';
				item.appendChild(box);
			}
			item = item.firstChild;
		}
		item.className = 'select';
		item.onclick = function(e) {
			var row = this.parentNode;
			if (!row.table)
				row = row.parentNode;
			row.select(!row.selected);
			if (!e) e = event;
			e.cancelBubble = true;
			if (e.stopPropagation)
				e.stopPropagation();
		}
		item.style.cursor = pointer;
		for (var j=1; j<row.cells.length; j++) {
			var col = table.columns[j];
			if (col.hidden || !table.editable) {
			}else if (col.type=='readonly' || col.type.charAt(0)=='*')
				row.cells[j].className = 'readonly';
			else if (table.isPrimary(col.name))
				row.cells[j].className = 'readonly';
			table._initcell(row.cells[j], i, j);
		}
	}
	if (table.editable)
		window.addDirty(table);
	return table;
}
function makeDropdown(id, down) {
	var drop = $(id);
	drop.onclick = function(e) {
		if (!this.target) return;
		if (!e) e = window.event;
		var el = e.target?e.target:e.srcElement;
		if (el.tagName == 'INPUT') return;
		if (this.target.style.display == 'none')
			this.expand();
		else this.collase();
	}
	drop.setTarget = function(target) {
		this.target = target;
		if (!target) return;
		cleanChild(target);
		var x = this.offsetLeft;
		var y = this.offsetTop;
		var p = this.offsetParent;
		while (p && p!=target.offsetParent) {
			x += p.offsetLeft;
			y += p.offsetTop;
			if (p.currentStyle) {
				x += parseInt(p.currentStyle.borderLeftWidth)||0;
				y += parseInt(p.currentStyle.borderTopWidth)||0;
			}
			p = p.offsetParent;
		}
		y += this.offsetHeight;
		with (target.style) {
			left = x + "px";
			top = y + "px";
			display = 'none';
			position = 'absolute';
			zIndex = 9999;
		}
		if (target.initial) return;
		target.initial = true;
		addHandle('mousedown', function(e) {
			var obj = e.target?e.target:event.srcElement;
			if (!drop.contains(obj) && !target.contains(obj))
				target.style.display = 'none';
		});
	}
	drop.expand = function() {
		if (!this.target ||
			!this.target.firstChild) return;
		this.target.style.display = 'block';
	}
	drop.collase = function() {
		if (!this.target) return;
		this.target.style.display = 'none';
	}
	drop.setImage = function(url) {
		if (this.firstChild.tagName == 'IMG')
			this.firstChild.src = url;
	}
	drop.getTitle = function() {
		var item = this.lastChild;
		if (!item) item = this;
		if (item.tagName != 'INPUT')
			return item.innerText;
		return item.value;
	}
	drop.setTitle = function(title) {
		var item = this.lastChild;
		if (!item) item = this;
		if (item.tagName != 'INPUT')
			item.innerHTML = title;
		else
			item.value = title;
	}
	drop.setTarget(down);
	drop.onsearch = null;
	drop.dropdown = false;
	if (drop.lastChild && drop.lastChild.tagName == 'INPUT') {
		var input = drop.lastChild;
		if (!input.value)
			input.value = drop.title;
		else
			input.className = 'focus';
		input.onfocus = function(e) {
			if (this.value == drop.title)
				this.value = '';
			this.className = 'focus';
		}
		input.onblur = function(e) {
			if (this.value == '')
				this.value = drop.title;
			this.className = '';
		}
		input.onkeydown = function(e) {
			if (!drop.onsearch) return;
			if (!e) e = window.event;
			if (e.keyCode == 13) drop.onsearch(this.value);
		}
		drop.onclick = function(e) {
			if (!this.onsearch) return;
			if (!e) e = window.event;
			var el = e.target||e.srcElement;
			var offset = this.offsetWidth;
			offset += getPosition(this).x;
			if (this.dropdown) {
				if (offset > getCursor(e).x + 95) return;
				if (this.target.style.display == 'none')
					this.expand();
				else
					this.collase();
			}else {
				if (el != this) return;
				if (offset < getCursor(e).x + 20)
					this.onsearch(input.value);
			}
		}
	}
	return drop;
}
// Calendar
var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var daynames = new Array("日","一", "二", "三","四", "五", "六");
function getTitle(year, month) {
	month++;
	return year + "年" + month + "月";
}
function Calendar(id) {
	var table = $(id);
	if (!table) {
		table = document.createElement("TABLE");
		table.id = id;
		table.setAttribute("cellpadding", "0");
		table.className = 'calendar';
		var row = table.insertRow(-1);
		row.insertCell(-1).innerHTML = '&#171;';
		row.insertCell(-1).innerHTML = '&#8249;';
		row.insertCell(-1).colSpan = 3;
		row.insertCell(-1).innerHTML = '&#8250;';
		row.insertCell(-1).innerHTML = '&#187;';
		for (var i=0; i<7; i++) {
			var row = table.insertRow(-1);
			for (var j=0; j<7; j++)
				row.insertCell(-1);
		}
		table.style.display = "none";
		document.body.appendChild(table);
	}
	if (table.makeButton) return table;
	table.input = null;
	table.forwardDays = null;
	table.cellPadding = 0;
	table.cellSpacing = 0;
	table.style.position = "absolute";
	table.makeButton = function(el, step) {
		el.table = table;
		el.className = "btnUp";
		el.setAttribute('UNSELECTABLE', 'on');
		el.style.cursor = pointer;
		el.onclick = function() {
			this.table.nextMonth(step);
		}
		el.onmouseup = function() {
			this.className = "btnUp";
		}
		el.onmousedown = function() {
			this.className = "btnDown";
		}
	}
	addHandle('mousedown', function(e) {
		var obj = e.target?e.target:event.srcElement;
		if (obj!=table.input && !table.contains(obj))
			table.style.display = 'none';
	});
	table.getDays = function(year, month) {
		if (month != 1) return ((month%7)&1)?30:31;
		return ((0==year%4)&&(year%100))||(0==year%400)?29:28;
	}
	with (table.rows[0]) {
		table.makeButton(cells[0], -12);
		table.makeButton(cells[1], -1);
		cells[2].className = "month btnUp";
		table.makeButton(cells[3], 1);
		table.makeButton(cells[4], 12);
	}
	with (table.rows[1]) {
		firstChild.className = "sunday";
		lastChild.className = "satday";
		for (var i=0; i<cells.length; i++) {
			cells[i].setAttribute('UNSELECTABLE','on');
			cells[i].innerHTML = daynames[i];
		}
	}
	for (i=2; i<table.rows.length; i++) {
		var cell = table.rows[i].firstChild;
		while (cell) {
			cell.table = table;
			cell.onmousedown = function() {
				this.table.dateClicked(this);
			}
			cell.onmouseover = function(e) {
				addClass(this, 'hover');
			}
			cell.onmouseout = function(e) {
				removeClass(this, 'hover');
			}
			cell = cell.nextSibling;
		}
	}
	table.nextMonth = function(delta) {
		var year, month = this.month + delta;
		if (month > 11) {
			month -= 12;
			year = this.year + 1;
		}
		else if (month < 0) {
			month += 12;
			year = this.year - 1;
		}
		else year = this.year;
		var day = this.getDays(year, month);
		if (this.day < day) day = this.day;
		this.populate(year, month, day);
	}
	table.populate = function(year, month, day) {
		this.year = year = parseInt(year);
		this.month = month = parseInt(month);
		this.day = day = parseInt(day);
		this.rows[0].cells[2].innerHTML = getTitle(year, month);
		var first = new Date(year, month, 1);
		var count = this.getDays(year, month);
		if (i = week = first.getDay()) {
			if (month == 0)
				last = 31;
			else
				last = this.getDays(year, month - 1);
			while (i--) {
				this.rows[2].cells[i].className = "notday";
				this.rows[2].cells[i].innerHTML = last--;
			}
		}
		last = 1; i = 2;
		cell = this.rows[2].cells[week];
		while (cell) {
			if (day == last) {
				day = 0;
				cell.className = "today";
			}else if (i>5&&last<21)
				cell.className = "notday";
			else if (week == 0)
				cell.className = "sunday";
			else if (week == 6)
				cell.className = "satday";
			else
				cell.className = "theday";
			cell.innerHTML = last++;
			week = (week + 1) % 7;
			if (last > count) last = 1;
			cell = cell.nextSibling;
			if (!cell && ++i < this.rows.length)
				cell = this.rows[i].firstChild;
		}
		this.style.display = "block";
		this.style.zIndex = "1000";
	}
	table.dropdown = function(input, forwards) {
		var pt = getPosition(input);
		pt.y += input.offsetHeight;
		this.forwardDays = forwards;
		this.style.left = pt.x + 'px';
		this.style.top = pt.y + 'px';
		this.input = input;
		var theday;
		if (input.tagName == 'INPUT') {
			if (!input.setValue) {
				input.setValue = function(value) {
					if (value == this.value) return;
					this.value = value;
					if (input.form)
						input.form.dirty = true;
				}
				input.autocomplete = "off";
			}
			theday = input.value.split(/[-\.\/]/);
		}else
			theday = input.innerText.split(/[-\.\/]/);
		if (theday.length == 3)
			this.populate(theday[0], theday[1]-1, theday[2])
		else {
			var day = new Date();
			this.populate(day.getFullYear(), day.getMonth(), day.getDate());
		}
		if (pt.y + this.offsetHeight > document.body.offsetHeight) {
			pt.y -= this.offsetHeight;
			pt.y -= input.offsetHeight;
			this.style.top = pt.y + 'px';
		}
		setTimeout(function(){input.focus();},0);
	}
	table.dateClicked = function(cell) {
		if (!this.input) return;
		var month, day = cell.innerText;
		var year = this.year;
		if (cell.className.indexOf('notday') < 0)
			month = this.month;
		else if (day > 15) {
			month = this.month - 1;
			if (month < 0) {
				month = 11;
				year--;
			}
		}else {
			month = this.month + 1;
			if (this.month > 11) {
				month = 0;
				year++;
			}
		}
		if (typeof this.forwardDays == 'number') {
			var now = new Date();
			now.setHours(0, 0, 0, 0);
			var msec = now.getTime();
			msec += this.forwardDays * 86400000;
			var current = new Date(year, month, day);
			if (current.getTime() < msec) return;
		}
		if (++month < 10) month = '0'+month;
		if (day < 10) day = '0' + day;
		var date = year+"-"+month+"-"+day;
		this.input.setValue(date);
		this.style.display = "none";
	}
	return table;
}
function MultiSelector (lister) {
	this.lister = lister;
	this.id = 0;
	this.addElement = function (el) {
		if (el.tagName != 'INPUT' || el.type != 'file') return;
		el.name = 'Files[' + this.id++ + ']';
		el.selector = this;
		el.onchange = function() {
			var input = document.createElement('input');
			input.type = 'file';
			this.parentNode.insertBefore(input, this);
			this.selector.addElement(input);
			this.selector.addListRow(this);
			this.style.position = 'absolute';
			this.style.left = '-1000px';
		}
	}
	this.addListRow = function(el) {
		var newrow = document.createElement('div');
		var button = document.createElement('input');
		button.type = 'button';
		button.value = 'Delete';
		button.className = 'delete';
		button.element = el;
		button.onclick= function() {
			this.element.parentNode.removeChild(this.element);
			this.parentNode.parentNode.removeChild(this.parentNode);
			return false;
		};
		var matches = el.value.match(/([^\/\\]+)$/);
		newrow.innerHTML = matches[1];
		newrow.appendChild(button);
		this.lister.appendChild(newrow);
	}
}
function Listbox(id) {
	var listbox = $(id);
	if (!listbox || typeof listbox.activeNode == 'object')
		return listbox;
	listbox.maxVisible = 10;
	listbox.activeNode = null;
	listbox.itemClicked = null;
	listbox.addArray = function (array) {
		if (!array.length) return;
		for (var i=0; i<array.length; i++) {
			var itemid = array[i].itemid;
			if (typeof itemid == 'undefined')
				itemid = array[i].id;
			this.addText(array[i].title, itemid);
		}
		this.autoVisible();
	}
	listbox.autoVisible = function(size) {
		if (size) this.maxVisible = size;
		if (this.childNodes.length <= this.maxVisible)
			this.style.height = "auto";
		else {
			var height = this.firstChild.offsetHeight;
			if (!height) height = 19;
			height *= this.maxVisible;
			this.style.height = height + "px";
		}
	}
	listbox.addText = function (text, id) {
		var div = document.createElement('div');
		div.innerHTML = text;
		div.itemid = id;
		this.appendChild(div);
		this.addItem(div);
	}
	listbox.addItem = function (obj) {
		obj.status = 0;
		obj.owner = this;
		if (typeof obj.itemid == 'undefined')
			obj.itemid = obj.getAttribute('itemid');
		obj.setStatus = function(status) {
			if (this.status == status) return;
			this.status = status;
			var str = 'file';
			if (this.status)
				str += this.status;
			this.icon.src = str + '.gif';
		}
		obj.setTitle = function(title) {
			this.InnerHTML = title;
		}
		obj.getTitle = function() {
			return this.InnerHTML;
		}
		obj.getImage = function() {
			return this.icon.src;
		}
		obj.setImage = function(image) {
			this.icon.src = image;
		}
		obj.icon = new Image();
		obj.icon.src = 'file.gif';
		obj.insertBefore(obj.icon, obj.firstChild);
		obj.onclick = function(e) {
			if (!e) e = window.event;
			if (this.owner.itemClicked)
				this.owner.itemClicked(e, this);
		}
		obj.onmousedown = function(e) {
			if (!e) e = window.event;
			this.setActive();
		}
		obj.onmouseenter = function() {
			if (this.className != 'active')
				this.className = 'hover';
		}
		obj.onmouseleave = function() {
			if (this.className != 'active')
				this.className = '';
		}
		obj.setActive = function() {
			if (this.owner.activeNode) {
				if (this.owner.activeNode == this) return;
				this.owner.activeNode.className = '';
			}
			this.owner.activeNode = this;
			this.className = 'active';
		}
	}
	listbox.setIcon = function(obj, url) {
		if (!obj.icon) return;
		obj.icon.src = url;
	}
	listbox.onmousedown = function(e) {
		if (!e) e = window.event;
		var el = e.target?e.target:e.srcElement;
		if (this.contains(el)) return;
		if (e.button > 1) return;
		if (this.activeNode)
			this.activeNode.className = '';
		this.activeNode = null;
	}
	listbox.callback = function(func) {
		var child = this.firstChild;
		while (child) {
			func(child);
			child = child.nextSibling;
		}
	}
	listbox.clear = function() {
		this.activeNode = null;
		listbox.innerHTML = '';
	}
	cleanChild(listbox);
	var child = listbox.firstChild;
	while (child) {
		listbox.addItem(child);
		child = child.nextSibling;
	}
	return listbox;
}
function TreeView(id, rootline) {
	var treeview = $(id);
	if (!treeview || typeof treeview.subitem == 'object')
		return treeview;
	treeview.subitem = null;
	treeview.activeNode = null;
	treeview.itemid = 0;
	with (treeview.style) {
//		overflow = 'auto';
		listStyle = 'none';
		padding = '0';
		margin = '0';
	}
	treeview.level = rootline?0:-1;
	treeview.icon = new Image();
	treeview.icon.src = 'loading.gif';
	treeview.itemClicked = null;
	treeview.itemPopmenu = null;
	treeview.getFolder = function() {
		if (!this.activeNode) return null;
		if (this.activeNode.folder)
			return this.activeNode;
		return this.activeNode.getParent();
	}
	treeview.preInsert = function(p) {
		if (p.subitem) {
			var last = p.subitem.lastChild;
			var img = last.icon.previousSibling;
			if (img) img.src = img.src.replace(/\.gif/, '2.gif');
			if (!last.subitem) return;
			var items = last.subitem.childNodes;
			for (var i=0; i<items.length; i++) {
				img = items[i].firstChild.childNodes[p.level];
				if (img) img.src = 'line.gif';
			}
		}else if (p == this) {
			p.subitem = this;
		}else {
			p.subitem = document.createElement('ul');
			p.appendChild(p.subitem);
			p.icon.src = 'folder.gif';
			p.folder = true;
			var img = p.icon.previousSibling;
			if (img) img.src = img.src.replace(/join/, 'minus');
		}
	}
	treeview.createItem = function(title, itemid) {
		var obj = document.createElement('li');
		var div = document.createElement('div');
		obj.itemid = itemid;
		div.innerHTML = title;
		obj.folder = false;
		obj.appendChild(div);
		return obj;
	}
	treeview.makeList = function(array, p) {
		for (var i=0; i<array.length; i++) {
			var obj = this.createItem(array[i].title, array[i].itemid);
			if (array[i].folder) obj.folder = true;
			if (array[i].childs && array[i].childs.length) {
				var item = document.createElement('ul');
				this.makeList(array[i].childs, item);
				obj.appendChild(item);
			}
			p.appendChild(obj);
		}
	}
	treeview.addArray = function(array, p) {
		if (!array.length) return;
		if (p == null)
			p = this;
		else if (typeof p == 'number') {
			if (p >= this.childNodes.length) return;
			p = this.childNodes[p];
		};
		for (var i=0; i<array.length; i++) {
			this.preInsert(p);
			var itemid = array[i].itemid;
			if (typeof itemid == 'undefined')
				itemid = array[i].id;
			var obj = this.createItem(array[i].title, itemid);
			if (array[i].folder) obj.folder = true;
			if (array[i].childs && array[i].childs.length) {
				var item = document.createElement('ul');
				this.makeList(array[i].childs, item);
				obj.appendChild(item);
			}
			p.subitem.appendChild(obj);
			this.addItem(p, obj);
		}
	}
	treeview.addRoot = function(title, itemid, image) {
		var obj = this.addText(title, itemid);
		obj.setFolder(true);
		if (image) obj.setImage(image);
		if (this.itemClicked)
			this.itemClicked(null, obj);
	}
	treeview.addText = function(title, itemid, p) {
		if (p == null)
			p = this;
		else if (typeof p == 'number') {
			if (p >= this.childNodes.length) return;
			p = this.childNodes[p];
		}
		this.preInsert(p);
		var obj = this.createItem(title, itemid);
		p.subitem.appendChild(obj);
		this.addItem(p, obj);
		return obj;
	}
	treeview.toggle = function(obj) {
		obj.toggle();
	}
	treeview.setIcon = function(obj, url) {
		if (!obj.icon) return;
		obj.icon.src = url;
	}
	treeview.delItem = function(obj) {
		var owner = obj.parentNode;
		if (owner.childNodes.length < 2) {
			if (this == owner)
				owner.removeChild(obj);
			else {
				var item = owner.parentNode;
				item.removeChild(owner);
				var img = item.icon.previousSibling;
				if (img.src.indexOf('2.gif') > 0)
					img.src = 'join2.gif';
				else img.src = 'join.gif';
			}
		}else if (obj == owner.lastChild) {
			owner.removeChild(obj);
			var img = owner.lastChild.icon.previousSibling;
			if (img) img.src = img.src.replace(/2\.gif/, '.gif');
		}else
			owner.removeChild(obj);
		if (obj == this.activeNode)
			this.activeNode = null;
	}
	treeview.onselectstart = function(e) {
		return false;
	}
	treeview.oncontextmenu = function(e) {
		if (this.itemPopmenu) return false;
	}
	treeview.onmousedown = function(e) {
		if (!e) e = window.event;
		var el = e.target?e.target:e.srcElement;
		if (this.contains(el)) return;
		if (e.button > 1) return;
		if (this.activeNode)
			this.activeNode.firstChild.className = '';
		this.activeNode = null;
	}
	treeview.addItem = function(p, obj) {
		if (!p || obj.icon) return;
		if (!p.folder) {
			p.icon.src = 'folder.gif';
			p.folder = true;
		}
		obj.status = 0;
		obj.level = p.level + 1;
		var first = p.firstChild.firstChild;
		if (obj.childNodes.length < 2)
			obj.subitem = null;
		else {
			obj.subitem = obj.lastChild;
		}
		var div = obj.firstChild;
		obj.owner = this;
		if (obj.getAttribute('itemid'))
			obj.itemid = obj.getAttribute('itemid');
		obj.getParent = function() {
			var p = this.parentNode.parentNode;
			return p ? p : this.owner;
		}
		obj.setStatus = function(status, update) {
			if (this.status == status) return;
			this.status = status;
			var str = this.folder?'folder':'file';
			if (this.status)
				str += this.status;
			this.icon.src = str + '.gif';
			if (!update) return;
			var p = this.parentNode.parentNode;
			if (p) p.setStatus(status, true);
		}
		obj.updateStatus = function(self) {
			if (!this.subitem) return;
			var status = -1, multi = false;
			var child = this.subitem.firstChild;
			while (child) {
				if (!self && child.subitem)
					child.updateStatus();
				if (child.status < 0 ||
					status == child.status) {
				}else if (child.status == 0) {
					if (status < 0) status = 0;
				}else if (status < 1) {
					status = child.status;
				}else if (child.status < status) {
				//	status = child.status;
					status = 1; break;
				}child = child.nextSibling;
			}
			this.setStatus(status);
		}
		obj.callback = function(func) {
			if (!this.subitem) return;
			var child = this.subitem.firstChild;
			while (child) {
				func(child);
				child.callback(func);
				child = child.nextSibling;
			}
		}
		obj.setTitle = function(title) {
			var text = this.firstChild.lastChild;
			text.nodeValue = title;
		}
		obj.getTitle = function() {
			var text = this.firstChild.lastChild;
			return text.nodeValue;
		}
		obj.setColor = function(color) {
			var div = this.firstChild;
			div.style.color = color;
		}
		obj.findItem = function(title) {
			if (!this.subitem) return null;
			var child = this.subitem.firstChild;
			while (child) {
				if (child.getTitle() == title) return child;
				child = child.nextSibling;
			}return null;
		}
		obj.findItemId = function(itemid) {
			if (!this.subitem) return null;
			var child = this.subitem.firstChild;
			while (child) {
				if (child.itemid == itemid) return child;
				child = child.nextSibling;
			}return null;
		}
		obj.loaded = false;
		obj.setFolder = function(folder) {
			this.folder = folder?true:false;
			var str = folder?'folder':'file';
			if (this.status)
				str += this.status;
			this.icon.src = str + '.gif';
		}
		obj.getImage = function() {
			return this.icon.src;
		}
		obj.setImage = function(image) {
			this.icon.src = image;
		}
		obj.clear = function() {
			if (!this.subitem) return;
			this.removeChild(this.subitem);
			this.subitem = null;
		}
		obj.update = function(info) {
			if (info) {
				var array = eval(info);
				if (this.subitem) {
					this.removeChild(this.subitem);
					this.subitem = null;
				}
				var img = this.icon.previousSibling;
				if (!img) {
				}else if (img.src.indexOf('2.gif') > 0)
					img.src = 'join2.gif';
				else img.src = 'join.gif';
				this.owner.addArray(array, this);
			}else {
				var img = this.icon.previousSibling;
				if (!img) return;
				var last = '.gif';
				if (img.src.indexOf('2.gif') > 0)
					last = '2.gif';
				if (!this.subitem)
					img.src = 'join' + last;
				else if (this.subitem.style.display == '')
					img.src = 'minus' + last;
				else
					img.src = 'plus' + last;
			}
		}
		obj.reload = function() {
			var href = this.getAttribute('href');
			if (href) this.ajaxLoad(href);
		}
		obj.onload = null;
		obj.ajaxLoad = function(url, handle) {
			var oldimg = this.icon.src;
			if (oldimg.indexOf('loading.gif')>=0) return;
			this.icon.src = 'loading.gif';
			var ajax = new XmlHttp;
			var self = this;
			this.setAttribute('href', url);
			ajax.doGet(url, function(succ, info) {
				if (handle)
					handle(succ, info);
				else if (succ)
					self.update(info);
				self.icon.src = oldimg;
				self.loaded = true;
				if (succ && self.onload)
					self.onload();
			});
		}
		var item = div.lastChild;
		for (var i=0; i<p.level-1; i++) {
			var img = new Image();
			img.src = first.src;
			div.insertBefore(img, item);
			first = first.nextSibling;
		}
		if (p.level > treeview.level && p.level) {
			var img = new Image();
			if (p == p.parentNode.lastChild)
				img.src = 'blank.gif';
			else
				img.src = 'line.gif';
			div.insertBefore(img, item);
		}
		obj.icon = new Image();
		if (obj.folder || obj.className == 'folder') {
			obj.folder = true;
			obj.icon.src = 'folder.gif';
		}else
			obj.icon.src = 'file.gif';
		obj.icon.className = 'icon';
		if (p.level >= 0) {
			var img = new Image();
			if (obj.subitem || obj.folder) {
				if (obj == obj.parentNode.lastChild)
					img.src = 'plus.gif';
				else
					img.src = 'plus2.gif';
				if (obj.subitem)
					obj.subitem.style.display = 'none';
			}else if (obj == obj.parentNode.lastChild)
				img.src = 'join.gif';
			else
				img.src = 'join2.gif';
			div.insertBefore(img, item);
		}
		div.insertBefore(obj.icon, item);
		div.onclick = function(e) {
			if (!e) e = window.event;
			var obj = this.parentNode;
			if (obj.owner.itemClicked)
				obj.owner.itemClicked(e, obj);
		}
		div.onmousedown = function(e) {
			if (!e) e = window.event;
			var obj = this.parentNode;
			obj.setActive();
			if (e.button < 2)
				obj.toggle();
		}
		div.oncontextmenu = function(e) {
			var obj = this.parentNode;
			if (!obj.owner.itemPopmenu) return;
			if (!e) e = window.event;
			e.cancelBubble = true;
			return obj.owner.itemPopmenu(e, obj);
		}
		div.onmouseenter = function() {
			if (this.className != 'active')
				this.className = 'hover';
		}
		div.onmouseleave = function() {
			if (this.className != 'active')
				this.className = '';
		}
		obj.expand = function() {
			if (!this.subitem) return;
			if (this.subitem.style.display == '') return;
			this.subitem.style.display = '';
			var img = this.icon.previousSibling;
			if (img) img.src = img.src.replace(/plus/, 'minus');
		}
		obj.expandAll = function() {
			if (!this.subitem) return;
			this.subitem.style.display = '';
			var img = this.icon.previousSibling;
			if (img) img.src = img.src.replace(/plus/, 'minus');
			var child = this.subitem.firstChild;
			while (child) {
				if (child.subitem)
					child.expandAll();
				child = child.nextSibling;
			}
		}
		obj.collase = function() {
			if (!this.subitem) return;
			if (this.subitem.style.display == 'none') return;
			this.subitem.style.display = 'none';
			var img = this.icon.previousSibling;
			if (img) img.src = img.src.replace(/minus/, 'plus');
		}
		obj.toggle = function() {
			if (!this.subitem) return;
			if (this.subitem.style.display == 'none')
				this.expand();
			else
				this.collase();
		}
		obj.setActive = function() {
			if (this.owner.activeNode) {
				if (this.owner.activeNode == this) return;
				this.owner.activeNode.firstChild.className = '';
			}
			this.owner.activeNode = this;
			this.firstChild.className = 'active';
		}
		if (!obj.subitem) return;
		cleanChild(obj.subitem);
		var child = obj.subitem.firstChild;
		if (!child) {
			obj.removeChild(obj.lastChild);
			obj.subitem = null;
		}
		while (child) {
			cleanChild(child);
			this.addItem(obj, child);
			child = child.nextSibling;
		}
	}
	treeview.ajaxLoad = function(url, handle) {
		var ajax = new XmlHttp;
		var self = this;
		this.setAttribute('href', url);
		ajax.doGet(url, function(succ, info) {
			if (handle)
				handle(succ, info);
			else
				treeview.addArray(eval(info));
		});
	}
	treeview.findItemId = function(itemid) {
		var child = this.firstChild;
		while (child) {
			if (child.itemid == itemid) return child;
			child = child.nextSibling;
		}return null;
	}
	treeview.updateStatus = function(self) {
		var child = this.firstChild;
		while (child) {
			child.updateStatus(self);
			child = child.nextSibling;
		}
	}
	treeview.callback = function(func) {
		var child = this.firstChild;
		while (child) {
			func(child);
			child.callback(func);
			child = child.nextSibling;
		}
	}
	treeview.clear = function() {
		this.subitem = null;
		this.activeNode = null;
		while(true) {
			var child = this.firstChild;
			if (!child) break;
			this.removeChild(child);
		}
	}
	treeview.reload = function() {
		var href = this.getAttribute('href');
		if (!href) return;
		this.clear();
		this.subitem = null;
		this.activeNode = null;
		this.ajaxLoad(href);
	}
	treeview.getFolders = function(tree) {
		if (!tree) {
			var tree = TreeView(document.createElement("UL"));
			tree.className = 'treeview';
		}
		var child = this.firstChild;
		while (child) {
			if (child.folder) {
				var obj = tree.addText(child.getTitle(), child.itemid);
				obj.setFolder(true);
				obj.pointer = child;
			}
			child = child.nextSibling;
		}
		tree.itemClicked = function(e, obj) {
			if (!obj.pointer || obj.loaded) return;
			if (obj.pointer.loaded)
				this.syncItem(obj.pointer, obj);
			else {
				var ref = obj.pointer.owner;
				obj.pointer.onload = function() {
					obj.owner.syncItem(this, obj);
					this.collase();
				}
				ref.itemClicked(e, obj.pointer);
			}
		}
		tree.syncItem = function(src, dst) {
			dst.loaded = true;
			if (!src.subitem) return;
			var owner = dst.owner;
			var child = src.subitem.firstChild;
			while (child) {
				if (child.folder) {
					var obj = owner.addText(child.getTitle(), child.itemid, dst);
					obj.setFolder(true);
					obj.pointer = child;
				}
				child = child.nextSibling;
			}
		}
		return tree;
	}
	cleanChild(treeview);
	var child = treeview.firstChild;
	while (child) {
		cleanChild(child);
		treeview.addItem(treeview, child);
		child = child.nextSibling;
	}
	treeview.reload();
	return treeview;
}