﻿//通用获取页面控件函数
function $(s) { if (document.getElementById) { return eval('document.getElementById("' + s + '")'); } else { return eval('document.all.' + s); } }

// 去掉字符串两端的空白字符
String.prototype.Trim = function() {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}

// 只能是数字
String.prototype.isDigit = function() {
    var s = this.Trim();
    return (s.replace(/\d/g, "").length == 0);
}

// 构造类似StringBuilder的函数(连接多个字符串时用到，很方便)
function StringBuilder(str) {
    this.tempArr = new Array();
}
StringBuilder.prototype.Append = function(value) {
    this.tempArr.push(value);
    return this;
}
StringBuilder.prototype.Clear = function() {
    this.tempArr.length = 0;
}
StringBuilder.prototype.toString = function() {
    return this.tempArr.join('');
}

//全选 取消全选
//oChk 全选input控件
//spanName 显示 权限 和 取消 的 span 标签的id
//inputName 列表项的input控件的 name
function CheckAll(oChk, spanId, inputName) {
    var chks = document.getElementsByName(inputName);
    for (var i = 0; i < chks.length; i++) {
        chks[i].checked = oChk.checked;
    }
    if (oChk.checked) {
        $(spanId).innerText = "取消";
    }
    else {
        $(spanId).innerText = "全选";
    }
}

//对全选后进行删除进行验证
//inputName 列表项的input控件的 name
function CheckDeleteItems(form, inputName) {
    var chks = document.getElementsByName(inputName);
    var flag = false;
    for (var i = 0; i < chks.length; i++) {
        if (chks[i].checked == true) {
            flag = true;
            break;
        }
    }
    if (!flag) { alert("请至少选择一项！"); return false; }
    if (confirm("您确定删除吗？")) {
        if (document.getElementById("act"))
            document.getElementById("act").value = "del";
        form.submit();
    }
    else
        return false;
}
//自定义时间比较函数(8为数字比较)
function dateDiff(d1, d2) {
    /*
    作用:比较日期大小
    参数:d1 d2 (string类型)
    字符串型: 年-月-日  类型,如 2008-10-10
    返回值: -1/0/1 
    数字型
    d1>d2 返回1
    d1=d2 返回0
    d1<d2 返回-1
    */
    d1 = d1.split("-");
    d2 = d2.split("-");
    //将时间转换为标准时间表达式：  2008-10-10，2008-01-10，2008-10-01
    if (d1[1].length == 1)
        d1[1] = "0" + d1[1].toString();
    if (d2[1].length == 1)
        d2[1] = "0" + d2[1].toString();
    if (d1[2].length == 1)
        d1[2] = "0" + d1[2].toString();
    if (d2[2].length == 1)
        d2[2] = "0" + d2[2].toString();
    var dateStr1 = d1[0].toString() + d1[1].toString() + d1[2].toString();
    var dateStr2 = d2[0].toString() + d2[1].toString() + d2[2].toString();
    if (parseInt(dateStr1) > parseInt(dateStr2)) return 1;
    if (parseInt(dateStr1) == parseInt(dateStr2)) return 0;
    if (parseInt(dateStr1) < parseInt(dateStr2)) return -1;
}


//显示浮动层 (兼容ie和firefox等主流浏览器)
function showFloatDiv(oDivId, oDivWidth, oDivHeight, ev) {
    var xy = getMouseCoordinate(ev);
    var xyArr = xy.split("-");
    var x = parseFloat(xyArr[0]);
    var y = parseFloat(xyArr[1]);
    oDiv = document.getElementById(oDivId);
    oDiv.style.display = "block";
    oDiv.style.zIndex = "100000";
    var docuLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
    var docuTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
    if (document.all) {
        oDiv.style.left = x + docuLeft - oDivWidth;
        if (y >= document.documentElement.clientHeight - oDivHeight) {
            oDiv.style.top = y + docuTop - oDivHeight;
        }
        else
            oDiv.style.top = y + docuTop;
    }
    else {
        oDiv.style.left = (x + docuLeft - oDivWidth) + "px";
        if (y >= document.documentElement.clientHeight - oDivHeight) {
            oDiv.style.top = (y + docuTop - oDivHeight) + "px";
        }
        else
            oDiv.style.top = (y + docuTop) + "px";
    }
}
function closeFloatDiv(oDivId) {
    oDiv = document.getElementById(oDivId);
    if (oDiv)
        oDiv.style.display = "none";
}
//1.取鼠标的位置(Coordinate 坐标x和y) 兼容IE,FF和其他主流浏览器 (onmouseover="getMouseCoordinate(event)")
function getMouseCoordinate(ev) {
    var e = ev ? ev : (window.event ? window.event : null);
    if (e.pageX || e.pageY) {
        return e.pageX + "-" + e.pageY;
    }
    else {
        return event.x + "-" + event.y;
    }
}

