﻿//=========================================================================================================

function getObj(theObj, theDoc) {
    var p, i, foundObj;
    if (!theDoc) theDoc = document;
    if ((p = theObj.indexOf("?")) > 0 && parent.frames.length) {
        theDoc = parent.frames[theObj.substring(p + 1)].document;
        theObj = theObj.substring(0, p);
    }
    if (!(foundObj = theDoc[theObj]) && theDoc.all)
        foundObj = theDoc.all[theObj];
    for (i = 0; !foundObj && i < theDoc.forms.length; i++)
        foundObj = theDoc.forms[i][theObj];
    for (i = 0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
        foundObj = getObj(theObj, theDoc.layers[i].document);
    if (!foundObj && document.getElementById)
        foundObj = document.getElementById(theObj);
    return foundObj;
}
//=========================================================================================================
var popUpWin = 0;
function popUpWindow(URLStr, left, top, width, height) {
    if (popUpWin)
        if (!popUpWin.closed)
        popUpWin.close();
    popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');
}
//=========================================================================================================
function divClear(obj, delSelf, parentObj) {
    if (obj.hasChildNodes) {
        while (obj.childNodes.length > 0) {
            obj.removeChild(obj.childNodes[0]);
        }
        if (delSelf) {
            parentObj.removeChild(obj);
        }
    }
}
//获取制定元素编号的x,y像素=====================================================================================================
function getElementPos(elementId) {
    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera') != -1);
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
    var el = document.getElementById(elementId);
    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] };
}
//定义鼠标移进滑出事件=====================================================================================================
var cr = "";
function _Over(obj, strColor) {
    cr = obj.style.backgroundColor;
    obj.style.backgroundColor = (!strColor) ? "#f2f2f2" : strColor;
    obj.style.cursor = "hand";
    //obj.style.color = "#000000";
}

function _Out(obj, strColor) {
    obj.style.backgroundColor = (!strColor) ? cr : strColor;
    //obj.style.border = "solid 0px #9d9a9a";
    //obj.style.padding = "";
    //obj.style.margin = "";
}
//自定义window.alert事件=====================================================================================================
//window.alert = function(txt) 
function CustomAlert(tit, txt, iw, ih) {

    var shield = document.createElement("DIV");
    shield.id = "shield";
    shield.style.position = "absolute";
    shield.style.left = "0px";
    shield.style.top = "0px";
    shield.style.width = "100%";
    shield.style.height = "100%";
    shield.style.height = document.body.offsetHeight + "px";
    shield.style.background = "#000";
    shield.style.textAlign = "center";
    shield.style.zIndex = "10000";
    shield.style.filter = "alpha(opacity=0)";
    var alertFram = document.createElement("DIV");
    alertFram.id = "alertFram";
    alertFram.style.position = "absolute";
    //负边界居中法,相对于shield层的顶部时 

    alertFram.style.left = "50%";
    alertFram.style.top = "50%";
    alertFram.style.marginLeft = -iw / 2 + "px";
    alertFram.style.marginTop = -ih / 2 + "px";

    //负边界居中法,相对于shield层的底部时 
    alertFram.style.right = "50%";
    alertFram.style.bottom = "50%";
    alertFram.style.marginRight = -iw / 2 + "px";
    alertFram.style.marginBottom = -ih / 2 + "px";

    alertFram.style.width = iw + "px";
    alertFram.style.height = ih + "px";
    alertFram.style.background = "#fff";
    alertFram.style.textAlign = "center";
    alertFram.style.lineHeight = ih + "px";
    alertFram.style.zIndex = "10002";
    alertFram.style.filter = "alpha(opacity=90)";
    alertFram.style.border = "solid 1px #000";
    var strHtml = "";
    strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%;\">\n";
    strHtml += "<li onmousedown=\"oMove(parentNode.parentNode);\" title=\"移動\" style=\"cursor:move;background:#5eaf07;text-align:left;padding-left:5px;font-size:12px;font-weight:bold;color: #FFFFFF;height:25px;line-height:25px;border-left:1px solid #FFFFFF;border-top:1px solid #FFFFFF;border-right:1px solid #FFFFFF;float:none\">" + tit + "</li>\n";
    strHtml += "<li style=\"background:#efefef;text-align:center;font-size:12px;color: #000;height:168px;line-height:168px;border-left:1px solid #FFFFFF;border-right:1px solid #FFFFFF;float:none\">" + txt + "</li>\n";
    strHtml += "<li style=\"background:#002F9C;text-align:center;font-weight:bold;height:25px;line-height:25px; border-left:1px solid #FFFFFF;border-bottom:1px solid #FFFFFF;border-right:1px solid #FFFFFF;float:none\"><input type=button value=' 确定 ' onclick=\"remove()\"></li>\n";
    strHtml += "</ul>\n";

    alertFram.innerHTML = strHtml;
    document.body.appendChild(alertFram);
    document.body.appendChild(shield);

    var c = 0;
    this.doAlpha = function() {
        if (c++ > 50) { clearInterval(ad); return 0; }
        shield.style.filter = "alpha(opacity=" + c + ");";
    }
    this.remove = function() {
        alertFram.innerHTML = "";
        shield.style.filter = "";
        shield.id = "";
        shield.style.position = "";
        shield.style.left = "";
        shield.style.top = "";
        shield.style.width = "";
        shield.style.height = "";
        shield.style.background = "";
        shield.style.textAlign = "";
        shield.style.zIndex = "";
        shield.style.filter = "";
        alertFram.id = "";
        alertFram.style.position = "";
        alertFram.style.left = "";
        alertFram.style.top = "";
        alertFram.style.marginLeft = "";
        alertFram.style.marginTop = "";
        alertFram.style.width = "";
        alertFram.style.height = "";
        alertFram.style.background = "";
        alertFram.style.textAlign = "";
        alertFram.style.lineHeight = "";
        alertFram.style.zIndex = "";
    }
    var ad = setInterval("doAlpha()", 10);
    alertFram.focus();
    document.body.onselectstart = function() { return false; };
}

