﻿var xjLibrary = new Object();
xjLibrary.createEI = function(tagName,className,id,text)
{
    var child = document.createElement(tagName);
    if(className!=null)
        child.className = className;
    if(id!=null)
        child.id = id;
    if(text!=null)
        child.innerHTML = text;
    return child;
}
xjLibrary.createLoad = function(modal)
{
    var modalContent = this.createEI('DIV','modalContent',null,null);
    modalContent.style.width = modal.width;
    modalContent.style.height = modal.height;
    var img  = this.createEI('IMG',null,null,null);
    img.src = modal.src;
    modalContent.appendChild(img);
    if(modal.target==null){
        modalContent.style.left = (document.documentElement.clientWidth - modal.width)/2 + 'px';
        modalContent.style.top = (document.documentElement.clientHeight - modal.height)/2 + 'px';
        var modalBg = this.createEI('DIV','modalBackground',null,null);
        modalBg.style.width = document.documentElement.clientWidth + 'px';
        modalBg.style.height = document.documentElement.clientHeight + 'px';
        modalBg.appendChild(modalContent);
        document.getElementsByTagName('BODY')[0].appendChild(modalBg);
        }
    else{
        modalContent.style.left = (modal.target.clientWidth - modal.width)/2 + 'px';
        modalContent.style.top = (modal.target.clientHeight - modal.height)/2 + 'px';
        modal.target.appendChild(modalContent);
        }
    
}
xjLibrary.createBubbleTip = function(modal)
{
    xjLibrary.bubbleTip.enableTooltips(modal)
}
xjLibrary.createButton  = function(modal)
{
    var table = this.createEI('TABLE','btntable',modal.id,null);
    table.onmouseover = this.flashon;
    table.onmouseout = this.flashoff;
    table.setAttribute('cellspacing','0');
    table.setAttribute('cellpadding','0');
    table.className = 'btntable';
    var tbody = this.createEI('TBODY',null,null,null);
    var tr = this.createEI('TR',null,null,null);
    var td = this.createEI('TD',null,null,null);
    td.appendChild(this.createEI('DIV','btnleft','btnleft',null));
    var center = this.createEI('DIV','btncenter','btntext',modal.innerText);
    center.style.width = modal.width - 16 + 'px';
    td.appendChild(center);
    var arrow = this.createEI('DIV','btncenter','btnarrow',null);
    arrow.appendChild(this.createEI('DIV','btnarrow','btnsubarrow',null));
    td.appendChild(arrow);
    td.appendChild(this.createEI('DIV','btnright','btnright',null));
    tr.appendChild(td);
    tbody.appendChild(tr);
    table.appendChild(tbody);
    if(modal.target!=null)
        modal.target.appendChild(table);
    else
        document.getElementsByTagName('BODY')[0].appendChild(table);
}
///
xjLibrary.createTip = function(modal)
{
    var x_tip = this.createEI('DIV','x-tip xj-tip',modal.id,null);
    x_tip.style.width = modal.width - 6 + 'px';
    x_tip.style.textAlign = 'left';
    var x_tip_tl = this.createEI('DIV','x-tip-tl',null,null);
    var x_tip_tr = this.createEI('DIV','x-tip-tr',null,null);
    var x_tip_tc = this.createEI('DIV','x-tip-tc',null,null);
    var x_tip_header = this.createEI('DIV','x-tip-header x-unselectable',null,null);
    x_tip_header.style.cssText = 'mozuserselect: none; khtmluserselect: none';
    x_tip_header.setAttribute('unselectable','on');
    var span = this.createEI('SPAN','x-tip-header-text','divTiptitle',modal.titleHTML);
    span.style.cssText = 'float:left';
    x_tip_header.appendChild(span);
    if(modal.showClose){
        var img = this.createEI('IMG',null,'tipClose',null);
        img.src = 'Images/ModalClose.gif';
        img.onmouseout = function(){this.src = 'Images/ModalClose.gif';}
        img.onmouseover = function(){this.src = 'Images/ModalCloseRO.gif';}
        img.onclick = this.hideTooltip;
        img.setAttribute('alt','关闭');
        img.style.cssText = 'float: right; cursor: pointer';
        x_tip_header.appendChild(img);
        }
    x_tip_tc.appendChild(x_tip_header);
    x_tip_tr.appendChild(x_tip_tc);
    x_tip_tl.appendChild(x_tip_tr);
    x_tip.appendChild(x_tip_tl);
    
    var x_tip_bwrap = this.createEI('DIV','x-tip-bwrap',null,null);
    var x_tip_ml = this.createEI('DIV','x-tip-ml',null,null);
    var x_tip_mr = this.createEI('DIV','x-tip-mr',null,null);
    var x_tip_mc = this.createEI('DIV','x-tip-mc',null,null);
    var x_tip_body = this.createEI('DIV','x-tip-body x-tip-content','divTipcontent',modal.contentHTML);
    x_tip_mc.appendChild(x_tip_body);
    x_tip_mr.appendChild(x_tip_mc);
    x_tip_ml.appendChild(x_tip_mr);
    x_tip_bwrap.appendChild(x_tip_ml);
    x_tip.appendChild(x_tip_bwrap);
    
    var x_tip_bl = this.createEI('DIV','x-tip-bl x-panel-nofooter',null,null);
    var x_tip_br = this.createEI('DIV','x-tip-br',null,null);
    x_tip_br.appendChild(this.createEI('DIV','x-tip-bc',null,null));
    x_tip_bl.appendChild(x_tip_br);
    x_tip.appendChild(x_tip_bl);
    document.getElementsByTagName('BODY')[0].appendChild(x_tip);
}
xjLibrary.hideTooltip = function()
{
    var obj = document.getElementById('divTooltip');
    if(obj!=null&&typeof(obj)!='undefined'){
        obj.style.visibility = 'hidden';
    }
}
xjLibrary.showTooltip = function(objX,tipId)
{
    var obj = document.getElementById(tipId);
    var x = this.getPos(objX).x;
    if(x+obj.clientWidth <= document.body.clientWidth)
        obj.style.left = this.getPos(objX).x + 1 + 'px';
        else
            obj.style.left = document.body.clientWidth - obj.clientWidth + 'px';
    obj.style.top = this.getPos(objX).y + 21 + 'px';
    obj.style.visibility = 'visible';
}
xjLibrary.getPos = function(obj)
{
    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera') != -1);
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof

    var el = obj;

    if(el.parentNode === null || el.style.display == 'none') 
    {
        return false;
    }

    var parent = null;
    var pos = [];
    var box;

    if(el.getBoundingClientRect) //IE
    {
        box = el.getBoundingClientRect();
        var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);

        return {x:box.left + scrollLeft, y:box.top + scrollTop};
    }
    else if(document.getBoxObjectFor) // gecko
    {
        box = document.getBoxObjectFor(el);
     
        var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
        var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;

        pos = [box.x - borderLeft, box.y - borderTop];
    }
    else // safari & opera
    {
        pos = [el.offsetLeft, el.offsetTop];
        parent = el.offsetParent;
        if (parent != el) {
            while (parent) {
                pos[0] += parent.offsetLeft;
                pos[1] += parent.offsetTop;
                parent = parent.offsetParent;
            }
        }
        if (ua.indexOf('opera') != -1 
        || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) 
        {
            pos[0] -= document.body.offsetLeft;
            pos[1] -= document.body.offsetTop;
        } 
    }

    if (el.parentNode) { parent = el.parentNode; }
    else { parent = null; }

    while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') 
    { // account for any scrolled ancestors
        pos[0] -= parent.scrollLeft;
        pos[1] -= parent.scrollTop;

        if (parent.parentNode) { parent = parent.parentNode; } 
        else { parent = null; }
    }
    return {x:pos[0], y:pos[1]};
}
//bubbletip
xjLibrary.bubbleTip = new Object();
xjLibrary.bubbleTip.enableTooltips = function(modal){
    var links,imgs,areas,i,h;
    if(!document.getElementById || !document.getElementsByTagName) return;
    h=document.createElement("span");
    h.id="btc";
    h.setAttribute("id","btc");
    h.style.position="absolute";
    if(modal.target==null)
        document.getElementsByTagName("body")[0].appendChild(h);
    else
        modal.target.appendChild(h);
    if(modal.id==null) {
        links=document.getElementsByTagName("a");
        imgs = document.getElementsByTagName('IMG');
        areas = document.getElementsByTagName('AREA');
    }
    else{
        links=document.getElementById(modal.id).getElementsByTagName("a");
        imgs = document.getElementById(modal.id).document.getElementsByTagName('IMG');
        areas = document.getElementById(modal.id).document.getElementsByTagName('AREA');
    }
    for(i=0;i<links.length;i++){
        this.Prepare(links[i]);
    }
    for(i=0;i<imgs.length;i++){
        imgs[i].className = 'img_normal';
        this.ImgTip(imgs[i]);
        }
    for(i=0;i<areas.length;i++)
    {
        this.ImgTip(areas[i]);
    }
}
xjLibrary.bubbleTip.ImgTip = function(el)
{
    var tooltip,t,b,s,l;
    t=el.getAttribute("alt");
    if(t!=null&&typeof(t)!='undefined'&&t!=''){
        //if(t==null || t.length==0) t="link:";
        el.removeAttribute("alt");
        tooltip=this.CreateEl("span","tooltip");
        s=this.CreateEl("span","top");
        s.appendChild(document.createTextNode(t));
        tooltip.appendChild(s);
        b=this.CreateEl("b","bottom");
        //l=el.getAttribute("href");
        //if(l.length>30) l=l.substr(0,27)+"...";
        //b.appendChild(document.createTextNode(l));
        tooltip.appendChild(b);
        this.setOpacity(tooltip);
        el.tooltip=tooltip;
        el.onmouseover=this.showTooltip;
        el.onmouseout=this.hideTooltip;
        el.onmousemove=this.Locate;
    }
    else
    {
        el.onmouseover=this.showTooltip;
        el.onmouseout=this.hideTooltip;
    }
}
xjLibrary.bubbleTip.Prepare = function(el){
    var tooltip,t,b,s,l;
    t=el.getAttribute("title");
    if(t!=null&&typeof(t)!='undefined'&&t!=''){
        //if(t==null || t.length==0) t="link:";
        el.removeAttribute("title");
        tooltip=this.CreateEl("span","tooltip");
        s=this.CreateEl("span","top");
        s.appendChild(document.createTextNode(t));
        tooltip.appendChild(s);
        b=this.CreateEl("b","bottom");
        //l=el.getAttribute("href");
        //if(l.length>30) l=l.substr(0,27)+"...";
        //b.appendChild(document.createTextNode(l));
        tooltip.appendChild(b);
        this.setOpacity(tooltip);
        el.tooltip=tooltip;
        el.onmouseover=this.showTooltip;
        el.onmouseout=this.hideTooltip;
        el.onmousemove=this.Locate;
    }
}

