
/**************************************
 一些通用的处理方法
****************************************/

//给html元素增加事件处理
function addEventHandler(el,evName,handler){
    if(el.addEventListener)//Mozilla、FF
        el.addEventListener(evName,handler,false);
    else if(el.attachEvent)//IE
        el.attachEvent("on"+evName,handler);
    else
        el[evName]=handler;
}
//给html元素增加Enter键的事件处理(支持处理方法的参数传递)
function addEnterHandler(el,handler,params){
	el = (typeof(el)=="string") ? document.getElementById(el): el;
	if(el){
		addEventHandler(window,"load",function(){
			addEventHandler(el,"keyup",function(e){
				var ev = e || window.event;
				if(ev.keyCode=="13"){
					if(handler){
						var myParams = "";
						if(params){
							for(var i=0;i<params.length;i++){
								if(i==0) myParams += ",";
								myParams += "params["+i+"]";
							}
						}
						return eval("handler("+myParams+")");
					}
				}
			});
		});
	}
}

//限制输入框长度，1个汉字算2个字符长度(超过限制后禁止输入)
function limitMaxLength(el,maxLength){
	var el = (typeof(el)=="string") ? document.getElementById(el):el;
    var getLen = function(str){
        var len = 0;
        for (var i = 0,l = str.length; i < l; i++) {
            len = len + ((str.charCodeAt(i) > 255) ? 2 :1);
        }
        return len;
    }
    var stopEvent = function(e){
        var ev = e || event;
        ev.cancelBubble = true;
        if (ev.stopPropagation)
            ev.stopPropagation();

        if (ev.preventDefault)
            ev.preventDefault();
        ev.returnValue = false;
    };
	if(el && el.tagName){
        el.setAttribute("__oldValue",el.value);//保存旧值
	    if(/msie/i.test(navigator.userAgent))
	    {//ie浏览器
	        addEventHandler(el,"propertychange", function(e){
		        var ev = e || event;
		        var el = ev.srcElement;
		        if(ev.propertyName=="value"){
                    if(getLen(el.value) > maxLength){
                        el.value = el.getAttribute("__oldValue");
                    }else{
                        el.setAttribute("__oldValue",el.value)
                    }
		        }
	        });
	     }
	     else
	     {//火狐浏览器
	        addEventHandler(el,"input", function(e){
		        var ev = e || event;
		        var el = ev.srcElement|| ev.target;
		        var len = getLen(el.value);
		        if( len > maxLength){
		            stopEvent(ev);
		            el.value = el.getAttribute("__oldValue");
		        }
		        else{
		            el.setAttribute("__oldValue",el.value)
		        }
	        });
	     }
	}
}

//取消按钮处理方法(主要解决FF下刷新页面后表单数据并未重置)
function CancelBtn_OnClick(){
	var forms = arguments;
	if(forms.length<1) forms = document.forms;
	for(var i=0,l=forms.length;i<l;i++){
	    if(typeof(forms[i])=="string") {forms[i] = document.getElementById(forms[i]);}
	    if(forms[i] && forms[i].tagName=="FORM"){forms[i].reset();}
	}
	document.location.reload();
}

Date.prototype.format = function(format)
	{
    var o ={
        "M+" : this.getMonth()+1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "m+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth()+3)/3),
        "S" : this.getMilliseconds()
    };
    if(/(y+)/.test(format))
    {
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    };
    for(var k in o)
    {
    if(new RegExp("("+ k +")").test(format))
    {
    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    };
    };
    return format;
};
function subtractionHour(datetime0,hours){
	datetime0 = datetime0.replace(new RegExp("-","g"),"/");
	var d = new Date(datetime0);
 	var d2 = new Date(d.getTime()-hours*3600000);
 	return d2.format("yyyy-MM-dd hh:mm:ss");
}