function oMove(obj) {
    var otop, oleft;
    otop = event.y - obj.offsetTop;
    oleft = event.x - obj.offsetLeft;
    obj.setCapture();
    obj.onmousemove = function() {
        obj.style.left = event.x - oleft;
        obj.style.top = event.y - otop;
    }
    obj.onmouseup = function() {
        obj.onmousemove = null;
        obj.style.filter = null;
        obj.releaseCapture();
    }
}

//Format String==============================================================================
String.prototype.replaceAll = function(s1, s2) {
    return this.replace(new RegExp(s1, "g"), s2);
}

function EncodeString(tmpString) {
    //string tmpString = "";
    //alert(strM.replace(/a/g,"A"));
    tmpString = tmpString.replaceAll("&", "&amp;");
    tmpString = tmpString.replaceAll("\"", "&quot;");
    tmpString = tmpString.replaceAll("<", "&lt;");
    tmpString = tmpString.replaceAll(">", "&gt;");
    tmpString = tmpString.replaceAll("   ", "&nbsp;");
    tmpString = tmpString.replaceAll("     ", "&nbsp;&nbsp;");
    tmpString = tmpString.replaceAll("\t", "&nbsp;&nbsp;");
    tmpString = tmpString.replaceAll("\r", "<br/>");
    tmpString = tmpString.replaceAll("'", "＇");
    return tmpString;
}

function DecodeString(tmpString) {
    tmpString = tmpString.replaceAll("&amp;", "&");
    tmpString = tmpString.replaceAll("&quot;", "\"");
    tmpString = tmpString.replaceAll("&lt;", "<");
    tmpString = tmpString.replaceAll("&gt;", ">");
    tmpString = tmpString.replaceAll("&nbsp;", "   ");
    tmpString = tmpString.replaceAll("&nbsp;&nbsp;", "     ");
    tmpString = tmpString.replaceAll("&nbsp;&nbsp;", "\t");
    tmpString = tmpString.replaceAll("<br/>", "\r");
    tmpString = tmpString.replaceAll("＇", "'");

    return tmpString;
}

