// JavaScript Document
//UObject结合列表，散列表，游标等的数据结构
function UObject() {
	this.array = new Array() ;
}

UObject.prototype = new Object() ;

UObject.prototype.add = function(child, idxName) {
	if (! child) {return ;} 
	if(! idxName) {idxName = "id" ;} ;
	
	this.array.push (child) ;
	if($S(child[idxName])) {this[child[idxName]] = child ;}
}

//Cookie
function Cookie(document, name, hours, path, domain, secure){

    this.$document = document;
    this.$name = name;

    if (hours)  {this.$expiration = new Date((new Date()).getTime(  ) + hours*3600000); }
    else { this.$expiration = null; }

    if (path) {this.$path = path; } else {this.$path = null; }
    if (domain) {this.$domain = domain;} else {this.$domain = null; }
    if (secure) {this.$secure = true;}  else {this.$secure = false;}

}

Cookie.prototype.store = function ( ) {

    var cookieval = "";

    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) {
            continue;
        }
        if (cookieval != "") cookieval += '&';

        cookieval += prop + ':' + escape(this[prop]);

    }

    var cookie = this.$name + '=' + cookieval;

    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString(  );

    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    this.$document.cookie = cookie;
}

Cookie.prototype.load = function(  ) { 

    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page
    start += this.$name.length + 1;  // Skip name and equals sign

    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;

    var cookieval = allcookies.substring(start, end);
    var a = cookieval.split('&');    // Break it into an array of name/value pairs
    for(var i=0; i < a.length; i++)  // Break each pair into an array
        a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    return true;
}

Cookie.prototype.remove = function(  ) {

    var cookie;
    cookie = this.$name + '=';
    
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;

    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}

//====正则===============================================
function URegex() {}
URegex.exp_email = "[a-zA-Z0-9_-]+(\.([a-zA-Z0-9_-])+)*@[a-zA-Z0-9_-]+[.][a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*" ;
URegex.exp_mobile = "[0-9]{11}" ;
URegex.EMAIL = new RegExp("^" + URegex.exp_email + "$") ;
URegex.MOBILE = new RegExp("^" + URegex.exp_mobile + "$") ;
URegex.EMAIL_MOBILE = new RegExp("^(" + URegex.exp_email + ")|(" + URegex.exp_mobile + ")$") ;
URegex.URL = new RegExp("http://") ;
URegex.NUMBER = new RegExp("^[0-9]*$") ;

function deepCopy(origin, copy) {
	if (!origin || ! origin instanceof Object) {return null ;}
	if(!copy) {copy = new Object(); }
	
	if (origin instanceof Number) {return origin ;}
	if (origin instanceof String) {return origin ;}
	
	for (var i in origin) {
		if(origin[i]) {
			try{
				copy[i] = origin[i] ;
			}catch (ex) {
			}
		}
	}
	
	return copy ;
}

function evalSize(size, offset) {
	size = eval(size) ;
	if(offset) {size += eval(offset) ;}
	return size + "px" ;
}

//取得事件中的键盘值
function getKeyCode (eve) {
	if(eve) return eve.which ;
	if(event) return event.keyCode ;
	return "0" ;
}

//取得事件中的鼠标位置
function calPos (e, offset_x, offset_y) {
	if (!e) e = window.event ; //IE
	if (!offset_x) offset_x = 0 ;
	if(!offset_y) offset_y = 0 ;
	
	var target = e.target ? e.target : e.srcElement ;
	
	var loc = getAbsLocation (target) ;
	posX = loc.x + loc.width + offset_x ;
	posY = loc.y + offset_y ;
		
	return posX + "|" + posY ;
}

function enBright (elem, base) {
	elem = $(elem) ;
	if(!elem) {return ;}
	
	if(base) {elem.className = base ;}
	elem.baseStyle = elem.className ;
	elem.brt = (base ? base+"_" : "") + "bright" ;
	
	elem.onmouseover = function() {this.className = this.brt ;} ; 
	elem.onmouseout = function() {this.className = this.baseStyle ;} ; 
}

function expandFill (elem) {
	if(!elem) {return ;}
	elem.style.left = "0px" ;
	elem.style.top = "0px" ;
	elem.style.width =  this.document.body.clientWidth + "px" ;
	elem.style.height =  this.document.body.clientHeight + "px" ;
}

//页面相关
function UFront() {}

