var ie = !+"\v1", //判断浏览器
firefox = /a/[-1]=='a',
safari = window.openDatabase,
opera = !!window.opera,
chrome = /source/.test((/a/.toString+'')),
_$ = function(id,pareid){return typeof pareid == "undefined" ? (typeof id == "string" ? document.getElementById(id) : id) : (typeof pareid == "string" ? (typeof id == "string" ? eval("document.getElementById('" + pareid + "')." + id) : id) : (typeof id == "string" ? eval(pareid + "." + id) : id))} //Firefox必须有id值
_$name = function(id,pareid){return typeof pareid == "undefined" ? document.getElementsByName(id) : (typeof pareid == "string" ? _$(pareid).getElementsByName(id) : pareid.getElementsByName(id))}, //获取Name对象
_$tag = function(id,pareid){return typeof pareid == "undefined" ? document.getElementsByTagName(id) : _$(pareid).getElementsByTagName(id)}, //通过Tag获取对象
_$db = function(id,pareid){typeof pareid == "undefined" ? _$(id).disabled = true: eval("_$('" + id + "','" + pareid + "').disabled = true")}, //锁定按钮
_$class = function(name){return document.getElementsByClassName(name)}; //取得指定className的对像
document.getElementsByClassName = function(name){
	var getEleClass = [];
	var myre = new RegExp("(^| )"+name+"( |$)");
	var elems = document.getElementsByTagName("*");
	for(var h=0; h<elems.length; h++){
		if(myre.test(elems[h].className)) getEleClass.push(elems[h]);
	}
	return getEleClass;
}

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() //四舍五入*/
}

function addload(func){
window.attachEvent ? window.attachEvent("onload", func) : window.addEventListener("load", func, false);
}
function addclick(func){
window.attachEvent ? document.attachEvent("onclick", func) : document.addEventListener("click", func, false);
}

function offSet(obj){ //获取标签绝对位置
	var x = obj.offsetLeft;
	var y = obj.offsetTop;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	while(obj=obj.offsetParent){
		x += obj.offsetLeft;
		y += obj.offsetTop;
	}
	return{
		"x" : x,
		"y" : y,
		"w" : w,
		"h" : h
	}
}

function getStyle(obj,name){ //获取obj的CSS属性,getStyle("obj","backgroundColor");
	var elem = _$(obj);
	var style = elem.currentStyle ? elem.currentStyle : elem.ownerDocument.defaultView.getComputedStyle(elem,null);
	return style[name];
}
function setStyle(obj,styles){ //修改obj的CSS属性,setStyle("obj",{width:"200px",backgroundColor:"#000"});
	var elem = _$(obj);
	for(var p in styles){
		elem.style[p] = styles[p];
	}
	return elem;
}

