//document.getElementById 的快捷方式
function $(ID){
	var node = typeof ID == "string" ? document.getElementById(ID) : ID;
	if (node != null) {
		return node;
	}
	return null;
}

//获取当前对象所应用的样式
function GetCurrentStyle(el, prop) {
	if (el.currentStyle) {
		return el.currentStyle[prop];
	}else if (window.getComputedStyle) {
		prop = prop.replace(/([A-Z])/g, "-$1");
		prop = prop.toLowerCase();
		return window.getComputedStyle(el, "").getPropertyValue(prop);
	}
	return null;
}

//切换HTML元素显示/隐藏状态
function $toggle(el, flag){
	var el = typeof el == "string" ? $(el) : el;
	var curState = GetCurrentStyle(el, 'display');
	var flag = (curState == 'none' ? 'block' : 'none');
	el.style.display = flag;
}

//显示一个HTML元素
function $show(el){
	el = typeof(el) == "string" ? $(el) : el;
	el.style.display = 'block';
}

//隐藏一个HTML元素
function $hide(el){
	el = typeof(el) == "string" ? $(el) : el;
	el.style.display = 'none';
}

//当文档载入后执行一段函数
function onReady(fn) {
	if (typeof fn != "function") return;
	if (window.addEventListener) {
		window.addEventListener("load", fn, false);
	}else {
		window.attachEvent("onload", fn);
	}
}

/**
 * 非IE浏览器增加contains方法
 * @example obj.contains(obj);
 */
if(!(/msie/).test(navigator.userAgent.toLowerCase())) {
	if(typeof(HTMLElement) != "undefined") {
		HTMLElement.prototype.contains = function (obj) {
			while(obj != null && typeof(obj.tagName) != "undefind") {
				if(obj == this) return true;
				obj = obj.parentNode;
			}
			return false;
		};
	}
}

/**
 * 在指定节点上绑定相应的事件
 * @method $addEvent
 * @param {String} elm 需要绑定的节点id
 * @param {Function} func 事件发生时响应的函数
 * @param {String} evType 事件的类型如:click, mouseover
 * @example
 * 		//鼠标点击testEle则alert "clicked"
 * 		$addEvent2("testEle",function () {
 * 			alert("clicked")
 * 		},'click');
 */
function $addEvent(elm, func, evType, useCapture) {
	var elm = $(elm);
	if(!elm) return;
	var useCapture = useCapture || false;
	var evType = evType || 'click';
	if (elm.addEventListener) {
		elm.addEventListener(evType, func, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, func);
		return true;
	}
	else {
		elm['on' + evType] = func;
	}
};

//错误信息 ID_info
function $Red(el, Err){
	var el = el + "_info";
	$setClassName(el, "info_false");
	$inner(el, Err);
}

//通过 ID_info
function $Green(el){
	var el = el + "_info";
	$setClassName(el, "info_true");
	$inner(el, '&nbsp;');
}

//innerHTML 快捷方式
function $inner(el, info){
	el = typeof(el) == "string" ? $(el) : el;
	el.innerHTML = info;
}

//innerHTML 快捷方式
function Inner(el, info){
	el = typeof(el) == "string" ? $(el) : el;
	el.innerHTML = el.innerHTML + info;
}

//是否为空
function $empty(el){
	var el = $(el).value;
	if($trim(el) == '')
		return true;
	else
		return false;
}

//去除前后空格
function $trim(string){
	return string.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * 判断某字符串是否在目标数组中
 * @param {String}  目标关键字
 * @param {Array}   目标数组
 * @param {Boolean}   是否全等(即完全相同)
 */
function $inArr(key, arr, same) {
	if(!same) return arr.join(' ').indexOf(key) != -1;
	for (var i=0, l = arr.length; i< l ; i++) if(arr[i]==key) return true;
	return false;
}

/**
 * 为对象添加 className
 * @param {Object} 需要添加className的节点
 * @param {String}  要添加的 className
 */
function $addClassName(el, cls) {
	var el = $(el);
	if(!el) return;
	var clsNames = el.className.split(/\s+/);
	if(!$inArr(cls, clsNames, true)) el.className += ' '+cls;
};

/**
 * 覆盖对象的 className
 * @param {Object} 需要覆盖className的节点
 */
function $setClassName(el, cls){
	var el = $(el);
	if(!el) return;
	el.className = cls;
}

/**
 * 获取字串的长度 中文算两位
 * @param {Object} 需要计算的节点
 */
function ShowLength(el){
	var el = $(el);
	if(!el) return;
	var value = el.value;
    var strlen = value.length;
	var n=strlen;
    for (var i=0;i<strlen;i++){if (value.charCodeAt(i)<0||value.charCodeAt(i)>255) n++;}
	return n;
}

function Random(){
	var date = new Date();
	return date.getTime();
}

/**
 * 获取浏览器高度
 */
function GetClientHeight(){
	return document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
}

/**
 * 获取浏览器宽度
 */
function GetClientWidth(){
	return document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
}

/**
 * 获取文档高度
 */
function GetScrollHeight(){
	return document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
}

/**
 * 获取XML内容
 */
function LoadXml(url){
	var xmlDoc;
	if(window.ActiveXObject){
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = false;
        xmlDoc.load(url);
    }else if(document.implementation&&document.implementation.createDocument){
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", url, false);
		xmlhttp.send(null);
		xmlDoc = xmlhttp.responseXML;
    }
	return xmlDoc;
}

/**
 * 生成一个定时器
 */
function Remind(){
	setInterval("refreshremind()", 5000);
}
function refreshremind(){
	var xmlDoc;
	xmlDoc = LoadXml("/service/xml.php?type=delay");
	var items = xmlDoc.getElementsByTagName("item");
	var len = items.length;
	if(len){
		var type,nikename,message = '';
		for(var i = 0; i < len; i++){
			type = items[i].getAttribute("type");
			nikename = items[i].getAttribute("nikename");
			message += _remind(type, nikename);
		}
		if(message.length > 0){
			//art.dialog({id:'neemoremind'}).close();
			art.dialog({mouse:true, id:'neemoremind', content: message, left:'right', width:'18em', top:'bottom', fixed:true});
		}
	}
}
function _remind(type, nikename){
	if(type == 'powerOff'){
		return "<p>设备 " + nikename + " 已关机</p>";
	}else{
		return "<p>设备 " + nikename + " 已重启</p>";
	}
}
