﻿function G(id){
	if(document.getElementById(id)){
		return document.getElementById(id)
	}else{
		alert("找不到id："+id)	
	}
}

String.prototype.trim = function(){
//	return this.replace(/^\s+(.*?)\s+$/,"$1")
return this.replace(/^\s+(.*?)\s+$/,"$1")

}

var GLOBAL={
	lib_ver:0.01,
	lang:(function(){//检测语种
		if(document.cookie.indexOf('lang')==-1){
			var expire = new Date((new Date()).getTime() + 10000 * 3600000);
			expire = " expires=" + expire.toGMTString();
			document.cookie = 'lang=zh;'+expire
			return 'zh'
		}else{
			var name = document.cookie.split(/[=;]/);
			for(var i=0; i<name.length; i++){
				if(name[i].indexOf('lang')!=-1){
					return name[(i+1)];
					break;
				}
			}
		}
	})(),
	url:function(){//获取url参数 
		var url={};
		try{
			if(document.location.href.indexOf('?')!=-1){
				var arr = document.location.href.split(/[?]/)[1].split(/[&=]/);
				var length=arr.length;
				for(var i=0; i<length;i=i+2){
					url[arr[i]]=arr[i+1];
				}
				return url;
			}
		}catch(e){
			return ''
		}
	}(),
	browser:(function(){
		var userAgent = window.navigator.userAgent.toLowerCase();
		var val={
				ie:/msie/.test(userAgent),
				ie6:/msie 6/.test(userAgent),
				ie7:/msie 7/.test(userAgent),
				ie8:/msie 8/.test(userAgent),
				firefox:/firefox/.test(userAgent),
				chrome:/chrome/.test(userAgent),
				safari:/safari/.test(userAgent)&&!(/chrome/.test(userAgent)),
				opera:/opera/.test(userAgent)
			}
		return val;
	})()
}


