﻿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, ""); }
//判断是否按下了ctrl
var ctrl = 0;
var clickColor = "#e4f3d4";
jQuery.jMessageBox.settings.yesButtonText = '确定';

$(function() {
    $(".list-table th input[type=checkbox]").css("float", "left").click(function() {
        var obj = $(this);
        if ($(obj).attr("check") != 'no')
            ChangeCheck(obj);
    });

    $(".edit-table").attr("cellpadding", "0").attr("cellspacing", "1");

    //如果是create界面则关闭左侧菜单
    var lurl = this.location.pathname;
    if (lurl.indexOf("Create") > 0) {
        //隐藏菜单
        window.parent.HideMenu();
    }
    /*
    var txtList = $(".edit-table").find("input:[type='text']");
    txtList.each(function() {

        if ($(this) != null && $(this).attr("readonly") != true && $(this).attr("disabled") != true) {
    //alert($(this).attr("disabled"));
    $(this).focus();
    return false;
    }
    });
    */

    //    $(".edit-table input").keydown(function(event) {
    //        if (event.keyCode == 13) {
    //            var WshShell = new ActiveXObject("WScript.Shell");
    //            WshShell.sendKeys("{TAB}");
    //        }
    //    });
    //    $(".edit-table select").keydown(function(event) {
    //        if (event.keyCode == 13) {
    //            var WshShell = new ActiveXObject("WScript.Shell");
    //            WshShell.sendKeys("{TAB}");
    //        }
    //    });

    $(".edit-table input,select").keypress(function(e) {
        try {
            if (e.which == 13) // 判断所按是否回车键   $('input,select')
            {
                var inputs = $(".edit-table input[type='text'],select[name != 'selectPageSize']"); // 获取表单中的所有输入框   
                var idx = inputs.index(this); // 获取当前焦点输入框所处的位置   
                if (idx == inputs.length - 1) // 判断是否是最后一个输入框
                {
                    // AutoSave();
                } else {
                    if (inputs[idx + 1] != null && inputs[idx + 1] != undefined)
                        inputs[idx + 1].focus(); // 设置焦点   
                }
                return false; // 取消默认的提交行为   
            }
        }
        catch (e) {
        }
    });


    AddInputsStyles();

    $(".search-table input[type=submit]").click(function() {
        $(".search-table input").each(function() {
            if ($(this).attr("onfocus") != null && $(this).attr("onfocus") != undefined) {
                if (!CheckDateFormat2($(this).val())) {
                    $(this).val('');
                    $(this).focus();
                    return false;
                }
                else {
                    var obj = $(this).val();
                    if (obj.length == 0) {
                        $(this).val('');
                    }
                    else if (obj.length == 8) {
                        var year = obj.substring(0, 4);
                        var month = obj.substring(4, 6);
                        var day = obj.substring(6, 8);
                        $(this).val(year + "-" + month + "-" + day);
                    }
                }
            }
        });
    });

    $(".edit-table .datepicker").blur(function() {
        var ele = $(this);
        // if (!CheckDateFormat(ele.val())) {
        //修改为不用加- mcs
        if (!CheckDateFormat2(ele.val())) {
            jQuery.jMessageBox.show({
                width: 350,
                title: '系统提示',
                message: '日期格式不对，请输入8位日期数字，如20100101！',
                focus: false,
                yesButton: {
                    text: '确定',
                    click: function() {
                        jQuery.jMessageBox.hide();
                        ele.val('');
                        ele.focus();
                    }
                }
            });
        }
        else {
            var obj = ele.val();
            if (obj.length == 0) {
                ele.val('');
            }
            else if (obj.length == 8) {
                var year = obj.substring(0, 4);
                var month = obj.substring(4, 6);
                var day = obj.substring(6, 8);
                ele.val(year + "-" + month + "-" + day);
            }
        }
    });
});


//加载数据等待函数
function Loadimg(title, msg) {
    var lhtml = '';
    lhtml = '<div><table id="tb_iframe"><tr><td>';
    lhtml += '<div id="loading"><img src="/UI1.0/Images/loading.gif"/>' + msg + '</div></td></tr></table></div>';

    jQuery.jMessageBox.show({
        width: 500,
        title: title,
        message: lhtml
    });
}

