/*************************
*功能：销项发票退票
*创建人：张振华
*时间：2010-08-15
*备注：此时间是加注释时间
**************************/

var strSaveTypeP040203; //保存类型
var strDivP040203 = "divP040202";
var strTableP040203 = "Tp040203";

//弹出层
function showDialogP040203(strType) {
    try {

        //清除浮动层
        $("div .operate").hide();

        //记录保存标志位
        strSaveTypeP040203 = strType;

        //弹出空的层
        creatDiv_ShowDlgHaveTbl(strDivP040203, "销项发票退票", true, true, strTableP040203, "420");

        //向层里面添加数据
        if (strSaveTypeP040203 == "1") {
            addDivContentP040203("", "");
        }
        else if (strSaveTypeP040203 == "2") {
            addDivContentP040203("2", objSelRowP0402.djbh);
        }

        //显示发票编号
        $("#" + strDivP040203 + "_pop_box_top").append("<h3 id='idH3'>发票编号：" + objSelRowP0402.fpbh + "</h3>");

        //移除保存新增按钮
        $("div .btn02", "#" + strDivP040203).remove();
        $("div .btn03 a", "#" + strDivP040203).css('margin-left', "-50");
    }
    catch (e) {
        proceError("P040203", e.ToString());
    }
}

//添加弹出层
function addDivContentP040203(strType, strDjbh) {

    //常用变量定义
    var i = 0;
    var config = ParseData(window.external.DoOptions(strTableP040203, "GetTableConfig", "{}"));
    var item;
    var strForm;
    var objRecord;

    //退票数据
    if (strDjbh != "") {
        objRecord = ParseData(window.external.DoOptions(strTableP040203, "GetList", "{\"djbh\":\"" + strDjbh + "\"}"));
    }

    //加载头部层内容
    strForm = "<ul>";
    for (i = 0; i < config.pageFooter.length; i++) {
        Item = config.pageFooter[i];
        if (Item.hidden == true) {
            continue;
        }
        if (Item.edittype == "text") {
            if (Item.index == "bz") {
                strForm += "<li class='bz'>";
            }
            else if (Item.index == "tpdate") {
                strForm += "<li class='inp_70'>";
            }
            else {
                strForm += "<li class='inp_150'>";
            }
            strForm += Item.xsmc + "：";
            strForm += "<input type='text'  name='" + Item.index + "' ";
            strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'  "
            strForm += " value=''  ";
            strForm += " id='" + Item.index + "'>";
            strForm += "</li>";
        }
    }
    strForm += "</ul>";

    //添加到弹出层里
    $("#" + strDivP040203 + "_divform").append(strForm);

    //退票日期
    $("#tpdate", "#" + strDivP040202).val(getNowDate());
    inputAndBindData("tpdate");
    $("#tpdate", $("#" + strDivP040202 + "_divform")).blur(function () {
        $("#" + strDivP040202 + "_divform").data("change", true);
    });

    //制单人
    $("#czydm", "#" + strDivP040203).attr("disabled", true);
    $("#czydm", "#" + strDivP040203).val(window.parent.objLoginInfo.name);

    //修改的时候取数据
    if (strType == "2") {
        $("#tpdate", "#" + strDivP040203).attr("disabled", true);
        $("#tpdate", "#" + strDivP040203).val(objRecord.Records[0].tpdate);
        $("#czydm", "#" + strDivP040203).val(objRecord.Records[0].czymc);
        $("#bz", "#" + strDivP040203).val(escape(objRecord.Records[0].bz));
    }

    //设定状态
    $(":input", "#" + strDivP040203 + "_divform").change(function () {
        $("#" + strDivP040203 + "_divform").data("change", true);
    });
    $("#" + strDivP040203 + "_divform").data("change", false);

    //设置弹出层位置
    $("#" + strDivP040203).dialog("option", "position", "center");

    setTimeout('$("#bz", "#" + strDivP040203).focus()', 100);
}

//保存到数据库
function saveToDbP040203(dialog, save) {

    //数据库返回结果
    var objRecord;

    //保存到数据库中的字符串
    var strData = "{";
    strData += "\"djbh\":\"" + objSelRowP0402.djbh + "\"";
    strData += ",\"tpdate\":\"" + $("#tpdate", "#" + strDivP040203).val() + "\"";
    strData += ",\"czydm\":\"" + window.parent.objLoginInfo.code + "\"";
    strData += ",\"bz\":\"" + trim($("#bz", "#" + strDivP040203).val()) + "\"";
    strData += "}";

    if (strSaveTypeP040203 == "1") {
        //新增
        objRecord = ParseData(window.external.DoOptions("Tp040203", "Add", strData));
    }
    else if (strSaveTypeP040203 == "2") {
        //修改
        objRecord = ParseData(window.external.DoOptions("Tp040203", "Edit", strData));
    }

    //向数据库中添加失败
    if (objRecord.ReturnCode != 0) {
        window.parent.cihoAlert(objRecord.MSG);
        return;
    }

    //退票后改变前台的状态
    $("#" + strTableP0402).setCell(objSelRowP0402.id, "zt", "2");

    cihoDlgClose(dialog);

    //设定层的状态
    $("#" + strDivP040202 + "_divform").data("change", false);
}

//检查表单
function checkFormP040203() {

    var strDate = $("#tpdate", "#" + strDivP040203).val();
    // 退票日期不能为空
    if (strDate == "") {
        window.parent.cihoAlert(msg.dt.inputTpDt, null, function () { $("#tpdate", "#" + strDivP040203).focus(); });
        return false;
    }

    //验证日期格式
    if (checkDatePattern(strDate) == false) {
        window.parent.cihoAlert(msg.dt.dtTypeErr, msgBox.dlgBtn.Ok, function () { $("#tpdate", "#" + strDivP040203).focus(); });
        return false;
    }

    //验证日期在规定日期中
    if (strDate) {
        strDate = strDate.replace(/-/g, "/");
        if (strDate < window.parent._startDate) {
            window.parent.cihoAlert(msg.other.djrqExtent, null, function () { $("#tpdate", "#" + strDivP040203).focus(); });
            return false;
        }

    }
}

//保存数据
function saveMyDataTp040203(dialog, save) {

    //验证表单
    if (checkFormP040203() == false) {
        return;
    }

    //保存数据
    saveToDbP040203(dialog, save);
}