// JavaScript Document
// 出始化页中所有 类型(即属性controlType)为<MyControlType参数值>的自定义组件
/* MyControlType=<"MyTabPage"|"MyListView">*/
function _initAll(MyControlType,funcInit){
	var _table=document.all.tags("TABLE");
	for (var i=0;i<_table.length;i++)
		if (_table[i].controlType==MyControlType) funcInit(_table[i]);
}

//测试oSub是否在controlType为MyControlType的对象中
//如果是则返回controlType为MyControlType的这个对象,不然返回null
function _getInControl(oSub,MyControlType){
	var _Control=oSub;
	while(_Control.controlType!=MyControlType&&_Control.tagName!="BODY")
		_Control=_Control.parentElement;
	return (_Control.tagName!="BODY")?_Control:null;
}

//测试oSub是否在oParent的对象中,如果是则返回oParent对象,不然返回null
function _getInElement(oSub,oParent){
	var _Control=oSub;
	while(_Control!=oParent&&_Control.tagName!="BODY")
		_Control=_Control.parentElement;
	return (_Control.tagName!="BODY")?_Control:null;
}

//返回oSub上层的第一个id为IdName的元素,如果没有则返回null
function _getInElementByID(oSub,IdName){
	var _Control=oSub;
	while(_Control.id!=IdName&&_Control.tagName!="BODY")
		_Control=_Control.parentElement;
	return (_Control.tagName!="BODY")?_Control:null;
}

//返回oSub上层的第一个tagName为TagName的元素,如果没有则返回null
function _getInElementByTagName(oSub,TagName){
	var _Control=oSub;
	while (_Control.tagName!=TagName){
		if (_Control.tagName=="BODY"){_Control=null;break;}
		_Control=_Control.parentElement;
	}	
	return _Control;
}

function _getMapImg(map){
	var _imgs=document.images;
	var mapId=map.parentElement.name;
	for(var i=0;i<_imgs.length;i++){
		if (_imgs[i].useMap=="#"+mapId){
			return _imgs[i]; 
		}
	}
	return null;
}

// 获得oE元素的绝对宽度(可以换转百分比类型的宽度)。
function _getClientWidth(oE){
	var _w1="";
	var _w2="";
	var _w=oE.width;
	//if(!_w) _w=oE.style.width;
	if(_w!=null) if(_w!=""){
		_w=String(_w);
		if (_w.substr(_w.length-1,1)=="%"){
			_w2="%";
			_w1=_w.substr(0,_w.length-1)
		}else{
			_w2="";
			_w1=_w;
		}
	}
	if(isNaN(_w1)) _w1="";
	if (oE.tagName=="BODY") _w=oE.offsetWidth;	//clientWidth;
	else if(_w2==""&&_w1!="") _w=_w1;
	else if(_w2=="%"&&_w1!=""){
		var _parent=oE.offsetParent;
		var _parentWidth=_getClientWidth(_parent);
		var _styleP=_parent.currentStyle;
		if (_parent.tagName=="TABLE"){
			var _cells=oE.parentElement.cells;
			for (i=0;i<_cells.length;i++){
				var _cell=_cells[i];
				var _styleC=_cell.currentStyle;
				_parentWidth-=((_styleC.borderLeftWidth.ToNumber()!=0)?_styleC.borderLeftWidth.ToNumber():_parent.border.ToNumber());
				_parentWidth-=((_styleC.borderRightWidth.ToNumber()!=0)?_styleC.borderRightWidth.ToNumber():_parent.border.ToNumber());
			}
			_parentWidth-=_parent.cellPadding.ToNumber()*2*_cells.length;
			_parentWidth-=_parent.cellSpacing.ToNumber()*(_cells.length+1);
			_parentWidth-=_parent.border.ToNumber()*2;
		}else{
			//_parentWidth-=_styleP.borderLeftWidth.ToNumber();
			//_parentWidth-=_styleP.borderRightWidth.ToNumber();
			//_parentWidth-=_styleP.paddingLeft.ToNumber();
			//_parentWidth-=_styleP.paddingRight.ToNumber();
			_parentWidth-=oE.currentStyle.marginLeft.ToNumber();
			_parentWidth-=oE.currentStyle.marginRight.ToNumber();
		}
		//if (_parent.id=="TabPage_Pages") alert(_parentWidth +"\n"+ _parent.clientWidth+"\n"+ _parent.offsetWidth);
		if (oE.hspace)	_parentWidth-=oE.hspace.ToNumber()*2;
		_w=Math.floor(_parentWidth*Number(_w1)/100);
	}
	else{_w=oE.offsetWidth;}
	//else if(oE.clientWidth) _w=oE.clientWidth;
	if(oE.tagName!="TABLE"){
		_w-=oE.currentStyle.borderLeftWidth.ToNumber();
		_w-=oE.currentStyle.borderRightWidth.ToNumber();
		_w-=oE.currentStyle.paddingLeft.ToNumber();
		_w-=oE.currentStyle.paddingRight.ToNumber();
	}
	return _w;
}

