﻿if (window.PageJS == null)
{
    window.PageJS = true;
    window.status = document.title
    if (window.idPart == null)
    {
        window.idPart = "";
    }

    /*取得最上层的页面*/
    function GetTop(closeDialog)
    {
        if (closeDialog == null)
        {
            closeDialog = true
        }

        function _GetTop(w)
        {
            if (w.window._isRootWindow || (w.dialogArguments == null && w.parent == null))
            {
                return w;
            }

            if (w.dialogArguments)
            {
                if (closeDialog)
                {
                    w.close();
                }
                return _GetTop(w.dialogArguments);
            }
            else if (w.parent && w.parent != w)
            {
                return _GetTop(w.parent);
            }
            else
            {
                return w;
            }
        }

        try
        {
            return _GetTop(window);
        }
        catch (e)
        {
            return window;
        }
    }

    function CloseOpener()
    {
        try
        {
            if (window.top.opener && window.top.opener.location.host == window.location.host)
            {
                window.top.opener.top.opener = null;
                window.top.opener.top.close();
            }
        } catch (e) { }
    }

    function $(id, container)
    {
        if (container == null)
        {
            container = document;
        }

        if (container.getElementById)
        {
            return document.getElementById(id);
        }
        else if (container.all)
        {
            return container.all(id);
        }
    }

    //获取事件的返回值
    function GetEventValue(event, nullValue)
    {
        if (event == null)
        {
            event = window.event;
        }

        if (event == null)
        {
            if (nullValue == null)
            {
                return false
            }
            return nullValue;
        }
        else
        {
            return event.returnValue != false && event.cancelBubble != true
        }
    }

    function CheckParentDisabledEvent(element)
    {
        if (element == null)
        {
            return;
        }

        if (element.onclick && element._onclick == null)
        {
            element._onclick = element.onclick;
            element.onclick = function()
            {
                var _element = event.srcElement;
                if (GetEventValue() && GetDisabled(_element) == false)
                {
                    element._onclick();
                }
            }
        }
        /*else if(element.onclick==null)
        {
        element.onclick=CancelEvent;
        element._onclick=true;
        }*/

        if (element.onmousedown && element._onmousedown == null)
        {

            element._onmousedown = element.onmousedown;
            element.onmousedown = function()
            {
                var _element = event.srcElement;
                if (GetEventValue() && GetDisabled(_element) == false)
                {
                    element._onmousedown();
                }
            }
        }
        /*else if(element.onmousedown==null)
        {
        element.onmousedown=CancelEvent
        element._onmousedown=true;
        }*/

        if (element.href)
        {
            try
            {
                element.href = "#";
            }
            catch (e)
            {
            }
        }

        CheckParentDisabledEvent(element.parentElement);
    }

    function CancelEvent(event)
    {
        if (event == null)
        {
            event = window.event;
        }

        if (event)
        {
            event.returnValue = false;
            event.cancelBubble = true;
            return false
        }
    }

    function CheckDisabled()
    {
        if (event)
        {
            var element = event.srcElement;
            if (element && element.nodeName != "BODY" && GetDisabled(element))
            {
                if (element.checkEvented != true)
                {
                    CheckParentDisabledEvent(element);
                    element.checkEvented = true;
                }
                try
                {
                    element.href = "#";
                }
                catch (ex) { return; }
                element.blur();
                CancelEvent();
            }
        }
        return true
    }

    function GetEventValue(event, nullValue)
    {
        if (event == null)
        {
            event = window.event;
        }

        if (event == null)
        {
            if (nullValue == null)
            {
                return false
            }
            return nullValue;
        }
        else
        {
            return event.returnValue != false && event.cancelBubble != true
        }
    }

    function GetDisabled(element)
    {
        if (element == null)
        {
            return false;
        }
        else if (element.disabled)
        {
            return true;
        }
        else
        {
            return GetDisabled(element.parentElement);
        }
    }

    window.attachEvent("onload", function()
    {
        document.onmousedown = CheckDisabled
        document.attachEvent("onmouseover", CheckDisabled)
    })

    function Exec(hendler)
    {
        var type = typeof (hendler);
        if (type == "string")
        {
            return eval(hendler.Format(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10]));
        }
        else if (type == "function")
        {
            return hendler(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10]);
        }
    }

    //取得对象相对页面的位置,将结果放到对象的 X,Y属性中,position用来存放坐标
    /*function OC_GetPosition(obj,position,endElement)
    {
    var endTagName="BODY";
    if(endElement!=null && typeof(endElement)=="string")
    {
    endTagName=endElement;
    }
	
	x=obj.offsetLeft;
    y=obj.offsetTop;
    var objParent=obj.offsetParent;
    while(objParent!=null && objParent !=endElement && !Compare(objParent.tagName,endTagName))
    {
    x+=objParent.offsetLeft-objParent.scrollLeft;
    y+=objParent.offsetTop-objParent.scrollTop;
    objParent=objParent.offsetParent;
    }
    if(position)
    {
    position[0]=x;
    position[1]=y
    }
    else
    {
    obj.X=x;
    obj.Y=y;
    }

	return obj;
    }*/

    var cancelEvent = false
    //模式对话框中按Esc退出
    function CloseByKey()
    {
        try
        {
            if (event.keyCode == 27 && event.returnValue != false && event.cancelBubble != true)
            {
                var element = event.srcElement;
                if (element && element.type == 'text')
                {
                    try
                    {
                        var range = document.selection.createRange();
                        if (!IsEmpty(range.text))
                        {
                            element.blur()
                        }
                    } catch (e) { }
                    return;
                }

                window.setTimeout(function()
                {
                    if (cancelEvent != true)
                    {
                        try
                        {
                            if (window.top.currentDialog && window.top.currentDialog.closed == false)
                            {
                                window.top.currentDialog.close();
                                return;
                            }
                        }
                        catch (e)
                        {
                            if (window.currentDialog && window.currentDialog.closed == false)
                            {
                                window.currentDialog.close();
                                return
                            }
                        }

                        if (window.dialogArguments)
                        {
                            window.close();
                            return;
                        }
                    }
                    cancelEvent = false
                }, 100);
            }
            else if ((event.keyCode == 122 || (event.keyCode == 65 && event.shiftKey)) && window.dialogArguments)
            {
                try
                {
                    ShowFormerlyPos();
                } catch (e) { }
            }
            else if (event.shiftKey)
            {
                window.status = event.keyCode;
            }

        } catch (e) { }
    }

    document.attachEvent("onkeyup", CloseByKey);

    window.attachEvent("onunload", function()
    {
        try
        {
            if (window.top.currentDialog && window.top.currentDialog.closed == false && window.top.currentDialog.dialogArguments == window)
            {
                window.top.currentDialog.close();
            }
        }
        catch (e)
        {
            if (window.currentDialog && window.currentDialog.closed == false)
            {
                window.currentDialog.close();
            }
        }
    })

    function HideHref()
    {
        document.attachEvent("onmousemove", HideAHref);
        function HideAHref()
        {
            var e = event.srcElement;
            if (e.tagName == "A")
            {
                window.status = e.innerText;
            }
        }
    }

    //通过Tag往上获取对象
    function GetParentByTag(obj, tag, layers)
    {
        if (layers == null)
        {
            layers = 1000;
        }

        tag = tag.toLowerCase();
        while (true)
        {
            if (obj == null)
            {
                return null;
            }

            try
            {
                if (obj.tagName.toLowerCase() == tag)
                {
                    return obj;
                }
                else
                {
                    if (--layers < 0)
                    {
                        return null;
                    }
                    obj = obj.parentElement;
                }
            } catch (e) { }
        }
    }

    function CheckAll(container, checked)
    {
        var inputs = container.getElementsByTagName("Input");
        for (var i = 0; i < inputs.length; i++)
        {
            var e = inputs[i];
            if (e.type == "checkbox")
            {
                e.checked = checked;
            }
        }
    }


    /*分页控件方法*/
    var selectAll = false;
    function Pager_SelectAll(dataGrid, id, e)
    {
        var index = 1;
        if (e.checked)
        {
            selectAll = e.checked;
        }
        else
        {
            selectAll = !selectAll;
        }

        while (true)
        {
            index++;
            var chk = null;
            if (index < 10)
            {
                chk = dataGrid.all(dataGrid.id + "_ctl0" + index + "_" + id)
            }
            else
            {
                chk = dataGrid.all(dataGrid.id + "_ctl" + index + "_" + id)
            }
            //var chk=dataGrid.all(dataGrid.id+"__ctl"+index+"_"+id);
            if (chk == null)
            {
                try
                {
                    chk = dataGrid.all(id);
                    if (chk != null)
                    {
                        for (var i = 0; i < chk.length; i++)
                        {
                            chk[i].checked = selectAll;
                        }
                    }
                } catch (e) { }
                break;
            }
            chk.checked = selectAll;
        }
    }

    function Pager_DblClick(dataGrid)
    {
        var e = GetParentByTag(event.srcElement, "TR");
        if (e != null && e.tag)
        {
            eval(dataGrid.DoubleClickCommand.replace(/@ID/gi, e.tag))
        }
    }


    function GetIDs(container, checkChecked)
    {
        if (container)
        {
            var inputs = container.getElementsByTagName("Input")
        }
        else
        {
            var inputs = document.getElementsByTagName("Input")
        }

        var ids = new Array();
        for (var i = 0; i < inputs.length; i++)
        {
            var e = inputs[i];
            if (e.tag != null && (e.type == 'checkbox'))
            {
                if (checkChecked == false || e.checked)
                {
                    ids[ids.length] = e.tag;
                }
            }
        }

        return ids
    }

    function GetSelectedRowID(container)
    {
        var ids = GetIDs(container, true);
        if (ids.length == 1)
        {
            return ids;
        }
        else
        {
            return null;
        }
    }

    function ReLoad()
    {
        if (window.dialogArguments)
        {
            window.location.reload();
            return;
        }
        var url = window.location.href;
        if (url.indexOf("?") > -1)
        {
            url += "&________R=" + Math.random();
        }
        else
        {
            url += "?________R=" + Math.random();
        }

        window.location.href = url
    }

    function Update(value)
    {


        if (event && event.returnValue == false)
        {
            return;
        }

        try
        {
            if (window.updateElementID)
            {
                __doPostBack(window.updateElementID, value);
                return;
            }

            var btn = $(window.idPart + "btnUpdate")

            if (btn == null)
            {
                btn = $("btnUpdate")
            }
            if (btn)
            {
                btn.click();
            }
            else
            {

                throw new Error("没有更新按扭");
            }
        }
        catch (e)
        {
            ReLoad();
        }
    }

    function ShowDialog(url, width, height, receiveControlID, functionName, execScript)
    {
        return ShowModalDialog(url, width, height, receiveControlID, functionName, execScript);
    }

    function ShowModeless(url, width, height)
    {
        return ShowModalDialog(url, width, height, "", "", "", false);
    }

    //打开一个对话框
    function ShowModalDialog(url, width, height, receiveControlID, functionName, execScript, isModal)
    {
        if (url.indexOf("?") > -1)
        {
            url += "&R=" + Math.random();
        }
        else
        {
            url += "?R=" + Math.random();
        }

        try
        {
            if (isModal == false)
            {
                try
                {
                    if (window.top.currentDialog && window.top.currentDialog.closed == false)
                    {
                        window.top.currentDialog.close();
                    }
                }
                catch (e)
                {
                    if (window.currentDialog && window.currentDialog.closed == false)
                    {
                        window.currentDialog.close();
                    }
                }

                window.currentDialog = window.showModelessDialog(url, window, 															//地址
			"scroll:no;status:no;center:yes;help:no;minimize:no;maximize:no;border:thin;dialogWidth:" +
					width + "px;dialogHeight:" + 																					//宽	
			height + "px; resizable=yes;")
                window.top.currentDialog = window.currentDialog;
                return window.currentDialog;
            }

            var returnVal = window.showModalDialog(url, window, 																		//地址
					"scroll:no;status:no;center:yes;help:no;minimize:no;maximize:no;border:thin;dialogWidth:" +
					width + "px;dialogHeight:" + 																					//宽	
					height + "px; resizable=yes;")																					//高

            //假如有返回值
            if (returnVal && returnVal != '')
            {
                var value = returnVal.split('|')[0];

                //有接收返回值的控件
                if (receiveControlID && receiveControlID != '')
                {
                    document.getElementById(receiveControlID).value = value;
                }

                //需要执行一个函数
                if (functionName && functionName != '')
                {
                    eval(functionName + "('" + value + "')")
                }

                if (execScript && execScript != "")
                {
                    eval(execScript);
                }

                //需要刷新
                if (returnVal.indexOf('|Refresh') > -1)
                {
                    Update(value)
                    //window.location.href=window.location.href;
                }

                //需要回传
                if (returnVal.indexOf('|PostBack') > -1)
                {
                    if (event == null || event.returnValue != false)
                    {
                        __doPostBack('', '');
                    }
                }

                return value;
            }
            else
            {
                return false
            }
        }
        catch (e)
        {
            if (e.message.indexOf('拒绝访问') > -1)
            {
                alert('请确定你的浏览器可以弹出网页对话框，可设置：\"临时允许弹出窗口\"，或者设置：\"工具>Internet选项>安全>自定义级别>使用弹出窗口阻止程序>禁用\" 后刷新页面，即可解决该问题！')
            }
        }
    }

    Array.prototype.IndexOf = function(value)
    {
        for (var i = 0; i < this.length; i++)
        {
            if (this[i] == value)
            {
                return i;
            }
        }
        return -1;
    }

    function FindControl(tagName, propertyName, valueOrPattern)
    {
        if (tagName && tagName != "")
        {
            var elements = document.getElementsByTagName(tagName)
        }
        else
        {
            var elements = document.all;
        }

        for (var i = 0; i < elements.length; i++)
        {
            if (valueOrPattern.test)
            {
                if (valueOrPattern.test(elements[i][propertyName]))
                {
                    return elements[i];
                }
            }
            else if (valueOrPattern = elements[i][propertyName])
            {
                return elements[i];
            }
        }
    }


    function SetPropertyWithUp(obj, propertyName, value)
    {
        if (obj)
        {
            try
            {
                obj[propertyName] = value;
            }
            catch (e)
            {
                return
            }
            SetPropertyWithUp(obj.parentElement, propertyName, value);
        }

    }



    /*格式化*/
    String.prototype.Format = function()
    {
        var i = 0;
        var r = this
        while (i < arguments.length)
        {
            var v = arguments[i];
            if (v == null)
            {
                v = "";
            }
            v = v.toString();
            r = r.replace(new RegExp("\\{" + i + "\\}", "g"), v)
            i++
        }
        return r
    }

    //格式化日期
    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
            "S+": this.getMilliseconds(), //millisecond
            "W+": "日一二三四五六".charAt(this.getDay()),
            "w": "日123456".charAt(this.getDay())
        }
        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))
            format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
        return format;
    }

    //设置向后台传递的值
    function SetArgs(value)
    {
        if (window.argsControl == null)
        {
            var form = document.forms[0];
            window.argsControl = document.createElement("input");
            window.argsControl.type = "hidden";
            window.argsControl.name = "_EventArgs";
            form.appendChild(window.argsControl);
        }
        window.argsControl.value = value;
    }


    if (window.frameElement)
    {
        if (!window.frameElement.history)
        {
            window.frameElement.history = new Array();
        }

        var array = window.frameElement.history;
        if (array[array.length - 1] != window.location.href)
        {
            array = (new Array()).concat(array);
            array.push(window.location.href);
            if (array.length > 10)
            {
                array.shift();
            }
            window.frameElement.history = array;
        }


        window.goBack = function(step)
        {
            if (step == null)
            {
                step = -1;
            }

            var array = new Array();
            array = array.concat(window.frameElement.history);
            while (step < 0 && array.length > 0)
            {
                step++;
                array.pop();
            }
            window.frameElement.history = array;
            var href = array[array.length - 1];
            if (href)
            {
                window.location.href = href;
            }

            if (window.OC && OC.EventLister)
            {
                OC.EventLister.CancelEvent();
            }
        }

    }
    else
    {
        window.goBack = window.history.go;
    }

}

//计算短信的长度超过长度则截断
function ContentChange(obj, maxLength)
{
    if (!maxLength)
        maxLength = 210;
    var l = obj.value.length;
    if (l > maxLength)
    {
        obj.runtimeStyle.cssText = "color:red";
        obj.value = obj.value.substring(0, maxLength);
        l = maxLength
    }
    else
    {
        obj.runtimeStyle.cssText = "";
    }
    document.all.contentLength.innerHTML = "(" + l + "字)";
}

//检测页面刷新
RefreshModule = function() { };

RefreshModule.HIDDEN_FIELD_ID = "__REFRESH_FIELD";

RefreshModule.prototype = {

    // id of the hidden form field
    hiddenFieldId: RefreshModule.HIDDEN_FIELD_ID,

    // gets a new guid and assigns its value to the hidden field
    createPageIdentifier: function()
    {
        var guid = this.newGuid();
        document.getElementById(this.hiddenFieldId).value = guid;
    },
    // creates and returns a new guid
    newGuid: function()
    {
        var g = "";
        for (var i = 0; i < 32; i++)
        {
            g += Math.floor(Math.random() * 0xF).toString(0xF);
        }
        return g;
    }
};

var refreshModule = new RefreshModule();