function $(str)
{
    return document.getElementById(str);
}
function _(str)
{
    return document.getElementsByTagName(str);
}
var ReWriteArtler = true;
function SelectAll(tempControl, GridTable)
{
    var theBox = tempControl;
    xState = theBox.checked;
    if (typeof (GridTable) == "undefined")
    {
        elem = theBox.form.elements;
        for (i = 0; i < elem.length; i++)
        {
            if (elem[i].type == "checkbox" && elem[i].id != theBox.id)
            {
                elem[i].checked = xState;
                GridView_RowChecked(elem[i]);
            }
        }
    }
    else
    {
        var theTable = $(GridTable);

        for (i = 1; i < theTable.rows.length; i++)
        {
            var objCheckBox = theTable.rows[i].cells[0].firstChild;
            if (objCheckBox.checked != null)
            {
                objCheckBox.checked = tempControl.checked;
                GridView_RowChecked(objCheckBox);
            }
        }
    }
}

function CheckSelected(tempControl, GridTable)
{
    var theBox = tempControl;
    var II = 0;


    if (typeof (GridTable) == "undefined")
    {
        elem = theBox.form.elements;
        for (i = 0; i < elem.length; i++)
        {
            if (elem[i].type == "checkbox" && elem[i].checked == true)
            {
                II += 1;
            }
        }
    }
    else
    {
        var theTable = $(GridTable);

        for (i = 0; i < theTable.rows.length; i++)
        {
            var objCheckBox = theTable.rows[i].cells[0].firstChild;
            if (objCheckBox.checked != null && objCheckBox.checked == true)
            {
                II += 1;
            }
        }
    }
    if (II > 0)
    {
        return true;
    }
    else
    {
        alert('请选中一项');
        return false;
    }
}

function GridView_RowChecked(o)
{
    if (o.type == "checkbox")
    {
        var obj = o.parentNode.parentNode;
        if (obj.tagName == "TR")
        {
            if (o.checked)
                obj.style.background = "#ffffcc";
            else
                obj.style.background = "#ffffff";
        }

    }
}

function GridView_RowOut(o)
{
    var MyCheckBox = null;
    if (o.tagName == "TR")
    {
        try
        {
            MyCheckBox = o.childNodes[0].childNodes[0];
        }
        catch (ex) { }
    }

    if (MyCheckBox == null || MyCheckBox.checked != true)
    {
        o.style.background = "#ffffff";
    }
    else
    {
        o.style.background = "#ffffcc";
    }

}
function GridView_RowOver(o)
{

    o.style.background = "#ffffcc";
}
/*自动绑定事件*/
function onmouseoverListener(e)
{
    e = e || window.event;
    var source = e.target || e.srcElement;
    var objtr = source.parentNode;
    if (objtr.tagName == "TD")//鼠标放在链接或者有颜色的字上的时候source就为它的标签得到objtr就可能为TD，所以要过滤一下。
    {
        objtr = objtr.parentNode;
    }
    else if (objtr.tagName == "TR")
    {
        GridView_RowOver(objtr);
    }
}
/*自动绑定事件*/
function onmouseoutListener(e)
{
    e = e || window.event;
    var source = e.target || e.srcElement;
    //source 为 td
    var objtr = source.parentNode;
    if (objtr.tagName == "TD")//鼠标放在链接或者有颜色的字上的时候source就为它的标签得到objtr就可能为TD，所以要过滤一下。
    {
        objtr = objtr.parentNode;
    }
    if (objtr.tagName == "TR")
    {
        GridView_RowOut(objtr)
    }
}
function onCheckedListener(e)
{
    e = e || window.event;
    var source = e.target || e.srcElement;
    GridView_RowChecked(source);
}

/*
自动绑定点击事件
*/
function attachMouseEvent(cid)
{
    if (cid == null)
    {
        cid = "GV_List";
    }
    var obj = document.getElementById(cid);
    if (obj == null)
    {
        return;
    }
    var lists = document.getElementById(cid).getElementsByTagName('tr');
    for (var i = 1; i < lists.length; i++)//从一开始是不算表头的意思
    {
        lists[i].onmouseover = onmouseoverListener;
        lists[i].onmouseout = onmouseoutListener;

        var cb = lists[i].childNodes[0].childNodes[0];
        if (cb != null)
        {
            if (cb.type == "checkbox")
            {
                cb.onclick = onCheckedListener;
            }
        }
    }
}

