/*------------------------文档信息---------------------------
///功能:      开票单位基本信息
///创建人：    ----------
///创建时间：   ---------
///修改人：     zhaixl
///修改时间：    2011.5.27
///详细说明：  本js文档在修改，新增，设置状态，加载浮动层中使用；
///             公共变量：savesign:1,保存；2，修改。
------------------------------------------------------------*/
var strTableNameP0610 = "Tkpdwxx";      //表格ID
var selRowId = 0;                            //行号

//格式化税率
function formatterP0610Percent(cellvalue, options, rowObject) {
    if (cellvalue != "") {
        return (parseFloat(cellvalue)*100).toFixed(window.parent.strPercentPlace) + "%";
    }
    else {
        return "&nbsp;";
    }
}

//浮动按钮
function selectRow_P0610(rowId) {
    selRowId = rowId;
    var btnHtml = "";
    var strZt = $("#" + strTableNameP0610).jqGrid('getCell', selRowId, "zt");
    if (strZt == "True" || strZt == "已启用") {
        btnHtml = "<a href=\"#\" onclick=\"updateP0610('" + selRowId + "');\">修改</a>-<a href=\"#\" onclick=\"deleteP0610('" + selRowId + "');return false;\">删除</a>-<a href=\"#\" onclick=\"setZtP0610('" + selRowId + "');\">停用</a>";
    }
    else {
        btnHtml = "<a href=\"#\" onclick=\"updateP0610('" + selRowId + "');\">修改</a>-<a href=\"#\" onclick=\"deleteP0610('" + selRowId + "');return false;\">删除</a>-<a href=\"#\" onclick=\"setZtP0610('" + selRowId + "');\">启用</a>";
    }
    createFloatButton.call(this, selRowId, btnHtml);
    btnHtml = null;
}

//修改操作
function updateP0610(rowId) {
    if (rowId != null) {
        alterRecord('2');
    }
    else {
        window.parent.cihoAlert(msg.edit.selEditRow);
    }
}

//删除操作
function deleteP0610(rowId) {
    window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
        if (result == msgBox.dlgRst.Yes) {
            var strJsonData = "{\"kpdwdm\":\"" + $("#" + strTableNameP0610).jqGrid("getCell", rowId, "kpdwdm") + "\"}";
            if (strJsonData!="{}") {
                var objResult = ParseData(window.external.DoOptions(strTableNameP0610, 'Delete', strJsonData));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    $("#" + strTableNameP0610).jqGrid("delRowData", rowId);
                    changeComboZt(strTableNameP0610);
                    resizeGridScroll(strTableNameP0610);
                    rowId = null;
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
            }            
            else {
                window.parent.cihoAlert(msg.del.selDelRow);
            }
        }
    });
}

//设置状态
function setZtP0610(rowId) {
    if (true == alterState(strTableNameP0610, rowId, 'zt', 'kpdwdm')) {
        changeComboZt(strTableNameP0610);
    };
    rowId = null;
}

//列表加载启用方法
function gridComplete_P0610() {
    var arrIds = $("#" + strTableNameP0610).jqGrid("getDataIDs");
    var strState = "1";
    for (var i = 0; i < arrIds.length; i++) {
        strState = $(this).getCell(arrIds[i], "zt");
        if (strState == "True") {
            //zt：状态
            $(this).setCell(arrIds[i], "zt", "已启用");
        }
        if (strState == "False") {
            $(this).setCell(arrIds[i], "zt", "已停用");
        }
    }
}

/*-----------------------------------------------------------------------------
//功能：点击添加按钮，弹出对话框
//参数：
//strType:1：添加
//2：修改
//strTableName 表名
-----------------------------------------------------------------------------*/
function alterRecord(strType) {
    try {
        $(".operate").hide();
        $("#divP0610").empty();
        //添加
        if (strType == "1") {
            selRowId = "0";
            savesign = "1"; //初次保存insert数据
        }
        //修改
        else if (strType == "2") {
            selRowId = jQuery('#' + strTableNameP0610).jqGrid('getGridParam', 'selrow');
            savesign = "2"; //update数据        
            if (selRowId == "" || selRowId == null) {
                window.parent.cihoAlert(msg.edit.selEditRow);
                return;
            }
        }
        //创建弹出修改窗口顶部按钮及风格
        creatDiv_ShowDialog("divP0610", "开票单位基本信息", false, true, strTableNameP0610);
        //添加表单到窗口中
        tableFormKpjbxx("divP0610", strType);
    } catch (e) { }
}