function LoadimgDiv(msg) {
    var html = '<div id="loading"><img src="/UI1.0/Images/loading.gif"/>&nbsp;&nbsp;' + msg + '</div>';
    return html;
}

//清空所有文本
function ClearAll(obj) {
    $("#" + obj + " input[type=text] ").val('');
}


//是否显示某个元素，及其集合元素
function ShowElement(id, visible) {
    if (typeof (id) == "string") {
        if (visible) {
            $('#' + id).show();
            $('*[name="' + id + '"]').show();
        }
        else {
            $('#' + id).hide();
            $('*[name="' + id + '"]').hide();
        }
    }
    else {
        if (visible) {
            $(id).show();
        }
        else {
            $(id).hide();
        }
    }
}

//是否可用某个元素，及其集合元素
function EnableElement(id, enabled) {
    if (typeof (id) == "string") {
        if (enabled) {
            $('#' + id).removeAttr('disabled');
            $('*[name="' + id + '"]').removeAttr('disabled');
        }
        else {
            $('#' + id).attr('disabled', 'disabled').unbind("mousemove");
            $('*[name="' + id + '"]').attr('disabled', 'disabled');
        }
    }
    else {
        if (enabled) {
            $(id).removeAttr('disabled');
        }
        else {
            $(id).attr('disabled', 'disabled');
        }
    }
}



//初始化列表 单击行变色 选中checkbox
function initClicklisttable() {
    $(".list-table tbody tr").unbind("click").click(function() {

        var bgColor = $(this).css("background-color");
        var chb = $(this).find("input[type='checkbox'][id^='cb_']");
        var rdo = $(this).find("input[type='radio'][id^='cb_']");
        rdo.attr("checked", "checked");

        if (ctrl == 0) {
            $(this).siblings().each(function() {
                if (chb != null && chb != undefined) {
                    if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null')
                        $(this).css("background-color", $(this).attr("bColor"));
                    else
                        $(this).css("background-color", "transparent");
                    $(this).find("input[type='checkbox'][id^='cb_']").removeAttr('checked');
                }
            });
        }
        if (chb != null && chb != undefined) {
            if (bgColor == "transparent") {
                $(this).css("background-color", clickColor);
                chb.attr("checked", "checked");
            }
            else if (bgColor == clickColor) {
                if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null')
                    $(this).css("background-color", $(this).attr("bColor"));
                else
                    $(this).css("background-color", "transparent");
                chb.removeAttr('checked');
            }
            else {
                if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null') {
                    if (bgColor == $(this).attr("bColor")) {
                        $(this).css("background-color", clickColor);
                        chb.attr("checked", "checked");
                    }
                }
            }
        }

    }).keydown(function() {
        if (event.keyCode == 17) {
            ctrl = 1;
        }
    }).keyup(function() {
        if (event.keyCode == 17) {
            ctrl = 0;
        }
    });
}

//初始化列表时，单击行变色，选中checkbox，仅单选
function initClicklistTableSingle() {
    $(".list-table tbody tr").unbind("click").click(function() {

        var bgColor = $(this).css("background-color");
        var chb = $(this).find("input[type='checkbox'][id^='cb_']");
        var rdo = $(this).find("input[type='radio'][id^='cb_']");
        rdo.attr("checked", "checked");

        if (ctrl == 0) {
            $(this).siblings().each(function() {
                if (chb != null && chb != undefined) {
                    if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null')
                        $(this).css("background-color", $(this).attr("bColor"));
                    else
                        $(this).css("background-color", "transparent");
                    $(this).find("input[type='checkbox'][id^='cb_']").removeAttr('checked');
                }
            });
        }
        if (chb != null && chb != undefined) {
            if (bgColor == "transparent") {
                $(this).css("background-color", clickColor);
                chb.attr("checked", "checked");
            }
            else if (bgColor == clickColor) {
                if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null')
                    $(this).css("background-color", $(this).attr("bColor"));
                else
                    $(this).css("background-color", "transparent");
                chb.removeAttr('checked');
            }
            else {
                if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null') {
                    if (bgColor == $(this).attr("bColor")) {
                        $(this).css("background-color", clickColor);
                        chb.attr("checked", "checked");
                    }
                }
            }
        }

    });
}

