function opacityState(state) {
	return 1-Math.sin(Math.PI*(5-state)/10);
}
function GTicks(a) {
	this.ticks = a;
	this.tick = 0;
}
GTicks.prototype.reset = function()
{
	this.tick = 0;
}
GTicks.prototype.next = function()
{
	this.tick++;
	var a=Math.PI*(this.tick/this.ticks-0.5);
	return (Math.sin(a)+1)/2;
}
GTicks.prototype.more = function()
{
   return this.tick < this.ticks
}
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 i = obj.childNodes.length;
	while (i--) {
		var child = obj.childNodes[i];
		if (child.nodeType != 1)
			obj.removeChild(child);
	}
}
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) {
	if (el.getBoundingClientRect) {
		var box = el.getBoundingClientRect();
		var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		return {x:box.left + scrollLeft, y:box.top + scrollTop};
	}
	var r = {x: el.offsetLeft, y: el.offsetTop};
	if (el.scrollLeft)
		r.x -= el.scrollLeft;
	if (el.scrollTop)
		r.y -= el.scrollTop;
	var par = el.offsetParent;
	while (par) {
		r.x += par.offsetLeft;
		r.y += par.offsetTop;
		par = par.offsetParent;
	}
	r.x -= document.body.scrollLeft;
	r.y -= document.body.scrollTop;
	return r;
}
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;
	modal = document.createElement("div");
	modal.id = "modal";
	with (modal.style) {
		position = 'absolute';
		backgroundColor = '#333';
		backgroundImage = 'url(.png)';
		left = '0px';
		top = '0px';
		width = '100%';
		opacity = 0.1;
		MozOpacity = 0.1;
		filter = "alpha(opacity=10)";
		height = document.body.scrollHeight + 'px';
		zIndex = 9999;
	}
	document.body.appendChild(modal);
}
function destroyModal() {
	var modal = $('modal');
	if (!modal) return;
	document.body.removeChild(modal);
}
function openWindow(url, width, height, modal) {
	var popup = $('popup');
	if (!popup) {
		popup = document.createElement('iframe');
		popup.setAttribute('frameborder', '0');
		popup.style.display = 'none';
		popup.id = 'popup';
		if (url.indexOf('.htm') < 0)
			popup.src = 'hidden.htm';
		else
			popup.src = url;
		popup.style.position = 'absolute';
		document.body.appendChild(popup);
	}
	popup.width = width;
	popup.height = height;
	height -= 2;
	if (!url) {
		popup.src = popup.getAttribute('src');
	}else if (popup.contentWindow) {
		var win = popup.contentWindow;
		var body = win.document.body;
		body.style.height = height + "px";
		if (win.loadPage)
			win.loadPage(url);
		else
			popup.style.display = 'block';
	}else {
		var body = popup.document.body;
		body.style.height = height + "px";
		if (popup.loadPage)
			popup.loadPage(url);
		else
			popup.style.display = 'block';
	}
	if (modal) createModal();
	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 = 10000;
}
function sizeWindow(width, height) {
	var popup = $('popup');
	if (!popup) return;
	popup.width = width;
	popup.height = height;
	height -= 2;
	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 = 10000;
	popup.style.display = 'block';
	if (popup.contentWindow)
		popup = popup.contentWindow;
	popup.document.body.style.height = height + "px";
}
function showWindow() {
	var popup = $('popup');
	if (!popup) return;
	if (popup.style.display!='none') return;
	popup.style.opacity = 1;
	popup.style.MozOpacity = 1;
	popup.style.filter = "alpha(opacity=100)";
	popup.style.display = 'block';
	popup.focus();
}
function closeWindow() {
	var popup = $('popup');
	if (!popup) return;
	if (popup.style.display=='none') return;
	var opacity = 0;
	if (arguments.length < 2)
		opacity = parseFloat(popup.style.opacity) - 0.2;
	if (opacity > 0.1) {
		popup.style.opacity = opacity;
		popup.style.MozOpacity = opacity;
		opacity = opacity * 100;
		popup.style.filter = "alpha(opacity="+opacity+")";
		setTimeout('closeWindow()', 50);
	}else if (window.reload) {
		window.location = location.href;
		window.reload = false;
	}else {
		destroyModal();
		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 = "info.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];
		btnOk.onclick = function() {
			no_callback();
			closeDialog(true);
		}
		btnOk.disabled = '';
		btnOk.style.display = '';
		btnClose.disabled = false;
		btnClose.style.display = 'none';
		//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);
	}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);
		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 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) {
	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) return true;
	}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 showRight(show) {
	var obj = $('right'); if (!obj) return;
	obj.style.display = show?'block':'none';
}
function OutlookBar(id) {
	var outlook = $(id);
	if (!outlook || typeof outlook.active == 'object')
		return outlook;
	outlook.active = null;
	outlook.current = null;
	outlook.itemClicked = null;
	outlook.titleHeight = 0;
	outlook.height = outlook.offsetHeight;
	outlook.addPanel = function(obj) {
		var item = obj.firstChild;
		var title = item.innerText;
		obj.owner = this;
		obj.control = item.nextSibling;
		if (!this.current) {
			this.current = obj;
			this.titleHeight = item.offsetHeight;
		}else {
			obj.style.height = this.titleHeight + "px";
			obj.control.style.height = "0px";
			this.height -= this.titleHeight;
		}
		obj.setHeight = function(height) {
			this.style.height = height + 'px';
			height -= this.owner.titleHeight;
			this.control.style.height = height + 'px';
		}
		obj.onclick = function(e) {
			var dlg_title = $('dlg_title');
			if (dlg_title)
				dlg_title.innerHTML = title;
			var outlook = this.owner;
			if (outlook.active || this == outlook.current) return;
			outlook.active = this;
			outlook.delta = 50;
			if (outlook.itemActived)
				outlook.itemActived(this);
			outlook.animate();
		}
	}
	outlook.animate = function() {
		var height = parseInt(this.current.style.height);
		height -= this.delta;
		if (height < this.titleHeight) {
			height = this.titleHeight;
			this.current.setHeight(height);
			this.active.setHeight(this.height);
			this.current = this.active;
			this.active = null;
		}else {
			this.current.setHeight(height);
			height = parseInt(this.active.style.height);
			this.active.setHeight(height + this.delta);
			this.delta = Math.ceil(this.delta * 1.6);
			setTimeout(function(){outlook.animate()}, 50);
		}
	}
	outlook.addItem = function(title, control) {
		var panel = document.createElement('div');
		panel.className = 'panel';
		var div = document.createElement('div');
		div.className = 'title';
		panel.appendChild(div);
		panel.appendChild(control);
		this.appendChild(panel);
		this.addPanel(panel);
	}
	outlook.start = function() {
		if (!this.current) return;
		this.current.setHeight(this.height);
		if (this.itemActived)
			this.itemActived(this.current);
	}
	var child = outlook.firstChild;
	for (; child; child=child.nextSibling) {
		if (child.nodeType != 1) continue;
		cleanChild(child);
		outlook.addPanel(child);
	}
	outlook.start();
	return outlook;
}
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, '');
			else if (text == 'deny!') {
				callback(false, '');
//				showDialog('<b>对不起，您没有权限访问该资源！</b>', 'error');
			}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=='') {
				callback(true, '');
				if (ajax.onsuccess)
					ajax.onsuccess('');
			}else if (text == 'deny!') {
		//	showDialog('<b>对不起，您没有权限访问该资源！</b>', 'error');
			}else if (text.substr(0,3)=='ok:') {
				var str = text.substr(3);
				callback(true, str);
				if (ajax.onsuccess)
					ajax.onsuccess(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);
	}
	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 params = '';
        for (var i=0; i<form.length; i++) {
			if (!form[i].name) continue;
			var type = form[i].type.toLowerCase();
			if ((type=="radio"||type=="checkbox")
				&& !form[i].checked) continue;
			if (params != '') params += "&";
			params += form[i].name + "=" + encodeURIComponent(form[i].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) {
		var transform = this.style.textTransform.toLowerCase();
		if (transform == 'uppercase')
			value = value.toUpperCase();
		else if (transform == 'lowercase')
			value = value.toLowerCase();
		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) {
					var transform = this.style.textTransform.toLowerCase();
					if (transform == 'uppercase')
						value = value.toUpperCase();
					else if (transform == 'lowercase')
						value = value.toLowerCase();
					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_locale);
		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);
	if (down) {
		drop.target = down.style;
		down.style.display = 'none';
		down.style.position = 'absolute';
		down.style.zIndex = 9999;
		addHandle('mousedown', function(e) {
			var obj = e.target?e.target:event.srcElement;
			if (!drop.contains(obj) && !down.contains(obj))
				down.style.display = 'none';
		});
	}else drop.target = null;
	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.display == 'none')
			this.expand();
		else this.collase();
	}
	drop.expand = function() {
		if (!this.target) return;
		var r = getPosition(this);
		this.target.left = r.x+"px";
		r.y += this.offsetHeight;
		this.target.top = r.y+"px";
		this.target.display = '';
	}
	drop.collase = function() {
		if (!this.target) return;
		this.target.display = 'none';
	}
	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.onsearch = null;
	if (drop.lastChild && drop.lastChild.tagName == 'INPUT') {
		drop.lastChild.onchange = function() {
			if (!this.value || !this.parentNode.onsearch) return;
			this.parentNode.onsearch(this.value);
		}
	}
	return drop;
}
function TabCtrl(id) {
	var tab = $(id);
	if (!tab || typeof tab.activeTab == 'object')
		return tab;
	tab.activeTab = null;
	tab.items = new Array();
	var all = tab.getElementsByTagName('a');
	if (all.length < 1) return;
	tab.selectTab = function(index) {
		if (index < 0 || index >= this.items.length) return;
		this.items[index].clicked();
	}
	var i, index = 0;
	for (i=0; i<all.length; i++) {
		var tabid = all[i].getAttribute('tabid');
		if (!tabid) continue;
		all[i].related = $(tabid);
		all[i].owner = tab;
		all[i].clicked = function() {
			if (this.owner.activeTab) {
				var active = this.owner.activeTab;
				if (active == this) return;
				if (active.parentNode != this.owner)
					active.parentNode.className = '';
				active.related.style.display = 'none';
			}
			this.owner.activeTab = this;
			if (this.parentNode != this.owner)
				this.parentNode.className = 'active';
			this.related.style.display = 'block';
		}
		all[i].onclick = function() {
			this.clicked();
			return false;
		}
		tab.items[index++] = all[i];
	}tab.selectTab(0);
	for (i=1; i<index; i++) {
		tab.items[i].related.style.display = 'none';
	}return tab;
}
// 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);
		}
		/*@cc_on
		this.iframe = document.createElement("IFRAME");
		this.iframe.style.position = "absolute";
		this.iframe.style.left = "-2px";
		this.iframe.style.top = "-2px";
		this.iframe.style.zIndex = -1;
		this.iframe.style.filter = "mask()";
		table.appendChild(iframe);@*/
		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";
		if (this.iframe) {
			this.iframe.style.width = this.offsetWidth + 'px';
			this.iframe.style.height = this.offsetHeight + 'px';
		}
		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) {
					var transform = this.style.textTransform.toLowerCase();
					if (transform == 'uppercase')
						value = value.toUpperCase();
					else if (transform == 'lowercase')
						value = value.toLowerCase();
					if (value == this.value) return;
					this.value = value;
					if (input.form)
						input.form.dirty = true;
					try{this.onchange();}catch(e){}
				}
				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.x + this.offsetWidth > document.body.offsetWidth) {
			pt.x = document.body.offsetWidth - this.offsetWidth;
			this.style.left = pt.x + 'px';
		}
		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 TreeView(id, rootline) {
	var treeview = $(id);
	if (!treeview || typeof treeview.subitem == 'object')
		return treeview;
	treeview.subitem = null;
	treeview.activeNode = null;
	treeview.itemid = 0;
	if (treeview.style.position != 'absolute')
		treeview.style.position = 'relative';
	treeview.style.overflow = 'auto';
	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);
			obj.intro = array[i].intro;
			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) {
			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 = 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.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.setTitle = function(title) {
			var a = this.firstChild.lastChild;
			a.nodeValue = title;
		}
		obj.getTitle = function() {
			var a = this.firstChild.lastChild;
			return a.nodeValue;
		}
		obj.findItem = function(title) {
			if (!this.subitem) return null;
			var items = this.subitem.childNodes;
			for (var i=0; i<items.length; i++)
				if (items[i].getTitle() == title) return items[i];
			return null;
		}
		obj.findItemId = function(itemid) {
			if (!this.subitem) return null;
			var items = this.subitem.childNodes;
			for (var i=0; i<items.length; i++)
				if (items[i].itemid == itemid) return items[i];
			return null;
		}
		obj.loaded = false;
		obj.setFolder = function(folder) {
			this.folder = folder?true:false;
			if (this.folder)
				this.icon.src = 'folder.gif';
			else
				this.icon.src = 'file.gif';
		}
		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) {
			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);
		}
		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;
			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 = event;
			var obj = this.parentNode;
			if (obj.owner.itemClicked)
				obj.owner.itemClicked(e, obj);
		}
		div.onmousedown = function(e) {
			if (!e) e = 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 all = this.subitem.childNodes;
			for (var i=0; i<all.length; i++)
				if (all[i].subitem) all[i].expandAll();
		}
		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;
		var items = obj.subitem.childNodes;
		for (var i=0; i<items.length; i++) {
			if (items[i].nodeType != 1) continue;
			cleanChild(items[i]);
			this.addItem(obj, items[i]);
		}
	}
	treeview.findItem = function(title) {
		if (!this.subitem) return null;
		var items = this.subitem.childNodes;
		for (var i=0; i<items.length; i++)
			if (items[i].getTitle() == title) return items[i];
		return null;
	}
	treeview.findItemId = function(itemid) {
		if (!this.subitem) return null;
		var items = this.subitem.childNodes;
		for (var i=0; i<items.length; i++)
			if (items[i].itemid == itemid) return items[i];
		return null;
	}
	treeview.ajaxLoad = function(url) {
		var ajax = new XmlHttp;
		var self = this;
		ajax.doGet(url, function(succ, info) {
			if (!succ) return;
			treeview.addArray(eval(info));
		});
	}
	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) {
			this.clear();
			this.subitem = null;
			this.activeNode = null;
			this.ajaxLoad(href);
		}
	}
	treeview.getFolders = function() {
		var tree = TreeView(document.createElement("UL"));
		tree.className = 'treeview';
		for (var i=0; i<this.childNodes.length; i++) {
			var item = this.childNodes[i];
			if (!item.folder) continue;
			var obj = tree.addText(item.getTitle(), item.itemid);
			obj.setFolder(true);
			obj.pointer = item;
		}
		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;
			for (var i=0; i<src.subitem.childNodes.length; i++) {
				var item = src.subitem.childNodes[i];
				if (!item.folder) continue;
				var obj = owner.addText(item.getTitle(), item.itemid, dst);
				obj.setFolder(true);
				obj.pointer = item;
			}
		}
		return tree;
	}
	cleanChild(treeview);
	for (var i=0; i<treeview.childNodes.length; i++)
		treeview.addItem(treeview, treeview.childNodes[i]);
	treeview.reload();
	return treeview;
}
function Marquee(id, autostop) {
	var marquee = $(id);
	if (!marquee || typeof marquee.timer == 'object')
		return marquee;
	marquee.timer = null;
	marquee.autostop = autostop?true:false;
	marquee.style.position = 'relative';
	marquee.style.overflow = 'hidden';
	var attr = marquee.getAttribute('height');
	if (attr) marquee.style.height = height + 'px';
	attr = marquee.getAttribute('width');
	if (attr) marquee.style.width = width + 'px';
	marquee.direction = marquee.getAttribute('direction');
	if (!marquee.direction)
		marquee.direction = 'left';
	if (marquee.direction == 'left' || marquee.direction == 'right') {
		marquee.style.whiteSpace = 'nowrap';
		if (marquee.offsetWidth >= marquee.scrollWidth) return;
		if (marquee.direction == 'right')
			marquee.scrollLeft = marquee.scrollWidth;
		marquee.size = marquee.scrollWidth;
	}else {
		if (marquee.offsetHeight >= marquee.scrollHeight) return;
		if (marquee.direction == 'bottom')
			marquee.scrollTop = marquee.scrollHeight;
		marquee.size = marquee.scrollHeight;
	}
	attr = marquee.getAttribute('scrollamount');
	if (!attr)
		marquee.scrollamount = 1;
	else
		marquee.scrollamount = parseInt(attr);
	attr = marquee.getAttribute('scrolldelay');
	if (!attr)
		marquee.scrolldelay = 50;
	else
		marquee.scrolldelay = parseInt(attr);
	attr = marquee.getAttribute('bgcolor');
	if (attr) marquee.style.backgroundColor = attr;
	attr = marquee.getAttribute('hspace');
	if (attr != null) {
		marquee.style.marginLeft = attr + 'px';
		marquee.style.marginRight = attr + 'px';
	}
	attr = marquee.getAttribute('vspace');
	if (attr != null) {
		marquee.style.marginTop = attr + 'px';
		marquee.style.marginBottom = attr + 'px';
	}
	marquee.innerHTML += marquee.innerHTML;
	marquee.onmouseout = function(e) {
		if (!this.autostop) return;
		if (!e) e = window.event;
		var obj = e.toElement||e.relatedTarget;
		if (obj && !this.contains(obj)) this.start();
	}
	marquee.onmouseover = function(e) {
		if (!this.autostop) return;
		if (!e) e = window.event;
		var obj = e.fromElement||e.relatedTarget;
		if (obj && !this.contains(obj)) this.stop();
	}
	marquee.doscroll = function() {
		if (this.direction == 'left') {
			if (this.size <= this.scrollLeft)
				this.scrollLeft -= this.size;
			else this.scrollLeft ++;
		}else if (this.direction == 'right') {
			if (this.scrollLeft <= 0)
				this.scrollLeft += this.size;
			else this.scrollLeft --;
		}else if (this.direction == 'top') {
			if (this.size <= this.scrollTop)
				this.scrollTop -= this.size;
			else this.scrollTop ++;
		}else if (this.direction == 'bottom') {
			if (this.scrollTop <= 0)
				this.scrollTop += this.size;
			else this.scrollTop --;
		}
	}
	marquee.start = function() {
		if (this.timer) clearInterval(this.timer);
		this.timer = setInterval(function() {
			marquee.doscroll();
		}, this.scrolldelay);
	}
	marquee.stop = function() {
		if (!this.timer) return;
		clearInterval(this.timer);
		this.timer = null;
	}
	marquee.start();
	return marquee;
}
function PopupMenu(id) {
	var menu = document.createElement('div');
	if (id) menu.id = id;
	menu.className = 'popmenu';
	menu.style.display = 'none';
	menu.style.position = 'absolute';
	menu.style.zIndex = 10000;
	menu.width = 100;
	menu.related = null;
	document.body.appendChild(menu);
	menu.oncontextmenu = function(e) {
		return false;
	}
	menu.setEnable = function(index, enable) {
		var item = this.childNodes[index];
		if (!item || item.className == 'sep') return;
		div.disable = enable?false:true;
		div.className = enable?'item':'disable';
	}
	menu.addItem = function(text, handle, icon, disable) {
		var div = document.createElement('div');
		div.disable = disable;
		div.className = disable?'disable':'item';
		div.innerHTML = text;
		div.owner = this;
		var img = new Image();
		img.src = icon?icon:'blank.gif';
		div.insertBefore(img, div.firstChild);
		div.onclick = function() {
			if (this.disable) return;
			handle(this.owner.related);
			this.owner.onblur();
		}
		div.onmouseup = function() {
			setTimeout(function() {
				if (!this.owner) return;
				this.owner.onblur();
			}, 0);
		}
		div.onmouseenter = function() {
			if (this.className != 'item') return;
			this.className = 'item hover';
		}
		div.onmouseleave = function() {
			if (this.className.indexOf('hover') > 0)
				this.className = 'item';
		}
		this.appendChild(div);
		return div;
	}
	menu.addSeparator = function() {
		var div = document.createElement('div');
		div.className = 'sep';
		var span = document.createElement('span');
		div.appendChild(span);
		this.appendChild(div);
	}
	menu.popupAt = function(e, width) {
		if (!this.firstChild) return;
		if (!e) e = window.event;
	    var x = e.clientX;
		var y = e.clientY;
		this.style.display = 'block';
		var height = this.offsetHeight;
		if (y + height > document.body.offsetHeight)
			y -= height;
		if (!width) width = this.width;
		if (x + width > document.body.offsetWidth)
			x -= width;
		if (document.all) width += 2;
		this.style.width = width + 'px';
		this.style.left = x + 'px';
		this.style.top = y + 'px';
	}
	var handle = function(e) {
		var el = e.target?e.target:e.srcElement;
		if (el == menu) return;
		if (!menu.contains(el))
			menu.onblur();
	}
	menu.onblur = function() {
		if (this.id)
			this.style.visibility = 'hidden';
		else {
			removeHandle('mousedown', handle);
			document.body.removeChild(this);
		}
	}
	addHandle('mousedown', handle);
	return menu;
}
function ImgAuto(i,W,H){
	if(!i) return;
	var MaxW = W, MaxH = H;
	var o=new Image();
	o.src=i.src;
	var w=o.width;
	var h=o.height;
	var t;
	if (w>MaxW){
		t=MaxW;
	}else{
		t=w;
	}
	if ((h*t/w)>MaxH){i.height=MaxH;i.width=MaxH/h*w;}else{i.width=t;i.height=t/w*h;}
}
function mail_keypress(e) {
	var key = e?e.which:event.keyCode;
	if (key==8||key==9) return true;	
	if (key==46||key==64) return true;
	if (key==45||key==95) return true;
	if (key>=48&&key<=57) return true;
	if (key>=65&&key<=90) return true;
	if (key>=97&&key<=122) return true;
	return false;
}
function isEmail(str){
       var reg = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
       return reg.test(str);
}