function doAttr(){ //自定义属性操作,doAttr(控件ID,自定义属性名称,属性值[设置用])
	var obj = _$(arguments[0]);
	if(obj.tagName.toLowerCase()=="select") obj = obj.options[obj.selectedIndex];
	if(arguments[2]!=undefined)
		obj.setAttribute(arguments[1],arguments[2]); //设置自定义属性
	else
		return obj.getAttribute(arguments[1]); //获取自定义属性
		//obj.attributes["value"].nodeName; //返回属性的名称
		//obj.attributes[0].nodeValue; //返回第一个属性的值
}
function delAttr(oid,otype){ //删除自定义属性,delAttr(控件ID,自定义属性名称)
	var obj = _$(arguments[0]);
	if(obj.tagName.toLowerCase()=="select") obj = obj.options[obj.selectedIndex];
	obj.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(obj){ //为Firefox兼容firstChild
	var node = _$(obj).firstChild;
	while(node.nodeType!=1) node = node.nextSibling;
	return node;
}
function glastChild(obj){ //为Firefox兼容lastChild
	var node = _$(obj).lastChild;
	while(node.nodeType!=1) node = node.previousSibling;
	return node;
}

function selectBoxes(state,oclass){ //显示隐藏SELECT,oclass为指定某些特定className的SELECT,空时代表全部,selectBoxes("visible"),selectBoxes("hidden")
	var selects = document.getElementsByTagName("SELECT");
	if(typeof oclass!="undefined" && oclass!="") var re = new RegExp("(^| )"+oclass+"( |$)");
	for(var i=0; i<selects.length; i++){
		if(typeof oclass!="undefined" && 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;
	}
}

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();
	exp.setTime(exp.getTime() - 1);
	var value = doCookie(name);
	if(value != null) document.cookie = name + "=" + value + ";expires=" + exp.toGMTString();
}

function fontLen(oStr){ //检测字符长度
var iCount,sStr,strTemp;
iCount = 0;
if(oStr=="") return iCount;
sStr = oStr.split("");
for(var i=0; i<sStr.length; i++){
	strTemp = escape(sStr[i]);
	if(strTemp.indexOf("%u",0) == -1){iCount = iCount + 1;}
	else{iCount = iCount + 2;} //表示是汉字
}
return iCount;
}

function cutFont(oStr,oNum){ //截取字符
var x = 0;
var 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 f_s(id,w,h,func){ //由快到慢
	var obj = _$(id);
	var w_add = 15; //递增量，数值越大缓冲效果越明显
	var h_add = 10;
	obj.style.display = "block";
	if(w!="") obj.style.width = "1px";
	if(h!="") obj.style.height = "1px";
	var changeWH = function(){
		var obj_w = parseInt(obj.currentStyle.width);
		var obj_h = parseInt(obj.currentStyle.height);
		if(w!=""){
			if(obj_w<w) obj.style.width = (obj_w+Math.ceil((w-obj_w)/w_add)) + "px"; //宽度变长，递增量越来越小，Math.ceil小数进一
			else clearbw();
		}
		if(h!=""){
			if(obj_h<h) obj.style.height = (obj_h+Math.ceil((h-obj_h)/h_add)) + "px";
			else clearbw();
		}
	}
	var clearbw = function(){
		if(w!="") obj.style.width = w + "px"; //重新设置宽度造成一种假象
		if(h!="") obj.style.height = h + "px";
		clearInterval(bw);
		bw = null;
		if(func) func();
	}
	var bw = setInterval(changeWH,1);
}
function s_f(id,w,h,func){ //由慢到快
	var obj = _$(id);	
	var w_add = 1;
	var h_add = 1;
	obj.style.display = "block";
	if(w!="") obj.style.width = "1px";
	if(h!="") obj.style.height = "1px";
	var changeWH = function(){
		var obj_w = parseInt(obj.currentStyle.width);
		var obj_h = parseInt(obj.currentStyle.height);
		w_add *= 1.15;
		h_add *= 1.15;
		if(w!=""){
			if(obj_w<w) obj.style.width = (obj_w+w_add) + "px"; //递增量越来越大
			else clearbw();
		}
		if(h!=""){
			if(obj_h<h) obj.style.height = (obj_h+h_add) + "px";
			else clearbw();
		}
	}
	var clearbw = function(){
		if(w!="") obj.style.width = w + "px"; //重新设置宽度造成一种假象
		if(h!="") obj.style.height = h + "px";
		clearInterval(bw);
		bw = null;
		if(func) func();
	}
	var bw = setInterval(changeWH,1);
}
function closeMe(id,w,h,func){ //由快到慢关闭
	var obj = _$(id);
	var w_add = 15;
	var h_add = 10;
	if(w!="") obj.style.width = w + "px";
	if(h!="") obj.style.height = h + "px";
	var changeWH = function(){
		var obj_w = parseInt(obj.currentStyle.width);
		var obj_h = parseInt(obj.currentStyle.height);
		if(w!=""){
			if(obj_w>1) obj.style.width = (obj_w-Math.ceil(obj_w/w_add)) + "px"; //递增量越来越小
			else clearbw();
		}
		if(h!=""){
			if(obj_h>1) obj.style.height = (obj_h-Math.ceil(obj_h/h_add)) + "px";
			else clearbw();
		}
	}
	var clearbw = function(){
		if(w!="") obj.style.width = w + "px"; //重新设置宽度造成一种假象
		if(h!="") obj.style.height = h + "px";
		clearInterval(bw);
		bw = null;
		if(func) func();
	}
	var bw = setInterval(changeWH,1);
}

function LoadImages(arrSrc,callBack){
//预载入图片，调用：
//new LoadImages(["http://www.baidu.com/img/baidu_logo.gif","http://www.baidu.com/img/baidu_logo.gif"]);
var IE = navigator.appName=="Microsoft Internet Explorer";
var Opera = navigator.appName.toLowerCase()=="opera";
var FF = !IE && !Opera;
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();
}

if(!ie){ /*For Firefox and Other*/
function __firefox(){ //兼容Firefox的event,可直接 my=event.pageX, my=event.pageY
HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
window.constructor.prototype.__defineGetter__("event", __window_event);
Event.prototype.__defineGetter__("srcElement", __event_srcElement);
}
function __element_style(){return this.style;}
function __window_event(){return __window_event_constructor();}
function __event_srcElement(){return this.target;}
function __window_event_constructor(){
if(document.all){return window.event;}
var _caller = __window_event_constructor.caller;
while(_caller!=null){
var _argument = _caller.arguments[0];
if(_argument){
var _temp = _argument.constructor;
if(_temp.toString().indexOf("Event")!=-1){return _argument;}
}
_caller = _caller.caller;
}
return null;
}
if(window.addEventListener){__firefox();}

if(typeof(HTMLElement)!="undefined" && !window.opera){ //兼容Firefox的outerHTML,可直接 obj.outerHTML
HTMLElement.prototype.__defineGetter__("outerHTML",function(){
var a=this.attributes, str="<"+this.tagName, i=0;
for(;i<a.length;i++)if(a[i].specified)str+=" "+a[i].name+'="'+a[i].value+'"';
if(!this.canHaveChildren)return str+" />";
return str+">"+this.innerHTML+"</"+this.tagName+">";
});
HTMLElement.prototype.__defineSetter__("outerHTML",function(s){
var r=this.ownerDocument.createRange();
r.setStartBefore(this);
var df=r.createContextualFragment(s);
this.parentNode.replaceChild(df, this);
return s;
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());
});
}

HTMLElement.prototype.__defineGetter__("children",function(){ //兼容Firefox的children,可直接 obj.children
var returnValue = new Object();
var number = 0;
for(var i=0; i<this.childNodes.length; i++){
if (this.childNodes[i].nodeType == 1){returnValue[number] = this.childNodes[i];number++;}
}
returnValue.length = number;
return returnValue;
});
}/*End Firefox*/

addload(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].getAttribute("id")==null||tagI[i].getAttribute("id")==""){if(tagI[i].name!=null) tagI[i].setAttribute("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].getAttribute("id")==null||tagS[i].getAttribute("id")==""){if(tagS[i].name!=null) tagS[i].setAttribute("id",tagS[i].name);}
	}
});