//制定需要初始化table 的id
function initClicklisttableByID(obj) {
    $("#" + obj + " tbody tr").unbind("click").click(function() {
        var bgColor = $(this).css("background-color");
        var chb = $(this).find("input[type='checkbox'][id^='cb_']");
        var rdo = $(this).find("input[type='radio'][id^='cb_']");
        rdo.attr("checked", "checked");

        if (ctrl == 0) {
            $(this).siblings().each(function() {
                if (chb != null && chb != undefined) {
                    if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null')
                        $(this).css("background-color", $(this).attr("bColor"));
                    else
                        $(this).css("background-color", "transparent");
                    $(this).find("input[type='checkbox'][id^='cb_']").removeAttr('checked');
                }
            });
        }
        if (chb != null && chb != undefined) {
            if (bgColor == "transparent") {
                $(this).css("background-color", clickColor);
                chb.attr("checked", "checked");
            }
            else if (bgColor == clickColor) {
                if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null')
                    $(this).css("background-color", $(this).attr("bColor"));
                else
                    $(this).css("background-color", "transparent");
                chb.removeAttr('checked');
            }
            else {
                if ($(this).attr("bColor") != null && $(this).attr("bColor") != undefined && $(this).attr("bColor") != 'null') {
                    if (bgColor == $(this).attr("bColor")) {
                        $(this).css("background-color", clickColor);
                        chb.attr("checked", "checked");
                    }
                }
            }
        }

    }).keydown(function() {
        if (event.keyCode == 17) {
            ctrl = 1;
        }
    }).keyup(function() {
        if (event.keyCode == 17) {
            ctrl = 0;
        }
    });
}

//初始化列表 单击行变色 选中checkbox
function initClicklisttableByID(obj, IsMul) {
    if (!IsMul)
        ctrl = 0;

    $("#" + obj + " tbody tr").unbind("click").click(function() {
        var bgColor = $(this).css("background-color");
        var chb = $(this).find("input[type='checkbox'][id^='cb_']");
        var rdo = $(this).find("input[type='radio'][id^='cb_']");
        rdo.attr("checked", "checked");

        if (ctrl == 0) {
            $(this).siblings().each(function() {
                if (chb != null && chb != undefined) {
                    $(this).css("background-color", "transparent");
                    $(this).find("input[type='checkbox'][id^='cb_']").removeAttr('checked');
                }
            });
        }
        if (chb != null && chb != undefined) {
            if (bgColor == "transparent") {
                $(this).css("background-color", clickColor);
                chb.attr("checked", "checked");
            }
            else if (bgColor == clickColor) {
                $(this).css("background-color", "transparent");
                chb.removeAttr('checked');
            }
        }

    }).keydown(function() {
        if (event.keyCode == 17 && IsMul) {
            ctrl = 1;
        }
    }).keyup(function() {
        if (event.keyCode == 17 && IsMul) {
            ctrl = 0;
        }
    });
}


//列表check全选/反选
function ChangeCheck(ele) {
    var ck = $(ele).attr('checked');
    if (ck) {
        $(".list-table input[type='checkbox'][id^='cb_']").attr('checked', 'checked');
        $(".list-table tbody tr").css("background-color", clickColor);
    } else {
        $(".list-table input[type='checkbox'][id^='cb_']").removeAttr('checked');
        $(".list-table tbody tr").css("background-color", "transparent");
    }
    return false;
}

//列表check全选/反选
function ChangeCheckByID(ele, Tid) {
    var ck = $(ele).attr('checked');
    if (ck) {
        $("#" + Tid + " input[type='checkbox'][id^='cb_']").attr('checked', 'checked');
        $("#" + Tid + " tbody tr").css("background-color", clickColor);
    } else {
        $("#" + Tid + " input[type='checkbox'][id^='cb_']").removeAttr('checked');
        $("#" + Tid + " tbody tr").css("background-color", "transparent");
    }
    return false;
}
//checkbox在MVC Model绑定下无法直接取得true,false，利用隐藏域来改进
function ChangeFormCheck(ele) {
    var ck = $(ele).attr('checked');
    if (ck) {
        $("#" + $(ele).attr('partner')).val('true');
    } else {
        $("#" + $(ele).attr('partner')).val('false');
    }
    return false;
}