//页面初始化
UFront.initCanvas = function() {
	
	//我们假定了Page上的一些通用容器
	//canvas : 顶级页面面板,如果页面中没有存在，则是body
	//messages : 消息文本
	//labels : 标签文本
	//parts : 所有的部件
	if(! window.canvas)   {window.canvas = window.document.body ; }
	if(! window.verimage_src)   {window.verimage_src = "" ;} 
	if(! window.messages) {window.messages = new Object() ;} 
	if(! window.labels)   {window.labels = new Object() ;} 
	if(! window.sectionFunc)   {window.sectionFunc = new Object() ;} 
	window.local_messages = new Object() ;
	window.local_labels = new Object() ;
	
	//目前
	window.getMessage = function(key) { 
		var rtn = window.local_messages[key] ; 
		if(! rtn) {rtn = window.messages[key] ; } 
		return rtn ? rtn : "" ;
	}
	window.getLabel = function(key) { 
		var rtn = window.local_labels[key] ? window.local_labels[key] : window.labels[key] ; 
		return rtn ? rtn : "" ;
	}
	window.getAuth = function(key) { 
		var rtn = null ;
		if(window.local_parts) {rtn = window.local_parts._auths[key] ; }
		rtn = rtn ? rtn : window.parts._auths[key] ; 
		return rtn ? rtn : "" ;
	}
	window.getPart = function(key) {
		var rtn = null ;
		if(window.local_parts) {rtn = window.local_parts[key] ; }
		if(!rtn && window.parts) {rtn = window.parts[key] ; }
		return rtn ;
	}
	
	//默认的Loading是非模态的，显现的，如果有其他的要求，在这里订制
	//默认的文本是DEFAULT_LOADING_MESSAGE”
	//两种：silent:安静,不需要提示, pause:模态，禁止其他动作
	window.effLoad = function(mode, msg) {this._tmpLoadMode = mode ; this._tmpLoadMsg = msg} ;
	window.getLoadMode = function () { var rtn =  this._tmpLoadMode; this._tmpLoadMode = null ; return rtn ;} ;
	window.getLoadMsg = function () { var rtn = this._tmpLoadMsg ; this._tmpLoadMsg = null ; return rtn ;} ;
	window.silentLoading = function (msg) {this.effLoad("silent", msg) ;} ;
	window.promptLoading = function (msg) {this.effLoad("prompt", msg) ;} ;
	window.pauseLoading = function (msg) {this.effLoad("pause", msg) ;} ;
	
	
	//隔离层
	window.showShield = function(zindex) { //###如何隐藏浏览器滚动条
		if(! this._shieldLay) {
			var lay = $("sys_shield") ;
			if(!lay) { lay = $CN("div") ; lay.id = "sys_shield" ;}
			
			lay.style.position = "absolute" ;
			lay.style.zIndex = zindex ? zindex : "600" ;
			this._shieldLay = lay ;
			this.document.body.appendChild(lay) ;
		}
		
		expandFill(this._shieldLay) ;
		$PD(this._shieldLay) ;
	}
	
	window.hiddenShield = function() {
		if(this._shieldLay) {
			$PR(this._shieldLay) ;
		}
	}
	
	//进度条层,mode可以选择是携带隔离层，还是不携带
	window.showLoading = function(msg) { 
		if(! this._loadingLay) {
			var lay = $("sys_loading") ;
			if(!lay) { lay = $CN("div") ; lay.id = "sys_loading" ;}
			
			lay.style.position = "absolute" ;
			lay.style.zIndex = "1001" ;
			var message = $CN("div") ;
			message.className = "message" ;
			lay.message = message ;
			lay.appendChild(message) ;
			
			this._loadingLay = lay ;
			this.document.body.appendChild(lay) ;
		}
		
		if(!$S(msg)) {msg = messages["default_loading"] ; }
		this._loadingLay.message.innerHTML = msg ;
		
		$PD(this._loadingLay) ;
	}
	
	window.hiddenLoading = function() {
		if(this._loadingLay) {
			$PR(this._loadingLay) ;
		}
	}
	
	//错误提示
	window.showFault = function(msg) { 
		this.hiddenLoading() ;
		if(!$S(msg)) {msg = "" ; }
		
		if(this.parts.sys_fault) {
			var dialog = this.parts.sys_fault ;
			if(dialog.members["message"]) {dialog.members["message"].setValue(msg) ;}
			dialog.open(null, "操作没有成功", true, "center", null) ;
		}else {
			$alert(msg) ;
		}
		
	}
	
	//登录框
	window.showLogin = function(msg) { 
		this.hiddenLoading() ;
		
		if(this.parts.sys_login) {
			var dialog = this.parts.sys_login ;
			if(!dialog.onOpening) {
				//尚未初始化
				dialog.onOpening = function() {
					this.members["message"].setValue("") ;
					$PH(this.members["message"]) ;
					window.errorMsgHolder = this.members["message"] ;
				}
				dialog.onClosing = function() {
					window.errorMsgHolder = null ;
				}
			}
			dialog.open(null, msg, true, "center", null) ;
		}else {
			$alert(msg) ;
		}
		
	}
	
	
	//显示Loading标记
    //需要一个遮蔽层，并在遮蔽层上显示loading标示
    DWREngine.setPreHook ( 
	  function () {
		  var mode = window.getLoadMode() ;
		  var msg = window.getLoadMsg() ;
		  switch (mode) {
			  case "silent" : break ;
			  case "pause" : window.showShield ("1000") ;
			  case "prompt" :
			  default : window.showLoading(msg) ; break ;
		  }
	  }
	);
  
    //显示Loading标记
    //取消那个遮蔽层
    DWREngine.setPostHook(
  	  function () {
  		window.hiddenLoading() ;
		window.hiddenShield() ;
		//任何时候的任何操作，在结束后都需要更新校验码（部分操作是会刷新校验码的）
		for (var i = 0 ; i < window.parts._verimages.length ; i ++ ){
			window.parts._verimages[i].refresh();
		}
		if(window.local_parts) {
			for (var i = 0 ; i < window.local_parts._verimages.length ; i ++ ){
				window.local_parts._verimages[i].refresh();
			}
		}
	  }
    );
  
    //定义DWR异常处理方法
	DWREngine.setErrorHandler(
		function (errorCode) {
			if(!errorCode) {errorCode = "" ;} 
			var errorMsg = errorCode ;
			if(window.getMessage(errorCode)) {errorMsg = window.getMessage(errorCode) ;}
			if(window.errorMsgHolder) {
				window.errorMsgHolder.setValue(errorMsg) ;
				errorMsg ? $PV(window.errorMsgHolder) : $PH(window.errorMsgHolder) ;
			}
			else {
				switch(errorCode) {
					case "not_logon" : window.showLogin(errorMsg) ;break ;
					default :window.showFault(errorMsg) ;break ;
				}
			}
		}
	);
	
	//=========================================================================
	//用于导航栏,label是文本，interval是间隔符,根据这些可以获得导航栏的文本
	window.navi = new Object() ;
	navi.interval = "<span>&nbsp;--&gt;&nbsp;</span>" ;
	navi.buildWelcome = function () {
		var label = window.getLabel("welcome") ;
		return "<span>" + label + "</span>" ; 
	}
	navi.buildDir = function (name) {
		var label = window.getLabel(name) ;
		return "<a href='javascript:(route(\"" + name + "\"))'>" + label + "</a>" ; 
	}
	navi.buildLeaf = function (name) {
		var label = window.getLabel(name) ;
		//改变标题栏
		if(! window.bak_title) {window.bak_title = window.document.title ;}
		window.document.title = window.bak_title + " - " + label ;
		
		return "<span>" + label + "</span>" ; 
	}
	navi.getContent = function (path) {
	
		path = path ? path : "" ;
		if(path == "") {return this.buildWelcome() ; } //首页欢迎词
		
		var content = "" ;
		content += this.buildDir("") ; //将“到首页”的链接前置
		
		var remain = path ;
		var token = "" ;
		do {
			content += this.interval ;
			
			var offset = remain.indexOf("$") ;
			if (offset >= 0) {
				token += remain.substring(0, offset) ;
				remain = remain.substring(offset + 1) ;
				content += this.buildDir(token) ;
			}else {
				token += remain ;
				remain = "" ;
				content += this.buildLeaf(token) ;
			}
			
			token += "$" ;
		}while (remain != "")
		
		return content ;
	}
	//===================================================================
	
	//长期的Cookie(一年)
	window.longtermCookie = new Cookie(document, "longterm", 8760) ;
	
	//初始化各个部件
	window.parts = UPart.buildParts(document) ;
	
	//消除所有链接的虚线框，
	for(var ii=0; ii<document.links.length; ii++) {
 		document.links[ii].onfocus=function(){this.blur() ;}
	}
	
	//代理页面上点击事件
	document.body.onmousedown = function () {
		
		if(window.ctxWindow && ! window.ctxWindow.using ) {
			window.ctxWindow.close();
		}
		
	};
	
	//页面在登录前后的变化
	window._afterLogin = new Array() ;
	window._afterLogout = new Array() ;
	window.addLoginAdvice = function (advice) {if(advice) {this._afterLogin.push(advice) ;}} ;
	window.addLogoutAdvice = function (advice) {if(advice) {this._afterLogout.push(advice) ;}} ;
	window.adaptToUser = function(user) {
		window.user = user ;
		//隐藏所有需要验证的部件
		for (var i in window.parts._auths) { $PH(window.parts._auths[i]) ; }
		if (window.local_parts) {for (var i in window.local_parts._auths) { $PH(window.local_parts._auths[i]) ; }}
		if(user) {
			var sections = user.sectionAuthors ;
			for (var i = 0 ; i < sections.length ; i ++) {
				$PV(window.getAuth(sections[i])) ;
			}
			for (var i = 0 ; i < this._afterLogin.length ; i ++ ) {
				var advice = this._afterLogin[i] ;
				if(advice) { try { advice.call(this, user) ;} catch(ex) {} }
			}
		}else {
			for (var i = 0 ; i < this._afterLogout.length ; i ++ ) {
				var advice = this._afterLogout[i] ;
				if(advice) { try { advice.call(this) ;}catch(ex) {} }
			}
		}
	}
}

UFront.fillRegion = function (path, region, content, initable) {
	
	DWRUtil.setValue(region, content) ;
	if(initable) {UFront.initContent(region) ;}
	
	if(window.sectionFunc[path]) {window.sectionFunc[path]() ;}
}

UFront.initContent = function(content) {
	window.local_parts = null ;
	
	content = $(content) ;
	if(!content) {return ;} 
	
	window.local_parts = UPart.buildParts(content) ;
	
	//if(window.local_parts._init) {window.local_parts._init.onclick();}
	
}

UFront.initPage = function() {
	var win = top ? top : window ;
	
	var errorSpan = $("errorCodeHolder") ;
	if(errorSpan && errorSpan.value) {
		if(win && win.showFault) {win.showFault(errorSpan.value) ;} 
	}
}


//可托拽控件
UFront.draggate = function (lay) {
	lay = $(lay) ;
	if (! lay) {return ;}
	
	//lay.style.cursor = "pointer" ;
	
	//托拽的目标，如果没有指定，则拖拽自身
	var dragee = lay.dragee ? lay.dragee : lay ;
	
	lay.onmousedown = function(event) {beginDrag(dragee, event) ; } ;
}



//窗口======================================================================================



//页面部件======================================================================================
function UPart() {}