//鼠标坐标
function _getMousePosition(ev){
	ev = ev || window.event;
	if(ev.pageX || ev.pageY){
		return {X:ev.pageX, Y:ev.pageY};
	}
	return {
		X:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		Y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}
function _getMouseOffset(target, ev){
	ev = ev || window.event;
	var docPos    = _getDocPosition(target);
	var mousePos  = _getMousePosition(ev);
	return {X:mousePos.X - docPos.X, Y:mousePos.Y - docPos.Y};
}

function IsMouseInScroll(obj,ev){
	var mPos=_getMouseOffset(obj,ev);
	var mMaxLeft=obj.clientWidth;
	var mMaxTop=obj.clientHeight;
	return (mPos.X>mMaxLeft||mPos.Y>mMaxTop)?true:false;
}


function _getDocPosition(e){
	var left = 0;
	var top  = 0;
	var left2 = 0;
	var top2 = 0;
	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		left2+= (e.offsetLeft-e.scrollLeft);
		top2 += (e.offsetTop-e.scrollTop);
		e     = e.offsetParent;
	}
	left += e.offsetLeft;
	top  += e.offsetTop;
	left2+= (e.offsetLeft-e.scrollLeft);
	top2 += (e.offsetTop-e.scrollTop);
	return {X:left, Y:top, X2:left2, Y2:top2};
}

function _getBorderWidth(e,in_e){
	if(!in_e) in_e=document.body; 
	var left = 0;
	var top  = 0;
	var right = 0;
	var bottom  = 0;
	while (e.offsetParent){
		left += e.offsetParent.currentStyle.borderLeftWidth.ToNumber();
		top += e.offsetParent.currentStyle.borderTopWidth.ToNumber();
		right += e.offsetParent.currentStyle.borderRightWidth.ToNumber();
		bottom += e.offsetParent.currentStyle.borderBottomWidth.ToNumber();
		if (in_e==e.offsetParent) break;
		e     = e.offsetParent;
	}
	/*left += e.currentStyle.borderLeftWidth.ToNumber();
	top += e.currentStyle.borderTopWidth.ToNumber();
	right += e.currentStyle.borderRightWidth.ToNumber();
	bottom += e.currentStyle.borderBottomWidth.ToNumber();*/
	return {Left:left, Right:right, Top:top, Bottom:bottom};
}

function _getClone(e,newName){
	if (!e) return null;
	var rt=null;
	if (e.tagName=="TR"||e.tagName=="TD"){
		var row=(e.tagName=="TD")?e.parentElement:e;
		var tb=e.offsetParent.cloneNode(true);
		var row1=tb.rows[row.rowIndex];
		while (tb.rows.length>1){
			var delIndex=(tb.rows[0]==row1)?1:0;
			tb.deleteRow(delIndex);
		}
		if(_getInControl(e,"MyTreeView")){
			var row1=tb.rows[0];
			for (var i=0;i<row1.cells.length-1;i++){
				row1.cells[i].style.visibility="hidden";
			}
		}
		rt=tb;
	}else{
		rt=e.cloneNode(true);
	}
	rt.CloneFrom=e;
	rt.id=(newName)?newName:null;
	//alert(rt.outerHTML);
	return rt;
}

function _copyHtmlAttribute(e,toE){
	for(var i=0;i<e.attributes.length;i++)
	if(e.attributes[i].specified){
		toE.setAttribute(e.attributes[i].nodeName,e.attributes[i].nodeValue);
	}
};
function _copyCssAttribute(e,toE){
	for(var p in e.style){
		toE.style.setAttribute(p,e.style.getAttribute(p));
	}
};



/* === 全局函数定义 === */
function __typeof__(objClass) 
{ 
	if ( objClass != undefined && objClass.constructor ) 
	{ 
		var strFun = objClass.constructor.toString(); 
		var className = strFun.substr(0, strFun.indexOf('(')); 
		className = className.replace('function', ''); 
		return className.replace(/(^\s*)|(\s*$)/ig, ''); 
	} 
	return typeof(objClass); 
}

function __getElementById(id){return document.getElementById(id);}
function __isDefined(what){
	var r=true;
	if (typeof what == "undefined") r=false;
	else if (what == null) r=false;
	return r;
}

function win_Open(sURL,sTagert,oSize,oPos){
	if (typeof oPos!="object"||!oPos)	oPos={X:null,Y:null};
	if (typeof oSize!="object"||!oSize)	oSize={W:null,H:null};
	if (typeof sTagert!="string")	sTagert="";
	if (typeof sURL!="string")		sURL="";
	sURL=sURL.Trim();
	if (sURL=="") return null;
	sTagert=sTagert.Trim(); if (sTagert=="") sTagert="_blank";
	sURL+=(sURL.indexOf("?")<0?"?":"&")+"R="+Math.random();
	
	var _flags="status=no,toolbar=no,menubar=no,location=no,resizable=yes";
	if (oSize.W==null) oSize.W=document.body.clientWidth;
	if (oSize.H==null) oSize.H=document.body.clientHeight;
	if (oPos.X==null) oPos.X=window.screenLeft+(document.body.clientWidth-oSize.W)/2;
	if (oPos.Y==null) oPos.Y=window.screenTop+(document.body.clientHeight-oSize.H)/2;
	_flags+= ",width="+oSize.W+",height="+oSize.H+",left="+oPos.X+",top="+oPos.Y;
	
	var _win=window.open(sURL,sTagert,_flags);
	_win.X=oPos.X;	_win.Y=oPos.Y;
	_win.W=oSize.W;	_win.H=oSize.H;
	
	return _win;
}

function getRandomNumber(_c,_width){
//获得一个随机数字字符串,
//_c为随机性系数
//_width为字符串最大长度
	if (typeof _c!="number") _c=1;
	var rt=0;
	var i=1;
	while(true){
		rt+=Math.random();
		i++;
		if (i>_c) break;
	}
	rt=String(rt);
	if (typeof _width=="number"){
		var _start=rt.length-_width;
		if (_start<0) _start=0;
		rt=rt.substr(_start);
	}
	return rt;
}

var _global={}; //定义全局对象

var JsLoaded_General=true;

