﻿//判断浏览器
function getOs() {
    if (navigator.userAgent.indexOf("MSIE") > 0) return 1;
    if (navigator.userAgent.indexOf("Opera") > 0) return 2;
    if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) return 3;
    if (isSafari = navigator.userAgent.indexOf("Safari") > 0) return 4;
    if (isCamino = navigator.userAgent.indexOf("Camino") > 0) return 5;
    if (isMozilla = navigator.userAgent.indexOf("Gecko") > 0) return 6;
    return 0;
}
//返回指定ID的标签
function getObj(element) {
    return document.getElementById(element);
}

//去除字符串首尾空格
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, "");
}
//打开新页面
function openwindow(url) {
    var win = window.open(url);
    if (!win)
        alert("无法打开新页面\n请关闭弹出窗口阻止程序");
}
//得到页面信息
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    }
    else {
        pageWidth = xScroll;
    }

    //get page offset
    var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
    var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, scrollLeft, scrollTop)
    return arrayPageSize;
}
//注册事件
function regEvent(obj, type, fn) {
    type = type.replace("on", "");
    if (obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() { obj['e' + type + fn](window.event); }
        obj.attachEvent('on' + type, obj[type + fn]);
    }
    else
        obj.addEventListener(type, fn, false);
}
function clsEvent(obj, type, fn) {
    type = type.replace("on", "");
    if (obj.detachEvent) {
        obj.detachEvent('on' + type, obj[type + fn]);
        obj[type + fn] = null;
    }
    else
        obj.removeEventListener(type, fn, false);
}
function doEvent(obj, type) {
    type = type.replace("on", "");
    if (Client.Engine.name == "ie") {
        obj.fireEvent('on' + type);
    }
    else {
        var e = document.createEvent("Events");
        e.initEvent(type, true, false);
        obj.dispatchEvent(e);
    }
}
//为指定标签添加或删除样式
function removeClassName(el, className) {
    el.className = el.className.replace(className, "").trim();
}
function addClassName(el, className) {
    removeClassName(el, className);
    el.className = (el.className + " " + className).trim();
}
//下拉框控制
function addOption(obj, text, key) {
    var oOption = new Option(text, key);

    if (getOs() == 3)
        obj.appendChild(oOption);
    else
        obj.options.add(oOption);
}
function clearOption(obj) {
    while (obj.options.length != 0) {
        if (getOs() == 3)
            obj.remove(0);
        else
            obj.options.remove(0);
    }
}
//获得页面参数
function getParameter(varName) {
    var query = location.search;
    if (query != null || query != "") {
        query = query.replace(/^\?+/, "");
        var qArray = query.split("&");
        var len = qArray.length;
        if (len > 0) {
            for (var i = 0; i < len; i++) {
                var sArray = qArray[i].split("=", 2);
                if (sArray[0] && sArray[1] && sArray[0] == varName) {
                    return unescape(sArray[1]);
                }
            }
        }
    }
    return '';
}
function tryParseInt(str) {
    if (str && parseInt(str))
        return parseInt(str);
    else
        return 0;
}