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,par){return (_$(par)||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 getStyle(o,style){ //获取o的CSS属性,getStyle("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(style);
	if(o.currentStyle)return o.currentStyle[formatstyle(style)];
	return false;
}
function setStyle(o,styles){ //修改o的CSS属性,setStyle("o","width:200px;background-color:#000;");
	o = _$(o);o.style.cssText += ";" + styles;return o;
}

function doAttr(){ //自定义属性操作,doAttr(控件ID,自定义属性名称,属性值[设置用])
	var o = _$(arguments[0]);
	if(o.tagName.toLowerCase()=="select"){o = o.options[o.selectedIndex]}
	if(arguments[2]){
		o.setAttribute(arguments[1],arguments[2]); //设置自定义属性
	}else{
		return o.getAttribute(arguments[1]); //获取自定义属性
		//o.attributes["value"].nodeName; //返回属性的名称
		//o.attributes[0].nodeValue; //返回第一个属性的值
	}
}
function delAttr(o,otype){ //删除自定义属性,delAttr(控件ID,自定义属性名称)
	var o = _$(arguments[0]);
	if(o.tagName.toLowerCase()=="select"){o = o.options[o.selectedIndex]};
	o.removeAttribute(otype);
}

function left(oStr,lngLen){ //获取左边部分
	if(lngLen>0){return oStr.substring(0,lngLen)}
	else{return null}
}
function right(oStr,lngLen){ //获取右边部分
	if(oStr.length>=0 && oStr.length-lngLen>=0 && oStr.length-lngLen<=oStr.length){
		return oStr.substring(oStr.length-lngLen,oStr.length)}
	else{return null}
}
function mid(oStr,starnum,endnum){ //获取中间部分
	if(oStr.length>=0){
		return oStr.substr(starnum,endnum)
	}else{return null}
}

function gfirstChild(o){ //为Firefox兼容firstChild
	var node = _$(o).firstChild;
	while(node.nodeType!=1){node = node.nextSibling};
	return node;
}
function glastChild(o){ //为Firefox兼容lastChild
	var node = _$(o).lastChild;
	while(node.nodeType!=1){node = node.previousSibling};
	return node;
}

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 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 ImgDiv(obj){ //产品图片垂直居中
	if(obj.style.display=="none"){
		if(obj.parentNode.tagName.toLowerCase()=="a"){
			obj.parentNode.previousSibling.previousSibling.style.display="none";
			obj.style.display="block";
			obj.style.marginLeft = obj.parentNode.parentNode.offsetWidth>obj.offsetWidth?(obj.parentNode.parentNode.offsetWidth-obj.offsetWidth)/2:-(obj.offsetWidth-obj.parentNode.parentNode.offsetWidth)/2;
			obj.style.marginTop = (obj.parentNode.parentNode.offsetHeight-obj.offsetHeight)/2;
		}else if(obj.parentNode.tagName.toLowerCase()!="a" && obj.className!="loadpic"){
			obj.previousSibling.previousSibling.style.display="none";
			obj.style.display="block";
			obj.style.marginLeft = obj.parentNode.offsetWidth>obj.offsetWidth?(obj.parentNode.offsetWidth-obj.offsetWidth)/2:-(obj.offsetWidth-obj.parentNode.offsetWidth)/2;
			obj.style.marginTop = (obj.parentNode.offsetHeight-obj.offsetHeight)/2;
		}
	}else{
		if(obj.parentNode.tagName.toLowerCase()=="a"){
			obj.style.marginLeft = obj.parentNode.parentNode.offsetWidth>obj.offsetWidth?(obj.parentNode.parentNode.offsetWidth-obj.offsetWidth)/2:-(obj.offsetWidth-obj.parentNode.parentNode.offsetWidth)/2;
			obj.style.marginTop = (obj.parentNode.parentNode.offsetHeight-obj.offsetHeight)/2;
		}else{
			obj.style.marginLeft = obj.parentNode.offsetWidth>obj.offsetWidth?(obj.parentNode.offsetWidth-obj.offsetWidth)/2:-(obj.offsetWidth-obj.parentNode.offsetWidth)/2;
			obj.style.marginTop = (obj.parentNode.offsetHeight-obj.offsetHeight)/2;
		}
	}
}

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 doCookie(){ //Cookie操作,doCookie(名字,值,过期时间)
	if(arguments[1]!=undefined){
		var Days = 30; //保存天数
		if(arguments[2]!=undefined) Days = arguments[2];
		var exp = new Date(); //本地当前时间
		exp.setTime(exp.getTime() + Days*24*60*60*1000);
		document.cookie = arguments[0] + "=" + escape(arguments[1]) + ";expires=" + exp.toGMTString();
	}else{
		var value = document.cookie.match(new RegExp("(^| )"+arguments[0]+"=([^;]*)(;|$)"));
		if(value != null) return unescape(value[2]);
		return null;
	}
}
function delCookie(name){ //删除Cookie
	var exp = new Date(), value = doCookie(name);
	exp.setTime(exp.getTime() - 1);
	if(value != null) document.cookie = name + "=" + value + ";expires=" + exp.toGMTString();
}

function fontLen(oStr){ //检测字符串长度
	var oStrs, strTemp, iCount = 0;
	if(oStr=="") return iCount;
	oStrs = oStr.split("");
	for(var i=0; i<oStrs.length; i++){
		strTemp = escape(oStrs[i]);
		if(strTemp.indexOf("%u",0) == -1){iCount = iCount + 1;}
		else{iCount = iCount + 2;} //汉字
	}
	return iCount;
}

function cutFont(oStr,oNum){ //截取字符串
	var x = 0, str = oStr.replace(/[\s\S]/g, function(d,i,s){
		if(d.charCodeAt(0)>127) x++;
		if(x+i>=oNum) return "";
		return d;
	});
	return str;
}

function getFreq(mainStr,oStr){ //某字符串在原文中出现的次数频率
	var count = 0, move = 0;
	do{
		move = mainStr.indexOf(oStr, move);
		if(move!=-1){
			count++;
			move += oStr.length;
		}
	}while(move!=-1);
	return count;
}

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();
}

doEvent.add(function(){
	var tag = _$tag("A");
	for(var i=0; i<tag.length; i++) tag[i].onfocus = function(){this.blur()};
	var tagA = _$tag("AREA");
	for(var i=0; i<tagA.length; i++) tagA[i].onfocus = function(){this.blur()};
	var tagI = _$tag("INPUT");
	for(var i=0; i<tagI.length; i++){
		var btype = doAttr(tagI[i],"type").toLowerCase();
		if(btype!="radio" && btype!="checkbox"){
			if(tagI[i].id==""){if(tagI[i].name!="")tagI[i].id=tagI[i].name}
		}
		if(btype=="submit" || btype=="button" || btype=="reset" || btype=="image" || btype=="radio" || btype=="checkbox")
		tagI[i].onfocus = function(){this.blur()}
	};
	var tagS = _$tag("SELECT");
	for(var i=0; i<tagS.length; i++){
		if(tagS[i].id==""){if(tagS[i].name!="")tagS[i].id=tagS[i].name}
	}
});