xjLibrary.bubbleTip.showTooltip = function(e){
    if(this.tooltip!=null&&typeof(this.tooltip)!='undefined'){
        document.getElementById("btc").appendChild(this.tooltip);}
    e = e || window.event;
    var obj = e.srcElement;
    if(obj.tagName.toLowerCase() == 'img')
        obj.className = 'img_hover';
    xjLibrary.bubbleTip.Locate(e);
}
xjLibrary.bubbleTip.hideTooltip = function(e){
    if(this.tooltip!=null&&typeof(this.tooltip)!='undefined'){
        var d=document.getElementById("btc");
        if(d.childNodes.length>0) d.removeChild(d.firstChild);}
    e = e || window.event;
    var obj = e.srcElement;
    if(obj.tagName.toLowerCase() == 'img')
        obj.className = 'img_normal';
}

xjLibrary.bubbleTip.setOpacity = function(el){
    el.style.filter="alpha(opacity:95)";
    el.style.KHTMLOpacity="0.95";
    el.style.MozOpacity="0.95";
    el.style.opacity="0.95";
}

xjLibrary.bubbleTip.CreateEl = function(t,c){
    var x=document.createElement(t);
    x.className=c;
    x.style.display="block";
    return(x);
}
xjLibrary.bubbleTip.Locate = function(e){
    var posx=0,posy=0;
    if(e==null) e=window.event;

    if(e.pageX || e.pageY){
        posx=e.pageX; posy=e.pageY;
        }
    else if(e.clientX || e.clientY){
        if(document.documentElement.scrollTop){
            posx=e.clientX+document.documentElement.scrollLeft;
            posy=e.clientY+document.documentElement.scrollTop;
            }
        else{
            posx=e.clientX+document.body.scrollLeft;
            posy=e.clientY+document.body.scrollTop;
            }
        }
    document.getElementById("btc").style.top=(posy+10)+"px";
    document.getElementById("btc").style.left=(posx-20)+"px";
}
////////////////
xjLibrary.flashon = function()
{
    this.className = 'btntable-hover';
}
xjLibrary.flashoff = function()
{
    this.className = 'btntable';
}
xjLibrary.AddCss = function(cssHref){
var l=this.createEI("link");
l.setAttribute("type",cssHref);
l.setAttribute("rel","stylesheet");
l.setAttribute("href","bt.css");
document.getElementsByTagName("head")[0].appendChild(l);
}
/*---ajax---------------------------------------------------------------------*/
xjLibrary.getXmlHttp = function()
{
    var xmlObj = null;
    try{
            xmlObj = new ActiveXObject("Msxml2.XmlHttp"); 
        } catch(error){
                            try{
                                    xmlObj = new ActiveXObject("Microsoft.XmlHttp");
                                }catch(error1){
                                                    try{
                                                            xmlObj = new XMLHttpRequest();
                                                          }catch(error2){alert("Your Browser Cant Support This Ajax Frame!");this.close();return;}
                                                    }}
    return xmlObj;
} 
xjLibrary.loadXML = function(xmlSource)
{
        var doc;
        // IE
        if (window.ActiveXObject)
        {
                doc = new ActiveXObject("Microsoft.XMLDOM");
                doc.async=false;
                try{
                   doc.loadXML(xmlSource);
                 }catch(e){
                     var oParser=new DOMParser();
                     doc=oParser.parseFromString(xmlSource,"text/xml");
                 }
                return doc;
        }
        //    Mozilla
        else if (document.implementation &&  document.implementation.createDocument)
        {
            doc= document.implementation.createDocument("","",null);
            try{
                   doc.loadXML(xmlSource);
                 }catch(e){
                     var oParser=new DOMParser();
                     doc=oParser.parseFromString(xmlSource,"text/xml");
                 }
           return doc;
        }
        else
        {
            alert('Your Browser Cant Support This Ajax Frame!');
        }
        return doc; 
}
xjLibrary.parseXML = function(st){
    if (isIE()){
        var  result  =   new  ActiveXObject( " microsoft.XMLDOM " );
        result.loadXML(st);
    } 
    else {
        var  parser  =   new  DOMParser();
        var  result  =  parser.parseFromString(st,  " text/xml " );
    }
    return  result;
}
/*---compatibility---------------------------------------------------------------------*/
function __firefox(){ 
    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(); 
} 
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
//FireFox Support:innerText Event
function isIE(){ //ie? 
   if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 
        return true; 
   else 
        return false; 
} 

