﻿
//check browser type
var browser = {
    IE: !!(window.attachEvent && !window.opera),
    Opera: !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/index.html') > -1,
    Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1, //firefox
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}

//getElementById
function $(idStr) {
    if (idStr != null) {
        element = document.getElementById(idStr);
        return element;
    }
    return false;
}
//getElementsByName
function $N(nameStr) {
    if (nameStr != null) {
        var elements = new Array();
        elements = document.getElementsByName(nameStr);
        return elements;
    }
    return false;
}
//getElementsByTagName
function $T(nameStr) {
    if (nameStr != null) {
        var elements = new Array();
        elements = document.getElementsByTagName(nameStr);
        return elements;
    }
    return false;
}

//popup layer
function popupLayer(ev, id, sw, sh, cobj) {
    var e = window.event || ev;
    this.title = '';
    this.bottom = '';
    this.content = '';
    cobj = cobj || '__content__';
    var sw1 = sw == null ? 350 : sw;

    this.show = function() {
        var layer = '<div style="margin: 0px; padding: 0px; cursor: move; background: #255ba5; padding-left: 4px; font-size: 12px; color: #fff; height: 25px; line-height: 25px; border-left: 1px solid #255ba5; border-top: 1px solid #255ba5; border-right: 1px solid #255ba5;" onmousedown="oMove(parentNode,event)"><span style="float: right; padding-right: 3px;"><a href="javascript:;" onclick="closeLayer(\'' + id + '\')" style="color: #fff;">关闭</a></span><span id=\"__title__\">' + this.title + '</span></div>';
        layer += '<div style="margin: 0; background: #fff; font-size: 12px; border-left: 1px solid #255ba5; border-right: 1px solid #255ba5; clear: both; overflow: hidden; padding: 3px;" id="' + cobj + '">' + this.content + '</div>';
        var bb = '';
        if (this.bottom == '') bb = 'height: 20px; ';
        layer += '<div style="' + bb + 'background: #255ba5; text-align: center; padding: 4px; border-left: 1px solid #255ba5; border-bottom: 1px solid #255ba5; border-right: 1px solid #255ba5; margin: 0;" id="__bottom__">' + this.bottom + '</div>';

        if (!document.getElementById(id)) {
            var _div = document.createElement('div');
            _div.id = id;
            _div.style.left = (((window.innerWidth || document.body.offsetWidth) - sw1) / 2) + 'px';
            _div.style.bottom = '50%';
            _div.style.marginBottom = '-150px';
            _div.style.width = sw1 + 'px';
            if (sh != null) _div.style.height = sh + 'px';
            _div.style.background = '#fff';
            _div.style.position = 'absolute';
            _div.style.zIndex = '999';
            _div.innerHTML = layer;
            document.body.appendChild(_div);
        }
        var offTop = document.getElementById(id).offsetTop;
        if (offTop < 0) {
            if (offTop > -100) {
                offTop = -100;
            } else {
                offTop -= 100;
            }
            var marBot = -150;
            marBot += offTop;
            document.getElementById(id).style.marginBottom = marBot + 'px';
        } else {
            if (offTop > 300) document.getElementById(id).style.marginBottom = '-0px';
            if (offTop < 100) document.getElementById(id).style.marginBottom = '-220px';
        }
        document.getElementById(id).style.height = document.getElementById(id).offsetHeight + 'px';

    }
}
//remove popup layer
function closeLayer(id) {
    document.body.removeChild(document.getElementById(id));
}
//move popup layer
function oMove(obj, even) {
    var otop, oleft;
    var e = window.event || even;
    otop = e.clientY - obj.offsetTop;
    oleft = e.clientX - obj.offsetLeft;

    if (document.all) obj.setCapture();

    obj.onmousemove = function(e) {
        e = e || window.event;
        obj.style.left = (e.clientX - oleft) + 'px';
        obj.style.top = (e.clientY - otop) + 'px';
    }
    obj.onmouseup = function() {
        obj.onmousemove = null;
        if (document.all) obj.releaseCapture();
    }
}

//get upload ui
function getUploadUI(id, type, prev) {
    //id : 将上传结果地址返回给该ID
    //type : 上传文件类型
    //prev : 预览元素ID
    prev = prev || '';
    var html = '<div style="margin: 3px; font-weight: bold;">选择要上传的文件：</div>';
    html += '<div style="padding: 3px;"><iframe src="/admin/editor/filemanager/connectors/upload.html?tt=' + id + '&tp=' + type + '&prev=' + prev + '" width="330px" height="25px" style="overflow: hidden; background: transparent;" scrolling="no" frameborder="0" framewidth="0" frameheight="0"></iframe></div>';
    if (browser.IE) {
        if (prev != '' && type == 'Image') {
            html += '&nbsp;<img style="width: 150px; height: 120px; border: outset 1px #ddd; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);" alt="" id="prev" />';
        }
    }
    return html;
}
//check all checkbox
function checkAll(e, name) {
    var sub = $N(name);
    for (var i = 0; i < sub.length; i++) {
        if (!sub.item(i).disabled) sub.item(i).checked = e.checked;
    }
}
//get selected checkbox
function getAllSelected(name, split) {
    var sub = $N(name);
    var tmp = '';
    split = split || ',';
    for (var i = 0; i < sub.length; i++) {
        if (sub.item(i).checked) tmp += split + sub.item(i).value;
    }
    if (tmp != '') tmp = tmp.substring(split.length);
    return tmp;
}
//alt color
function setBGColor(e, color) {
    e.style.background = color;
}

