﻿var isBrowser = function(vision) {
    if (typeof vision == 'undefiend')
        return false;
    vision = vision.toUpperCase();
    var isIE = !!window.ActiveXObject;
    var isIE6 = isIE && !window.XMLHttpRequest;
    var isIE8 = isIE && !!document.documentMode;
    var isIE7 = isIE && !isIE6 && !isIE8;
    if (isIE) {
        if (isIE6) {
            return vision === 'IE6';
        } else if (isIE8) {
            return vision === 'IE8';
        } else if (isIE7) {
            return vision === 'IE7';
        }
    }
    else {
        return vision === 'FF';
    }
}

//弹出窗口自动调整大小
function win_onLoad()
{
    var table = document.body.getElementsByTagName("table");

    if (!(table.length > 0))
        return ;
    
    var tb = table[0];
    
    for (var i = 0; i < table.length; i++ ) 
    {
        if(table[i].id == "tbWindow")
        {
            tb = table[i];
            break;
        }
    }

    var width = 732;
    if (tb != null)
    {
        width = tb.width-20;
    }
    //   alert(tb.offsetHeight);
    var height = tb.offsetHeight;
    // alert(width);
    width = eval(width * 1 + 30);
    height = isBrowser("IE6") ? eval(height * 1 + 50) :  eval(height * 1 + 95);

    var sWidth = screen.width;
    var sHeight = screen.height;

    var left = sWidth - ((sWidth - width) / 2 + width);
    if (left < 0)
    {
        left = 0;
    }
    var top = sHeight - ((sHeight - height) / 2 + height);
    if (top < 0)
    {
        top = 0;
    }
    //alert(width);
    if (width > screen.width - 50)
        width = screen.width - 50;

    if (height > screen.height - 50)
        height = screen.height - 50;
    //alert(width);
    window.moveTo(left, top);
    window.resizeTo(width, height);
}

// 如果复选框有选中的项,返回true, 否则返回false;
// 示例: if (!HaveItemSeleted('cbSelectOne')) {  alert("请选择要删除的用户!");  return false;   }
function HaveItemSeleted(id) 
{
    var objects = document.getElementsByTagName("INPUT");

    for (var i = 0; i < objects.length; i++) 
    {
        if (objects[i].type == "checkbox" && objects[i].id.indexOf(id) > -1 && objects[i].checked) 
        {
            return true;
        }
    }

    return false;
} 

// 全选或者全不选
// obj 全选复选框对象, id 选择项id
// 示例: <input type="checkbox" id="chkSelectAll" onclick="SelectedAll(this, 'cbSelectOne') " />
function SelectedAll(obj, id) 
{
    var objects = document.getElementsByTagName("INPUT");

    for (var i = 0; i < objects.length; i++) 
    {
        // alert("objects[i].id=" + objects[i].id +";id="+id);
        // alert(objects[i].id.indexOf(id));
        if (objects[i].type == "checkbox" && objects[i].id.indexOf(id) > -1) 
        {
            objects[i].checked = obj.checked;
        }
    }
}

// 打开一个新窗口
function OpenWindow(url, width, height)
{
    var top = (window.screen.height - height - 25) / 2; 
    var left = (window.screen.width - width) / 2;
    //window.open(url, '_blank', 'height=' + height + ',width=' + width + ',left=10,top=10,resizable=yes,scrollbars=yes');
    window.open(url, "_blank", "height=" + height + ", width=" + width + ", top=" + top + ",left=" + left + ", toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
}

//打开一个模态窗口
function OpenWindowShowModalDialog(url, width, height, param)
{
    if (url.indexOf("?") > 0)
    {
        url = url + "&rand=" + Math.round();
    }
    else
    {
        url = url + "?rand=" + Math.round();
    }

    var dialogWidth = width + "px"; //模态窗口宽度
    var dialogHeight = height + "px"; //模态窗口高度
    var dialogTop = (window.screen.height - height) / 2 + "px";
    var dialogLeft = (window.screen.width - width) / 2 + "px";

    //return window.showModalDialog(url, null, "dialogWidth=" + dialogWidth + ",dialogHeight=" + dialogHeight + ",dialogLeft=" + dialogLeft + ",dialogTop=" + dialogTop + ",toolbar=no,resizable=no,scrollbars=yes,location=no, status=no");
    return window.showModalDialog(url, param, "dialogWidth:" + dialogWidth + ";dialogHeight:" + dialogHeight + ";dialogLeft:" + dialogLeft + ";dialogTop:" + dialogTop + ";toolbar:no;resizable:no;scrollbars:yes;location:no;status:no;");
}

//判断一个字符串里面，是否包含非ASCII码的字符。
function CheckStringIsNonASCII(str) {
    //----      /[^\x00-\xff]/ig;
    var exp = new RegExp("[^\x00-\xff]", "ig");
    return exp.test(str)
}

function CheckIsPhoneNumber(str) {
    var exp = new RegExp("^(13|15|18)([0-9]){9}$");
    return exp.test(str);
}

///去左空格
function LTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1)
    {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
        {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}

///去右空格
function RTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1)
    {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
        {
            i--;
        }
        s = s.substring(0, i + 1);
    }
    return s;
}
//去左右空格
function Trim(str)
{
    return RTrim(LTrim(str));
}