//2.通过控件获取控件在页面的位置 (oControl是控件)
function getControlCoordinateByControl(oControl) {
    var leftPos = oControl.offsetLeft;
    var topPos = oControl.offsetTop;
    var height = oControl.offsetHeight;
    while (oControl = oControl.offsetParent) {
        leftPos += oControl.offsetLeft;
        topPos += oControl.offsetTop;
    }
    return leftPos + "-" + topPos;
}

//3.通过控件自身取得控件的宽和高,返回width和height
function getControlWidthAndHeightByControl(oControl) {
    var width = oControl.offsetWidth;
    var height = oControl.offsetHeight;
    return width + "-" + height;
}


/* 鼠标拖动层  (ie7测试通过) */

//当前存放移动元素的容器
var container = null;

//当前正在移动的元素
var draggingElement = null;

//存放拖拉过程的点位置
var oldLeft = 0;
var oldTop = 0;

//是否要开启透明效果
var enableOpacity = false; // 默认不允许

//允许拖动容器
function enableDragElements() {
    for (var i = 0; i < document.all.length; i++) {
        if (document.all[i].dragType && document.all[i].dragType == "container") {
            container = document.all[i];
            container.style.height = "100%";
            container.onmousedown = doContainerMouseDown;
            container.onmouseup = doContainerMouseUp;
            container.onmousemove = doContainerMouseMove;
        }
        if (document.all[i].dragType && document.all[i].dragType == "element") {
            document.all[i].style.position = "absolute";
            document.all[i].style.left = document.all[i].style.left ? document.all[i].style.left : 0;
            document.all[i].style.top = document.all[i].style.top ? document.all[i].style.top : 0;
        }
    }
}

//内部方法
function doContainerMouseDown() {
    draggingElement = event.srcElement;
    if (draggingElement == null) {
        return;
    }
    else {
        oldLeft = event.x;
        oldTop = event.y;
        while (draggingElement && (!draggingElement.dragType || draggingElement.dragType != "element")) {
            draggingElement = draggingElement.parentElement;
        }
        container = draggingElement;
        while (draggingElement && (!container.dragType || container.dragType != "container")) {
            container = container.parentElement;
        }
    }
}

function doContainerMouseMove() {
    if (draggingElement && draggingElement.dragType && draggingElement.dragType == "element") {
        if (draggingElement != null && enableOpacity) {
            draggingElement.style.filter = "Alpha(opacity=30)"; // 滤镜 
        }
        var left = parseInt(draggingElement.style.left.replace("px", ""));
        var top = parseInt(draggingElement.style.top.replace("px", ""));

        draggingElement.style.left = left + event.x - oldLeft;
        draggingElement.style.top = top + event.y - oldTop;
        oldLeft = event.x;
        oldTop = event.y;
    }
}

function doContainerMouseUp() {
    if (draggingElement != null && enableOpacity) {
        draggingElement.style.filter = "Alpha(opacity=100)"; // 滤镜 
    }
    draggingElement = null;
}


//在弹出窗口中显示其他网页 背景模态========================================start

