jQuery(document).ready(function($) {
    $("#SelectAll").click(
        function() {

            $("input[type='checkbox']").each(function() { this.checked = true; });
        });

    $("#ClearSelectAll").click(
            function() {
                $("input[type='checkbox']").each(function() { this.checked = false; });
            }
);

    $("#Refresh").click(
            function() {
                location.reload();
            }
    );

    $("#Back").click(
            function() {
                window.history.back();
            }
    );


    $('#Search').click(function() {
        $.fn.jmodal({
            title: $('#Dialog').attr("title"),
            content: $('#Dialog').html(),
            buttonText: { ok: '确定', cancel: '取消' },
            okEvent: function(obj, args) {
                $('#btOk').attr("disabled", "disabled");
                GoSearch();
            }
        });
    });

    NormalMenuOpenStatuInit();

});

function OpenClose(id) {
    
    $("dd[name=" + id + "]").toggle();
    var status = $("dd[name=" + id + "]").css("display");
    var openMenus = $.cookie("List_Menu_Normal");

    if (status != "none") {
        if (openMenus == null)
            openMenus = "*" + id + "*";
        else
            openMenus = openMenus + id + "*";
    }
    else {
        if (openMenus != null) {
            openMenus = openMenus.replace("*" + id + "*", "*");
        }
    }
    
    $.cookie("List_Menu_Normal", openMenus, { expires: 7 });
}