//tooltip==============================================================================
var tooltip = function() {
    var id = 'tt';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 10;
    var timer = 20;
    var endalpha = 95;
    var alpha = 0;
    var tt, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function(v, w) {
            if (tt == null) {
                tt = document.createElement('div');
                tt.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('id', id + 'top');
                c = document.createElement('div');
                c.setAttribute('id', id + 'cont');
                b = document.createElement('div');
                b.setAttribute('id', id + 'bot');
                tt.appendChild(t);
                tt.appendChild(c);
                tt.appendChild(b);
                document.body.appendChild(tt);
                tt.style.opacity = 0;
                tt.style.filter = 'alpha(opacity=0)';
                document.onmousemove = this.pos;
            }
            tt.style.display = 'block';
            c.innerHTML = v;
            tt.style.width = w ? w + 'px' : 'auto';
            if (!w && ie) {
                t.style.display = 'none';
                b.style.display = 'none';
                tt.style.width = tt.offsetWidth;
                t.style.display = 'block';
                b.style.display = 'block';
            }
            if (tt.offsetWidth > maxw) { tt.style.width = maxw + 'px' }
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { tooltip.fade(1) }, timer);
        },
        pos: function(e) {
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
            tt.style.top = (u - h) + 'px';
            tt.style.left = (l + left) + 'px';
        },
        fade: function(d) {
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                } else if (alpha < speed && d == -1) {
                    i = a;
                }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            } else {
                clearInterval(tt.timer);
                if (d == -1) { tt.style.display = 'none' }
            }
        },
        hide: function() {
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { tooltip.fade(-1) }, timer);
        }
    };
} ();
//popup iFrame include pages==========================================================================================================
function popupIFrame(url, w, h) {
    var b = navigator.appVersion.indexOf('MSIE 7.0') > 0;

    var bgDiv = document.createElement("div");
    bgDiv.id = "bgDiv";
    bgDiv.style.position = "absolute";
    bgDiv.style.left = "0px";
    bgDiv.style.top = "0px";
    bgDiv.style.width = "100%";
    bgDiv.style.height = (b) ? "100%" : window.screen.height + "px";
    bgDiv.style.background = "#000";
    bgDiv.style.textAlign = "center";
    bgDiv.style.zIndex = "100000";
    bgDiv.style.filter = "alpha(opacity=90)";
    bgDiv.style.margin = "0px";

    document.body.appendChild(bgDiv);

    var iframe = document.createElement("IFRAME");
    iframe.style.position = "absolute";
    iframe.style.zIndex = "100012";
    iframe.id = "iframe";
    iframe.style.overflow = "hidden";
    iframe.style.width = w + "px";
    iframe.style.height = h + "px";


    iframe.setAttribute("frameBorder", 0);

    iframe.src = url;
    iframe.style.left = "50%";
    iframe.style.top = "50%";
    iframe.style.marginLeft = -w / 2 + "px";
    iframe.style.marginTop = -h / 2 + "px";

    //负边界居中法,相对于shield层的底部时
    iframe.style.right = "50%";
    iframe.style.bottom = "50%";
    iframe.style.marginRight = -w / 2 + "px";
    iframe.style.marginBottom = -h / 2 + "px";

    document.body.appendChild(iframe);

    var closeSpan = document.createElement("div");
    closeSpan.id = "closeSpan";
    closeSpan.style.cursor = "hand";
    closeSpan.style.position = "absolute";
    closeSpan.style.borderTop = "solid 1px #696969";
    closeSpan.style.width = (w - 15) + "px";
    closeSpan.style.height = "30px";
    closeSpan.style.textAlign = "right";
    closeSpan.style.lineHeight = "30px";
    closeSpan.style.paddingRight = "15px";
    closeSpan.style.backgroundColor = "#e0e0e0";

    closeSpan.style.left = "50%";
    closeSpan.style.top = iframe.offsetTop + h + "px";
    closeSpan.style.marginLeft = -w / 2 + "px";
    closeSpan.style.right = "50%";
    closeSpan.style.marginRight = -w / 2 + "px";


    closeSpan.innerText = "关闭窗口";
    closeSpan.style.zIndex = "100012";
    closeSpan.onclick = function() {
        var o4 = document.getElementById("closeSpan");
        var o3 = document.getElementById("iframe");
        var o1 = document.getElementById("bgDiv");
        document.body.removeChild(o4);
        document.body.removeChild(o3);
        document.body.removeChild(o1);
    }

    closeSpan.onmouseover = function() {
        this.style.color = "#ff6600";
        this.style.backgroundColor = "#f2f2f2";
    }

    closeSpan.onmouseout = function() {
        this.style.color = "#000";
        this.style.backgroundColor = "#e0e0e0";
    }

    document.body.appendChild(closeSpan);
}