//框架自适应高度
function SetWinHeight(obj) {
    var win = obj;
    if (document.getElementById) {
        if (win && !window.opera) {
            if (win.contentDocument && win.contentDocument.body.offsetHeight) {
                win.height = win.contentDocument.body.offsetHeight;
            }
            else if (win.Document && win.Document.body.scrollHeight) {
                win.height = win.Document.body.scrollHeight;
            }
        }
    }
}

//打开颜色选择器
function getColor(e, ic) {
    var m = window.showModalDialog('js/color.html', ic, 'dialogWidth=254px; dialogHeight=223px; center=yes; help=no; status=no; scroll=no');
    if (m != null && m != undefined) {
        e.value = m;
        e.style.backgroundColor = m;
    }
}

//按语言隐藏与显示
function changeLanguage(value) {
    var url = document.URL;
    var purl = '';
    var pr = url;

    if (pr.indexOf('?') == -1) {
        purl = '?lang=' + value;
    } else {
        if (pr.indexOf('lang') == -1) {
            purl = '&lang=' + value;
        } else {
            pr = pr.replace(/([\?&]lang=)(?:\d+|en|zh)/, '$1' + value);
        }
    }
    purl = pr + purl;
    self.location.href = purl;
}
function setDisplayStyle(value, type) {
    var re = new RegExp('^lang_\\d+$', 'gi');
    if (type == null) type = 'TR';
    var nx = $T(type);
    var dis = '';
    if (value == '0') {
        dis = '';
    } else {
          dis = 'none';
    }

    for (var i = 0; i < nx.length; i++) {
        if (nx.item(i).id.match(re)) {
            $(nx.item(i).id).style.display = dis;
        }
    }
}

function isMoreThan(date1, date2) {
    if (date1 == null || date2 == null) return false;
    date1 = date1.toString();
    date2 = date2.toString();

    var dt = date1.split(' ');
    var dt1 = dt[0].split('-');
    var dt2 = [];
    if (dt1.length == 2) dt2 = dt[1].split(':');

    var d1 = null;

    if (dt2.length == 3) d1 = new Date(dt1[0], dt1[1], dt1[2], dt2[0], dt2[1], dt2[2]);
    if (dt2.length == 2) d1 = new Date(dt1[0], dt1[1], dt1[2], dt2[0], dt2[1]);
    if (dt2.length < 2) d1 = new Date(dt1[0], dt1[1], dt1[2]);

    dt = date2.split(' ');
    dt1 = dt[0].split('-');
    if (dt1.length == 2) dt2 = dt[1].split(':'); else dt2 = [];

    if (dt2.length == 3) d2 = new Date(dt1[0], dt1[1], dt1[2], dt2[0], dt2[1], dt2[2]);
    if (dt2.length == 2) d2 = new Date(dt1[0], dt1[1], dt1[2], dt2[0], dt2[1]);
    if (dt2.length < 2) d2 = new Date(dt1[0], dt1[1], dt1[2]);

    return d2 >= d1;
}

//输入日期并格式化
function SetDate(e, even) {
    even = even || window.event;

    var t = '';
    if (even.keyCode != 8 && even.keyCode != 46) {
        if (e.value.match(/^20[0-9]{2}$/g)) {
            t = e.value + '-';
            e.value = t;
        } else {
            if (!e.value.match(/^20[0-9]{2}\-$/g)) {
                if (e.value.length == 4) {
                    t = '';
                    e.value = t;
                }
            }
        }

        if (e.value.match(/^20[0-9]{2}\-(?:[0-9])$/g)) {
            var d = e.value.split('-');
            var d1 = parseInt(d[1], 10);

            if (d1 >= 2) {
                t = d[0] + '-' + '0' + d1 + '-';
                e.value = t;
            }
        }

        if (e.value.match(/^20[0-9]{2}\-(?:[0-1][0-9])$/g)) {
            var d = e.value.split('-');
            var d1 = parseInt(d[1], 10);

            if (d1 == 0) {
                t = d[0] + '-0';
                e.value = t;
            } else {
                if (d1 <= 12) {
                    t = e.value + '-';
                } else {
                    t = e.value.substring(0, 6);
                }
                e.value = t;
            }
        }

        if (!e.value.match(/^20[0-9]{2}\-(?:[0-1][0-9])\-(?:[1-31])$/g)) {
            var d = e.value.split('-');
            var d1 = parseInt(d[2], 10);

            if (d1.length == 1) {
                e.value = d[0] + '-' + d[1] + '-' + '0' + d1;
            } else {
                if (d1 >= 32) {
                    if (d1.length >= 3) {
                        t = e.value.substring(0, 10);
                    } else {
                        t = e.value.substring(0, 9);
                    }
                    e.value = t;
                }
            }
        }
    }
}