//将一个区域内的部件全部执行初始化
UPart.buildParts = function (region, parts) {
	region = $(region) ;
	if (! region) {return ;}
	
	if(!parts) { parts = new UObject() ; }
	parts._verimages = new Array() ;
	parts._auths = new Object() ;
	
	var partArray = region.getElementsByTagName("*") ;
	
	//根据区域内的标签，构建这些部件
	for (var i = 0 ; i < partArray.length ; i ++ ) {
		var part = partArray[i] ;
		//大小和位置
		UPart.setDimension(part) ;
		//有关风格的设定，这是为了简化美工渲染的工作量，没有影响任何行为
		UPart.setStyle(part) ;
		
		var ukind = part.getAttribute("ukind") ;
		var ugroup = part.getAttribute("ugroup") ;
		
		if(!ukind) {ukind = "" ;}
		
		//var tag = part.tagName.toLowerCase() ;
		//if(part.getAttribute("type")) {tag += "#" + part.getAttribute("type").toLowerCase() ;}
		
		//如果不申明控件，不予处理
		if(!ukind && !ugroup) {continue ;}
		
		//if(part.abc) {alert(part.tagName) ;}
		
		part.ukind = ukind ;
		part.ugroup = ugroup ;
		//id,name,label,manner是一个部件的基本属性，并且，部件存在setValue/getVaule的默认实现
		part.id = part.getAttribute("id") ;
		part.name = part.getAttribute("name") ;
		part.label = part.innerHTML ;
		
		part.members = new UObject() ;
		
		//存在一个动作调用栈，可以实时增加行为
		part.advices = new Array() ;
		part.addAdvice = function(advice) {this.advices.push(advice) ; }
		part.action = function() {
			for(var i = 0 ; i < this.advices.length ; i ++) {
				var advice = this.advices[i] ;
				advice.call(this) ;
			}
		}
		
		UPart.setManner(part) ;
		
		part.setLabel = function(label) {part.innerHTML = label ? label : "" ;} ;
		part.getLabel = function() {return part.innerHTML ; } ;
		
		part._innerSign = null ;
		part.setValue = function(value) {part._innerSign = value ;} ;
		part.getValue = function() {return part._innerSign ;} ;
		
		parts.add(part) ;
	}
	
	//组合关系
	for (var i = 0 ; i < parts.array.length ; i ++ ) {
		var part = parts.array[i] ;
		UPart.assemble(part, parts) ;
	}
	
	//alert(parts["m1"].group) ;
	//alert("2" + "--" + parts["m1"].id + ":" + parts["m1"].group.id) ;
	
	//特殊化
	for (var i = 0 ; i < parts.array.length ; i ++ ) {
		var part = parts.array[i] ;
	
		//增加特性
		var tuple = part.ukind.split("|") ; 
		if(tuple.length > 0) {
			for (var j = 0 ; j < tuple.length ; j ++ ) {
				var kind = tuple[j] ;
				switch (kind) {
					case "widget" : UPart.UWidget(part) ;break ;
					
					case "tabber" : UPart.UTabber(part) ;break ; //标签页
					case "grid" : UPart.UGrid(part) ;break ; //表格
					case "form" : UPart.UForm(part) ;break ; //表单
					case "track" : UPart.UTrack(part) ; break ; //滑动
					
					case "sign" : break ; //记录信息
					case "label" : UPart.ULabel(part) ;break ; //显示信息
					case "check" : UPart.UCheck(part) ;break ; //显示信息
					case "command" : UPart.UCommand(part) ;break ; //执行操作，也可显示信息
					case "text" : UPart.UText(part) ;break ;
					
					case "folder" : UPart.UFolder(part) ;break ;     //折叠（显示/隐藏）的能力
					
					case "verimage" : UPart.UVerimage(part); parts._verimages.push(part) ; break ;   //表单的图片校验
					case "auth" : UPart.UAuth(part); if($S(part.name)){parts._auths[part.name] = part ;} break ; //依据用户订制
					case "submit" : UPart.USubmit(part); break ; //表单的提交行为持有者
					case "zoom" : UPart.UZoom(part); break ; //关联选择
					case "router" : UPart.URouter(part); break ; //画面路由
					
					case "datepicker" : UPart.UDatePicker(part); break ; 
					case "panel_calendar" : UPart.UCalendar(part);break ;
					
					
					case "keeper" :  UPart.UKeeper(part); break ; //可以被Cookie保存的能力
					
					case "init" : $PR(part); parts._init = part; break ; //初始化区域的能力,内容部分需要使用
					
					case "region" :
					default : break ;
				}
			}
		}
	}
	
	//统一进行初始化
	for (var i = 0 ; i < parts.array.length ; i ++ ) {
		var part = parts.array[i] ;
		if(part.init) {part.init() ;}
	}
	
	return parts ;
}

//组合Part间的关系
UPart.assemble = function (part,parts) {
	
	if(!part) {return ;}
	
	
	
	//部件存在属组，属下.
	var memberList = part.getAttribute("umember") ;
	var group = part.ugroup ;
	
	if(memberList) {
		var tuple = memberList.split("|") ;
		for(var i = 0 ; i < tuple.length ; i ++) {
			var member = parts[tuple[i]] ;
			if (member && member.id) {
				//树节点的双向关联
				member.group = part ;
				part.members.add(member, "name") ;
			} 
		}
	}
	
	if(group) {
		if (parts[group]) {
			parts[group].members.add(part, "name") ;
			part.group = parts[group] ;
		} 
	}
	
}

UPart.setDimension = function (view) {
	var dim = view.getAttribute("dim") ;
	if(!view || !dim) {return ;}
	
	var tuple = dim.split("|") ;
	if(tuple[0]) {view.style.width = evalSize(tuple[0]) ;} 
	if(tuple[1]) {view.style.height = evalSize(tuple[1]) ; } //view.style.lineHeight = 
}

UPart.setPosition = function (view) {
	var position = view.getAttribute("position") ;
	if(!view || !position) {return ;}
	
	if(position == "center") {
		if(!view.style.width) {view.style.width = "400px" ; } 
		if(!view.style.height) {view.style.height = "300px" ; } 
		view.style.left = "50%" ;
		view.style.top = "50%" ;
		view.style.marginLeft = (0 - Math.floor(parseInt(view.style.width)/2)) + "px" ;
		view.style.marginTop = (0 - Math.floor(parseInt(view.style.height)/2)) + "px" ;
		
		return ;
	}
	
	var tuple = position.split("|") ;
	if(tuple[0]) {view.style.left = evalSize(tuple[0]) ;} 
	if(tuple[1]) {view.style.top = evalSize(tuple[1]) ;} 
}

UPart.setStyle = function(view) {
	var style = view.className ;
	
	if(!style) {return ;}
	if(style.length > 2 && style.substring(0,2) == "r_") {
		var base = style.substring(2) ;
		enBright(view, base) ;
	}
}

UPart.setManner = function (view) {
	var manner = view.getAttribute("manner") ;
	if(!view || !manner) {return ;}
	
	var tuple = manner.split("|") ;
	//对于名称来说，r_ s_等都具有特殊意义
	view.setClassName = function (style) {
		if(!style) {return ;}
		if(style.length > 2 && style.substring(0,2) == "r_") {
			this.baseStyle = style.substring(2) ;
			this.className = this.baseStyle ;
			this.onmouseover = function() {this.className = this.baseStyle + "_bright" ;} ;
			this.onmouseout = function() {this.className = this.baseStyle ;}
		}else if(style.length > 2 && style.substring(0,2) == "s_") {
			this.className = style.substring(2) ;
			this.onmouseover = function() {} ;
			this.onmouseout = function() {} ;
		}else {
			this.className = style ;
			this.onmouseover = function() {} ;
			this.onmouseout = function() {} ;
		}
	}
	
	if(tuple[0]) {
		view.setClassName(tuple[0]) ;
	} 
	
	//存在滚动的情况
	if (tuple.length > 1) {
		view._manners = tuple ;
		view._mannerIdx = 0 ;
		view.addAdvice(function () {
			this._mannerIdx =  (this._mannerIdx + 1) % this._manners.length ;
			this.setClassName(this._manners[this._mannerIdx]) ;
		}) ;
	}
}

//根据一组values，来设置部件的值
UPart.setValues = function (parts, values) {
	if(! values || ! parts) {return ;}
	for(var i in values) {
		if(parts[i] && parts[i].setValue) {parts[i].setValue(values[i]) ;}
	}
		
}