function SubmitWithCheck(formid, url) {
    var msg = checkform();
    if (msg != "") {
        jQuery.jMessageBox.settings.yesButtonText = '确定';
        jQuery.jMessageBox.show('系统消息', msg);
        return;
    }
    var form;
    if (formid) {
        form = $('#' + formid);
    } else {
        form = $('form')[0];
        if (form && $(form).attr('id') == 'search-form')
            form = $('form')[1];
    }
    if (form) {
        if (url)
            form.attr('action', url);
        form.submit();
    }
    return false;
}

function SubmitWithCheck4(formid, url,tableid) {
    var msg = checkform(tableid);
    if (msg != "") {
        jQuery.jMessageBox.settings.yesButtonText = '确定';
        jQuery.jMessageBox.show('系统消息', msg);
        return;
    }
    var form;
    if (formid) {
        form = $('#' + formid);
    } else {
        form = $('form')[0];
        if (form && $(form).attr('id') == 'search-form')
            form = $('form')[1];
    }
    if (form) {
        if (url)
            form.attr('action', url);
        form.submit();
    }
    return false;
}

//function SubmitWithCheck2(formid, url) {
//    alert("2");
//    var msg = checkform();
//    if (msg != "") {
//        jQuery.jMessageBox.settings.yesButtonText = '确定';
//        jQuery.jMessageBox.show('系统消息', msg);
//        return;
//    }
//    var form = $('#' + formid);
//    form.attr('action', url);

//    alert("3");
//    form[0].submit();
//}

function SubmitWithCheck2(formid, url) {
    var msg = checkform();
    if (msg != "") {
        jQuery.jMessageBox.settings.yesButtonText = '确定';
        jQuery.jMessageBox.show('系统消息', msg);
        return;
    }
    var form = $('#' + formid);
    form.attr('action', url);
    form[0].submit();
}

//验证方法
function checkform() {
    var msg = "";
    $("table").find("input").each(function() {
        var value = $(this).val();
        var requiredcheck = $(this).attr("required");
        if (requiredcheck != undefined) {
            if (value == null || value.trim() == '') {
                //($(this).attr("requiredmsg"));
                msg += "<br/>" + $(this).attr("requiredmsg");
            }
        }

        var rangecheck = $(this).attr("range");
        if (rangecheck != undefined) {
            if (!(value == null || value.trim() == '')) {
                var arrRange = rangecheck.split(',');
                var min = parseInt(arrRange[0]);
                var max = parseInt(arrRange[1]);
                if (value.trim().length < min || value.trim().length > max)
                //alert($(this).attr("rangemsg"));
                    msg += "<br/>" + $(this).attr("rangemsg");
            }
        }

        var regexcheck = $(this).attr("regex");
        if (regexcheck != undefined) {
            if (!(value == null || value.trim() == '')) {
                var partern = new RegExp(regexcheck);
                if (!partern.test(value))
                //alert($(this).attr("regexmsg"));
                    msg += "<br/>" + $(this).attr("regexmsg");
            }
        }

    });
    return msg;
}


//验证方法
function checkform(obj) {
    var msg = "";
    $("#" + obj).find("input").each(function() {
        var value = $(this).val();
        var requiredcheck = $(this).attr("required");
        if (requiredcheck != undefined) {
            if (value == null || value.trim() == '') {
                //($(this).attr("requiredmsg"));
                msg += "<br/>" + $(this).attr("requiredmsg");
            }
        }

        var rangecheck = $(this).attr("range");
        if (rangecheck != undefined) {
            if (!(value == null || value.trim() == '')) {
                var arrRange = rangecheck.split(',');
                var min = parseInt(arrRange[0]);
                var max = parseInt(arrRange[1]);
                if (value.trim().length < min || value.trim().length > max)
                //alert($(this).attr("rangemsg"));
                    msg += "<br/>" + $(this).attr("rangemsg");
            }
        }

        var regexcheck = $(this).attr("regex");
        if (regexcheck != undefined) {
            if (!(value == null || value.trim() == '')) {
                var partern = new RegExp(regexcheck);
                if (!partern.test(value))
                //alert($(this).attr("regexmsg"));
                    msg += "<br/>" + $(this).attr("regexmsg");
            }
        }

    });
    return msg;
}