//自定义用javascript/css实现GridView行背景色交替、鼠标划过行变色，点击行变色选中t事件=====================================================================================================
//参数依次为（后两个如果指定为空值，则不会发生相应的事件）：
//GridView ID, 正常行背景色,交替行背景色,鼠标指向行背景色,鼠标点击后背景色
function GridViewColor(GridViewId, NormalColor, AlterColor, HoverColor, SelectColor) {
    //获取所有要控制的行
    if (!document.getElementById(GridViewId));
    return;

    var AllRows = document.getElementById(GridViewId).getElementsByTagName("tr");

    //设置每一行的背景色和事件，循环从1开始而非0，可以避开表头那一行
    for (var i = 1; i < AllRows.length; i++) {
        //设定本行默认的背景色
        AllRows[i].style.background = i % 2 == 0 ? NormalColor : AlterColor;

        //如果指定了鼠标指向的背景色，则添加onmouseover/onmouseout事件
        //处于选中状态的行发生这两个事件时不改变颜色
        if (HoverColor != "") {
            AllRows[i].onmouseover = function() { if (!this.selected) this.style.background = HoverColor; }
            if (i % 2 == 0) {
                AllRows[i].onmouseout = function() { if (!this.selected) this.style.background = NormalColor; }
            }
            else {
                AllRows[i].onmouseout = function() { if (!this.selected) this.style.background = AlterColor; }
            }
        }

        //如果指定了鼠标点击的背景色，则添加onclick事件
        //在事件响应中修改被点击行的选中状态
        if (SelectColor != "") {
            AllRows[i].onclick = function() {
                this.style.background = this.style.background == SelectColor ? HoverColor : SelectColor;
                this.selected = !this.selected;
            }
        }
    }
}
//动态调整iframe的高度===============================================================================================================
var getFFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers   
var FFextraHeight = getFFVersion >= 0.1 ? 16 : 0

function dyniframesize(iframename) {
    var pTar = null;
    if (document.getElementById) {
        pTar = document.getElementById(iframename);
    }
    else {
        eval('pTar = ' + iframename + ';');
    }
    if (pTar && !window.opera) {
        //begin resizing iframe   
        pTar.style.display = "block"
        if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {
            //ns6 syntax   
            pTar.height = pTar.contentDocument.body.offsetHeight + FFextraHeight;
        }
        else if (pTar.Document && pTar.Document.body.scrollHeight) {
            //ie5+ syntax   
            pTar.height = pTar.Document.body.scrollHeight;
        }
    }
}

//=============================================================================================================================================
function screenInfo() {
    var s = "";
    s += "\r\n网页可见区域宽：" + document.body.clientWidth;
    s += "\r\n网页可见区域高：" + document.body.clientHeight;
    s += "\r\n网页可见区域宽：" + document.body.offsetWidth + " (包括边线的宽)";
    s += "\r\n网页可见区域高：" + document.body.offsetHeight + " (包括边线的宽)";
    s += "\r\n网页正文全文宽：" + document.body.scrollWidth;
    s += "\r\n网页正文全文高：" + document.body.scrollHeight;
    s += "\r\n网页被卷去的高：" + document.body.scrollTop;
    s += "\r\n网页被卷去的左：" + document.body.scrollLeft;
    s += "\r\n网页正文部分上：" + window.screenTop;
    s += "\r\n网页正文部分左：" + window.screenLeft;
    s += "\r\n屏幕分辨率的高：" + window.screen.height;
    s += "\r\n屏幕分辨率的宽：" + window.screen.width;
    s += "\r\n屏幕可用工作区高度：" + window.screen.availHeight;
    s += "\r\n屏幕可用工作区宽度：" + window.screen.availWidth;
    alert(s);
}
//=======================================================================================================================================================
function getParams(strname) {
    var hrefstr, pos, parastr, para, tempstr;
    hrefstr = window.location.href;
    pos = hrefstr.indexOf("?")
    parastr = hrefstr.substring(pos + 1);
    para = parastr.split("&");
    tempstr = "";

    for (i = 0; i < para.length; i++) {
        tempstr = para[i];
        pos = tempstr.indexOf("=");
        if (tempstr.substring(0, pos) == strname) {
            return tempstr.substring(pos + 1);
        }
    }
    return null;
}
function createBackground() {

    doSelect(true);
    var bgObj = document.createElement("div");
    bgObj.setAttribute('id', 'waitbgDiv');
    bgObj.style.position = "absolute";
    bgObj.style.top = "0";
    bgObj.style.background = "#c1d8f1";
    bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=85, FinishOpacity=75, Style=0)";
    bgObj.style.opacity = "0.6";
    bgObj.style.left = "0";
    bgObj.style.width = "100%";
    bgObj.style.height = window.screen.height + "px";
    bgObj.style.zIndex = "200";
    document.body.appendChild(bgObj);
}

function clearBackground() {
    var bgObj = document.getElementById("waitbgDiv");
    if (bgObj)
        document.body.removeChild(bgObj);
    doSelect(false);
}
function doSelect(b) {
    for (var i = 0; i < document.all.length; i++) {
        var o = document.all[i];
        if (o.tagName.toLowerCase() == "select") {
            o.style.visibility = (b) ? "hidden" : "visible";
        }
    }
}
