// 让Mozilla支持innerText
try{
	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;
	}
	); 
}
catch(e){}

// IE6背景缓存
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}





var
// 定义Cookie域
NameSpace = 'Mudoo',



// 用ID获取元素
$ = function(element) {
	return typeof(element) == 'object' ? element : document.getElementById(element);
},

// 用Tag获取元素
$t = function(tag, efrom) {
	return efrom == undefined ? document.getElementsByTagName(tag) : efrom.getElementsByTagName(tag);
},

// 用Class获取元素
$c = getElementsByClassName = function(className, parentElement) {
	var elems = ($(parentElement)||document.body).getElementsByTagName("*");
	var result=[];
	for (i=0; j=elems[i]; i++){
	   if ((" "+j.className+" ").indexOf(" "+className+" ")!=-1){
			result.push(j);
	   }
	}
	return result;
},

// 判断浏览器
brower = function() {
	var ua = LCase(navigator.userAgent);
	var os = new Object();
	os.isFirefox = ua.indexOf ('gecko') != -1;
	os.isOpera = ua.indexOf ('opera') != -1;
	os.isIE = !os.isOpera && ua.indexOf ('msie') != -1;
	os.isIE7 = os.isIE && ua.indexOf ('7.0') != -1;
	return os;
},

// 获取传递参数
request = function(strName) {
	var strHref = self.location.toString();
	var intPos = strHref.indexOf('?');
	var strRight = strHref.substr(intPos + 1);
	
	var arrTmp = strRight.split('&');
	for(var i = 0; i < arrTmp.length; i++) {
		var arrTemp = arrTmp[i].split('=');
		if(LCase(arrTemp[0]) == LCase(strName)) return arrTemp[1];
	}
	return '';
},

// 限制最大输入(带可输入统计)
maxInput = function(e, _max, _obj) {
	var _brower = brower();
	e = $(e);
	_obj = $(_obj);
	if(_brower.isIE) {
		myAddEventListener(e, "propertychange", total);
	//}else if(_brower.isFirefox) {
	}else{
		myAddEventListener(e, "input", total);
	}

	total();
	function total() {
		var _len = e.value.length;
		if(_len>_max) {
			var _lastN = (e.value.substr(_max-1, 2) == "\r\n");
			if(_lastN) {
				e.value = e.value.substr(0, _max-1);
				_len-=2;
			}else{
				e.value = e.value.substr(0, _max);
			}
		}
		if(_obj!=null) _obj.innerHTML = (_max-_len)<0 ? 0 : _max-_len;
	}
},

// 读取Cookie
getCookie = function(name) {
	name = NameSpace +'_'+ name;
	var value=document.cookie.match('(?:^|;)\\s*'+name+'=([^;]*)');
	return value?unescape(value[1]):'';
},
// 写入Cookie
setCookie = function(name,value,expire,domain,path) {
	name = NameSpace +'_'+ name;
	value=escape(value);
	value+=(domain)?'; domain='+domain:'';
	value+=(path)?'; path='+path:'';
	if(expire){
		var date=new Date();
		date.setTime(date.getTime()+(expire*86400000));
		value+='; expires='+date.toGMTString();
	}
	document.cookie=name+'='+value;
},
// 移除Cookie
removeCookie = function(name) {
	setCookie(name,'',-1);
},

// 返回字符串的小写
LCase = function(Str) {
	if(Str == null) return;
	return Str.toLowerCase();
},