function NormalMenuOpenStatuInit() {
    var openMenus = $.cookie("List_Menu_Normal");
    if (openMenus != null) {
        var opens = openMenus.split("*");
        if (opens.length > 0) {
            for (var i = 0; i < opens.length; i++) {
                if (opens[i] != null) {
                    $("dd[name=" + opens[i] + "]").css("display", "block");
                }
            }
        }
    }
}




        function DownLoadFile(URL) {
            newWin = window.open(URL, "newWin",
    "directories=no,scrollbars=yes,location=no,resizable=no,left=140,top=140,menubar=no,width=370,height=340");
            newWin.creator = self;
            newWin.focus();
        }

        function Confirm(msg) {
            return window.confirm(msg);
        }



        jQuery.cookie = function(name, value, options) {
            if (typeof value != 'undefined') {
                options = options || {};
                if (value === null) {
                    value = '';
                    options = $.extend({}, options);
                    options.expires = -1;
                }
                var expires = '';
                if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                    var date;
                    if (typeof options.expires == 'number') {
                        date = new Date();
                        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                    } else {
                        date = options.expires;
                    }
                    expires = '; expires=' + date.toUTCString();
                }
                var path = options.path ? '; path=' + (options.path) : '';
                var domain = options.domain ? '; domain=' + (options.domain) : '';
                var secure = options.secure ? '; secure' : '';
                document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
            } else {
                var cookieValue = null;
                if (document.cookie && document.cookie != '') {
                    var cookies = document.cookie.split(';');
                    for (var i = 0; i < cookies.length; i++) {
                        var cookie = jQuery.trim(cookies[i]);
                        if (cookie.substring(0, name.length + 1) == (name + '=')) {
                            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                            break;
                        }
                    }
                }
                return cookieValue;
            }
        };


        function OpenFullModalDialog(url) {
            var strFeatureInfo = 'dialogTop:0;dialogWidth:' + (window.screen.availWidth)
            + 'px;DialogHeight=' + (window.screen.availHeight) + 'px;help=no;center=yes;status:no;resizable:no';
            var returnVal = window.showModalDialog(url, '', strFeatureInfo); 
            
            if(returnVal=="True")   
            {   
               
               location.reload();
            }

        }



        function FillTrueName(accountTextName, trueTextName, nullMsgTitle) {

            var account = $("input[name='" + accountTextName + "']").val();
           
            if (account.length > 0) {
                $.ajax({
                    type: "get",
                    url: "/ajax/CommAjaxMgr.ashx?method=getuserinfobyaccount&account=" + account + "&rnd=" + Math.random(),
                    dataType: "json",
                    beforeSend: function(XMLHttpRequest) {
                        //ShowLoading();
                    },
                    success: function(data, textStatus) {
                        var userTrueName = "";
                        if (data == null) {
                            alert(nullMsgTitle+"不存在!");
                        }
                        else {
                            var userInfo = data[0];
                            if(userInfo != null)
                                userTrueName = userInfo.TureName;
                            else
                                alert(nullMsgTitle+"不存在!");
                        }

                        $("input[name='" + trueTextName + "']").val(userTrueName);
                    },
                    complete: function(XMLHttpRequest, textStatus) {

                    },
                    error: function() {
                        //请求出错处理
                        alert("账户信息获取失败!");
                    }
                });
            }
            else {
                //alert(nullMsg);
            }
        }



        function FillManagerInfo(employeeAccountTextName, managerAccountTextName, managerTureTextName, nullMsgTitle) {

            var account = $("input[name='" + employeeAccountTextName + "']").val();

            if (account.length > 0) {
                $.ajax({
                    type: "get",
                    url: "/ajax/CommAjaxMgr.ashx?method=getdepartmanagerinfobyemployeeaccount&account=" + account + "&rnd=" + Math.random(),
                    dataType: "json",
                    beforeSend: function(XMLHttpRequest) {
                        //ShowLoading();
                    },
                    success: function(data, textStatus) {
                        var userTrueName = "";
                        var userAccount = "";
                        if (data == null) {
                            alert(nullMsgTitle + "不存在!");
                        }
                        else {
                            var userInfo = data[0];
                            if (userInfo != null) {

                                $.each(userInfo, function(index, obj) {
                                if (userAccount.length <= 0)
                                    userTrueName = obj.TureName;
                                else
                                    userTrueName = userTrueName + "," + obj.TureName;


                                if (userAccount.length <= 0)
                                    userAccount = obj.UserName;
                                else
                                    userAccount = userAccount + "," + obj.UserName;
              
                                });   
                                
                            }
                            else
                                alert(nullMsgTitle + "不存在!");
                        }

                        $("input[name='" + managerAccountTextName + "']").val(userAccount);
                        $("input[name='" + managerTureTextName + "']").val(userTrueName);
                    },
                    complete: function(XMLHttpRequest, textStatus) {

                    },
                    error: function() {
                        //请求出错处理
                        alert("账户信息获取失败!");
                    }
                });
            }
            else {
                //alert(nullMsg);
            }
        }




        function FillDirectManagerInfo(employeeAccountTextName, managerAccountTextName, managerTureTextName, nullMsgTitle) {

            var account = $("input[name='" + employeeAccountTextName + "']").val();

            if (account.length > 0) {
                $.ajax({
                    type: "get",
                    url: "/ajax/CommAjaxMgr.ashx?method=getdirectmanagerinfobyemployeeaccount&account=" + account + "&rnd=" + Math.random(),
                    dataType: "json",
                    beforeSend: function(XMLHttpRequest) {
                        //ShowLoading();
                    },
                    success: function(data, textStatus) {
                        var userTrueName = "";
                        var userAccount = "";
                        if (data == null) {
                            alert(nullMsgTitle + "不存在!");
                        }
                        else {
                            var userInfo = data[0];
                            if (userInfo != null) {

                                $.each(userInfo, function(index, obj) {
                                    if (userAccount.length <= 0)
                                        userTrueName = obj.TureName;
                                    else
                                        userTrueName = userTrueName + "," + obj.TureName;


                                    if (userAccount.length <= 0)
                                        userAccount = obj.UserName;
                                    else
                                        userAccount = userAccount + "," + obj.UserName;

                                });

                            }
                            else
                                alert(nullMsgTitle + "不存在!");
                        }

                        $("input[name='" + managerAccountTextName + "']").val(userAccount);
                        $("input[name='" + managerTureTextName + "']").val(userTrueName);
                    },
                    complete: function(XMLHttpRequest, textStatus) {

                    },
                    error: function() {
                        //请求出错处理
                        alert("账户信息获取失败!");
                    }
                });
            }
            else {
                //alert(nullMsg);
            }
        }
        


        String.prototype.trim = function() {return this.replace(/(^\s*)|(\s*$)/g, "");}
        String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); }
        String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); }

        function left(mainStr, lngLen) {
            if (mainStr == null)
                return "";
                
            if (lngLen>0) {return mainStr.substring(0,lngLen)} 
            else{return null} 
        } 

        function right(mainStr,lngLen) { 
        // alert(mainStr.length) 
            if (mainStr.length-lngLen>=0 && mainStr.length>=0 && mainStr.length-lngLen<=mainStr.length) { 
                return mainStr.substring(mainStr.length-lngLen,mainStr.length)} 
        else{return null} 
        } 
        function mid(mainStr,starnum,endnum){ 
            if (mainStr.length>=0){ 
                return mainStr.substr(starnum,endnum) 
            }else{return null} 
        //mainStr.length
        }

        function lcase(str) {
           
            if (str != null)
                return str.toLowerCase();
            else
                return "";
        }

        function instr(srouce , part) {
            return srouce.indexOf(part);
        }

        function cint(str) {
            return parseInt(str);
        }

        function trim(str) {
            if (str != null)
                return str.trim();
            else
                return "";
        }

        function Len(str) {
            if (str != null)
                return str.toString().length;
            else
                return 0;
        }

        function cstr(obj) {
            if (obj != null)
                return obj.toString();
            else
                return "";
        }
        
        function IsNumeric(sText)
        {
           var ValidChars = "0123456789.";
           var IsNumber=true;
           var Char;
           var index;
           for (index = 0; index < sText.length && IsNumber == true; index++) 
           {
               Char = sText.charAt(index); 
              if (ValidChars.indexOf(Char) == -1) 
              {
                 IsNumber = false;
              }
           }
           return IsNumber;
       }

       function split(source, spliter) {
           if (source == null)
               return new Array();
               
           return source.split(spliter);
       }

       function Focus(obj) {
           try {
               if (obj != null)
                   obj.focus();
           }
           catch (e) {
           }
       }