/// <summary>
/// 弹窗出模态页面
/// </summary>
/// <param name="url">目标链接地址</param>
/// <param name="w">弹出窗体的宽</param>
/// <param name="h">弹出窗体的高</param>
function ShowModalPage(url, w, h) {
    var msgw, msgh, bordercolor;
    msgw = w; //Width
    msgh = h; //Height 
    titleheight = 25 //title Height
    bordercolor = "#336699"; //boder color
    titlecolor = "#99CCFF"; //title color
    var sWidth, sHeight;
    sWidth = "100%"; //document.body.offsetWidth;
    sHeight = screen.height;
    var bgDiv = document.createElement("div");
    bgDiv.setAttribute('id', 'bgDiv');
    bgDiv.style.position = "absolute";
    bgDiv.style.top = "0px";
    bgDiv.style.left = "0px";
    bgDiv.style.background = "#000";
    bgDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=0, startY=0, finishX=100, finishY=100,style=1,opacity=35,finishOpacity=70";
    bgDiv.style.opacity = "0.6";
    bgDiv.style.width = "100%";
    bgDiv.style.height = document.documentElement.scrollHeight + "px";
    bgDiv.style.zIndex = "10000";
    var bgObj = document.createElement("iframe");
    bgObj.style.bottom = 0 - document.documentElement.scrollTop + "px";
    bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=0, startY=0, finishX=100, finishY=100,style=1,opacity=0,finishOpacity=70";
    bgObj.style.width = "100%";
    bgObj.style.height = "100%";
    bgDiv.appendChild(bgObj);
    document.body.appendChild(bgDiv);
    var msgObj = document.createElement("div");
    msgObj.setAttribute("id", "alertDiv");
    msgObj.setAttribute("align", "center");
    msgObj.style.background = "white";
    msgObj.style.border = "0px solid " + bordercolor;
    msgObj.style.position = "absolute";
    msgObj.style.left = "50%";
    msgObj.style.top = (230 + parent.document.documentElement.scrollTop) + "px"; //"50%";//(window.innerHeight-h)/2+"px";
    msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
    msgObj.style.marginLeft = (0 - msgw / 2) + "px";
    msgObj.style.marginTop = (0 - msgh / 2) + "px";
    msgObj.style.width = msgw + "px";
    msgObj.style.textAlign = "center";
    msgObj.style.lineHeight = "25px";
    msgObj.style.zIndex = "10001";
    var title = document.createElement("h4");
    title.setAttribute("id", "alertTitle");
    title.setAttribute("align", "right");
    title.style.width = "auto";
    title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
    var cspan = document.createElement("span");
    cspan.setAttribute("id", "close");
    cspan.setAttribute("align", "right");
    cspan.style.cursor = "pointer";
    cspan.style.margin = "10px";
    cspan.style.color = "#fff";
    cspan.style.fontSize = "18px";
    cspan.innerHTML = "×";
    cspan.className = "closeSpan";
    cspan.onclick = function() {
    document.body.removeChild(bgDiv);
    document.body.removeChild(msgObj);
    }
    title.appendChild(cspan);
    msgObj.appendChild(title);
    document.body.appendChild(msgObj);
    var txt = document.createElement("p");
    txt.style.margin = "0em 0"
    txt.style.padding = "0"
    txt.setAttribute("id", "alertTxt");
    var ifrm = document.createElement("iframe");
    ifrm.id = "openWindow";
    ifrm.src = encodeURI(url);
    ifrm.style.width = "100%";
    ifrm.style.border = "0";
    ifrm.setAttribute("frameborder", "0", 0);
    ifrm.setAttribute("scrolling", "no", 0);
    txt.appendChild(ifrm);
    document.getElementById("alertDiv").appendChild(txt);
    window.setInterval(function() { reinitIframe("openWindow"); }, 200);
}
function reinitIframe(ctlId) {
    try {
        var oIframe = document.getElementById(ctlId);
        var brHeight = oIframe.contentWindow.document.body.scrollHeight;
        var drHeight = oIframe.contentWindow.document.documentElement.scrollHeight;
        var rheight = Math.max(brHeight, drHeight);
        oIframe.height = rheight;
    }
    catch (ex) { }
}
//在弹出窗口中显示其他网页 背景模态========================================end