// 去除左右空格
Trim = function (value) {
	if(value.length < 1){
		return'';
	}
	value = RTrim(value);
	value = LTrim(value);
	if(value==''){
		return '';
	}
	else{
		return value;
	}
},
// 去除右边空格
RTrim = function(value) {
	var w_space = String.fromCharCode(32);
	var v_length = value.length;
	var strTemp = '';
	if(v_length < 0){
		return'';
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(value.charAt(iTemp) == w_space) {
		
		}else{
			strTemp = value.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	}
	return strTemp;

},
// 去除左边空格
LTrim = function(value) {
	var w_space = String.fromCharCode(32);
	if(v_length < 1) {
		return'';
	}
	var v_length = value.length;
	var strTemp = '';
	var iTemp = 0;

	while(iTemp < v_length){
		if(value.charAt(iTemp) == w_space) {
		
		}else{
			strTemp = value.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
},
// 判断文档类型
getBrowserDocument = function() {
   var _dcw = document.documentElement.clientHeight;
   var _dow = document.documentElement.offsetHeight;
   var _bcw = document.body.clientHeight;
   var _bow = document.body.offsetHeight;		   
   if(_dcw == 0) return document.body;
   if(_dcw == _dow) return document.documentElement;		   
   if(_bcw == _bow && _dcw != 0) 
	 return document.documentElement;
   else
	 return document.body;
},	

// 获取页面大小
getPageSize = function() {
	var xScroll, yScroll; 
	if (window.innerHeight && window.scrollMaxY) { 
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	} 
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// Modify By Mudoo
	var visibleWidth, visibleHeight;
	visibleWidth = document.body.clientWidth;
	visibleHeight = document.body.clientHeight;
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) { 
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,visibleWidth,visibleHeight);
	return arrayPageSize;
},

// 获取页面滚动位置
getPageScroll = function() {
	var XScroll,yScroll;
	if (self.pageXOffset) {
	XScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft){  // Explorer 6 Strict
	XScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
	XScroll = document.body.scrollLeft;
	}
	if (self.pageYOffset) {
	yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
	yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
	yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array(XScroll,yScroll);
	return arrayPageScroll;
},

// 获取鼠标位置
/*
getXY = function (e) {
	var XY;
	if(brower().isIE) XY = new Array(event.clientX, event.clientY);
	if(!brower().isIE) XY = new Array(e.pageX, e.pageY);
	return XY;
},
*/
getXY = function (e){
	var XY;
	if(brower().isIE){
		var scrollPos;
		if (typeof window.pageYOffset != 'undefined'){
			scrollPos ={
				x : window.pageXOffset, y : window.pageYOffset
			};
		}else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
			scrollPos ={
				x : document.documentElement.scrollLeft, y : document.documentElement.scrollTop
			};
		}else if(typeof document.body != 'undefined'){
			scrollPos ={
				x : document.body.scrollLeft, y : document.body.scrollTop
			};
		}
		XY ={
			x : window.event.clientX + scrollPos.x - document.body.clientLeft,
			y : window.event.clientY + scrollPos.y - document.body.clientTop
		};
	}else{
		XY ={
			x: e.pageX, y: e.pageY
		};
	}
	return XY;
},

// 获取元素坐标
/*
getPlace = function(e){
	var _left = e.offsetLeft; 
	var _top = e.offsetTop; 
	if(e.offsetParent!=null) {
		_left += getPlace(e.offsetParent)[0]; 
		_top += getPlace(e.offsetParent)[1];
	}
	return new Array(_left, _top); 
},
*/
getCoords = function(node){
	var x = node.offsetLeft;
	var y = node.offsetTop;
	var parent = node.offsetParent;
	while (parent != null){
		x += parent.offsetLeft;
		y += parent.offsetTop;
		parent = parent.offsetParent;
	}
	return {x: x, y: y};
},

// 拖动元素
DragEle = function(obj, dObj, minX, minY, maxX, maxY) {
	obj = $(obj);
	dObj = $(dObj);
	obj.style.cursor = "move";
	obj.onmousedown = function(e) {
		with(dObj) {
			// 记录滚动条当前位置
			style.position = 'absolute';
			_tX = offsetLeft;
			_tY = offsetTop;
			//_sX = getXY(e)[0];
			//_sY = getXY(e)[1];
			_sX = getXY(e).x;
			_sY = getXY(e).y;
		}
		document.ondragstart = "return false;";
		document.onselectstart = "return false;";
		document.onselect = "document.selection.empty();";
		if(dObj.setCapture) {
			dObj.setCapture();
		}else if(window.captureEvents) {
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		}
		document.onmousemove = function(e){
			// 设置滚动条位置
			with(dObj){
				//_mX = _tX+getXY(e)[0]-_sX;
				//_mY = _tY+getXY(e)[1]-_sY;
				_mX = _tX+getXY(e).x-_sX;
				_mY = _tY+getXY(e).y-_sY;
				_mX = _mX<minX ? minX : _mX;
				_mY = _mY<minY ? minY : _mY;
				_mX = _mX>maxX ? maxX : _mX;
				_mY = _mY>maxY ? maxY : _mY;
				style.left = _mX +'px';
				style.top = _mY +'px';
			}
		};
		document.onmouseup = function() {
			if(dObj.releaseCapture) {
				dObj.releaseCapture();  
			}else if(window.captureEvents) {
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			}
			document.onmousemove = null;  
			document.onmouseup = null;  
			document.ondragstart = null;  
			document.onselectstart = null;  
			document.onselect = null;  
		}
	};
},

// 设置不透明度
setOpacity = function (obj,opacity) {
	if(opacity >=1 ) opacity = opacity / 100;				
	try{obj.style.opacity = opacity; }catch(e){}			
	try{if(obj.filters) obj.filters('alpha').opacity = opacity * 100;}catch(e){}
},

// 显示隐藏元素
displayEle = function(obj, status) {
	obj = $(obj);
	if(obj==null) return;
	if(status==undefined) {
		obj.style.display = obj.style.display=='none' ? '' : 'none';
	}else{
		obj.style.display = (status) ? '' : 'none';
	}
},

// 初始化Select默认值
initSelect = function(_ele, _val) {
	_ele = $(_ele);
	if(_ele==null) return;
	var _ops = _ele.options;
	for(var i=0; i<_ops.length; i++) {
		if(LCase(_ops[i].value)==LCase(_val)) {
			_ele[i].selected = true;
			break;
		}
	}
}

// 显示隐藏Select
displaySelect = function(status) {
	var s = document.getElementsByTagName('select');
	for (var i=0;i<s.length;i++){
		//s[i].style.display = status ? '' : 'none';
		s[i].style.visibility = status ? 'visible' : 'hidden';
	}
},

// 生成元素(添加到最后)
appendElement = function(tagName, Attribute, strHtml, refChild) {
	var cEle = document.createElement(tagName);
	// 赋值属性值
	for (var i in Attribute){
		cEle.setAttribute(i, Attribute[i]);
	}
	cEle.innerHTML = strHtml;
	
	refChild.appendChild(cEle);
	return cEle;
},
// 生成元素(添加到指定位置)
insertElement = function(tagName, Attribute, strHtml, refNode) {
	var cEle = document.createElement(tagName);
	// 赋值属性值
	for (var i in Attribute){
		cEle.setAttribute(i, Attribute[i]);
	}
	cEle.innerHTML = strHtml;
	var refChild = refNode.firstChild;
	while(refChild.nodeType!=1) {
		refChild = refChild.nextSibling;
	}
	//alert(refChild.tagName +" _ "+ refChild.nodeType);
	refNode.insertBefore(cEle, refChild);
	return cEle;
},

// 获取字符串长度，中文算两个字符。
getLen = function(str) {
	str = str.replace(/\x00-\xff]/g, 'en');
	return str.length;
	
	/*
	var i,sum;
	sum=0;
	for(i=0;i<str.length;i++) {
		if ((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
			sum=sum+1;
		else
			sum=sum+2;
	}
	return sum;
	*/
},

// 生成随机数
RandStr = function(n, u){
	var tmStr = "abcdefghijklmnopqrstuvwxyz0123456789";
	var Len = tmStr.length;
	var Str = "";
	for(i=1;i<n+1;i++){
		Str += tmStr.charAt(Math.random()*Len);
	}
	return (u ? Str.toUpperCase() : Str);
},

// 元素事件操作
eventListeners = [],
findEventListener = function(node, event, handler){
	var i;
	for (i in eventListeners){
		if (eventListeners[i].node == node && eventListeners[i].event == event && eventListeners[i].handler == handler){
			return i;
		}
	}
	return null;
},
myAddEventListener = function(node, event, handler){
	if (findEventListener(node, event, handler) != null){
		return;
	}
	if (!node.addEventListener){
		node.attachEvent('on' + event, handler);
	}else{
		node.addEventListener(event, handler, false);
	}
	eventListeners.push({node: node, event: event, handler: handler});
},
removeEventListenerIndex = function(index){
	var eventListener = eventListeners[index];
	delete eventListeners[index];
	if (!eventListener.node.removeEventListener){
		eventListener.node.detachEvent('on' + eventListener.event,
		eventListener.handler);
	}else{
		eventListener.node.removeEventListener(eventListener.event,
		eventListener.handler, false);
	}
},
myRemoveEventListener = function(node, event, handler){
	removeEventListenerIndex(findEventListener(node, event, handler));
},
cleanupEventListeners = function(){
	var i;
	for (i = eventListeners.length; i > 0; i--){
		if (eventListeners[i] != undefined){
			removeEventListenerIndex(i);
		}
	}
},

// Open Dialog
showDialogs = function(url,width,height) {
	var dialogWin = null;
	if(brower().isIE) {
		dialogWin = window.showModelessDialog(url,window, 'dialogWidth:'+width+'px;dialogHeight:'+height+'px;edge:Raised;center:1;help:0;status:0;resizable:1;');
	}else{
		var left = screen.availWidth/2 - width/2;
		var top = screen.availHeight/2 - height/2;
		dialogWin = window.open(url, "", "dependent=yes,width="+width+"px,height="+height+"px,left="+left+",top="+top);
	}
	return dialogWin;
},
showDialog = function(url,width,height) {
	var dialogWin = null;
	if(brower().isIE) {
		dialogWin = window.showModalDialog(url,window, 'dialogWidth:'+width+'px;dialogHeight:'+height+'px;edge:Raised;center:1;help:0;resizable:1;');
	}else{
		var left = screen.availWidth/2 - width/2;
		var top = screen.availHeight/2 - height/2;
		dialogWin = window.open(url, "", "width="+width+"px,height="+height+",left="+left+",top="+top);
		
		function addFocus() {
			if(dialogWin.closed == false){
				dialogWin.focus();
			}
		}
		myAddEventListener(window, 'focus', addFocus);
	}
	return dialogWin;
},
windowOpen = function(url,win,width,height) {
	return window.open(url,win,'width='+width+'px,height='+height+'px;toolbar=no,menubar=no,location=no,directories=no,status=yes');
},

// 弹出信息
M_Alert = function(msg, doMain) {
	try{
		if(window.parent.popMsg != undefined) {
			try{
				window.parent.frame.onLoad();
			}catch(e){}
			window.parent.popMsg('alert', '系统提示', msg, doMain);
		}else{
			try{
				frame.onLoad();
			}catch(e){}
			popMsg('alert', '系统提示', msg, doMain);
		}
	}catch(e){
		alert(msg);
		try{
			eval(doMain);
		}catch(e){}
	}
},
// 确认操作
M_Confirm = function(e, msg, doMain) {
	e = e || event;
	e = e.target || e.srcElement;
	var eTag = LCase(e.tagName);
	if(eTag == 'a') {
		var href = e.getAttribute('href');
		if(doMain==undefined) doMain = window != top ? 'oFrame.location="'+ href +'"' : 'window.location="'+ href +'"';
	}else if(eTag == 'form') {
		if(doMain==undefined) doMain = window != top ? 'oFrame.'+ e.name +'.onsubmit="return true;"; oFrame.'+ e.name +'.submit();' : 'window.'+ e.name +'.onsubmit="return true"; window.'+ e.name +'.submit();';
	}
	
	try{
		if(window.parent.popMsg != undefined) {
			window.parent.popMsg('confirm', '系统提示', msg, doMain);
		}else{
			popMsg('confirm', '系统提示', msg, doMain);
		}
	}catch(e){
		if(confirm(msg)) {
			try{
				eval(doMain);
			}catch(e){}
		}
	}
	return false;
};

/*
// Ajax类
Ajax = function(url, Syne) {
	var XMLHttp=null;
	var o=this;
	this.url=url;
	this.Syne=Syne;
	
	this.sendData = function(){
		if (window.XMLHttpRequest) {
			XMLHttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			XMLHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		with(XMLHttp){
			open('GET', this.url, this.Syne);
			onreadystatechange = this.CallBack;
			send(null);
		}
	}
	
	this.CallBack=function(){
		if (XMLHttp.readyState == 4) {
			if(XMLHttp.status == 200 || XMLHttp.status==0) {
				// 
			}else{
				urlError(XMLHttp.status);
			}
		}
	}
	
	this.getText=function(){
		if (XMLHttp==null) {return '还没加载 XMLHttpRequest';}
		if (XMLHttp.readyState==4) {return XMLHttp.responseText;}
		return XMLHttp.readyState;
	}
},
*/
