var doEvent = { //注册事件
add : function(func,o,type){
	o = o||window; type = type||"load";
	o.attachEvent ? o.attachEvent("on"+type,func) : o.addEventListener(type,func,false)},
del : function(func,o,type){
	o = o||window; type = type||"load";
	o.detachEvent ? o.detachEvent("on"+type,func) : o.removeEventListener(type,func,false)}
},
ie = !+"\v1", //判断浏览器
firefox = /a/[-1]=="a", opera = !!window.opera, chrome = /source/.test((/a/.toString+"")), safari = window.openDatabase,
_$ = function(id,par){if(!id)return null;if(typeof id=="object")return id;return par ? eval("document.getElementById('"+par+"')."+id) : document.getElementById(id)}, //id[Object]
_$name = function(id){return document.getElementsByName(id)}, //name[Array]
_$tag = function(id,par){return (_$(par)||document).getElementsByTagName(id)}, //tagName[Array]
_$class = function(id,par){
	if(document.getElementsByClassName){return (_$(par)||document).getElementsByClassName(id)};
	var o = [], elems = _$tag("*",par);for(var h=0; h<elems.length; h++){
	if(new RegExp("(^| )"+id+"( |$)").test(elems[h].className))o.push(elems[h])};return o;
}, //className[Array]
_$db = function(id,par){par ? eval("_$('"+id+"','"+par+"').disabled=true") : _$(id).disabled=true}; //禁用按钮

String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g,"")}; //清除前后空格
Number.prototype.toFixed = function(d){
	var s=this+"";if(!d)d=0;
	if(s.indexOf(".")==-1)s+=".";s+=new Array(d+1).join("0");
	if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+ (d+1) +"})?)\\d*$").test(s)){
		var s="0"+ RegExp.$2, pm=RegExp.$1, a=RegExp.$3.length, b=true;
		if(a==d+2){a=s.match(/\d/g); if(parseInt(a[a.length-1])>4){
			for(var i=a.length-2; i>=0; i--){a[i] = parseInt(a[i])+1;
			if(a[i]==10){a[i]=0; b=i!=1;} else break;}
		}
		s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");
	}if(b)s=s.substr(1);return (pm+s).replace(/\.$/, "");} return this+"";
	/*保留小数, 四舍五入, 0.499.toFixed(0)
	Math.ceil() //小数进一
	Math.floor() //取整数部分
	Math.round() //四舍五入*/
};
Array.prototype.doSort = function(){ //数字排序
	//if(arr.constructor==Array){} //判断是否数组
	var clean = [];
	this.sort(function(a,b){return a-b;});
	for(var i=0; i<this.length; i++){
		if(this[i]==this[i+1]){
			continue;
		}else{
			clean.push(this[i]);
		}
	};
	return clean;
};

function offset(o){ //获取标签绝对位置
	var o = _$(o), w = o.offsetWidth, h = o.offsetHeight, x = o.offsetLeft, y = o.offsetTop;
	while(o=o.offsetParent){x+=o.offsetLeft||0;y+=o.offsetTop||0};
	return{"w":w, "h":h, "x":x, "y":y}
}

function findChild(o){ //为Firefox兼容
	var o = _$(o), f = o.firstChild, l = o.lastChild, p = o.previousSibling, n = o.nextSibling;
	while(f!=null&&f.nodeType!=1){f = f.nextSibling}
	while(l!=null&&l.nodeType!=1){l = l.previousSibling}
	while(p!=null&&p.nodeType!=1){p = p.previousSibling}
	while(n!=null&&n.nodeType!=1){n = n.nextSibling};
	return{"f":f, "l":l, "p":p, "n":n}
}