//弹出进度条div==================================================start
function ShowProgressBarPage(leftValue,tipInfo) {
    var sWidth, sHeight;
    sWidth = "100%"; //document.body.offsetWidth;
    sHeight = screen.height;
    var bgDiv = document.createElement("div");
    bgDiv.setAttribute('id', 'bgDiv');
    bgDiv.style.position = "absolute";
    //bgDiv.style.bottom = 0 - document.documentElement.scrollTop + "px";
    bgDiv.style.background = "#333";
    bgDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=0, startY=0, finishX=100, finishY=100,style=1,opacity=35,finishOpacity=70";
    bgDiv.style.opacity = "0.6";
    bgDiv.style.top = "0";
    bgDiv.style.left = "0";
    bgDiv.style.width = "100%";
    bgDiv.style.height = document.documentElement.scrollHeight + "px";
    bgDiv.style.zIndex = "10000";
    var bgObj = document.createElement("iframe");
    //bgObj.style.bottom = 0 - document.documentElement.scrollTop + "px";
    bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=0, startY=0, finishX=100, finishY=100,style=1,opacity=0,finishOpacity=70";
    bgObj.style.top = "0";
    bgObj.style.left = "0";
    bgObj.style.width = "100%";
    bgObj.style.height = "100%";
    bgDiv.appendChild(bgObj);
    document.body.appendChild(bgDiv);
    var msgObj = document.createElement("div");
    msgObj.style.position = "absolute";
    msgObj.style.left = "30%";// leftValue + "%";
    msgObj.style.top =  (200 + document.documentElement.scrollTop) + "px";
    msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
    msgObj.className = "imgProgressBar";
    msgObj.innerHTML = "<span>" + tipInfo + "</span>";
    msgObj.style.width = 500 + "px";
    msgObj.style.height = 70 + "px";
    msgObj.style.zIndex = "10002";
    document.body.appendChild(msgObj);
}
//弹出进度条div==================================================end

//打开新窗口,居中显示
function openNewWinFixCenter(pgUrl, pgName, wWidth, wHeight, isScroll) {
    var newWin = null;
    var leftPos = (screen.width) ? (screen.width - wWidth) / 2 : 0;
    var topPos = (screen.height) ? (screen.height - wHeight) / 2 : 0;
    var winSetting = 'height=' + wHeight + ',width=' + wWidth + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=' + isScroll + ', menubar=no,resizable=yes,status=no,titlebar=no,toolbar=no,directories=no';
    newWin = window.open(pgUrl, pgName, winSetting);
}

//创建div，显示指定标题与内容==================================================start
function createDivToShowTxt(obj, txtTitle, txtContent, oDivWidth, ev) {
    //判断是否存在该div
    var oDiv = document.getElementById("newDivToShowTxt");
    if (oDiv != null)
        oDiv.parentNode.removeChild(oDiv);    

    //创建div
    oDiv = document.createElement("div");
    oDiv.id = "newDivToShowTxt";
    oDiv.style.width = oDivWidth + "px";

    //调整div位置
    var t = obj.offsetTop;
    var l = obj.offsetLeft;
    var w = obj.offsetWidth;
    var h = obj.offsetHeight;
    while (obj = obj.offsetParent) {
        t += obj.offsetTop;
        l += obj.offsetLeft;
    }
    oDiv.style.position = "absolute";
    oDiv.style.top = (h + t) + "px";
    var dw = document.documentElement.offsetWidth;
    oDiv.style.left = (dw - oDivWidth) < (w + l) ? (dw - oDivWidth) : (w + l) + "px"; //保证弹出框不出右边界
    oDiv.style.zIndex = "100001";
    //iframe
    var oIframe = document.createElement("iframe");
    oIframe.style.position = "absolute";
    oIframe.top = "0px";
    oIframe.left = "0px";
    oIframe.width = "100%";
    oIframe.height = "100%";
    oDiv.appendChild(oIframe);
    document.body.appendChild(oDiv); 
    //contentDiv
    var showDiv = document.getElementById("newDivToShowTxt2");
    if (showDiv)
        showDiv.parentNode.removeChild(showDiv);
    showDiv = document.createElement("div");
    showDiv.id = "newDivToShowTxt2";
    showDiv.className = "helpDivStyle";
    showDiv.style.width = oDivWidth + "px";
    showDiv.style.height = "auto";
    showDiv.style.position = "absolute";
    showDiv.style.top = oDiv.style.top;
    showDiv.style.left = oDiv.style.left;
    showDiv.style.zIndex = "100002";
    //标题
    var oSpan = document.createElement("span");
    oSpan.innerHTML = txtTitle;
    oSpan.className = "helpTitle";
    showDiv.appendChild(oSpan);
    //关闭按钮
    var close = document.createElement("span");
    close.className = "helpColsed";
    close.title = "关闭";
    close.onclick = function() {
        closeFloatDiv('newDivToShowTxt');
        closeFloatDiv('newDivToShowTxt2');
    };
    showDiv.appendChild(close);
    //内容
    var oDivContent = document.createElement("div");
    oDivContent.className = "helpContent";
    oDivContent.innerHTML = txtContent;
    showDiv.appendChild(oDivContent);
    document.body.appendChild(showDiv);
    document.getElementById("newDivToShowTxt").style.height =document.getElementById("newDivToShowTxt2").clientHeight;
}
//创建div，显示指定标题与内容==================================================end

