﻿// JScript 文件

/***
对HTML的操作
*/
var EL={};
EL.get=function(id){if(typeof id == 'string')return document.getElementById(id);return id;}
EL.style=function(el,key,value){
    el=EL.get(el);
    if(typeof key == 'string'){
        var key=key.replace(/\-([a-z])/,function(match,p){return p.toUpperCase();});
        if(typeof value == 'undefined')return el.style[key];
        el.style[key]=value;
    }
    else if(typeof key == 'object')
        for(var k in key) EL.style(el,k,key[k]);
}
EL.attr=function(el,name,value){
    el=EL.get(el);
    if(name=='class')name='className';
    if(name=='for')name='forHTML';
    if(typeof(value)=='undefined')return el[name];
    el[name]=value;
}
EL.create=function(tagName,parent,id){
    var ele=document.createElement(tagName);
    if(id)ele.id=id;
    if(parent && parent.nodeType==1)parent.appendChild(ele);
    return ele;
}
EL.removeChild=function(p,c){if(c)p.removeChild(c);else while(p.firstChild)p.removeChild(p.firstChild);}
EL.addClass=function(el,className){el=EL.get(el);var cs=el.className || '';if(cs!='')className=' '+className; cs=cs.split(/\s+/g);var isExist=false;for(var i=cs.length-1;i>=0;i--)if(cs[i]==className){isExist=true;break;}if(!isExist)el.className=(el.className || '')+className}
EL.removeClass=function(el,className){el=EL.get(el);var cs=el.className || ''; cs=cs.split(/\s+/g);for(var i=cs.length-1;i>=0;i--)if(cs[i]==className)cs.splice(i,1);el.className=cs.join(' ');}
EL.getPosFromEvent=function(e){e=e || event;if(e.pageX)return {x:e.pageX,y:e.pageY};else return {x:e.x+document.documentElement.scrollLeft,y:e.y+document.documentElement.scrollTop};}
EL.getPos=function(el){var x=el.offsetLeft,y=el.offsetTop;while((el=el.offsetParent)){x+=el.offsetLeft;y+=el.offsetTop}return {x:x,y:y};}
var TL={};
TL.GetQuery=function(key){
    var re = new RegExp("[\&\?]" +key+ "\=([^\&\?]*)", "ig");
    var uri=window.location.search;
    var ms=uri.match(re);
    return (ms?ms[0].substr(key.length+2):null);
}
TL.each=function(source,fn){
    if(typeof source =='array')for(var i=0;i<source.length;i++)fn.apply(source[i],[i,source[i]]);
    else for(var i in source) fn.apply(source[i],[i,source[i]]);
}
TL.encodeHTML=function(s){return s.replace(/</ig,'&lt;').replace(/>/gi,'&gt;').replace(/\r\n/gi,'<br />').replace(/\n/gi,'<br />');}
TL.decodeHTML=function(s){return s.replace(/&lt;/ig,'<').replace(/&gt;/gi,'>').replace(/<br>/gi,'\n');}
TL.getPage=function(con,NavNum,PageCount,PageIndex,PageIndexURL){
    if (PageCount > 1)
    {
        function f(i,PageIndex){
            var Selected = i + 1 == PageIndex;
            var temp = Selected ?'pageOn' : 'page';
            return temp;
        }
        var div=EL.create('div',con);
        div.className='clearfix';
        div.style.padding='4px';        
        
        var i = 0;
        var from = 1, to = PageCount - 2;

        //if (NavNum > 0)// NavNum=0 表示显示所有分页导航
        {
            var HalfNum = NavNum / 2;
            from = PageIndex - HalfNum;
            to = from + NavNum - 1;
            if (from < 1) { from = 1; to = from + NavNum - 1; }
            if (to > PageCount - 2) to = PageCount - 2;
        }

        //第一页
        var a=EL.create('a',div);
        a.href=PageIndexURL.replace('{0}',(i + 1).toString());
        a.className=f(i,PageIndex);
        a.innerHTML=(i+1).toString()
        
        // 分隔线
        var span;
        if (from > 1){span=EL.create('span',div);span.style.cssText='display:block;float:left;';span.innerHTML='..';}

        for (i = from; i < PageCount && i <= to; i++){
            a=EL.create('a',div);
            a.href=PageIndexURL.replace('{0}',(i + 1).toString());
            a.className=f(i,PageIndex);
            a.innerHTML=(i+1).toString()
        }

        // 分隔线
        if (to < PageCount - 2){span=EL.create('span',div);span.style.cssText='display:block;float:left;';span.innerHTML='..';}

        //最后一页
        i = PageCount - 1;
        var a=EL.create('a',div);
        a.href=PageIndexURL.replace('{0}',(i + 1).toString());
        a.className=f(i,PageIndex);
        a.innerHTML=(i+1).toString()

    }
}
/***
string
*/
String.prototype.trim=function(){return this.replace(/^\s*|\s*$/g,'');}

/****
事件操作
*/
var EventUtil={};
EventUtil.add=function(el,type,fn){
    if(window.attachEvent)window.attchEvent(el,'on'+type,fn);
    else if(window.addEventListerner)window.addEventListerner(el,type,fn,false);
    else el['on'+type]=fn;
}
EventUtil.remove=function(){
    if(window.detchEvent)window.detchEvent(el,'on'+type,fn);
    else if(window.removeEventListerner)window.removeEventListerner(el,type,fn,false);
    else el['on'+type]=null;
}