if(!isIE()){ //firefox innerText define
   HTMLElement.prototype.__defineGetter__("innerText", 
    function(){
         var anyString = "";
         var childS = this.childNodes;
         for(var i=0; i<childS.length; i++) {
              if(childS[i].nodeType==1)
                    anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
              else if(childS[i].nodeType==3)
                    anyString += childS[i].nodeValue;
         }
         return anyString;
        }); 
    HTMLElement.prototype.__defineSetter__("innerText", 
    function(sText){ 
        this.textContent=sText; 
    }); 
}
if(!isIE())
{
    XMLDocument.prototype.loadXML = function(xmlString)
    {
        var childNodes = this.childNodes;
        for (var i = childNodes.length - 1; i >= 0; i--)
            this.removeChild(childNodes[i]);

        var dp = new DOMParser();
        var newDOM = dp.parseFromString(xmlString, "text/xml");
        var newElt = this.importNode(newDOM.documentElement, true);
        this.appendChild(newElt);
    };

    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var oNSResolver = this.createNSResolver(this.documentElement)
          var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
          var aResult = [];
          for( var i = 0; i < aItems.snapshotLength; i++)
          {
             aResult[i] =  aItems.snapshotItem(i);
          }
          return aResult;
       }

       // prototying the Element
       Element.prototype.selectNodes = function(cXPathString)
       {
          if(this.ownerDocument.selectNodes)
          {
             return this.ownerDocument.selectNodes(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }

    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var xItems = this.selectNodes(cXPathString, xNode);
          if( xItems.length > 0 )
          {
             return xItems[0];
          }
          else
          {
             return null;
          }
       }
       
       // prototying the Element
       Element.prototype.selectSingleNode = function(cXPathString)
       {    
          if(this.ownerDocument.selectSingleNode)
          {
             return this.ownerDocument.selectSingleNode(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }
}
function $(objName)
{
    return document.getElementById(objName);
}