﻿//通用获取页面控件函数
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";
    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 = document.documentElement.scrollHeight; //screen.height;
    var bgObj = document.createElement("div");
    bgObj.setAttribute('id', 'bgDiv');
    bgObj.style.position = "absolute";
    bgObj.style.bottom = "0";  //0 - document.documentElement.scrollTop + "px";
    bgObj.style.background = "#000";
    bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=0, startY=0, finishX=100, finishY=100,style=1,opacity=25,finishOpacity=70";
    bgObj.style.opacity = "0.6";
    bgObj.style.left = "0";
    bgObj.style.top = "0";
    bgObj.style.width = sWidth; //+ "px";
    bgObj.style.height =  sHeight + "px";//高度自适应
    bgObj.style.zIndex = "10000";
    document.body.appendChild(bgObj);
    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 = (250 + document.documentElement.scrollTop) + "px"; //document.body.offsetHeight / 2 + sHeight - document.body.offsetHeight;  //"50%";
    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 = "3px";
    cspan.innerHTML = "[关闭]";
    cspan.onclick = function() {
        document.body.removeChild(bgObj);
        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

//加入收藏
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);
        }
    }
}