//窗口
UPart.UWidget = function(content) {
	content = $(content) ;
	if (! content) {return ;}
	
	try{
		document.body.removeChild(content) ;
	}catch(ex) {}
	
	//窗口初始为不可见
	var widget = $CN("div") ;
	widget.style.overflow = "hidden" ;
	document.body.appendChild(widget) ;
	widget.className = "widget" ;
	widget.style.position = "absolute" ;
	widget.style.display = "none" ;
	
	widget.content = content ;
	content.widget = widget ;
	
	//标题栏===================================================
	var caption = $CN("div") ;
	caption.className = "caption" ;
	//文本
	var title = document.createElement("span") ;
	title.className = "txt_title" ;
	content._title = title ;
	content.setCaption = function (cap) {
		if(cap) {this._title.innerHTML = cap ;}
	}
		
	//处理标题栏
	var icon = document.createElement("button") ;
	icon.className = "btn_icon" ;
	icon.disabled = "disabled" ;
	
	var btnClose = document.createElement("button") ;
	btnClose.title = "关闭" ;
	btnClose.className = "btn_close" ;
	btnClose.content = content ;
	btnClose.onclick = function () {this.content.close();} ;
	
	//组合
	caption.appendChild (icon) ;
	caption.appendChild (title) ;
	caption.appendChild (btnClose) ;
		  
	widget.appendChild (caption) ;
	content.caption = caption ;
	caption.dragee = widget ;
	//标题栏[end]===================================================
	
	//主体栏===================================================
	var body = $CN("div") ;
	body.style.overflow = "auto" ;
	body.appendChild (content) ;
	widget.appendChild (body) ;
	content.body = body ;
	content.dragee = widget ;
	//主体栏[end]===================================================
	
	//屏蔽层========================================================
	var shield = $CN("div") ;
	content.shield = shield ;
	shield.style.overflow = "hidden" ;
	shield.style.position = "absolute" ;
	shield.className = "shield" ;
	$PR(shield) ;
	document.body.appendChild(shield) ;
	
	//屏蔽层[end]========================================================
	
	widget.onmouseover = function () {this.content.using = "using" ; } ;
	widget.onmouseout = function () {this.content.using = null ; } ;
	
	widget.onMovingX = function (deltaX) {
		if(!this.style.left || this.style.left.search("px") < 0) {this.style.left = this.offsetLeft - parseInt(this.style.marginLeft) + "px" ;}
		var left = parseInt(this.style.left) + deltaX ;
		if(left > 0 ) {this.style.left = left + "px" ; return true ; }
		return false ;
	}
	
	widget.onMovingY = function (deltaY) {
		if(!this.style.top || this.style.top.search("px") < 0) {this.style.top = this.offsetTop - parseInt(this.style.marginTop) + "px" ;}
		var top = parseInt(this.style.top) + deltaY ;
		if(top > 0 ) {this.style.top = top + "px" ; return true ; }
		return false ;
	}
	
	
	content.open = function(echo, caption, model, position, dim) {
		
		if(!dim) {dim = content.getAttribute("wdim") ;}
		
		//有关选中状态及回复
		if(echo && echo.getValue) {
			for(var i = 0 ; i < this.members.array.length ; i ++ ) {
				var member = this.members.array[i] ;
				if(member && member.preset) {member.preset(echo.getValue()) ;}	
			}
		}
		if(echo && echo.setValue) {this.reply = function(value) {echo.setValue(value); this.close(); } ;}
		else { this.reply = function(value){this.close();} ; }		
		
		//标题栏决定是否显示
		if(caption) {
			this.setCaption (caption) ;
			UFront.draggate(this.caption) ;
			$PD(this.caption) ;
		}else {
			this.setCaption ("") ;
			//UFront.draggate(this) ;
			$PR(this.caption) ;
		}
		
		//是否model方式弹出
		if(model) {
			expandFill(this.shield) ;
			$PD(this.shield) ;
			this.widget.style.zIndex = "700" ;
			this.onModel = true ;
		}else {
			this.onModel = null ;
			window.ctxWindow = this ;
		}
		
		//窗口的风格
		this.widget.setAttribute("position", position) ;
		this.widget.setAttribute("dim", dim) ;
		UPart.setDimension(this.widget) ;
		UPart.setPosition(this.widget) ;
		
		$PD(this.widget) ;
		if(this.onOpening) {this.onOpening() ;}
		
		return this.widget ;
	}
	
	content.close = function() {
		if(this.onClosing) {this.onClosing() ;}
		$PR(this.widget) ;
		$PR(this.shield) ;
		if(this.onModel) {
		}else {
			window.ctxWindow = null ;
		}
	}
	
}

//关联选择项
//对于zoom来说，它需要感知"target"的存在，即传值得目标
UPart.UZoom = function(zoom, parts) {
	zoom = $(zoom) ;
	if (! zoom) {return ;}
	
	//由于处理上的差别，div在做为输入框使用时，需要重置尺寸
	var dim = zoom.getAttribute("dim") ;
	var tuple = [200, 14] ;
	if(dim) {
		var tuple = dim.split("|") ;
		if(tuple[0]) {zoom.style.width = evalSize(tuple[0], ($IE()?2:0)) ;} 
		if(tuple[1]) {zoom.style.height = evalSize(tuple[1], 2) ; }
	}
	
	//图片按钮
	var icon = zoom.getAttribute("icon") ;
	var triple = ["", 14, 14] ;
	if(icon) {triple = icon.split("|") ;}
	
	$AN(zoom, "input", "editor") ;
	$AN(zoom, "img", "toggle") ;
	
	zoom.editor.className = "inline" ;
	zoom.editor.group = zoom ;
	zoom.editor.action = function () {this.group.action() ;} ;
	zoom.editor.style.width = evalSize(tuple[0]-triple[1]-2) ;
	zoom.editor.style.height = evalSize(tuple[1]-2) ;
	UPart.UText(zoom.editor) ;
	zoom.setValue = function(value) {this.editor.setValue(value) ;} ;
	zoom.getValue = function() {return this.editor.getValue() ;} ;
	
	zoom.toggle.src = triple[0] ;
	zoom.toggle.style.width = evalSize(triple[1]) ;
	zoom.toggle.style.height = evalSize(triple[2]) ;
	
	//弹出窗口
	var popup = zoom.getAttribute("popup") ;
	if(popup) {
		var tuple = popup.split("|") ;
		if(tuple.length==5 && tuple[0]) {
			zoom.toggle.onclick = function(event) {
				var host = window.getPart(tuple[0]) ;
				if(host) {host.open(this.parentNode, tuple[1], tuple[2], calPos(event, 6, 0), tuple[3]+"|"+tuple[4]) ;}
			}
		}
	}
	
}

UPart.URouter = function(router) {
	router = $(router) ;
	if (! router || !$S(router.name)) {return ;}
	
	router.style.cursor = "pointer" ;
	
	if(!router.getLabel()) {
		router.setLabel(window.getLabel(router.name)) ;
	}
	
	router.onclick = function () {
		this.action() ;
		if(window.route) {
			window.route(router.name) ;
		}
	}
}

UPart.UDatePicker = function(part) {
	part = $(part) ;
	if (! part || !part.editor ) {return ;}
	
	part.editor.toDisplay = function(date) {
		if(!date || !date instanceof Date) {return "";}
		else {return date.toLocaleDateString() ;}
	}
	part.editor.toEdition = function(date) {
		if(!date || !date instanceof Date) {return "";}
		else {return date.getYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() ;}
	}
	part.editor.toSignal = function(text) {
		if(!text) {return null ;}
		var tuple = text.split("-") ;
		if(tuple.length != 3) {return null ;}
		var year = eval(tuple[0]) ;
		var month = eval(tuple[1]) ;
		var date = eval(tuple[2]) ;
		return new Date(year,month-1,date) ;
	}
}

