﻿function Publicdo(xml){
    if(xml.getElementsByTagName("message").length>0){
        if(xml.getElementsByTagName("message")[0].firstChild.nodeValue!=""){
            alert(xml.getElementsByTagName("message")[0].firstChild.nodeValue);
        }
    }            
    if(xml.getElementsByTagName("code").length>0){
        if(xml.getElementsByTagName("code")[0].firstChild.nodeValue!=""){
            eval(xml.getElementsByTagName("code")[0].firstChild.nodeValue);
        }
    }
}
var _Sys = {};
function SysInfo(){
    var ua = navigator.userAgent.toLowerCase();
    if (window.ActiveXObject)
        _Sys.ie = ua.match(/msie ([\d.]+)/)[1]
    else if (document.getBoxObjectFor)
        _Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
    else if (window.MessageEvent && !document.getBoxObjectFor)
        _Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
    else if (window.opera)
        _Sys.opera = ua.match(/opera.([\d.]+)/)[1]
    else if (window.openDatabase)
        _Sys.safari = ua.match(/version\/([\d.]+)/)[1];
}
SysInfo();
var TS = new Object();
TS.addEvent = function (eventname,callback,obj){
    if(obj==null){
        obj = window;
    }
	if (window.attachEvent){
		obj.attachEvent(eventname,callback);
	}else{
		if(eventname.toUpperCase().indexOf("ON")==0){
			eventname = eventname.substring(2,eventname.length);
		}
		obj.addEventListener(eventname,callback,false)
	}
}
TS.$ = function(_id){ return typeof _id == "string" ? document.getElementById(_id) : _id;}
TS.OpenWindow = function(url,width,height,second){
    var h = window.screen.availHeight;
    var w = window.screen.availWidth;
    var t = (h - height)/2;
    var l = (w - width)/2;
    var win = window.open(url,second,"width="+width+",height="+height+",toolbar=no,menubar=no,scrollbars=no,top="+t+",left="+l);
    //var win = window.showModelessDialog(url,second,"dialogwidth="+width+"px,dialogHeight="+height+"px,toolbar=no,menubar=no,scrollbars=no,dialogTop="+t+",dialogLeft="+l);
}
TS.GridView = {};
TS.GridView.Scale = function(gvid,cellindex,erroramount){
    if(cellindex<0){return;}
    if(erroramount<0){erroramount=0;}
    var gv = TS.$(gvid);
    if(!gv){
        return;
    }
    var otherwidth = 0;
    if(gv.rows.length>1){
        for(var i=0; i<gv.rows[0].cells.length; i++){
            if(i != cellindex){
                otherwidth += parseInt(gv.rows[0].cells[i].style.width);
            }
        }
        if(document.documentElement.clientWidth - otherwidth - erroramount<0){
            gv.rows[0].cells[cellindex].style.width = document.documentElement.clientWidth - erroramount + "px";
        }else{
            gv.rows[0].cells[cellindex].style.width = (document.documentElement.clientWidth - otherwidth - erroramount)  + "px";
        }
        if(!_Sys.ie){
            var divs;
            for(var j=1; j<gv.rows.length; j++){
                divs = gv.rows[j].cells[cellindex].getElementsByTagName("div");
                if(divs.length>0){
                    divs[0].style.width = document.documentElement.clientWidth - otherwidth - erroramount  + "px";
                }
            }
        }
    }
    TS.addEvent("onresize",function(){TS.GridView.Scale(gvid,cellindex,erroramount);});
    TS.GridView.Render(gvid);
}
TS.GridView.lastrow = null;
TS.GridView.Render = function (gvid){
    var trs = TS.$(gvid).childNodes[0].childNodes;
    for(var i=0;i<trs.length;i++){
        if(trs[i].nodeType==1){
            trs[i].onmouseover = function(){this.className = "rowselect " + this.className;}
            trs[i].onmouseout = function(){this.className=this.className.replace("rowselect ","");}
            trs[i].onclick = function(){
                if(TS.GridView.lastrow != null){
                    TS.GridView.lastrow.className = TS.GridView.lastrow.className.replace("clickrow","");
                }
                this.className = "clickrow";
                TS.GridView.lastrow=this;
            }
        }
    }
}
String.prototype.format=function(){
    var args=arguments;
    var result=this.replace(/\{(\d+)\}/g,function($0,$1){return args[parseInt($1)].toString();});
    return result;
}
TS.QueryString = function (name){    
    var sReg = "[\\?\\&]({0})=([^\\&]+)";
    var reg=new RegExp(sReg.format(name));
    var match = location.search.match(reg);
    if(match != null){
        var result=match[2];
        return result;
    }
    return null;
}
TS.Sort = function (column,type){
    var url = location.href;
    var _column = TS.QueryString("sort");
    var _type = TS.QueryString("sorttype");
    if(_column == null || _type == null){
        url = url + ( url.indexOf("?") > 0 ? "&" : "?" )+"sort="+column+"&sorttype="+type;
    }else{
        url = url.replace("sort="+_column,"sort="+column).replace("sorttype="+_type,"sorttype="+type);
    }
    location.href = url;
}
TS.PageSize = function (size){
    var url = location.href;
    var _size = TS.QueryString("PageSize");
    if(_size == null){
        url = url + ( url.indexOf("?") > 0 ? "&" : "?" )+"PageSize="+size;
    }else{
        url = url.replace("PageSize="+_size,"PageSize="+size);
    }
    location.href = url;
}
TS.SetSort = function (){
    var tabs = document.getElementsByTagName("table");            
    var column = TS.QueryString("sort");
    var type = TS.QueryString("sorttype");            
    if(column == null || type == null){
        return;
    }
    for(var i=0; i<tabs.length; i++){
        if(tabs[i].className=="gv"){                    
            var ths = tabs[i].getElementsByTagName("th");  
            for(var j=0; j<ths.length; j++){
                var a = ths[j].getElementsByTagName("a")[0];
                if(a.href.indexOf("javascript:TS.Sort('"+column)>-1){
                    a.href =a.href.replace("'desc'","'"+(type=="desc"?"asc":"desc")+"'");
                    a.innerHTML = (type=="desc"?"↓":"↑")+ a.innerHTML.replace(/↑|↓/g,"");
                    return;
                }
            }
        }
    }
}
TS.getData = function (htmlcontrol,urlkey){
    TS.$(htmlcontrol).value = TS.QueryString(urlkey)==null ? "" : decodeURI(TS.QueryString(urlkey));
    if(urlkey == "PageSize" && TS.QueryString(urlkey)==null){
        TS.$(htmlcontrol).value = 20;//默认选中20条
    }
}