function TableRowColor(tid)
{
    var obj = document.getElementById(tid);
    if (obj == null)
    {
        return;
    }

    var lists = obj.getElementsByTagName('tr');
    for (var i = 0; i < lists.length; i++)
    {
        if ((i % 2) == 0)
            lists[i].style.background = "#ffffcc";
    }
}
function GetCurrentIframeID()
{
    var aFrms = parent.document.getElementsByTagName("IFRAME");
    var fid = null;
    for (i = 0; i < aFrms.length; i++)
    {
        if (aFrms[i].contentWindow == window)
        {
            fid = aFrms[i].id;
            break;
        }
    }
    return fid;
}
function RefurbishTitle()
{
    try
    {
        var fid = GetCurrentIframeID();

        if (fid == null)
            return;

        fid = fid.replace("fID_", "");

        parent.document.getElementById("nobr_" + fid).innerHTML = document.title;
        parent.document.getElementById("nobr_" + fid).title = document.title;
    }
    catch (ex) { }
}

function AutoIframeHeight()
{
//    var aFrms = parent.document.getElementsByTagName("IFRAME");
//    var fid;
//    for (i = 0; i < aFrms.length; i++)
//    {
//        if (aFrms[i].contentWindow == window)
//        {
//            fid = aFrms[i].id;
//            break;
//        }
//    }
//    if (fid == null)
//        return;

//    var h = document.body.scrollHeight + 20;
//     
//    if (h < 520)
//    {
//        h = 520;
//    }

//    parent.$(fid).style.height = h;

//    parent.$('TabPageContent').style.height = h;

//    window.setTimeout("AutoIframeHeight()", 500);
}

function NewTabPage(command, title, url)
{
    parent.NewTabPage(command, title, url, 'no');
    return false;
}

function DeleteTagPage()
{
    //debugger
    var fid = GetCurrentIframeID();

    if (fid == null)
        return;

    var Tabid = fid.replace("fID_", "");
    parent.CloseTagPage(Tabid);
    return false;
}

/*允许多个window.onload*/
function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function ()
        {
            oldonload();
            func();
            
        }
    }
}



//////////////////////////////////////////////////////////////////////////

function msgBox(boxtitle, boxtype, msg, url)
{
    var boxwidth = (msg.toString().length * 12 + 60);
    if (boxwidth < 200)
    {
        boxwidth = 200;
    }
    if (boxwidth > 200)
    {
        boxwidth = 200;
    }

    $("msg_div_main").style.width = boxwidth + "px";
    $("msg_div_main").style.left = (_("body")[0].clientWidth - boxwidth) / 2;

    if (document.documentElement.clientHeight > document.documentElement.scrollHeight)
    {
        $("msg_div_main").style.top = document.documentElement.clientHeight / 2 - 180 + "px";
    }
    else
    {
        $("msg_div_main").style.top = document.documentElement.scrollHeight / 2 - 180 + "px";
    }

    var msg_div_main_but_tmp = "<br /><br />"
				+ "<button class='btn' id='msg_div_main_but' "
				+ "onclick=\"msg_close_tmp_biyuan();" + url + "\">确 定</button>";
    switch (boxtype * 1)
    {
        case 1:
            $("msg_div_main_content").innerHTML = msg + msg_div_main_but_tmp;
            //$("msg_div_main_but").focus();
            break;
        case 2:
            $("msg_div_main_content").innerHTML = msg + msg_div_main_but_tmp
							 + "&nbsp;&nbsp;<button class='msg_div_main_but' "
							 + "onclick='msg_close_tmp_biyuan();'>取 消</button>";
            //$("msg_div_main_but").focus();
            break;
        case 3:
            $("msg_div_main_content").innerHTML = msg;
            break;
            defualt:
            $("msg_div_main_content").innerHTML = msg;
            break;
    }
    $("msg_div_main_title").innerHTML = boxtitle;
    $("msg_div_main").style.zIndex = 200;
    $("msg_div_main").style.display = "block";

    $("msg_div_all").style.zIndex = 100;
    $("msg_div_all").style.display = "block";
    $("msg_div_all").style.position = "absolute";
    $("msg_div_all").style.top = "0";
    $("msg_div_all").style.background = "#777";
    $("msg_div_all").style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
    $("msg_div_all").style.opacity = "0.6";
    $("msg_div_all").style.left = "0";
    $("msg_div_all").style.width = "100%";
    $("msg_div_all").style.height = ((document.documentElement.clientHeight > document.documentElement.scrollHeight) ? document.documentElement.clientHeight : document.documentElement.scrollHeight) + "px";


    $("msg_div_all").oncontextmenu = function ()
    {
        return false;
    }
    $("msg_div_main").oncontextmenu = function ()
    {
        return false;
    }
}
function msg_close_tmp_biyuan()
{
    $('msg_div_all').style.display = 'none';
    $('msg_div_main').style.display = 'none';
    return false;
}