//getFileIconTextHtml
function getFileIconTextHtml(fileType,txt)
{
    fileType = String(fileType).toLowerCase();
    var fType = {
        "no" : "ico-file-1",
        "xls" : "ico-file-2",
        "xlst" : "ico-file-2",
        "csv" : "ico-file-2",
        "doc" : "ico-file-3",
        "docx" : "ico-file-3",
        "ppt" : "ico-file-4",
        "7z" : "ico-file-5",
        "rar" : "ico-file-6",
        "zip" : "ico-file-7",
        "iso" : "ico-file-8",
        "eml" : "ico-file-9",
        "htm" : "ico-file-10",
        "html" : "ico-file-10",
        "sql" : "ico-file-11",
        "hlp" : "ico-file-13",
        "exe" : "ico-file-15",
        "pdf" : "ico-file-17",
        "psd" : "ico-file-18",
        "ai" : "ico-file-19",
        "fla" : "ico-file-20",
        "swf" : "ico-file-21",
        "txt" : "ico-file-22",
        "log" : "ico-file-22",
        "rm" : "ico-file-25",
        "wmv" : "ico-file-26",
        "mp3" : "ico-file-29",
        "jpg" : "ico-file-31",
        "gif" : "ico-file-32",
        "png" : "ico-file-33",
        "bmp" : "ico-file-34",
        "tif" : "ico-file-35",
        "emf" : "ico-file-36",
        "wmf" : "ico-file-37",
        "dll" : "ico-file-41",
        "bt" : "ico-file-42",
        "folder" : "ico-file-43",
        "chm" : "ico-file-44"
    };
    var myclass = fType[fileType] ? fType[fileType]: fType["no"];
    //return "<span class=\"ico-span\"><b class=\"ico-file "+myclass+"\"></b>"+txt+"</span>";
	//解决文件名文字太长的问题
	var txt2='',m=40, s1='',s2='', N=txt.length, n=txt.replace(/[^\x00-\xff]/g,'--').length;
	var m2=10, m1=m-m2-6, x1=0, x2=0, p=0;
	if(n>m){
		while(x1<m1){
			x1 += (txt.charCodeAt(p++)>0xff)?2:1;
		}
		s1=txt.substring(0,p);
		p=0;
		while(x2<m2){
			x2 += (txt.charCodeAt(N-1-p++)>0xff)?2:1;
		}
		s2=txt.substring(N-p,N);
		txt2 = s1 + ('......').substr(0,n-x1-x2) + s2;
		txt2 = txt2.replace(/&/g,'&amp;');
		txt = txt.replace(/&/g,'&amp;');
		return "<span class=\"ico-span\" title=\""+txt+"\"><button class=\"ico-file "+myclass+"\" disabled=''/></button>"+txt2+"</span>";
	}else{
		txt = txt.replace(/&/g,'&amp;');
		return "<span class=\"ico-span\"><button class=\"ico-file "+myclass+"\" disabled=''/></button>"+txt+"</span>";
	}
}

//Cookie操作================================================================================
var HttpCookie = function( name, value, expires, path, domain )
{
	if( name ) this.Name = name;
	if( value ) this.Value = value;
	if( expires ) this.Expires = expires;
	if( path ) this.Path = path;
	if( domain ) this.Domain = domain;
};
HttpCookie.prototype =
{
	Name:'', Value:'', Expires:'', Path:'/', Domain:'',
	toCookie: function()
	{
		var NewCookie = this.Name + '=' + this.Value;
		if( this.Expires )	NewCookie += (';expires=' + this.Expires);
		if( this.Path )		NewCookie += (';path=' + this.Path);
		if( this.Domain )	NewCookie += (';domain=' + this.Domain);
		return NewCookie;
	}
}
var CookieHelper = function(){};

//将时间转换成Millisecond, 再转换为UTC String
CookieHelper.ConvertToUTCString = function( hourNumber )
{
	var Timestamp = ( !hourNumber || hourNumber == 0 ) ? (0) : (new Date().getTime() + (hourNumber * 1000 * 60 * 60));
	return new Date(Timestamp).toUTCString();
};
// 设定Cookie ( 名称, 值, 保存小时数, 路径, 网域 )
CookieHelper.Set = function( cookieName, cookieValue, expireHour, path, domain )
{
	//新增一个HttpCookie, 指定键、值，
	var HC =
	new HttpCookie
	(
		cookieName,
		escape(cookieValue), //(这边用escape做值的编码)
		CookieHelper.ConvertToUTCString(expireHour), //将小时数转换为Milliseconds
		path,
		domain
	);

	//利用我们自定HttpCookie的toCookie方法取得字符串并设定给Cookie
	document.cookie = HC.toCookie();
};

// 取得Cookie ( 名称 )
CookieHelper.Get = function( cookieName )
{
	// 因为document.cookie是一连串的字符串(所有Cookie都在这边)
	// 所以Raz用正则表达式来取得我们要的值
	var regex = new RegExp( ("(^| )" + cookieName + "=([^;]*)(;|$)") );
	var Matchs = document.cookie.match( regex );
	if( Matchs ) return unescape( Matchs[2] ); //最后unescape
	return null; //若没有值则回传null
};

// 删除Cookie ( 名称, 路径, 网域 )
CookieHelper.Delete = function( cookieName, path, domain )
{
	// 测试若无法取得值就不动作
	if( !CookieHelper.Get(cookieName) ) return;

	// 新增一个HttpCookie, 不同的是值为空, 并且时间为0
	var HC =
	new HttpCookie
	(
		cookieName,
		null,
		CookieHelper.ConvertToUTCString(0)
	);

	document.cookie = HC.toCookie();
};

//設置焦點
function focus(id)
{
	var el = typeof(id)=="string" ? document.getElementById(id) : id;
	if(el && el.focus) el.focus();
}