TS.Loading = function(str){
    var loading = TS.$("loadingbox");
    var container = TS.$("loadingcontainer");
    if(!loading){
        loading = document.createElement("div");
        loading.id = "loadingbox";
        container = document.createElement("div");
        container.className = "container";
        container.id = "loadingcontainer"
        if(str){
            container.innerHTML = str;
        }else{
            container.innerHTML = "数据加载中，请稍后！";
        }
        loading.appendChild(container);
        document.body.appendChild(loading);
        TS.addEvent("onresize",function(){TS.LoadingResize();});
    }
    var selects = document.getElementsByTagName("select");
    for(var i=0; i<selects.length; i++){
        selects[i].style.display = "none";
    }
    with(loading.style){
        display = "block";
        height = document.documentElement.clientHeight + "px";
	    width = document.documentElement.clientWidth + "px";
    }
    with(container.style){
        marginTop = (document.documentElement.clientHeight - 40) / 2 +"px";
    }
}
TS.LoadingClose = function(){
    var selects = document.getElementsByTagName("select");
    for(var i=0; i<selects.length; i++){
        selects[i].style.display = "";
    }
    var loading = TS.$("loadingbox");
    if(loading){
        loading.style.display = "none";
    }
}
TS.LoadingResize = function(){
    var loading = TS.$("loadingbox");
    var container = TS.$("loadingcontainer");
    if(loading){
        loading.style.height = document.documentElement.clientHeight + "px";
	    loading.style.width = document.documentElement.clientWidth + "px";
    }
    if(loading){
        container.style.marginTop = (document.documentElement.clientHeight - 40) / 2 +"px";
    }
}