UPart.UCalendar = function(cal) {
	cal = $(cal) ;
	if (! cal ) {return ;}
	
	cal.cellus = new Array(42) ;
	var tbody = cal.getElementsByTagName("tbody")[0] ;
	var idx = 0 ;
	for (var j = 0 ; j < 6 ; j ++) {
		var bodyRow = $CN("tr") ;
		for (var k = 0 ; k < 7 ; k ++) {
			var td = $CN("td") ;
			bodyRow.appendChild(td) ;
			cal.cellus[idx] = td ;
			td.cal = cal ;
			td.defClass = "week" ;
			if (k == 0) { td.defClass = "sat" ;}
			if (k == 6) { td.defClass = "sun" ;}
			//td.innerHTML = idx ;
			idx ++ ;
		}
		tbody.appendChild(bodyRow) ;
	}
	
	//preset是窗体内部空间的通用调度方法，用于在弹出时根据环境决定其状态
	cal.preset = function (date) {
		if(! date) {date = new Date();}
		if (! date instanceof Date) {return ;}
		
		this.current = date ;
		
		this.year=date.getFullYear();  //定义year属性，年份，默认值为当前系统年份。
		this.month=date.getMonth()+1;  //定义month属性，月份，默认值为当前系统月份。
		this.date=date.getDate();  //定义date属性，日，默认值为当前系统的日。
		this.day=date.getDay();
		
		this.rearrange() ;
	}
	
	cal.setYear = function(year) {
		if(!year) {return ;}
		year = eval(year) ;
		if(year > 0 && year < 3000) {
			this.year = year ;
			this.rearrange() ;
		}
	}
	
	cal.setMonth = function(month) {
		if(!month && month != 0) {return ;}
		month = eval(month) ;
		if(month >= 1 && month <= 12) {this.month = month ; }
		if(month == 0) {this.month = 12 ; this.year -- ;}
		if(month == 13) {this.month = 1 ; this.year ++ ;}
		
		this.rearrange() ;
	}
	
	cal.members["b_prevYear"].onclick = function() {this.group.setYear(--this.group.year) ;} ;
	cal.members["b_nextYear"].onclick = function() {this.group.setYear(++this.group.year) ;} ;
	cal.members["b_prevMonth"].onclick = function() {this.group.setMonth(--this.group.month) ;} ;
	cal.members["b_nextMonth"].onclick = function() {this.group.setMonth(++this.group.month) ;} ;	
	cal.members["t_year"].action = function() {this.group.setYear(eval(this.value)) ;} ;	
	cal.members["t_month"].action = function() {this.group.setMonth(eval(this.value)) ;} ;	
	
	cal.rearrange = function () {
		var firstDay = new Date(this.year,this.month-1,1);
		var today = new Date() ;
		var weekOffset = firstDay.getDay() ;
		var dayCount = 0;
		
		//改变指示器
		this.members["t_year"].value = this.year ;
		this.members["t_month"].value = this.month ;
		
		switch(this.month)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
			  dayCount=31;
			  break;
			case 4:
			case 6:
			case 9:
			case 11:
			  dayCount=30;
			  break;
			case 2:
			  if((this.year%4==0)&&(this.year%100!=0)||(this.year%400==0))
				dayCount=29;
			else
				dayCount=28; 
		}
		//清除当前日期表格状态
		for (var i = 0 ; i < this.cellus.length ; i ++) {
			var td = this.cellus[i] ;
			td.className = "blank" ;
			td.innerHTML = "&nbsp;" ;
			td.onmouseover = null ;
			td.onmouseout = null ;
			td.onclick = null ;
		}
		//填写表格
		for (var j = 1 ; j < dayCount + 1 ; j ++) {
			var td = this.cellus[j + weekOffset - 1] ;
			td.innerHTML = j ;
			if (today.getYear() == this.year && today.getMonth() == this.month-1 && j == today.getDate()) {
				td.className = "today" ;
			}else {
				td.className = td.defClass ;
			}
			
			if (this.current && 
				this.current.getYear() == this.year && 
				this.current.getMonth() == this.month-1 && 
				j == this.current.getDate()) {//被选中
				td.className = "marker" ;
			}
			
			enBright(td) ;
			
			td.onclick = function () {
				var cal = this.cal ;
				cal.date = eval(this.innerHTML) ;
				if (cal.group && cal.group.reply) {
					var date = new Date(cal.year,cal.month-1,cal.date) ;
					cal.group.reply(date) ;
				}
			} ;
		}
	}
}

UPart.UAuth = function(auth) {
	auth = $(auth) ;
	if (! auth || !$S(auth.name)) {return ;}
	
	$PH(auth) ;
}

UPart.UKeeper = function(keeper) {
	keeper = $(keeper) ;
	if (! keeper) {return ;}
	
	var name = keeper.name ;
	keeper.oldGetValue = keeper.getValue ;
	
	if(name && keeper.oldGetValue) {
		keeper.getValue = function() {
			var value = this.oldGetValue();
			if(value) {
				window.longtermCookie[this.name] = value ;
				window.longtermCookie.store() ;
			}
			return value;
		} ;
	}
	
	if(name && window.longtermCookie.load() && window.longtermCookie[name]) {
		if(!keeper.getValue() && keeper.setValue) {keeper.setValue(window.longtermCookie[name]) ; }
	}
}


//文本，这是最基本的用于显示字符的部件，只是显示,没有操作行为==================
UPart.ULabel = function(label) {
	label = $(label) ;
	if (! label) {return ;}
	
	label.setValue = function (value) {
		if(! value) {value = "" ;}
		this.innerHTML = value ;
	} ;
	
	label.getValue = function () { return this.innerHTML ;} ;
	
	var value = label.getAttribute("value") ;
	label.setValue(value) ;
	
}

UPart.UCheck = function(check) {
	check = $(check) ;
	if (! check) {return ;}
	
	check.sign = null ;
	
	check.values = ["", ""] ;
	var sign = check.getAttribute("signal") ;
	if(sign) {
		var tuple = sign.split("|") ;
		if(tuple[0]) {check.values[0] = tuple[0] ;} 
		if(tuple[1]) {check.values[1] = tuple[1] ;} 
	}
	
	check.setValue = function (value) {
		if(! value) {value = "" ;}
		if(value == this.values[0]) {this.setAttribute("checked", "checked") ;}
		else {this.setAttribute("checked", "") ;}
	} ;
	
	check.getValue = function () { 
		if(this.checked) {return this.values[0] ;}
		else {return this.values[1] ;}
	} ;
	
	//check.setValue("accept") ;
	
}

//按钮，这是基本的操作行为的触发者,可以以button或链接方式存在===========================================
UPart.UCommand = function(command) {
	command = $(command) ;
	if (! command) {return ;}
	
	command.style.cursor = "pointer" ;
	
	command.setLabel = function (label) {
		if(! label) {label = "" ;}
		this.innerHTML = label ;
	} ;
	
	//command是有value这个概念的，一般来说，可以存放一些类似ID的值，
	command.setValue = function (value) {this.innerValue = value ;} ;
	command.getValue = function () {return this.innerValue ;} ;
	
	//command.addAdvice(function(){alert(45);}) ;
	//如果没有指定onclick事件，则执行doself
	var onclick = command.getAttribute("onclick") ;
	if(!onclick) {command.onclick = function() {this.action();} ;}
	
	//var label = command.getAttribute("label") ;
	//if(label) {command.setLabel(label) ;}
	
}

UPart.UFolder = function(folder) {
	folder = $(folder) ;
	if (! folder) {return ;}
	
	folder.sign = null ;
	
	folder.values = ["", ""] ;
	var sign = folder.getAttribute("signal") ;
	if(sign) {
		var tuple = sign.split("|") ;
		if(tuple[0]) {folder.values[0] = tuple[0] ;} 
		if(tuple[1]) {folder.values[1] = tuple[1] ;} 
	}
	
	folder.labels = ["", ""] ;
	var label = folder.getAttribute("text") ;
	if(label) {
		var tuple = label.split("|") ;
		if(tuple[0]) {folder.labels[0] = tuple[0] ;} 
		if(tuple[1]) {folder.labels[1] = tuple[1] ;} 
	}
	
	//记录折叠状态的变量
	folder._fold = 0 ;
	folder.addAdvice(function() {
		this._fold = (this._fold + 1) % 2 ;
		if(this.labels[this._fold]) {this.setLabel(this.labels[this._fold]) ;}
		this.sign = this.values[this._fold] ;
		for(var i = 0 ; i < this.members.array.length ; i ++ ) {
			var member = this.members.array[i] ;
			if(!member) {return ;}
			
			if(this._fold == 1) { $PD(member) ; } 
			if(this._fold == 0) { $PR(member) ; } 
		}
	});
	
	folder.getValue = function () {return this.sign} ;
	
	//默认都是不选中状态
	if(folder.labels[0]) {folder.setLabel(folder.labels[0]) ; }
	folder.sign = folder.values[0] ;
	for(var i = 0 ; i < folder.members.array.length ; i ++ ) {
		var member = folder.members.array[i] ;
		$PR(member) ;
	}
							
}

//文本输入框,可以是单行,也可能是多行,或是口令======================================================================================
UPart.UText = function(text) {
	text = $(text) ;
	if(!text) {return ;}
	
	var tag = text.tagName.toLowerCase() ;
	var type = text.getAttribute("type") ;
	type = type ? type.toLowerCase() : "" ;
	
	switch (tag) {
		case "input" :
			//对于Text都有内在的值
			text.signal = null ;
			//对可以填写的字符的判定
			text.regex = text.getAttribute("allow") ;
			
			text.setValue = function(value) {
				this.signal = value ;
				var display = value ;
				if(this.toDisplay) {display = this.toDisplay(value) ;}
				this.value = display ? display : "" ;
			} ;
			text.getValue = function() {
				//this.signal = this.value ;
				var rtn = this.toSignal ? this.toSignal(this.value) : this.value ;
				return rtn ;
			} ;
			
			if(type=="password") {
				text.clear = function() {this.setValue(null) ;} ;
			}
			
			
			text.onfocus = function() {
				this.oldValue = this.value ;
				if(this.toEdition) {this.value = this.toEdition(this.signal) ;}
				this.select();
			} ;
			
			
			text.originOnblur = text.getAttribute("onblur");
			text.onblur = function () {
				if(this.originOnblur) {this.originOnblur();} 
				this.signal = this.value ;
				if(this.toSignal) {this.signal = this.toSignal(this.value) ; }
				if(this.toDisplay) {this.value = this.toDisplay(this.signal);}
			}
			
			text.onkeydown = function(eve) {
				var key = getKeyCode(eve) ;
				if(key == "13") {this.blur() ; this.action();}
			} ;
			
			text.onkeyup = function(eve) {this.check() ; } ;
			
			//flag为了表明，在非法值时，是否回退内容
			text.check = function() {
				if(!this.regex) {return true ;}
				if(!this.value) {return false ;}
				var reg = new RegExp(this.regex) ;
				if(!reg.test(this.value)) {
					this.value = this.oldValue ;
					return false;
				}
				this.oldValue = this.value ;
				return true ;
			} ;
			
			break ;
		case "textarea" :
			text.setValue = function (value) {this.innerHTML = value ? value : "" ;} ;
			text.getValue = function () {return this.innerHTML ;} ;
			break ; 
		default :break ;
	}
	
	var value = text.getAttribute("text") ;
	if(value) {text.setValue(value) ;}
	
	text.oldValue = text.value ;
}