function ShowOneLineTips(obj, content, position, index) {
    var totalDiv = document.createElement("div");
    totalDiv.setAttribute('id', 'totalDiv' + index);
    totalDiv.className = "PopUpPageTotal" + position;
    totalDiv.style.position = "absolute";
    var tAndl = getControlCoordinateByControl(obj);
    var t = parseInt(tAndl.split('-')[0]);
    var l = parseInt(tAndl.split('-')[1]);
    var w = obj.offsetWidth;
    var h = obj.offsetHeight;

    var dw = document.documentElement.offsetWidth;

    if (position == "Bottom") {
        totalDiv.style.top = (t + h + 2 - 47) + "px";
        totalDiv.style.left = (l + w / 2 + 27) + "px";
    } else if (position == "Top") {
        totalDiv.style.top = (t - 495) + "px";
        totalDiv.style.left = (l + w / 2 + 440) + "px";
    } else if (position == "Left") {
        totalDiv.style.top = (t + h / 2) + "px";
        totalDiv.style.left = (l + w / 2) + "px";
    } else if (position == "Right") {
        totalDiv.style.top = (t + h - 232) + "px";
        totalDiv.style.left = (l + w / 2 + 277) + "px";
    }

    var contentDiv = document.createElement("div");
    contentDiv.setAttribute('id', 'contentDiv' + index);
    contentDiv.className = "PopUpPageLeft";
    contentDiv.innerHTML = content;

    var closeDiv = document.createElement("div");
    closeDiv.setAttribute('id', 'closeDiv' + index);
    closeDiv.className = "PopUpPageRight";
    closeDiv.innerHTML = "×";
    closeDiv.style.cursor = "pointer";
    closeDiv.onclick = function() {
        document.body.removeChild(totalDiv);
    }


    totalDiv.appendChild(contentDiv);
    totalDiv.appendChild(closeDiv);
    document.body.appendChild(totalDiv);
}

//解决火狐不支持outerHTML属性的问题=================================================start
if (typeof (HTMLElement) != "undefined" && !window.opera) {
    HTMLElement.prototype.__defineGetter__("outerHTML", function() {
        var a = this.attributes, str = "<" + this.tagName, i = 0; for (; i < a.length; i++)
            if (a[i].specified)
            str += " " + a[i].name + '="' + a[i].value + '"';
        if (!this.canHaveChildren)
            return str + " />";
        return str + ">" + this.innerHTML + "</" + this.tagName + ">";
    });
    HTMLElement.prototype.__defineSetter__("outerHTML", function(s) {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df = r.createContextualFragment(s);
        this.parentNode.replaceChild(df, this);
        return s;
    });
    HTMLElement.prototype.__defineGetter__("canHaveChildren", function() {
        return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());
    });
}
//解决火狐不支持outerHTML属性的问题=================================================end

//限制某些输入框的输入长度
function isOver(sText, len) {
    var intlen = sText.value.length;
    if (intlen > len) {
        sText.value = sText.value.substr(0, len);
    }
}

//加入收藏
function addFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}
//设为首页
function setHome(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
}


//用于首页控件切换
function showAnotherContr(obj,lis) {
    var liArray = lis.split('&');
        for(var i=0; i<liArray.length; i++)
        {
            if(liArray[i] == obj.id)
            {
                $(liArray[i]).className = "CurrentTab";
                $(liArray[i] + "Div").style.display = "";
            }
            else
            {
                $(liArray[i]).className = "ProcessTab";
                $(liArray[i] +"Div").style.display = "none";
            }
        }
    }