function getCss(o,s){ //获取o的CSS属性,getCss("o","background-color");
	var formatstyle = function(spL){
		var spL=spL.split("-"), reT=spL[0];
		for(var i=1, parT; parT=spL[i]; i++){reT+=parT.charAt(0).toUpperCase()+parT.substr(1)};
		return reT;
	};
	o = _$(o);
	if(window.getComputedStyle)return window.getComputedStyle(o,null).getPropertyValue(s);
	if(o.currentStyle)return o.currentStyle[formatstyle(s)];
	return o;
}
function setCss(o,s){ //修改o的CSS属性,setCss("o","width:200px;background-color:#000;");
	o = _$(o);
	if(typeof s=="string")
        o.style.cssText = s;
    else    
        for(var i in s)
            o.style[i] = s[i];
	return o;
}

function doAttr(o,otype,val){ //自定义属性操作,doAttr(控件ID,自定义属性名称,属性值[设置用,为空时删除])
	o = _$(o);
	if(o.tagName.toLowerCase()=="select"){o = o.options[o.selectedIndex]}
	if(typeof val!="undefined"){
		if(val!="")o.setAttribute(otype,val); //设置自定义属性
		else o.removeAttribute(otype); //删除自定义属性
		return o;
	}else{
		//o.attributes["value"].nodeName; //返回属性的名称
		//o.attributes[0].nodeValue; //返回第一个属性的值
		return o.getAttribute(otype); //获取自定义属性
	}
}

function doCookie(name,val,date){ //Cookie操作,doCookie(名字,值[为空时删除],过期时间)
	if(typeof val!="undefined"){
		var days = 30, //保存天数
		exp = new Date(); //本地当前时间
		if(val!=""){
			if(typeof date!="undefined")days = date;
			exp.setTime(exp.getTime() + days*24*60*60*1000);
			document.cookie = name + "=" + escape(val) + ";expires=" + exp.toGMTString();
		}else{
			var value = doCookie(name);
			exp.setTime(exp.getTime() - 1);
			if(value!=null)document.cookie = name + "=" + value + ";expires=" + exp.toGMTString();
		}
	}else{
		var value = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
		if(value!=null)return unescape(value[2]);
		return null;
	}
}

function left(str,num){ //获取左边部分
	if(num>0){return str.substring(0,num)}
	else{return null}
}
function right(str,num){ //获取右边部分
	if(str.length>=0 && str.length-num>=0 && str.length-num<=str.length){
		return str.substring(str.length-num,str.length)}
	else{return null}
}
function mid(str,snum,lnum){ //获取中间部分
	if(str.length>=0){
		return str.substr(snum,lnum)
	}else{return null}
}

function fontLen(str){ //检测字符串长度
	var strs, tmp, icount = 0;
	if(str=="")return icount;
	strs = str.split("");
	for(var i=0; i<strs.length; i++){
		tmp = escape(strs[i]);
		if(tmp.indexOf("%u",0) == -1){icount = icount + 1}
		else{icount = icount + 2;} //汉字
	};
	return icount;
}

function cutFont(str,num){ //截取字符串
	var x = 0, str = str.replace(/[\s\S]/g, function(d,i,s){
		if(d.charCodeAt(0)>127) x++;
		if(x+i>=num) return "";
		return d;
	});
	return str;
}

function getFreq(mstr,str){ //某字符串在原文中出现的次数频率
	var count = 0, move = 0;
	do{
		move = mstr.indexOf(str, move);
		if(move!=-1){
			count++;
			move += str.length;
		}
	}while(move!=-1);
	return count;
}

function selectBoxes(state,oclass){ //显示隐藏SELECT,oclass为指定某些特定className的SELECT,空时代表全部,selectBoxes("visible"),selectBoxes("hidden")
	var selects = _$tag("SELECT");
	if(oclass && oclass!="") var re = new RegExp("(^| )"+oclass+"( |$)");
	for(var i=0; i<selects.length; i++){
		if(oclass && oclass!=""){
			if(re.test(selects[i].className)) selects[i].style.visibility = state;
		}else{
			selects[i].style.visibility = state;
		}
	}
}