UPart.UTabber = function (tabber) {
	tabber = $(tabber) ;
	if(!tabber) {return ;}
	$PR(tabber) ;
	
	for(var i = 0 ; i < tabber.members.array.length ; i ++ ) {
		var tablet = tabber.members.array[i] ;
		if(!tablet) {return ;}
		tablet.addAdvice(function() {
			if(this.group.prevMarker) {
				//避免循环引用，必须保证prevMarker只会被使用一次
				var tmp = this.group.prevMarker ;
				this.group.prevMarker = null ;
				tmp.action() ;
			}
			this.group.prevMarker = this;
			this.group.innerValue = this.name ;
		} ) ;
	}
	
	tabber.setValue = function (value) {
		if(!$S(value)) {return ;}
		var member = this.members[value] ;
		if(member) {this.innerValue = value; member.action() ;}
		else {
			if(this.innerValue && this.members[this.innerValue]) {this.members[this.innerValue].action() ; } 
		}
	}
	
	tabber.getValue = function () {
		return this.innerValue ;
	}
	
	tabber.init = function () {
		tabber.setValue(tabber.getAttribute("default")) ;
	}
	
}

UPart.UForm = function (form) {
	form = $(form) ;
	if(!form) {return ;}
	if("SPAN" == form.tagName) {$PR(form) ;}
	
	/*
	for(var i = 0 ; i < form.members.array.length ; i ++ ) {
		var field = form.members.array[i] ;
		if(!field) {return ;}
		field.addAdvice(function () {this.group.submit() ;}) ;
	}*/
	
	form_value = new Object() ;	
	
	form.setValue = function (value) {
		if(! value) {return ;}
		this._value = value ;
		for(var i in form.members) {
			if("array" == i || "add" == i || "" == i) {continue ;}
			if(value[i]) {form.members[i].setValue(value[i])  ;}
		}	
	}
	
	form.getValue = function () {
		var value = new Object() ;
		if(this.id) { value["sys_formname"] = this.id ; } ;
		for(var i in form.members) {
			if("array" == i || "add" == i || "" == i) {continue ;}
			value[i] = form.members[i].getValue() ;
		}
		this._value = value ;
		if(form.members["sys_vericode"]) {form.members["sys_vericode"].setValue(null);}
		return value ;
	}
	
	//校验相关
	form.preValidate = function (value) {} ;
	form.failValidate = function (name,msg) {
		if (msg) {alert(msg);}
		if (name && this.members[name] && this.members[name].onfocus) {
			this.members[name].onfocus() ;
		}
	} ;
	
	form.performSubmit = function (value){$alert(value) ;} ;
	form.submit = function () {
		var value = this.getValue() ;
		var rtn = this.validate(value) ;
		if(rtn == 0) { //通过校验
			for(var i in this.members) {
				if("array" == i || "add" == i || "" == i) {continue ;}
				if(this.members[i].clear){this.members[i].clear() ;}
			}
			this.performSubmit(this._value) ;
		}
	}
	
	form.addVerifDup = function (fname, frefer, message) {
		if(!(fname && frefer && message)){return ;}; 
		this.verifDup.push({name:fname,refer:frefer,msg:message}) ;} ;
		
	form.addVerifRequire = function (fname, message) {
		if(!(fname && message)){return ;};  
		this.verifRequire.push({name:fname,msg:message}) ;} ;
		
	form.addVerifFormat = function (fname, fregex, message) {
		if(!(fname && message && fregex)){return ;};  
		this.verifFormat.push({name:fname, regex:fregex, msg:message}) ;} ;
		
	form.addVerifCommon = function (message) {
		if(!message){return ;};  
		this.verifCommon.push({msg:message}) ;} ;
	
	form.validate = function (value, after) {
		
		this.verifDup = new Array() ;
		this.verifRequire = new Array() ;
		this.verifFormat = new Array() ;
		this.verifCommon = new Array() ;
		this.preValidate(value) ;
		
		//通用行为
		for (var i = 0 ; i < this.verifCommon.length ; i ++  ) {
			var msg = this.verifCommon[i].msg ;
			this.failValidate(null, msg) ;
			return 1 ;
		}
		
		//必要字段
		for (var i = 0 ; i < this.verifRequire.length ; i ++  ) {
			var fname = this.verifRequire[i].name ;
			var msg = this.verifRequire[i].msg ;
			if (! this._value[fname]) {
				this.failValidate(fname,msg) ;
				return 1;
			}
		}
		//重复字段必须一致
		for (var i = 0 ; i < this.verifDup.length ; i ++  ) {
			var fname = this.verifDup[i].name ;
			var refer = this.verifDup[i].refer ;
			var msg = this.verifDup[i].msg ;
			if (this._value[fname] != this._value[refer]) {
				this.failValidate(fname,msg) ;
				return 1;
			}
		}
		//数据格式符合
		for (var i = 0 ; i < this.verifFormat.length ; i ++  ) {
			var fname = this.verifFormat[i].name ;
			var regex = this.verifFormat[i].regex ;
			var msg = this.verifFormat[i].msg ;
			
			if (! this._value[fname]) { continue ;}
			if (! this._value[fname].match(regex)) {
				this.failValidate(fname,msg) ;
				return 1;
			}
		}
		
		return 0 ;
	}
	
}


UPart.UTrack = function (track) {
	track = $(track) ;
	if(!track) {return ;}
	
	//纯粹的滑道只是一个容器,其外表应该由box指定
	track.style.padding = track.style.margin = track.style.borderWidth = "0px" ;
	
	//track必须有slider, 可以有box,如果没有指定，则box指其自身
	if(!track.slicer) {
		var slicer = track.members["slicer"] ;
		if(!slicer) {return ;}
		track.slicer = slicer ;
	}
	track.slicer.track = track ;
	UFront.draggate(track.slicer) ;
	if(!track.box) {track.box = track.members["box"] ; }
	if(!track.box) {track.box = track ; } 
	
	//Track有方向区别，不同的方向，实现方式是不同的
	var direct = track.getAttribute("direct") ;
	if(!direct) {direct = "T2B" ;} 
	switch (direct) {
		case "L2R" : 
		case "R2L" :
			track.getUnitSize = function () {
				var tmp = this.range ;
				if(!tmp || tmp < 1) { tmp = 1 ;}
				return this.clientWidth / tmp ;
			}
			track.calSect = function (sect) {
				this.slicer._width = Math.floor(sect * this.unitSize) ;
				this.slicer.style.width = this.slicer._width + "px" ;
				this.slicer._maxLeft = this.clientWidth - this.slicer._width ;
			}
			track.calValue = function (value) {
				this.slicer.style.marginLeft = Math.floor(value * this.unitSize) + "px" ;
			}
			track.slicer.onMovingX = function (deltaX) {
				if(!this.style.marginLeft) {this.style.marginLeft = "0px" ;}
				if(!this._left) {this._left = parseInt(this.style.marginLeft) ;}
				this._left += deltaX ;
				if(this._left < 0 ) {this.style.marginLeft = "0px"; return true ; }
				else if(this._left > this._maxLeft ) {
					this.track.onSlice(this.track.range - this.track.sect + 1) ;
					this.style.marginLeft = this._maxLeft + "px"; return true ; 
				}else {
					this.track.onSlice(Math.ceil(this._left/this.track.unitSize)) ;
					this.style.marginLeft = this._left + "px" ; 
					return true ;
				}
			} ;
			break ;
		case "T2B" :
		case "B2T" :
			track.getUnitSize = function () {
				var tmp = this.range ;
				if(!tmp || tmp < 1) { tmp = 1 ;}
				return parseInt(this.style.height) / tmp ;
			}
			track.calSect = function (sect) {
				this.slicer._height = Math.floor(sect * this.unitSize) ;
				this.slicer.style.height = this.slicer._height + "px" ;
				this.slicer._maxTop = parseInt(this.style.height) - this.slicer._height - 2 ;
			}
			track.calValue = function (value) {
				this.slicer.style.marginTop = Math.floor(value * this.unitSize) + "px" ;
			}
			track.slicer.onMovingY = function (deltaY) {
				if(!this.style.marginTop) {this.style.marginTop = "0px" ;}
				if(true) {this._top = parseInt(this.style.marginTop) ;}
				this._top += deltaY ;
				if(this._top < 0 ) {this.style.marginTop = "0px"; return true ; }
				else if(this._top > this._maxTop ) {
					this.track.onSlice(this.track.range - this.track.sect + 1) ;
					this.style.marginTop = this._maxTop + "px"; return true ; 
				}else {
					this.track.onSlice(Math.ceil(this._top/this.track.unitSize)) ;
					this.style.marginTop = this._top + "px" ; 
					return true ;
				}
			}
			break ;
		default : 
			return ;
	}
	
	
	
	track.disable = function () {$PR(this.slicer) ;} ;
	track.enable = function () {$PD(this.slicer) ;} ;
	
	track.setScale = function (range, sect) {
		this.range = Math.floor(range) ;
		this.sect = Math.floor(sect) ;
		this.unitSize = this.getUnitSize() ;
		if(!sect || !range || sect==0 || range==0 || sect >= range) { this.disable() ; return ;}
		this.calSect(sect) ;
	} ;
	
	track.setValue = function (value) {
		this._value = value ;
		if((!value && !(value==0)) || value < 0 || (value > this.range-this.sect && value != 0)) {return ;}
		this.calValue(value) ;
		this.onSlice(value+1) ;
	}
	
	track.onSlice = function(from) {
		if(from && from != this._value) {
			this._value = from ;
			if(this._client && this._client.scroll) {
				this._client.scroll(from) ;
			}
		}
	}
	
	track.prev = function() { if(this._value >= 2) {this.onSlice(this._value-1); this.calValue(this._value-1) ; } } ;
	track.next = function() { if(this._value <= (this.range - this.sect)) {this.onSlice(this._value+1); this.calValue(this._value-1);} } ;
	
	//track.setScale(7, 4) ;
	//track.setValue(0) ;
	
}