var lib = {
	ajax:function(url,model,data,callBackType,callBackFun){
		this.url = 	url;//请求地址
		this.model = model.toUpperCase();//get ? post
		this.data = data//提交的参数
		this.callBackType = callBackType.toUpperCase();//text?json?xml
		this.callBackFun = callBackFun;//回调方法
		this.checkCrossDomin=function(){
			if(/[a-zA-Z]/.test(window.location.host)){//非ip
				var host = window.location.host.split('.')
				if(host.length==2){//xxx.com
					host=host.join('.')	
				}else {// www.domain.com   www.domain.com.cn
					host.splice(0,1)
					host=host.join('.')
				}
			}else{//ip
				var host = window.location.host
			}
			if(this.url.indexOf(host)==-1 && this.url.indexOf('http')!=-1){
				return false;
			}else{
				return true;
			}
		},
		this._this = this;
	},
	getCurrentTime:function(flag){//时间戳；  sec,min,hour,day,week,mon,year
		var cDate=new Date();
		switch (flag){
			case "sec":
			cDate=cDate.getSeconds();
			break;
			case "min":
			cDate=cDate.getMinutes();
			break;
			case "hour":
			cDate=cDate.getHours();
			break;
			case "day":
			cDate=cDate.getDate();
			break;
			case "week":
			cDate=cDate.getDay();
			break;
			case "mon":
			cDate=cDate.getMonth()+1;
			break;
			case "year":
			cDate=cDate.getFullYear();
			break;
			default://毫秒
			cDate=cDate.getTime();
			break;	
		}
		return cDate;
	},
	bind:function(elem,type,fn){//绑定事件
		if(document.addEventListener){
			elem.addEventListener(type,fn,false);
		}else{
			var typeRef = "_" + type;
			if(!elem[typeRef]){
				elem[typeRef] = [];//obj的_type不存在，则建立obj[_type],并=[]
			}
			for(var j in elem[typeRef]){//遍历obj内，检测有没有同名的eventHandle  
				if(elem[typeRef][j] == fn){
					return;
				}
			}
			elem[typeRef].push(fn);//没有同名，则添加新的句柄
			elem["on"+type] = function(){//把相应数组里的代码片段依次apply到obj执行
				for(var k in this[typeRef]){
					this[typeRef][k].apply(this,arguments);
				}
			}    
		}
	},
	unbind:function(elem,type,fn){//删除事件
		if(document.removeEventListener){
			elem.removeEventListener(type,fn,false);
		}else{
			if(elem["_"+type]){
				for(var i in elem["_"+type]){
					if(elem["_"+type][i] == fn){
						elem["_"+type].splice(i,1);
						break;
					}
				}
			}
		}
		return elem;
	},
	createNodes:function(obj,tagName,tagAttr,returnType){//创立节点
		var o=obj?obj:document.body;
		var element = document.createElement(tagName);
		if(tagAttr){
			var attribArguments = tagAttr.split(/"&|="|"/)
			var length = attribArguments.length%2==0?attribArguments.length:attribArguments.length-1;
			for(var i=0; i<length; i=i+2){
					element[attribArguments[i]]=attribArguments[i+1]
			}
		}
		if(returnType==0){
			o.appendChild(element);
			return element;
		}else if(returnType==1){
			o.appendChild(element);
		}else{
			return element;
		}
	},
	removeNodes:function(node){//删除节点
		document.body.removeChild(node);
	},
	getEventObj:function (e){// 获取event事件对象
		return e.target || e.srcElement;
	},
	getEvent:function(event){//获取不同浏览器的事件模型
		return event || window.event;
	},
	stopPropagation:function (event){//阻止事件气泡
		var e = this.getEvent(event);
		if(e.stopPropagation){
			e.stopPropagation();
		}else{
			e.cancelBubble = true;
		}
	},
	preventDefault:function(event){//阻止默认行为
		var e = this.getEvent(event);
		if(e.preventDefault){
			e.preventDefault();
		}else{
			e.returnValue = false;
		}
	},
	opacity:function(obj,beginAlpha,endAlpha,time){//渐变
		var length= (time*0.01)>=1?Math.floor(time*0.01):1
		var add=(endAlpha-beginAlpha)/length;
		var c_alpha= beginAlpha;
		var i=0;
		if(document.all){
			obj.style.filter='alpha(opacity='+c_alpha*100+')';
		}else{
			obj.style.opacity = c_alpha;
		}
		obj.style.display='block';
		var f=setInterval(function(){
				if(i<length){				
					if(document.all){
						obj.style.filter='alpha(opacity='+(c_alpha+add)*100+')';
					}else{
						obj.style.opacity = c_alpha+add;
					}
					c_alpha=c_alpha+add;
					i++;
					if(i>=length){
						clearInterval(f)
					}
				}
		},100)
	},
	hasClass:function(element, className) { 
		var reg = new RegExp('(\\s|^)'+className+'(\\s|$)');
		return element.className.match(reg);
	},
	addClass:function(element, className) { 
		if (!this.hasClass(element, className)) 
		{ 
			element.className += " "+className; 
		} 
	},
	removeClass:function(element, className) { 
		if (lib.hasClass(element, className)) { 
			var reg = new RegExp('(\\s|^)'+className+'(\\s|$)'); 
			element.className = element.className.replace(reg,' '); 
		} 
	},
	subStr:function(str,maxlen){//整理字符串  不大于maxle
		var s=''
		String(str).length>maxlen?s=String(str).substring(0,(maxlen-1))+"..":s=String(str)
		return s
	},
	getRound:function(){
		return Math.round(Math.random()*10000)	
	}
	
/*	char:{//字符串处理
		trim:function(str){return str.replace(/(^\s*)|(\s*$)/g,"")}
	},*/
	
}


/*
*@名称: ajax通讯类
*@作者:jyb
*@参数：url请求地址,model请求方式,data 提交参数,callBackType 返回值类型,callBackFun返回值处理函数, 新实例对象名称
*@使用方法：var ajax2 = new lib.ajax('returnTEXT.php','get','id=ddd&sid=3','text',ajax2CallbackFun); ajax2.send(ajax2);
*/


lib.ajax.prototype={
	xmlHttp:false,
	create:function(){
		if (typeof XMLHttpRequest != "undefined") {
			this.xmlHttp = new XMLHttpRequest();
		}else if (window.ActiveXObject){
			var aVersions = ["Msxml2.XMLHttp.5.0","Msxml2.XMLHttp.4.0","Msxml2.XMLHttp.3.0","Msxml2.XMLHttp","Microsoft.XMLHttp"];
			for (var i=0; i<aVersions.length;i++){
				try{
					this.xmlHttp = new ActiveXObject(aVersions[i]);
					break;
				}catch (e){}
			}
		}
	},
	send:function(){
		var _this = this._this;
		this.create();
		if(this.checkCrossDomin()==true){
			this.xmlHttp.onreadystatechange = function(){
				ajaxCallback(_this);
				}
			if(this.model == 'GET'){
				if(this.data==''){
					this.xmlHttp.open(this.model,this.url+'?'+'rand='+lib.getRound(),true);//(data==''?':');
				}else{
					this.xmlHttp.open(this.model,this.url+'?'+this.data+'&rand='+lib.getRound(),true);
				}
				this.xmlHttp.send(null);
			}else{
				this.xmlHttp.open(this.model,this.url,true);
				this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
				this.xmlHttp.send(this.data);
			}
		}else{
			if(this.model=="GET" && this.callBackType!="XML"){
				
					this.crossDomin(_this)
				
			}else{
				alert('只支持Get方式,并且只能解析非xml格式数据 (Get support only way, and only non-xml format data analysis)')	
			}
		}
	},
	crossDomin:function(_this){
		var node = lib.createNodes('','script','type="text/javascript"&src="'+this.url+'?'+this.data,0)
		if(document.all){
			node.onreadystatechange = function(){
				var state = node.readyState;
				if (state == "loaded" || state == "interactive" || state == "complete") {
					if(_this.callBackFun!=''){//无需回调
						_this.callBackFun(crossDomainData);
					}
					lib.removeNodes(node);
					}
			};
		}else{
			node.onload=function(){
				if(_this.callBackFun!=''){//无需回调
					_this.callBackFun(crossDominData);
				}
				lib.removeNodes(node);
			}
		}
	}
}

function ajaxCallback(_this){
	if (_this.xmlHttp.readyState == 4||_this.xmlHttp.readyState == 'complete'){
		if(_this.xmlHttp.status == 200){
			if(_this.callBackFun!=''){//有无回调
				if(_this.callBackType=='TEXT'){
					_this.callBackFun(_this.xmlHttp.responseText,_this);
				}else if(_this.callBackType=='JSON'){
					try{
						_this.callBackFun(eval("("+_this.xmlHttp.responseText+")"));
					}catch(e){
						alert(_this.url+'返回值异常')
					}
					
					//_this.callBackFun(_this.xmlHttp.responseJson);
				}else{
					_this.callBackFun(_this.xmlHttp.responseXML,_this);
				}
			}
		}
	}
}

/*
*@名称: xml解析类
*@作者:jyb
*@参数：xmlData
*@使用方法：var xml = new lib.xml(data);
*/


/*
 * 
 * @param {Object} selector
 */
/*var $$ = function(selector){
	if (window==this) return new $$(selector);
	var doms=(function(){
		if(/#/.test(selector)){
			return document.getElementById(selector.substring(1,selector.length));
		}else if(/\./.test(selector)){
			if(document.all){
				var _arr=[];
				var dom=document.getElementsByTagName("*")
				var length=dom.length;
				for(var i=0;i<length;i++){
					if(dom[i].getAttribute("className")==selector.substring(1,selector.length)){
						_arr.push(dom[i])
					}
				}
				return _arr;
			}else{
				return document.getElementsByClassName(selector.substring(1,selector.length))
			}
		}else{
			return document.getElementsByTagName(selector)
		}
	})()
	var arr = [];
	if(doms.item){
		for(var i=0; i<doms.length; i++){
			arr.push(doms.item(i));
		}
	}else{
		arr=[doms]
	}
	return this.setArray(arr);
}
$$.prototype.setArray = function( arr ) {
	this.length = 0;
	[].push.apply( this, arr );
	return this;
}
$$.fn = $$.prototype;

//插件扩展
$$.fn.each = function(method){
    for(var i=0,l=this.length; i<l; i++){
        method.call(this[i],i);
    }
}

//绑定及删除事件 ×不能删除匿名事件函数
$$.fn.bind=function(type,eventHandle){//绑定事件 类型 方法名
	this.each(function(i){
		if(document.addEventListener){
			this.addEventListener(type,eventHandle,false);
		}else{
			var typeRef = "_" + type;
			if(!this[typeRef]){
				this[typeRef] = [];//obj的_type不存在，则建立obj[_type],并=[]
			}
			for(var j in this[typeRef]){//遍历obj内，检测有没有同名的eventHandle  
				if(this[typeRef][j] == eventHandle){
					return;
				}
			}
			this[typeRef].push(eventHandle);//没有同名，则添加新的句柄
			this["on"+type] = function(){//把相应数组里的代码片段依次apply到obj执行
				for(var k in this[typeRef]){
					this[typeRef][k].apply(this,arguments);
				}
			}    
		}
	})
}
$$.fn.unbind=function(type,eventHandle){//删除事件:类型，方法名
	this.each(function(i){
		if(document.removeEventListener){
			this.removeEventListener(type,eventHandle,false);
		}else{
			var typeRef = "_" + type;
			if(this[typeRef]){
				for(var j in this[typeRef]){
					if(this[typeRef][j]==eventHandle){
						this[typeRef].splice(j,1);//删除数组元素  splice(起始索引,个数)
						break;
					}
				}
			}
		}
	})
}

//创立及删除节点 tagAttr用:;分割   如title:aaa;className:red;href:aaa.asp?id=1&name=1
$$.fn.createNodes=function(tagName,tagAttr,returnType){//创立节点
	this.each(function(i){
		var element = document.createElement(tagName);
		if(tagAttr){
			
			var attribArguments = tagAttr.split(/[:;]/)
			var length = attribArguments.length%2==0?attribArguments.length:attribArguments.length-1;
			for(var j=0; j<length; j=j+2){
					element[attribArguments[j]]=attribArguments[j+1]
			}
		}
		if(returnType==0){
			this.appendChild(element);
			return element;
		}else if(returnType==1){
			this.appendChild(element);
		}else{
			return element;
		}
	})
}
$$.fn.removeNodes=function(){//删除节点
	var	_this=[];
	alert(this)
	this.each(function(i){
		_this[i]=this;
	})
	var length=_this.length;
	if (length!=0) {
		_this = _this.reverse();
		for (var j=0; j<length; j++){
			_this[j].parentNode.removeChild(_this[j]);
		}
	}
}
*/


