function ImgDiv(o){ //图片垂直居中
	var o = _$(o), par = o.parentNode;
	if(o.style.display=="none"){
		if(par.tagName.toUpperCase()=="A"){findChild(par).p.style.display = "none";par = par.parentNode};
		if(o.className!="load")o.style.display = "block";
		o.style.marginLeft = par.offsetWidth>o.offsetWidth?(par.offsetWidth-o.offsetWidth)/2:-(o.offsetWidth-par.offsetWidth)/2;
		o.style.marginTop = (par.offsetHeight-o.offsetHeight)/2;
	}else{
		if(par.tagName.toUpperCase()=="A"){par = par.parentNode};
		o.style.marginLeft = par.offsetWidth>o.offsetWidth?(par.offsetWidth-o.offsetWidth)/2:-(o.offsetWidth-par.offsetWidth)/2;
		o.style.marginTop = (par.offsetHeight-o.offsetHeight)/2;
	}
}

function showtip(e,o,f,obj,w){ //浮动提示框, showtip(event,"o",1)
	o = _$(o);
	if(f){
		o.style.display = "block";
		o.style.position = "absolute";
		o.style.zIndex = "9999";
		o.style.textAlign = "center";
		o.style.fontSize = "12px";
		o.style.background = "#ffffff";
		o.style.border = "#cccccc 1px solid";
		o.style.padding = "2px";
		o.style.opacity = "0.9";
		o.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=#aaaaaa,direction=135,strength=2) alpha(opacity=90)";
		o.style.cssText += ";-moz-border-radius:4px;-webkit-border-radius:4px;border-radius:4px;";
		if(obj)o.innerHTML = doAttr(obj,"tip");
		if(w){
			o.style.width = w + (typeof(w)=="number"?"px":"");
			o.style.height = "18px";
		}else{
			o.style.width = (findChild(o).f.offsetWidth+4)+"px";
			o.style.height = (findChild(o).f.offsetHeight+4)+"px";
			o.className = "ImgDiv";
		};
		/*
		x: 设置或者是得到鼠标相对于目标事件的父元素的外边界在x坐标上的位置
		clientX: 相对于客户区域的x坐标位置, 不包括滚动条, 就是正文区域
		offsetx: 设置或者是得到鼠标相对于目标事件的父元素的内边界在x坐标上的位置
		screenX: 相对于用户屏幕
		*/
		e = e||event;
		var x = e.clientX, y = e.clientY, mx = x+10+document.body.scrollLeft, my = y+10+document.body.scrollTop;
		if((x+10+o.offsetWidth)>document.body.clientWidth){mx = x-o.offsetWidth+document.body.scrollLeft}
		if((y+10+o.offsetHeight)>document.body.clientHeight){my = y-o.offsetHeight+document.body.scrollTop};
		o.style.left = mx + "px";
		o.style.top = my + "px";
		selectBoxes("hidden","CurPage");
	}else{o.style.display="none";selectBoxes("visible","CurPage")}
}

function request(name,p){ //获取URL参数, 格式: (?或#)Param1=Value1&Param2=Value2
	switch(p){
		case "#":
			var strHref = this.location.href;
			var intPos = strHref.indexOf("#");
			if(intPos!=-1){
				var strRight = strHref.substr(intPos + 1);
				var arrTmps = strRight.split("&");
				for(var i=0; i<arrTmps.length; i++){
					var arrTmp = arrTmps[i].split("=");
					if(arrTmp[0].toUpperCase() == name.toUpperCase()) return unescape(arrTmp[1]);
				}
			}
			return null;break;
		default:
			var intPos = document.location.search.substr(1);
			if(intPos!=""){
				var arrTmps = intPos.split("&");
				for(var i=0; i<arrTmps.length; i++){
					var arrTmp = arrTmps[i].split("=");
					if(arrTmp[0].toUpperCase() == name.toUpperCase()) return unescape(arrTmp[1]);
				}
			}
			return null;break;
	}
}