UPart.UVerimage = function (image) {
	image = $(image) ;
	if(!image) {return ;}
	
	image._tmp = 0 ;
	image.src_bak = window.verimage_src ;
	
	image.refresh = function () {
		//this._tmp = (this._tmp + 1) % 2 ;
		this._tmp ++ ;
		this.src = this.src_bak + "?" + this._tmp + "=" + this._tmp ; 
	}
	
	image.refresh() ;
}

UPart.USubmit = function (submit) {
	submit = $(submit) ;
	if(!submit) {return ;}
	
	submit.addAdvice(function () {
		if(this.submit) {this.submit() ; }
		else if(this.group && this.group.submit) {this.group.submit() ; }
	}) ;
}

//表格，这里的表格是纯粹的数据,所以，一般来说，应该都是<tbody>标签
UPart.UGrid = function (grid) {
	grid = $(grid) ;
	if(!grid) {return ;}
	
	//对于表格，其模型是一组数据的集合(datum)，但它的值value是被标记的一个行
	//我们认为，这组数据中，如果需要被唯一区分，则至少有一个可被用为主键的字段，这个字段名默认为"index"
	grid.datum = [] ;
	grid.value = null ;
	grid.idxName = "index" ;
	
	//表格还需要保存其行数据，用Map方式存取，我们认为，这些行只有可以唯一区分时才有效
	grid.rowMap = new Object() ;
	
	//表格有其被选择的行的阈值，-1:任意行；0：不可选择；1：单选； an:可被选的行数，默认为1
	//默认状况，没有被选择
	grid.threshold = 1 ;
	grid.markerCount = 0 ;
	
	//将说明性的行删除
	DWRUtil.removeAllRows(grid) ;
	
	if(!grid.cellFuncs) {grid.cellFuncs = new Array() ;}
	grid.cellFuncs.unshift(function(data) { return count ++ ; }) ;
	
	grid.capacity = grid.getAttribute("gcapacity") ;
	if(! grid.capacity) {grid = 10 ;}
	grid.capacity = eval(grid.capacity) ;
	
	grid.createBlankRow = function() {
		var tr = $CN("tr") ;
		var td = $AN(tr, "td") ;
		td.innerHTML = "&nbsp;" ;
		tr.className = "blank" ;
		return tr ;
	}
	
	//[Begin]处理一个滚动条==========================================================
		var tr = $CN("tr") ;
		tr.className = "slider" ;
		for(var i = 0 ; i < grid.cellFuncs.length ; i ++) {
			var td = $CN("td") ;
			tr.appendChild(td) ;
		}
		var td = $AN(tr, "td", "td") ;
		td.className = "slider" ;
		var prev = $AN(td, "div") ;
		prev.className = "prev" ;
		var track = $AN(td, "div") ;
		track.className = "track" ;
		track.style.height = grid.getAttribute("gheight") + "px" ;
		var slicer = $AN(track, "div") ;
		slicer.className = "slicer" ;
		slicer.style.marginTop = "0px" ;
		track.box = td ;
		track.slicer = slicer ;
		UPart.UTrack(track) ;
		var next = $AN(td, "div") ;
		next.className = "next" ;
		
		prev._client = track ;
		prev.onclick = function() {this._client.prev() ;} ;
		next._client = track ;
		next.onclick = function() {this._client.next() ;} ;
		
		track.box.rowSpan = grid.capacity + 1 ;
		track._client = grid ;
		track.row = tr ;
		grid.track = track ;
		
	//[End]处理一个滚动条==========================================================
	
	grid.optFuncs = {
		rowCreator:function(options) {
			var row = document.createElement("tr");
			row.baseStyle = (count % 2 == 0) ? "light" : "dark" ;
			row.className = row.baseStyle ;
			row.onmouseover = function () {this.className = "bright" ;} ;
			row.onmouseout = function () {this.className = this.baseStyle ;} ;
			return row;
		},
	  	cellCreator:function(options) {
			var td = document.createElement("td");
			return td;
		}
	} ;
	
	var count = 0;
	
	//设置数据集（区别于setValue）
	grid.setDatum = function (datum) {
		this.datum = datum ;
		
		DWRUtil.removeAllRows(this) ;
		
		var datum = datum ;
		if (!datum ||! datum instanceof Array) {return ;}
		if (! this.cellFuncs) {return ;}
		
		this.appendChild(this.track.row) ;
		count = 1 ;
		DWRUtil.addRows(this, datum, this.cellFuncs, this.optFuncs) ;
		for (var i = 0 ; i < this.capacity ; i ++ ) {
			this.appendChild(this.createBlankRow()) ;
		}
		
		this.gridRows = $A(this.childNodes) ;
		this.gridRows.shift() ;
		
		for(var i = 0 ; i < this.gridRows.length ; i ++) {
			this.removeChild(this.gridRows[i]) ;
		}
		
		var size = datum.length ;
		this.track.setScale(size, this.capacity) ;
		this.track.setValue(0) ;
		
		return ;
	}
	
	grid.scroll = function (from) {
		
		if(!from) {return ;}
		if(from > this.datum.length-this.capacity+1) {from = this.datum.length-this.capacity+1 ;} 
		
		if(this._oldRows) {
			for(var i = 0 ; i < this._oldRows.length ; i ++ ) {this.removeChild(this._oldRows[i]) ; }
		}
		this._oldRows = new Array() ;
		for(var i = (from-1) ; i < (from+this.capacity-1) ; i ++ ) { 
			this._oldRows.push(this.gridRows[i]) ;
			this.appendChild(this.gridRows[i]) ; 
		}
	}
	
	//设置值，值表示选中，可以是单值，也可以是多值
	grid.setValue = function(value) {
		if (! value) { return ;}
		
		if( ! (value instanceof Array) ) {
			var tmp = new Array();
			tmp.push(value) ;
			value = tmp ;
		}
		
		var idxName = this.idxName ;
		
		for (var i = 0 ; i < value.length ; i ++ ) {
			var entry = value[i] ;
			var index = entry[idxName] ;
			if(this.rowMap[index]) {this.rowMap[index].select() ;}
		}
	}
	
	//取出值，值表示选中，可以是单值，也可以是多值
	grid.getValue = function() {
		
		var value = new Array() ;
		
		for(var i in this.rowMap ) {
			var row = this.rowMap[i] ;
			if(row.marked && row.value) {
				value.push(row.value) ;
			}
		}
		
		return (value.length == 1) ? value[0] : value ;
	}
	
}