function Link(url) {
    window.location.href = url;
    return false;
}


function ListTableTrdbClick(tr, url) {
    if (tr != null && tr != undefined && url != null && url != undefined) {
        var id = $(tr).find("input[type='checkbox']").eq(0).attr("id").replace('cb_', '');
        Link(url + id);
    }

    //隐藏菜单
    window.parent.HideMenu();
}

function SubmitEdit(formid, url) {
    var form;
    if (formid) {
        form = $('#' + formid);
    } else {
        form = $('form')[0];
        if (form && $(form).attr('id') == 'search-form')
            form = $('form')[1];
    }
    if (form) {
        var checklist = $(form).find("input[type='checkbox']:checked");
        if (typeof (checklist) != "undefined") {
            var cbcount = checklist.length;
            if (cbcount == 1) {
                url = url + $(form).find("input[type='checkbox']:checked").eq(0).attr("id").replace('cb_', '');
            }
            else {
                jQuery.jMessageBox.settings.yesButtonText = '确定';
                jQuery.jMessageBox.show('系统消息', "一次只能编辑一项<br/>请重新选择需要编辑的项！");
                return false;
            }
        }

        if (url) {
            Link(url);
        }

        //隐藏菜单
        window.parent.HideMenu();
    }
    return false;
}

function SubmitOther(formid, url, oper) {
    var form;
    if (formid) {
        form = $('#' + formid);
    } else {
        form = $('form')[0];
        if (form && $(form).attr('id') == 'search-form')
            form = $('form')[1];
    }
    if (form) {
        var checklist = $(form).find("input[type='checkbox']:checked");
        if (typeof (checklist) != "undefined") {
            var cbcount = checklist.length;
            if (cbcount == 1) {
                url = url + $(form).find("input[type='checkbox']:checked").eq(0).attr("id").replace('cb_', '');
            }
            else {
                jQuery.jMessageBox.settings.yesButtonText = '确定';
                jQuery.jMessageBox.show('系统消息', "一定要选择" + oper + "一项<br/>请重新选择需要" + oper + "的项！");
                return false;
            }
        }

        if (url)
            Link(url);
    }
    return false;
}

function SubmitOtherQueryString(formid, url, oper, oth) {
    var form;
    if (formid) {
        form = $('#' + formid);
    } else {
        form = $('form')[0];
        if (form && $(form).attr('id') == 'search-form')
            form = $('form')[1];
    }
    if (form) {
        var checklist = $(form).find("input[type='checkbox']:checked");
        if (typeof (checklist) != "undefined") {
            var cbcount = checklist.length;
            if (cbcount == 1) {
                url = url + $(form).find("input[type='checkbox']:checked").eq(0).attr("id").replace('cb_', '') + oth;
            }
            else {
                jQuery.jMessageBox.settings.yesButtonText = '确定';
                jQuery.jMessageBox.show('系统消息', "一定要选择" + oper + "一项<br/>请重新选择需要" + oper + "的项！");
                return false;
            }
        }

        if (url)
            Link(url);
    }
    return false;
}


function Submit(formid, url) {
    /*
    本函数在系统中为删除专用 
    zhangqing 2010-08-04 添加删除提示
    */

    jQuery.jMessageBox.show({
        width: 500,
        title: '系统提示',
        message: "您确定要删除选中的记录吗？删除以后将无法恢复",
        yesButton: {
            text: '确定',
            click: function() {

                var form;
                if (formid) {
                    form = $('#' + formid);
                } else {
                    form = $('form')[0];
                    if (form && $(form).attr('id') == 'search-form')
                        form = $('form')[1];
                }
                if (form) {
                    if (url)
                        form.attr('action', url);
                    form.submit();
                    //是否需要验证
                    //        var formValidations = $(form).get(0)[Sys.Mvc.FormContext._formValidationTag];
                    //        if (formValidations) {
                    //            
                    //        }
                    //        else {
                    //            form.submit();
                    //        }
                }
                return false;

                jQuery.jMessageBox.hide();
            }
        },

        cancelButton: {
            text: '关闭',
            click: function() {
                jQuery.jMessageBox.hide();
            }
        }
    });
}