function XHR(){ //创建XMLHttpRequest
	if(window.XMLHttpRequest) //非IE
		return new XMLHttpRequest();
	else if(window.ActiveXObject) //IE
		return new ActiveXObject("MsXml2.XmlHttp");
}
function doValue(url,state){ //AJAX返回数据,doValue("Index.html",1);
	if(url==""){
		alert("网址错误");
		return false;
	}
	if(url.indexOf("?")==-1) url += "?rndnum="+escape(Math.random());
	else url += "&rndnum="+escape(Math.random());
	var xmlHttp = new XHR();
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);
	if(xmlHttp.status==200){ //服务端完成处理并返回数据
		var ResponseText = unescape(xmlHttp.responseText);
		if(state) return document.write(ResponseText);
		else return ResponseText;
	}else{ //服务器出现异常
		if(state) return document.write("-2");
		else return "-2";
	}
}
function setInnerHTML(htmlCode){
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf("msie")>=0 && ua.indexOf("opera")<0){
		htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
		htmlCode = htmlCode.replace(/<script([^>]*)>/gi, '<script$1 defer="true">');
		return right(htmlCode,htmlCode.length-38);
	}else{return htmlCode}
}

function LoadImages(arrSrc,callBack){
//预载入图片, new LoadImages(["http://www.baidu.com/img/baidu_logo.gif","http://www.baidu.com/img/baidu_logo.gif"]);
	this.Length = arrSrc.length;
	this.LoadedLen = 0; //已经被加载的图片个数
	var _this = this;
	if(_this.Length<1){callBack(arrSrc);return}
	if(opera){ //经测试,OPERA与别的浏览器加载方式不同,所以特别独立开来
		for(var i=0; i<_this.Length; i++){
			var tmpImg = new Image();
			tmpImg.src = arrSrc[i];
			tmpImg.onload = function(){
				_this.LoadedLen++;
				if(_this.LoadedLen==_this.Length && callBack) callBack(arrSrc);
			}
		};
		return;
	};
	this.Load = function(){
		_this.LoadedLen++;
		if(_this.LoadedLen<_this.Length) _this.DownImg();
		else if(callBack) callBack(arrSrc);
	};
	this.DownImg = function(){
		var tmpImg = new Image();
		tmpImg.src = arrSrc[_this.LoadedLen];
		if(ie){
			if(tmpImg.readyState=="complete") _this.Load();
			else tmpImg.onreadystatechange = function(){
				if(this.readyState=="complete") _this.Load();
			}
		}else{tmpImg.onload = _this.Load}
	};
	this.DownImg();
}

function setHome(sObj,sURL){
//<a href="javascript:;" onClick="setHome(this,'<%=CompanyUrl%>')">设为首页</a>
//<a href="javascript:;" onClick="setFav('<%=CompanyName%>','<%=CompanyUrl%>')">加入收藏Collection</a>
	try{sObj.style.behavior="url(#default#homepage)";sObj.setHomePage(sURL)}
	catch(e){
		if(window.netscape){
			try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")}
			catch(e){alert('此操作被浏览器拒绝!\n请在浏览器地址栏输入"about:config"并回车\n然后将[signed.applets.codebase_principal_support]的值设置为"true", 双击即可。')}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',sURL);
		}
	}
}
function setFav(sTitle,sURL){
	try{window.external.addFavorite(sURL,sTitle)}
	catch(e){
		try{window.sidebar.addPanel(sTitle,sURL,"")}
		catch(e){alert("加入收藏失败, 请使用Ctrl+D进行添加")}
	}
};

doEvent.add(function(){
	var tagA = _$tag("A");
	for(var i=0; i<tagA.length; i++) tagA[i].onfocus = function(){this.blur()};
	var tagAREA = _$tag("AREA");
	for(var i=0; i<tagAREA.length; i++) tagAREA[i].onfocus = function(){this.blur()};
	var tagI = _$tag("INPUT");
	for(var i=0; i<tagI.length; i++){
		var _type = doAttr(tagI[i],"type").toLowerCase();
		if(_type=="submit" || _type=="button" || _type=="reset" || _type=="image" || _type=="radio" || _type=="checkbox")
		tagI[i].onfocus = function(){this.blur()}
	};
	var tagS = _$tag("SELECT");
	for(var i=0; i<tagS.length; i++){
		if(tagS[i].id=="" && tagS[i].name!="") tagS[i].id = tagS[i].name;
	};
});
