﻿/*常用方法集合
接受参数:无

返回类型:无

应用技术：javascript

制作人：
黄若儒 Roy.Huang

注意:
null
*/
var Method=function(){};

try{//设置命名空间
	Class.setNameSpace("com.gzgi.util.Method",Method);
}
catch(e){}

Method.init=function(t){//将所有有方法带出指定对象或window
	t=t?t:window;
	for(var i in this)
		if(!t[i] && i!="init" && i!="finally")
			t[i]=this[i];
};

Method.destroy=function(t){//将所有带出指定对象或window中的方法删除
	t=t?t:window;
	for(var i in this)
		if(t[i] && t[i]==this[i] && i!="init" && i!="finally")
			t[i]=undefined;
};

Method.GEI=function(t){//根据ID获取对象
	if(typeof(t)!="object")
		t=document.getElementById(t);
	return t;
};

Method.GEN=function(t){//根据名获取对象集合
	t=this.GENS(t);
	if(t) t=t[0];
	return t;
};

Method.GENS=function(t){//根据名获取对象集合
	if(typeof(t)!="object")
		t=document.getElementsByName(t);
	return t;
};

Method.GET=function(t){//根据类型获取对象集合
	t=this.GETS(t);
	if(t) t=t[0];
	return t;
};

Method.GETS=function(t){//根据类型获取对象集合
	if(typeof(t)!="object")
		t=document.getElementsByTagName(t);
	return t;
};

Method.CE=function(t){//根据tag名创建元素
	return document.createElement(t);
};

Method.AC=function(p,c){//填加元素appendChild
	p.appendChild(c);
	return p;
};

Method.RC=function(c,p){//删除元素
	p=p?p:c.parentNode;
	p.removeChild(c);
	c=null;
};

Method.trim=function(s){//去除左右两边\*字符
	return this.trimRight(this.trimLeft(s));
};

Method.trimLeft=function(s){//去除左边\*字符
	return s.replace(/^\s+/g,"");
};

Method.trimRight=function(s){//去除右边\*字符
	return s.replace(/\s+$/g,"");
};

Method.trimAll=function(s){//去除全部\*字符
	return s.replace(/\s+/g,"");
};

Method.replaceAll=function(s,a,b){//replace所有
	if(s && s.length>0 && a && a.length>0 && b){
		var i=s.indexOf(a);
		while(i!=-1){
			s=s.substring(0,i)+b+s.substring(i+a.length);
			i+=b.length;
			i=s.indexOf(a,i);
		}
	}
	return s;
};

Method.getLength=function(s){//返回字符串的长度(中文为2个字符)
	return s.replace(/[^\x00-\xff]/g,"**").length;
};

Method.getFileType=function(s){//获取上传文件类型
	var regExp=/\.([^\.]+)(\?|$)/;
	if(regExp.test(s))
		return s.match(/\.([^\.]+)(\?|$)/)[1];
	else
		return "";
};

Method.fitXML=function(s){//对字符串进行XML编码
	var c=["&","&amp;","<","&lt;",">","&gt;","'","&apos;","\"","&quot;"];
	s=this.trim(s);
	for(var i=0;i<c.length/2;i=i+2)
		s=this.replaceAll(s,c[i],c[i+1]);
	return s;
};

Method.StringToNumber=function(s){//将字符转换为数字
	s+="";
	while(s.indexOf("0")==0)
		s=s.substring(1,s.length);
	return s.length>0?parseInt(s):0;
};

Method.NumberToString=function(s,l){//将数字转换为字符,有补位
	s+="";
	if(l){
		l=this.StringToNumber(l);
		while(s.length<l)
			s="0"+s;
	}
	return s;
};

Method.StringToDate=function(s){//传入YYYY-MM-DD hh:mm:ss.ms返回日期对象
	var date=[0,0,0];
	var time=[0,0,0,0];
	s=s.split(" ");
	if(s.length>0){
		var t=s[0].split("-");
		for(var i=0;i<t.length;i++)
			date[i]=t[i];
	}
	if(s.length>1){
		s=s[1].split(".");
		if(s.length>0){
			var t=s[0].split(":");
			for(var i=0;i<t.length;i++)
				time[i]=t[i];
		}
		if(s.length>1)
			time[3]=s[1];
	}
	return new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2],time[3]);
};

Method.DateToString=function(date,format){//根据日期返回字符
	return format.replace("yyyy",date.getYear()).replace("MM",date.getMonth()+1).replace("dd",date.getDate()).replace("hh",date.getHours()).replace("mm",date.getMinutes()).replace("ss",date.getSeconds()).replace("ms",date.getTime()%(60*60*24*365));
};