function SubmitOnOperNoMetion(formid, url, oper) {
    var form;
    if (formid) {
        form = $('#' + formid);
    } else {
        form = $('form')[0];
        if (form && $(form).attr('id') == 'search-form')
            form = $('form')[1];
    }
    if (form) {
        if (url)
            form.attr('action', url);
        form.submit();
    }
}

function SubmitOnOperByPosi(url, method, oper, position) {
    jQuery.jMessageBox.show({
        width: 500,
        title: '系统提示',
        message: oper,
        yesButton: {
            text: '确定',
            click: function() {
                var form = $("" + position + "")[1];
                form.action = url;
                form.method = method;
                form.submit();
                return false;

                jQuery.jMessageBox.hide();
            }
        },

        cancelButton: {
            text: '关闭',
            click: function() {
                jQuery.jMessageBox.hide();
            }
        }
    });
}

function SubmitOnOper(formid, url, oper) {
    jQuery.jMessageBox.show({
        width: 500,
        title: '系统提示',
        message: oper,
        yesButton: {
            text: '确定',
            click: function() {

                var form;
                if (formid) {
                    form = $('#' + formid);
                } else {
                    form = $('form')[0];
                    if (form && $(form).attr('id') == 'search-form')
                        form = $('form')[1];
                }
                if (form) {
                    if (url)
                        form.attr('action', url);
                    form.submit();
                }
                return false;

                jQuery.jMessageBox.hide();
            }
        },

        cancelButton: {
            text: '关闭',
            click: function() {
                jQuery.jMessageBox.hide();
            }
        }
    });
}



//jMessageBox扩展
//MyMessagebox.prototype.show = function() {
//    jQuery.jMessageBox.show({
//        width: 350,
//        title: this.title,
//        message: this.messag,
//        focus: false,
//        yesButton: {                      //如果不要这个按纽可不写这一块
//            text: '确定',
//            click: function() {
//                this.retuen = true;
//                jQuery.jMessageBox.hide();
//                //                this.functionTrueGo;
//            }
//        },
//        cancelButton: {                //如果不要这个按纽可不写这一块
//            text: '取消',
//            click: function() {
//                this.retuen = false;
//                jQuery.jMessageBox.hide();
//                //                this.functionFalseGo;
//            }
//        }
//    });
//}





//显示隐藏搜索框
$(function() {

    var bn_searcht = $("#bn_seachtable");
    var searchState = $.cookie("searchState");
    var searchTable = $(".search-table");
    if (searchTable.attr("show") != 'true') {
        if (searchState) {
            if (searchState == "none")
                bn_searcht.val("显示搜索");
            else
                bn_searcht.val("隐藏搜索");
            searchTable.css("display", $.cookie("searchState").replace("$", ""));

        }
        else
            searchTable.css("display", "none");
    }

    bn_searcht.click(function() {
        var seachTb = $("table.search-table");
        if (seachTb.css("display") == "none") {
            $(this).val("隐藏搜索");
            seachTb.css("display", "");
            $.cookie("searchState", "$");
        }
        else {
            $(this).val("显示搜索");
            seachTb.css("display", "none");
            $.cookie("searchState", "none");
        }
    });

});
//json日期乱码转换(少算了一天)
//function ChangeJsonDate(obj) {
//    $.ajax({
//        type: "get",
//        url: "/Home/ChangeJsonDate",
//        dataType: 'json',
//        data: { obj: obj },
//        cache: false,
//        async: false,
//        success: function(data) {
//            result = data;
//        }
//        
//    });
//     return result;
// }
//json日期乱码转换
function ChangeDateFormat(cellval) {
    if (cellval == null || cellval == "")
        return "";
    var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return date.getFullYear() + "-" + month + "-" + currentDate;
}