//加入对话框移动代码
var msg_md = false, msg_mobj, msg_ox, msg_oy;
document.onmousedown = function ()
{
    if (typeof (event.srcElement.msg_canmove) == "undefined")
    {
        return;
    }
    if (event.srcElement.msg_canmove)
    {
        msg_md = true;
        msg_mobj = $(event.srcElement.msg_forid);
        msg_ox = msg_mobj.offsetLeft - event.x;
        msg_oy = msg_mobj.offsetTop - event.y;
    }
}
document.onmouseup = function ()
{
    msg_md = false;
}
document.onmousemove = function ()
{
    if (msg_md)
    {
        msg_mobj.style.left = event.x + msg_ox;
        msg_mobj.style.top = event.y + msg_oy;
    }
}


document.writeln("<div id='msg_div_all' style='display:none;'></div>"
	+ "<div id='msg_div_main' style='display:none;'>"
	+ "<table width='100%' height='29' border='0' cellspacing='0' cellpadding='0'>"
	+ "<tr>"
	+ "<td width='25'><img src='../img/bg_01.gif' width='25' height='29' alt='' /></td><td background='../img/bg_02.gif' width='3'></td>"
	+ "<td background='../img/bg_02.gif' msg_canmove='true' msg_forid='msg_div_main' id='msg_div_main_title'></td>"
	+ "<td background='../img/bg_02.gif' align='right' style='padding-top:4px'>"
	+ "<img src='../img/bg_05.gif' width='21' height='21' alt='关闭' "
	+ "onMouseover=\"this.src='../img/bg_13.gif'\" "
	+ "onMouseout=\"this.src='../img/bg_05.gif'\" onMouseup='msg_close_tmp_biyuan();' "
	+ "onMousedown=\"this.src='../img/bg_18.gif'\"></td>"
	+ "<td width='6'><img src='../img/bg_06.gif' width='6' height='29' alt='' /></td>"
	+ "</tr>"
	+ "</table>"
	+ "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"
	+ "<tr>"
	+ "<td width='3' background='../img/bg_07.gif'></td>"
	+ "<td bgcolor='#F7F7F7' align='center'><br /><span id='msg_div_main_content'></span><br /><br /></td>"
	+ "<td width='3' background='../img/bg_08.gif'></td>"
	+ "</tr>"
	+ "<tr>"
	+ "<td width='3' height='3'><img src='../img/bg_09.gif' width='3' height='3' alt='' /></td>"
	+ "<td background='../img/bg_11.gif'></td>"
	+ "<td width='3' height='3'><img src='../img/bg_10.gif' width='3' height='3' /></td>"
	+ "</tr>"
	+ "</table>"
	+ "</div>");

if (ReWriteArtler == true)
{
    window.alert = myAlert;
}


/*
重写alert
*/
function myAlert(info)
{
    msgBox("提示框标题", 1, info, "");
}

/*window.load事件*/
addLoadEvent(attachMouseEvent);
addLoadEvent(RefurbishTitle);

/*
实现Request.QueryString效果
*/
Request =
{
    QueryString: function (item)
    {
        var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
        return svalue ? svalue[1] : svalue;
    }
}

/*
重写验证控件的JS
*/
function ValidatorUpdateDisplay(val)
{
    if (typeof (val.display) == "string")
    {
        if (val.display == "None")
        {
            return;
        }
        if (val.display == "Dynamic")
        {
            //val.style.display = val.isvalid ? "none" : "inline";
            // return;
        }
    }
    if ((navigator.userAgent.indexOf("Mac") > -1) &&
        (navigator.userAgent.indexOf("MSIE") > -1))
    {
        val.style.display = "inline";
    }
    alert(val.innerHTML);
    // val.style.visibility = val.isvalid ? "hidden" : "visible";
}



function ChangeDisplay(id)
{
    var obj = document.getElementById(id);
    if (obj != null)
    {
        if (obj.style.display == "none")
        {
            obj.style.display = "block";
        }
        else
        {
            obj.style.display = "none";
        }
    }
}

Date.prototype.format = function (format)
{
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "f+": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format))
    {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o)
    {
        if (new RegExp("(" + k + ")").test(format))
        {
            var str;

            if (RegExp.$1.length == 1)
            {
                str = o[k];
            }
            else if (RegExp.$1.length == 2)
            {
                str = ("00" + o[k]).substr(("" + o[k]).length);
            }
            else
            {
                str = ("000" + o[k]).substr(("" + o[k]).length);
            }


            format = format.replace(RegExp.$1, str);
        }
    }
    return format;
}