/*
//表格，这里的表格是纯粹的数据,所以，一般来说，应该都是<tbody>标签
UPart.UGrid = function (grid) {
	grid = $(grid) ;
	if(!grid) {return ;}
	
	//对于表格，其模型是一组数据的集合(datum)，但它的值value是被标记的一个行
	//我们认为，这组数据中，如果需要被唯一区分，则至少有一个可被用为主键的字段，这个字段名默认为"index"
	grid.datum = [] ;
	grid.value = null ;
	grid.idxName = "index" ;
	
	//表格还需要保存其行数据，用Map方式存取，我们认为，这些行只有可以唯一区分时才有效
	grid.rowMap = new Object() ;
	
	//表格有其被选择的行的阈值，-1:任意行；0：不可选择；1：单选； an:可被选的行数，默认为1
	//默认状况，没有被选择
	grid.threshold = 1 ;
	grid.markerCount = 0 ;
	
	//每个表格需要在设计页面时留下关于行的大纲
	var rows = grid.getElementsByTagName("tr") ;
	if(rows.length > 0) {
		var tds = rows[0].getElementsByTagName("td") ;
		
		//tr上记载了作为主键的字段名
		grid.idxName = rows[0].getAttribute("index") ;
		
		grid.tmpRow = new Array() ;
			
		for(var j = 0 ; j < tds.length ; j ++) {
			var cell = tds[j] ;
			var td = $CN("td") ;
			td.innerHTML = cell.innerHTML ;
			grid.tmpRow.push(td) ;
		}
	}
	
	//将说明性的行删除
	DWRUtil.removeAllRows(grid) ;
	
	//设置数据集（区别于setValue）
	grid.setDatum = function (datum) {
		this.datum = datum ;
		
		DWRUtil.removeAllRows(this) ;
		
		var datum = datum ;
		if (! datum instanceof Array) {return ;}
		if (! this.tmpRow) {return ;} 
		
		//填充数据
		for(var i = 0 ; i < datum.length ; i ++) {
			var tr = $CN("tr") ;
			
			//对于行，它知道自己所属的表格，以及它所具有的值
			tr.grid = this ;
			tr.value = datum[i] ;
			
			if(datum[i][this.idxName]) {this.rowMap[datum[i][this.idxName]] = tr ;} ;
			
			//增加一个行，包括其中所有的单元格
			for(var j = 0 ; j < grid.tmpRow.length ; j ++) {
				var cell = grid.tmpRow[j] ;
				var td = $CN("td") ;
				td.innerHTML = cell.innerHTML ;
				tr.appendChild(td) ;
			}
			
			//对于一个行，本身存在一个部件集合
			tr.parts = UPart.buildParts(tr) ;
			UPart.setValues(tr.parts, datum[i]) ;
			
			//设置斑马线，亮显
			tr.baseStyle = (i % 2 == 0) ? "light" : "dark" ;
			tr.className = tr.baseStyle ;
			tr.onmouseover = function () {this.className = "bright" ;} ;
			tr.onmouseout = function () {this.className = this.baseStyle ;} ;
			
			tr.onclick = function () {
				if(! this.marked) {
					this.select() ;
					if (this.grid.onselect) {this.grid.onselect(this.value) ; }
				}else {
					this.unselect() ;
				}
			} ;
			
			//行被选择的事件
			tr.unselect = function() {
				//取消选中
				this.className = this.baseStyle ;
				this.marked = null ;
				this.grid.markerCount -- ;
				this.grid.prevMarker = null ;
				this.onmouseover = function () {this.className = "bright" ;} ;
				this.onmouseout = function () {this.className = this.baseStyle ;} ;
			}
			tr.select = function() {
				//开始选中
				if(this.grid.markerCount == this.grid.threshold) {if(this.grid.prevMarker) {this.grid.prevMarker.unselect();}} ;
				this.className = "marker" ;
				this.marked = "marked" ;
				this.grid.markerCount ++ ;
				this.grid.prevMarker = this ;
				this.onmouseover = function () {} ;
				this.onmouseout = function () {} ;
			}
			
			this.appendChild(tr) ;
		}
	}
	
	//设置值，值表示选中，可以是单值，也可以是多值
	grid.setValue = function(value) {
		if (! value) { return ;}
		
		if( ! (value instanceof Array) ) {
			var tmp = new Array();
			tmp.push(value) ;
			value = tmp ;
		}
		
		var idxName = this.idxName ;
		
		for (var i = 0 ; i < value.length ; i ++ ) {
			var entry = value[i] ;
			var index = entry[idxName] ;
			if(this.rowMap[index]) {this.rowMap[index].select() ;}
		}
	}
	
	//取出值，值表示选中，可以是单值，也可以是多值
	grid.getValue = function() {
		
		var value = new Array() ;
		
		for(var i in this.rowMap ) {
			var row = this.rowMap[i] ;
			if(row.marked && row.value) {
				value.push(row.value) ;
			}
		}
		
		return (value.length == 1) ? value[0] : value ;
	}
	
}


//弹出窗口 url:路径；echo:回应者；model:模态；position：位置；dim:尺寸
openWindow = function (echo, caption, url, model, position, dim) {
	//在顶层
	var canvas = UFront.getCanvas() ;
		
	if(!canvas.widgets) {canvas.widgets = new Object() ;} 
	
	var wdg = canvas.widgets[url] ;
	if(!wdg) {
		wdg = $CN("div") ;
		wdg.setAttribute("ukind", "widget") ;
		wdg.setAttribute("caption", caption) ;
		wdg.className = "widget" ;
		wdg.style.position = "absolute" ;
		UPart.UWidget(wdg) ;
		canvas.widgets[url] = wdg ;
		canvas.document.body.appendChild(wdg) ;
	}
	
	//有关位置和尺寸。如果没有指定位置，则采用鼠标相对定位
	if(! position) {position = getMousePos () ;} 
	wdg.setAttribute("position", position) ;
	wdg.setAttribute("dim", dim) ;
	UPart.setPosition(wdg) ;
	UPart.setDimension(wdg) ;
	
	wdg.echo = echo ;
	wdg.setLocation(url) ;
	wdg.open(model) ;
}

UPart.UWidget = function(widget) {
	widget = $(widget) ;
	if (! widget) {return ;}
	
	widget.setContent = function(content) {
		this.innerHTML = content ;
	}
	
	//窗口初始为不可见
	widget.style.display = "none" ;
	
	//标题栏
	if(widget.getAttribute("caption")) {
		var caption = $CN("div") ;
		caption.className = "caption" ;
		//文本
		var title = document.createElement("span") ;
		title.className = "txt_title" ;
		title.innerHTML = widget.getAttribute("caption") ;
			
		//处理标题栏
		var icon = document.createElement("button") ;
		icon.className = "btn_icon" ;
		icon.disabled = "disabled" ;
		
		var btnClose = document.createElement("button") ;
		btnClose.title = "关闭" ;
		btnClose.className = "btn_close" ;
		btnClose.onclick = function () {widget.close();} ;
		
		//组合
		caption.appendChild (icon) ;
		caption.appendChild (title) ;
		caption.appendChild (btnClose) ;
			  
		widget.appendChild (caption) ;
		widget.caption = caption ;
		
		UFront.draggate(widget) ;
	}
	
	var content = $CN("div") ;
	content.innerHTML = "<iframe src='' width='100%' height='100%' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></iframe>" ;
	content.frm = content.getElementsByTagName("iframe")[0] ;
	
	widget.appendChild (content) ;
	widget.content = content ;
	
	widget.setLocation = function(location) {
		if(!location) {return ;} 
		this.content.frm.src = location ;
	}
	
	widget.onmouseover = function () {this.using = "using" ; } ;
	widget.onmouseout = function () {this.using = null ; } ;
	
	
	widget.open = function(model) {
		$PD(this) ;
		if(model) {
			UFront.showShield() ;
			this.style.zIndex = "700" ;
			widget.onModel = true ;
		}else {
			widget.onModel = null ;
			UFront.getCanvas().ctxWindow = widget ;
		}
	}
	
	widget.close = function(model) {
		//this.setLocation(" ") ;
		$PR(this) ;
		if(widget.onModel) {
			UFront.hiddenShield() ;
		}else {
			UFront.getCanvas().ctxWindow = null ;
		}
	}
}
*/