//8位数日期验证
function CheckDateFormat2(obj) {
    var isValidate = true;
    try {
        if (obj.length == 8 || obj.length == 10) {

            var year = "";
            var month = "";
            var day = "";
            if (obj.length == 8) {
                year = obj.substring(0, 4);
                month = obj.substring(4, 6);
                day = obj.substring(6, 8);
            }
            else if (obj.length == 10) {
                year = obj.split('-')[0];
                month = obj.split('-')[1];
                day = obj.split('-')[2];
            }
            if (month.substring(0, 1) == '1')
                month = parseInt(month);
            else if (month.substring(0, 1) == '0')
                month = parseInt(month.substring(1));

            if (day.substring(0, 1) == '1')
                day = parseInt(day);
            else if (day.substring(0, 1) == '0')
                day = parseInt(day.substring(1));

            if (month == 2) {
                if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
                    if (day > 29)
                        isValidate = false;
                }
                else {
                    if (day > 28)
                        isValidate = false;
                }
            }
            else if (day > 31 || day < 1)
                isValidate = false;
            else if (month > 12 || month < 1)
                isValidate = false;
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (parseInt(day) > 30)
                    isValidate = false;
            }
        }
        else if (obj.length == 0) {
            //表示空值不验证
        }
        else {
            isValidate = false;
        }

    }
    catch (e) { isValidate = false; }
    return isValidate;
}

// 添加输入框的focus效果
function AddInputsStyles() {
    $('.edit-table td:not(.caption) input').each(function() { InputsOnFocus($(this)); });
    $('.edit-table td:not(.caption) select').each(function() { InputsOnFocus($(this)); });
    $('.edit-table td:not(.caption) textarea').each(function() { InputsOnFocus($(this)); });
}

function InputsOnFocus(ele) {
    $(ele).focus(
        function() {
            var td = $(this).parents('td').eq(0);
            if (td.is('td')) {
                td.addClass('focus');
            }
        }
    ).blur(
        function() {
            var td = $(this).parents('td').eq(0);
            if (td.is('td')) {
                td.removeClass('focus');
            }
        }
    );
}

//构建json字符串 CreateJson(".edit-table input,select")
function CreateJson(path) {
    var obj = new Object();
    objstr = "";
    $(path).each(function() {
        if ($(this).attr("type") != null && $(this).attr("type").toLowerCase() == "checkbox") {
            objstr += "obj." + $(this).attr("name") + "=\"" + ($(this).attr("checked") ? 1 : 0) + "\";";
        }
        else if ($(this).attr("type") != null && $(this).attr("type").toLowerCase() == "select") {
            objstr += "obj." + $(this).attr("name") + "=\"" + $(this).find("option:selected").attr("value") + "\";";
        }
        else {
            if ($(this).attr("rel") != "suggestTable" && $(this).attr("rel") != "suggest" && $(this).attr("name") != "selectPageSize" && $(this).attr("name").length > 0) {
                objstr += "obj." + $(this).attr("name") + "=\"" + $(this).val().replace(new RegExp("'", "gm"), "@").replace(new RegExp("\"", "gm"), "#") + "\";";
            }
            else if ($(this).attr("rel") == "suggest" && "#" + $(this).attr("id") == $(this).attr("datacontainer")) {
                objstr += "obj." + $(this).attr("name") + "=\"" + $(this).val().replace(new RegExp("'", "gm"), "@").replace(new RegExp("\"", "gm"), "#") + "\";";
            }
        }
    });
    var tesss = objstr;
    eval(tesss);
    return $.toJSON(obj);
}



//判断编辑页的内容是否改变
function checkSubmit(id) {
    var oChanged = false;

    $("#" + id).find("input:text,input:password,input:file,input:hidden,TextArea").each(function() {
        if ($(this).attr("orgvalue") != undefined && $(this).attr("orgvalue") != null && $(this).attr("orgvalue") != $(this).val()) {
            oChanged = true;
        }
    }
    );

    $("#" + id).find("input:checkbox,input:radio").each(function() {
        if ($(this).attr("orgvalue") != undefined && $(this).attr("orgvalue") != null && $(this).attr("orgvalue") != $(this).attr("checked")) {
            oChanged = true;
        }

    });

    $("#" + id).find("select").each(function() {

        var defaultIndex = 0;

        if ($(this).attr("selectIndex") != null) {
            defaultIndex = parseInt($(this).attr("selectIndex"));
        }
        else {
            $(this).find("option").each(function() {
                if ($(this).attr("orgvalue") != undefined && $(this).attr("orgvalue") != null && $(this).attr("orgvalue")) {
                    defaultIndex = $(this).attr("index");
                }
            });
        }

        if ($(this).find("option:selected").attr("index") != defaultIndex) {
            oChanged = true;
        }

    });
    return oChanged;
}