//生成表单，修改时，添加数据
function tableFormKpjbxx(dlgId,strType) {
    var names = $('#' + strTableNameP0610).jqGrid('getGridParam', "colNames");
    var indexs = $('#' + strTableNameP0610).jqGrid('getGridParam', "colModel");
    var sign = "";
    //表单开始
    var strForm = "";
    var strAppendTmp = "";
    strForm += "<ul>";
    for (var i = 1; i < names.length; i++) {
        strAppendTmp = "";
        //必填项，标识为红色星号
        if (indexs[i]["editrules"].required == true) {
            sign = "<font color='red'>*</font>";
        }
        else {
            sign = "";
        }
        //以下子段不显示
       if (indexs[i]["name"] == "id" || indexs[i]["name"] == "kpdwdm" || indexs[i]["name"] == "zt"
            ||indexs[i]["name"]=="a17"||indexs[i]["name"]=="a13") {
            continue;
        }
        //编号自动生成，不可编辑
        if (indexs[i]["name"] == "a2") {
            var strBh = "";
            //新增
            if (strType == "1") {
                //系统生成编号
                var strbh = ParseData(window.external.DoOptions(strTableNameP0610, 'MakeBH', '{}'));
                strBh = strbh.BH;
            }
            else {//修改
                strBh = jQuery('#' + strTableNameP0610).jqGrid('getCell', selRowId, i);
            }
            strAppendTmp += "<li>";
            strAppendTmp += names[i] + "：";
            strAppendTmp += "<input type='text'  name='" + indexs[i]["name"] + "' ";
            strAppendTmp += " value='" + strBh + "'";
            strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='10'";
            strAppendTmp += "onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>";
            strAppendTmp += "</li>";
            strForm += strAppendTmp;
            continue;
        }
        //text框判断。
        if (indexs[i]["edittype"] == "text") {
            if (indexs[i]["name"] == "a10" || indexs[i]["name"] == "a11") {
                strAppendTmp += "<li>";
                strAppendTmp += sign;
                strAppendTmp += names[i] + "：";
                strAppendTmp += "<input type='text' value='' class='decimal' name='" + indexs[i]["name"] + "'";
                strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='10'"; //限制输入长度
                strAppendTmp += " onkeypress='return checkNum(event)'>%"; //限制输入，粘贴特殊字符
                strAppendTmp += "</li>";

            } else if (indexs[i]["name"] == "a15") {
                strAppendTmp += "<li>";
                strAppendTmp += sign;
                strAppendTmp += names[i] + "：";
                strAppendTmp += "<input type='text' value='' class='numIme' name='" + indexs[i]["name"] + "'";
                strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='100'"; //限制输入长度
                strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>"; //限制输入，粘贴特殊字符
                strAppendTmp += "</li>";
            }
            else {
                strAppendTmp += "<li>";
                strAppendTmp += sign;
                strAppendTmp += names[i] + "：";
                strAppendTmp += "<input type='text' value='' name='" + indexs[i]["name"] + "'";
                strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='50'"; //限制输入长度
                strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>"; //限制输入，粘贴特殊字符
                strAppendTmp += "</li>";
            }
        }
        strForm += strAppendTmp;
    } //FOR循环结束
    strForm += "</ul>";
    $("#" + dlgId + "_divform").append(strForm);
    //chkDecimal(dlgId);
    chkIme(dlgId);
    //修改时，加载value
    if (strType.toString() == '2') {
        for (var i = 0; i < indexs.length; i++) {
            if (indexs[i]["name"] == "a10" || indexs[i]["name"] == "a11") {
                var strKpsl = $("#" + strTableNameP0610).jqGrid('getCell', selRowId, i);
                strKpsl = strKpsl.substr(0, strKpsl.length - 1);
                $('#' + indexs[i].name, '#' + dlgId + '_divform').val(strKpsl);
            }
            else {
                $('#' + indexs[i].name, '#' + dlgId + '_divform').val(jQuery('#' + strTableNameP0610).jqGrid('getCell', selRowId, i));
            }
        }
    }
    //格式化数据
    $("#a10", $("#" + dlgId + "_divform")).autoNumeric({ mDec: window.parent.strPercentPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    $("#a11", $("#" + dlgId + "_divform")).autoNumeric({ mDec: window.parent.strPercentPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    $("#" + dlgId).dialog("option", "position", "center");
    $("#a3", $("#" + dlgId + "_divform")).focus();
    //form change设定
    $(":input", $("#" + dlgId + "_divform")).change(function () {
        $("#"+dlgId+"_divform").data("change", true);
    });
    //释放内存
    names = null;
    indexs = null;
    strForm = null;
    strAppendTmp = null;
}

//check表单数据是否改变
function closeMyDlg(dlgId, type,strTableName) {
    //数据有变化
    if ($("#" + dlgId + "_divform").data("change")) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         saveMyData(dlgId, type, strTableName);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         cihoDlgClose(dlgId);
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        cihoDlgClose(dlgId);
    }
}

//保存时验证数据
function saveMyData(dlgId, type, strTableName) {
    if (true == formDataCheck(strTableName,dlgId,type)) {
        saveP0610(dlgId,strTableName, type);
    }
    strTableName = null;
    dlgId = null;
    type = null;
}
/*-----------------------------------------------------------------------------
//数据有效性验证：
//              必填项，是否为空；
//              数据格式是否正确。
//正常：返回true
//      否则false
-----------------------------------------------------------------------------*/
function formDataCheck(strTableName,dlgId,savetype) {
    //heck必填项是否为空
    if (false == dataRequired(strTableName,dlgId)) {
        return false;
    }
    //非法字符
    var filter = new RegExp("[&,*,【】,。,:,；,？,、,|,￥,！,@,#,……,-,——,·,~,A-Za-z]");
    if (filter.test($("#a11", $("#" + dlgId + "_divform")).val())) {
        window.parent.cihoAlert(msg.sz.kp.input_sz, null, getSlFocus_2);
        return false;
    }
    var filters = new RegExp("[&,*,【】,。,:,；,？,、,|,￥,！,@,#,……,-,——,·,~,A-Za-z]");
    if (filters.test($("#a10", $("#" + dlgId + "_divform")).val())) {
        window.parent.cihoAlert(msg.sz.kp.input_sz, null, getSlFocus_1);
        return false;
    }
    //根据要求税率值<=100
//    var limit = parseInt($("#a10", $("#"+dlgId+"_divform")).val());
//    if (limit > 100.1) {
//        window.parent.cihoAlert(msg.sz.kp.slLength, null, getSlFocus_1);
//        return false;
//    }
//    var limits = parseInt($("#a11", $("#"+dlgId+"_divform")).val());
//    if (limits > 100.1) {
//        window.parent.cihoAlert(msg.sz.kp.slLength, null, getSlFocus_2);
//        return false;
//    }
    //验证邮箱格式是否正确
    var strMail = trim($("#a9", $("#" + dlgId + "_divform")).val());
    if (strMail != "") {
        var strRet = checkEmail(strMail);
        if (strRet == false) {
            window.parent.cihoAlert(msg.other.emailTypeErr, null, getEmailFocus);
            return false;
        }
    }
    //check单位名称是否重复
    var arrIds = $("#" + strTableName).jqGrid("getDataIDs");
    var strZymc = trim($("#a3", $("#" + dlgId + "_divform")).val());
    var strDwbh = trim($("#a2", $("#" + dlgId + "_divform")).val());
    for (var i = 0; i < arrIds.length; i++) {
        if (strZymc == $("#" + strTableNameP0610).jqGrid("getCell", arrIds[i], "a3") && arrIds[i] != selRowId) {
            window.parent.cihoAlert(msg.sz.kp.nameQqual, msgBox.dlgBtn.Ok, function (result) { $("#a3", $("#" + dlgId + "_divform")).focus(); });
            return false;
        }
    }
    for (var intI = 0; intI < arrIds.length; intI++) {
        if (strDwbh != "" && strDwbh == $("#" + strTableNameP0610).jqGrid("getCell", arrIds[intI], "a2") && arrIds[intI] != selRowId) {
            window.parent.cihoAlert(msg.sz.kp.codeQqual, msgBox.dlgBtn.YesNo, function (result) {
                if (result == msgBox.dlgRst.Yes) {
                    saveP0610(dlgId, strTableNameP0610, savetype);
                }
                else {
                    $("#a2", $("#" + dlgId + "_divform")).focus();
                }
            });
            return false;
        }
    }
    strTableName = null;
    dlgId = null;
    strMail = null;
    arrIds = null;
    return true;
}
/*------------------获取焦点的方法---------------------------*/
function getEmailFocus() {
    $("#a9").focus();
}
function getNameFocus() {
    $("#a3").focus();
}
function getSlFocus_1() {
    $("#a10").focus()
}
function getSlFocus_2() {
    $("#a11").focus()
}
/*-----------------------------------------------------------*/

//保存数据
function saveP0610(dlgId, strTableName, saveType) {
    var strRecord = getSaveDataKpInfo(dlgId);
    var strJsonData = "";
    if (savesign == "1") {  //新增时的json串
        strJsonData = strRecord;
    }
    else if (savesign == "2") {     //修改时的json串
        strJsonData = strRecord.substring(0, strRecord.length - 1) + ",\"kpdwdm\":\"" + $("#" + strTableName).jqGrid("getCell", selRowId, "kpdwdm") + "\"}";
    }
    var strRet = saveData(strTableName, strJsonData, savesign);
    selRowId = strRet.id;
    if (strRet.ReturnCode == "0") {
        if (savesign == "1") {
            $("#" + strTableName).jqGrid("addRowData", selRowId, ParseData(strJsonData), "first");
            $("#" + strTableName).jqGrid("setCell", selRowId, "zt", "已启用");
            $("#" + strTableName).jqGrid("setCell", selRowId, "kpdwdm", strRet.kpdwdm);
            
        }
        else {
            $("#" + strTableName).jqGrid("setRowData", selRowId, ParseData(strJsonData));
        }

        //数据标志为没有change
        $("#"+dlgId+"_divform").data("change", false);
        strJsonData = null;
        savesign = "1";
        selRowId = "0";
        strRecord = null;
        strRet = null;
        //保存新增
        if (saveType == '2') {
            $(":input", $("#" + dlgId + "_divform")).val("");
            //系统生成编号
            var strBh = ParseData(window.external.DoOptions(strTableNameP0610, 'MakeBH', '{}'));
            strbh = strBh.BH;
            $("#a2").val(strbh);
            $("#a3").focus();
        }
        //退出
        else {
            cihoDlgClose(dlgId);
            dlgId = null;
            strTableName = null;
        }
        changeComboZt(strTableNameP0610);
        resizeGridScroll(strTableNameP0610);
    }
}

//组成json串
function getSaveDataKpInfo(strDialogID) {
    var strJsonData = "";
    //input标签控件的查找
    $(":input", $("#" + strDialogID + "_divform")).each(function () {
        if ($(this).attr("id") != null && $(this).attr("id") != "") {
            if ($(this).attr("type") == "text") {
                if ($(this).attr("name") == "a10" || $(this).attr("name") == "a11") {
                    var kpsl = parseFloat(($(this).val()) / 100);
                    strJsonData += "\"" + $(this).attr("id") + "\":\"" + kpsl + "\",";
                }
                else {
                    strJsonData += "\"" + $(this).attr("id") + "\":\"" + trim($(this).val()) + "\",";
                }
            }
            else if ($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio") {
                strJsonData += "\"" + $(this).attr("id") + "\":\"" + $(this).attr("checked") + "\",";
            }
        }
    });
    strJsonData = "{" + strJsonData.substr(0, strJsonData.length - 1) + "}";
    return strJsonData;
}

//浮点数验证
function chkDecimal(dlgId) {
    $(".decimal", $("#" + dlgId + "_divform")).autoNumeric({ mDec: window.parent.strPercentPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled").attr("maxlength", "5");
}

//check汉字输入
function chkIme(dlgId) {
    $("#a15,#a9,#a7", $("#"+dlgId+"_divform")).bind("paste dragenter", function () { return false; }).css("ime-mode", "disabled").attr("maxlength", "20");
}


