var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));

function InitAjax() {
    var http_request = false;

    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    return http_request;
}

// 全选
function checkAll(form) {
    for (var i=0;i<form.elements.length;i++) {
        var e = form.elements[i];
        if (e.name != 'chkall')
        e.checked = form.chkall.checked;
    }
}

// 选择
function chooseItem(obj,bgcolor) {
    var img = obj.getElementsByTagName("img");

    var inputs = obj.getElementsByTagName("input");
    var inputs_num = inputs.length;
    for (var i=0; i<inputs_num ; i++) {
        if (inputs[i].type == 'checkbox') {
            if (inputs[i].checked == false) {
                inputs[i].checked = true;
                img[0].src = "./images/control/check_true.gif";
                obj.style.backgroundColor=bgcolor;
            } else {
                inputs[i].checked = false;
                img[0].src = "./images/control/check_false.gif";
                obj.style.backgroundColor='';
            }
        }
    }
}

function chooseAllItem(form,bgcolor) {
    var trObj;
    for (var i=0;i<form.elements.length;i++) {
        var e = form.elements[i];
        if (e.type == 'checkbox' && e.name != 'chkall') {
            e.checked = form.chkall.checked;
            trObj = e.parentNode.parentNode;
            if (trObj.nodeName == 'TR') {
                var imgs = trObj.getElementsByTagName("img");

                var inputs = trObj.getElementsByTagName("input");
                var inputs_num = inputs.length;
                for (var j=0; j<inputs_num ; j++) {
                    if (inputs[j].type == 'checkbox') {
                        inputs[j].checked = e.checked;
                        if (inputs[j].checked == true) {
                            imgs[0].src = "./images/control/check_true.gif";
                            trObj.style.backgroundColor=bgcolor;
                        } else {
                            imgs[0].src = "./images/control/check_false.gif";
                            trObj.style.backgroundColor='';
                        }
                    }
                }
            }
        }
    }
}

// 跳转
function gotoUrl(url, msg)
{
    if ('' != msg) {
        if(confirm(msg)) {
            window.location = url;
            return false;
        }
    } else {
        window.location = url;
        return false;
    }
}

// 提交
function actionSubmit(form, url, msg) {
    if('' != msg) {
        if(confirm(msg)) {
            form.action = url;
            form.submit();
            return false;
        } else {
            return false;
        }
    } else {
        form.action = url;
        form.submit();
        return false;
    }
}

//显示图片
function showpic(picurl,setwidth,setheight) {
    var picwidth;
    var picheight;
    var iWidth;
    var iHeight;

    var maxwidth  = parseInt(setwidth);
    var maxheight = parseInt(setheight);

    var img = new Image();
    img.src = picurl;
    picwidth  = img.width;
    picheight = img.height;

    if (picwidth == 0) {
        picwidth = setwidth;
    }

    if (picheight == 0) {
        picheight = setheight;
    }
    img.alt = picwidth+"×"+picheight;

    if (picwidth >= picheight && picwidth > maxwidth) {
        iWidth  = maxwidth;
        iHeight = parseInt(picheight*iWidth/picwidth);
    } else if(picwidth < picheight && picheight > maxheight) {
        iHeight = maxheight;
        iWidth  = parseInt(picwidth*iHeight/picheight);
    } else {
        iWidth  = picwidth;
        iHeight = picheight;
    }

    var newText = '<img src="'+picurl+'" width="'+iWidth+'" height="'+iHeight+'" border="0">';
    document.write(newText);
}


// 取对象
function $(oid) {
    return document.getElementById(oid);
}

function findPos(obj) {
    this.top=0;
    this.left=0;
    this.width=obj.offsetWidth;
    this.height=obj.offsetHeight;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            this.top += obj.offsetTop;
            this.left += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
}

function setPos(o, s) {
    var top;
    var left;
    top  = s.offsetTop;
    left = s.offsetLeft;
    if (s.offsetParent) {
        while (s = s.offsetParent) {
            top += s.offsetTop;
            left += s.offsetLeft;
        }
    }
    o.style.top = top + 'px';
    o.style.left = left + 'px';
}

function getEvent(eventobj) {
    if (!eventobj || is_ie) {
        window.event.returnValue = false;
        window.event.cancelBubble = true;
        return window.event;
    } else {
        eventobj.stopPropagation();
        eventobj.preventDefault();
        return eventobj;
    }
}

//大图片缩小
function ImageLoad(img)
{
    img.resized = false;
    if(img.width>500)
    {
        img.width=500;
        img.resized = true;
    }
    img.style.zoom = 1;
}