//默认tbody中的第一个行被选中
//obj table id
function TableTbodyFristRowSelected(obj) {
    if (obj != null && obj != undefined && $("#" + obj) != null && $("#" + obj) != undefined) {
        if ($("#" + obj + " tbody tr").length > 0)
            $("#" + obj + " tbody tr").first().click();
    }
}

//原项目转移过来的
//乘法函数，用来得到精确的乘法结果
//说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
//调用：accMul(arg1,arg2)
//返回值：arg1乘以arg2的精确结果
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

//除法函数，用来得到精确的除法结果
//说明：javascript的除法结果会有误差，在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
//调用：accDiv(arg1,arg2)
//返回值：arg1除以arg2的精确结果
function accDiv(arg1, arg2) {
    var t1 = 0, t2 = 0, r1, r2;
    try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
    try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""))
        r2 = Number(arg2.toString().replace(".", ""))
        return (r1 / r2) * pow(10, t2 - t1);
    }
}

function GetModifyText(i) {
    var result = "";
    switch (i) {
        case "0":
            result = "不变";
            break;
        case "1":
            result = "修改";
            break;
        case "2":
            result = "删除";
            break;
        case "3":
            result = "新增";
            break;
    }
    return result;
}

/*
功能:控制用户输入框中的值长度
描述:当文本框中的字符包含中文以及英文的都给指定长度，1个中文=2个字符
作者:童勇雷
时间:2011-12-08
引用方式:maxlength="4" onpropertychange="subObjMaxlength(this)" onBlur="subObjMaxlength(this)"
*/
function subObjMaxlength(obj) {
    try {
        var maxlen = obj.maxLength;
        var strValue = obj.value;
        var strTemp = "";
        var i, sum;
        sum = 0;
        for (i = 0; i < strValue.length; i++) {
            if ((strValue.charCodeAt(i) >= 0) && (strValue.charCodeAt(i) <= 255))
                sum = sum + 1;
            else
                sum = sum + 2;
            if (sum <= maxlen) {
                strTemp += strValue.charAt(i);
            }
            else {
                obj.value = strTemp;
                break;
            }
        }
    } catch (e) { return e; }
}

/*--------------------------------实现jMessageBox拖动效果--------------------------------*/
var objMove = null;
var pX = 0;
var pY = 0;
document.onmouseup = MUp;
document.onmousemove = MMove;
function MDown(objMoveId) {
    var hitpoint = event.srcElement;

    if (hitpoint.tagName == "INPUT"

        || hitpoint.tagName == "TEXTAREA"

        || hitpoint.tagName == "SELECT") {

        objMove = null;

        return;

    }



    objMove = document.getElementById(objMoveId);

    if (objMove == null) {

        return;

    }



    objMove.style.cursor = "move";

    //objMove.setCapture();

    pX = event.x - objMove.style.pixelLeft;

    pY = event.y - objMove.style.pixelTop;

    //window.alert (pX + "," + pY);

}

function MMove() {

    if (objMove != null) {

        objMove.style.left = event.x - pX;

        objMove.style.top = event.y - pY;

    }

}

function MUp() {

    if (objMove != null) {

        //objMove.releaseCapture();

        objMove.style.cursor = "default";

        objMove = null;

    }

}

function resetDialog(objID) {
    var Obj = document.getElementById(objID).style;
    Obj.left = (document.body.offsetWidth - 300) / 2;
    Obj.top = (document.body.offsetHeight - 150) / 2;

}

// start  通过javascript获得url参数
function GetUrlParms() {
    var args = new Object();
    var query = location.search.substring(1); //获取查询串   
    var pairs = query.split("&"); //在逗号处断开   
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('='); //查找name=value   
        if (pos == -1) continue; //如果没有找到就跳过   
        var argname = pairs[i].substring(0, pos); //提取name   
        var value = pairs[i].substring(pos + 1); //提取value   
        args[argname] = unescape(value); //存为属性   
    }
    return args;
}

//使用方法：
//var args = new Object();
//args = GetUrlParms();
//如果要查找参数key:
//value = args[key]
// end 通过javascript获得url参数