//显示提示信息
function ShowMessage(e, str)
{
    var oThis = arguments.callee;
    if (!str)
    {
        oThis.sug.style.visibility = 'hidden';
        document.onmousemove = null;
        return;
    }
    
    if (!oThis.sug)
    {
        var div = document.createElement('div'), css = 'top:0; left:0; position:absolute; z-index:100; visibility:hidden';
        div.style.cssText = css;
        div.setAttribute('style', css);
        
        var sug = document.createElement('div'), css = 'font:normal 12px/16px "宋体"; white-space:nowrap; color:black; padding:3px; position:absolute; left:0; top:0; z-index:10; background:#FFFFE1; border:1px solid black';
        sug.style.cssText = css;
        sug.setAttribute('style', css);
        
        var dr = document.createElement('div'), css = 'position:absolute; top:3px; left:3px; background:#333; filter:alpha(opacity=50); opacity:0.5; z-index:9';
        dr.style.cssText = css;
        dr.setAttribute('style', css);
        
        var ifr = document.createElement('iframe'), css = 'position:absolute; left:0; top:0; z-index:8; filter:alpha(opacity=0); opacity:0';
        ifr.style.cssText = css;
        ifr.setAttribute('style', css);
        
        div.appendChild(ifr);
        div.appendChild(dr);
        div.appendChild(sug);
        div.sug = sug;
        document.body.appendChild(div);
        oThis.sug = div;
        oThis.dr = dr;
        oThis.ifr = ifr;
        div = dr = ifr = sug = null;
    }
    
    var e = e || window.event, obj = oThis.sug, dr = oThis.dr, ifr = oThis.ifr;
    obj.sug.innerHTML = str;

    var w = obj.sug.offsetWidth, h = obj.sug.offsetHeight, dw = document.documentElement.clientWidth || document.body.clientWidth; dh = document.documentElement.clientHeight || document.body.clientHeight;
    var st = document.documentElement.scrollTop || document.body.scrollTop, sl = document.documentElement.scrollLeft || document.body.scrollLeft;
    var left = e.clientX + sl + 17 + w < dw + sl && e.clientX + sl + 15 || e.clientX + sl - 8 - w, top = e.clientY + st + 17 + h < dh + st && e.clientY + st + 17 || e.clientY + st - 5 - h;
    obj.style.left = left + 10 + 'px';
    obj.style.top = top + 10 + 'px';
    dr.style.width = w + 'px';
    dr.style.height = h + 'px';
    ifr.style.width = w + 3 + 'px';
    ifr.style.height = h + 3 + 'px';
    obj.style.visibility = 'visible';
    document.onmousemove = function(e)
    {
        var e = e || window.event, st = document.documentElement.scrollTop || document.body.scrollTop, sl = document.documentElement.scrollLeft || document.body.scrollLeft;
        var left = e.clientX + sl + 17 + w < dw + sl && e.clientX + sl + 15 || e.clientX + sl - 8 - w, top = e.clientY + st + 17 + h < dh + st && e.clientY + st + 17 || e.clientY + st - 5 - h;
        obj.style.left = left + 'px';
        obj.style.top = top + 'px';
    }
}
