var imagePath = "";
var divHeight = ""; // 查询面板高度
var dataGridWidth = ""; // 查询面板宽度
var dataGridHeight = "";
var bodyWidth = 0;
var bodyHeight = 0;
var editor1 = null;
var operation = "";
var serverIp = "";
var addWinWidth = 0;  //新增窗口宽度
var addWinHeight = 0;  //新增窗口高度
var languageType = "chinese";
var statusArray = [];
var subjectArray = [];
var activePhase;
var rows=[];

//扩展Date的format方法
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    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() {
    divHeight = $("#queryDiv").height();
	dataGridWidth = $("#queryDiv").width();
	// dataGridHeight = divHeight;
	
	bodyWidth = document.body.clientWidth;
	bodyHeight = document.documentElement.clientHeight;
	addWinWidth = (bodyWidth/100)*80;
	addWinHeight = (addWinWidth/16)*8;

	//初始状态为关闭
	$("#addNewsWinId").window('close');
	$("#exportWindow").window('close');
	$("#editStatusWinId").window('close');
	$("#addEmailInfoWinId").window('close');
	$("#emailSendWinId").window('close');
    $("#subjectWinId").window('close');
	
	//初始状态是隐藏
	$("#newsTypeId01").hide();//无用
	$("#newsTypeId02").hide();
	
	//获得相应数据，在jsp中定义的JD
    var statusData=JD.statusJson;
    var subjectData=JD.subjectJson;
    //获得相应数据长度
    var length1 = 0;
    var length2 = 0;
    $.each(statusData,function(n,v){length1++});
    $.each(subjectData,function(n,v){length2++});
    statusArray = new Array(length1);
    subjectArray = new Array(length2);
    //根据相应数据长度以及值进行数组的赋值
    $.each(statusData,function(n,v){statusArray[n]= v.dataName;});
    $.each(subjectData,function(n,v){subjectArray[n]= v.dataName;});

    //初始化下来菜单用于增删改查数据
    $.each(JD.phases, function (n, v) {
        if (v.active) {
            activePhase = v.id;
        }
    });

    initDataTable();
	serverIp = document.location.hostname;
	initInputWidth();
    initExportWindow();
    initShowPhoto();


    $("#showPhotoWinId").window({
		onBeforeClose:function (){
			$("#showPhotoId").attr('src','');
		}
	})
	$("#showPhotoWinId").window('close');

    // $.each(subjectArray,function(n,v){
    //     var href = "/admin/business/registerManage/exporgExcel2?&phase="+activePhase+"&bigger=1&subject="+n+
    //         "&fields=submitTime,city,name,gender,birthday,telphone,mobileTel,email,company,address,invoiceTitle,invoiceType,dcNumber,title,dateOfBirth,gender_en,name_en,name_en_last,company_en,address_en,country,phone,email,,city_en,subject";
    //     var html="<a href='"+href+"' class='exp-btn'>"+v+"</a>";
    //     $(".quick-export").append(html);
    // })
    
});
/*
 * 初始化查看图片窗口
 */
function initShowPhoto(){

	$("#showPhotoWinId").window({
		title : '查看图片',
		width : addWinWidth/1.5,
		height : addWinHeight, 
		left : (dataGridWidth - addWinWidth),
		top : (bodyHeight - addWinHeight),
		closable : true, // 否显示关闭按钮
		collapsible : false, // 是否显示折叠按钮
		minimizable : false, // 是否显示最小化按钮
		maximizable : false, // 是否显示最大化按钮
		close : true, // 初始是否关闭
		modal : true 
	});  
	$(".certificatePhoto").click(function(){
		var photoId = $(this).attr('src');
		$("#showPhotoId").attr('src',photoId);
		$("#showPhotoWinId").window('open');
	})
}

function initExportWindow(){
    $(".dropable .ele .up").on("click",function(){
        $from = $(this).parent().parent();
        $to = $from.prev(".block");
        if($to.length>0){
            $to.insertAfter($from);
        }
    });
    $(".dropable .ele .down").on("click",function(){
        $from = $(this).parent().parent();
        $to = $from.next(".block");
        if($to.length>0){
            $to.insertBefore($from);
        }
    });
}

function initDataTable(){
	var h = document.documentElement.clientHeight;
	$("#registersTable").datagrid({
		height: (h - divHeight) / 3 * 2 - 30,
		nowrap : true,
		striped : true,
		pagination : true,
		rownumbers : true,
        singleSelect : true,
		pageList : [10,20,30,40,50],
		autoRowHeight : false, 
		fitColumns : true,
        queryParams : {
            phase:activePhase
        },
		title : "用户首次注册报名科目列表",
		url : '/admin/business/registerManage/queryAllRegisterInfo',
		columns : [ [ {
			field : 'ck',
			title : 'id',
			width : 15, 
			checkbox : true
		},{
			field : 'name',
			title : '姓名',
			align : 'left',
			width : 20,
            formatter:view
		}, {
			field : 'subject',
			title : '报名科目',
			align : 'center',
            formatter:function(val,row){
                return subjectArray[val];
            },
			width : 40
		}, {
			field : 'registerStatus',
			title : '报名状态',
			align : 'center',
            formatter:function(val,row){
                return statusArray[val];
            },
			width : 20
		},{
            field : 'phase',
            title : '报名期数',
            align : 'center',
            formatter:function(val,row){
                return row.phase.name;
            },
            width : 20
        }, {
            field : 'email',
            title : '邮箱',
            align : 'center',
            width : 30
        }, {
            field : 'submitTime',
            title : '提交时间',
            align : 'center',
            formatter:function(val,row){
                if(val==''||val==null){
                    return "";
                }
                return new Date(parseInt(val)).format("yyyy-MM-dd hh:mm:ss");
            },
            width : 20
        }] ],
        onCheck : function(index, row) { // 双击"任一行"执行
			subjectTableById(row.id)
		},
		onLoadSuccess : function(data) { // 加载成功之后执行
		}
	});
	subjectTableById(0);
}

function subjectTableById(id){
	var url = "";
	if(0 == id){
		url = '/admin/business/registerManage/queryAllSubjectInfo';
	}
	if(0 != id){
		url = '/admin/business/registerManage/queryAllSubject?id='+ id;
	}
	var h = document.documentElement.clientHeight;
	$("#subjectTable").datagrid({
		height: (h - divHeight) / 3*1+6,
		nowrap : true,
		striped : true,
		pagination : true,
		rownumbers : true,
		pageList : [5,10,15],
		singleSelect : true,
		autoRowHeight : false,
		fitColumns : true,
		queryParams : {
			phase:activePhase
		},
		title : "用户再次报名科目列表",
		url : url,
		columns : [ [ {
			field : 'ck',
			title : 'id',
			width : 15,
			checkbox : true
		}, {
			field : 'registerName',
			title : '姓名',
			align : 'center',
			width : 20
		},{
			field : 'subject',
			title : '报名科目',
			align : 'center',
			formatter:function(val,row){
				return subjectArray[val];
			},
			width : 40
		}, {
			field : 'registerStatus',
			title : '报名状态',
			align : 'center',
			formatter:function(val,row){
				return statusArray[val];
			},
			width : 20
		},
			{
				field : 'phaseName',
				title : '报名期数',
				align : 'center',
				width : 20
			},
		// 	{
		// 	field : 'appreciationTaxType',
		// 	title : '发票类型',
		// 	align : 'center',
		// 	width : 30,
        //     formatter:function (val,row) {
        //         if (1 == val){
        //             return "增值税普通发票";
        //         }
        //         if (2 == val){
        //             return "增值税专用发票";
        //         }
        //     }
		// },
			{
			field : 'taxIssue',
			title : '发票开具时间',
			align : 'center',
			width : 30,
            formatter:function (val,row) {
                if (1 == val){
                    return "款项到账当年开具";
                }
                if (2 == val){
                    return "款项到账次年开具";
                }
            }
		}, {
			field : 'submitTime',
			title : '提交时间',
			align : 'center',
			formatter:function(val,row){
				if(val==''||val==null){
					return "";
				}
				return new Date(parseInt(val)).format("yyyy-MM-dd hh:mm:ss");
			},
			width : 20
		}] ],
		onDblClickRow : function(index, row) { // 双击"任一行"执行

		},
		onLoadSuccess : function(data) { // 加载成功之后执行
		}
	});
}


function view(val,row){
    return "<a href=\"javascript:viewData("+row.id+")\">"+row.name+"</button>";
}
function status(val,row){
    return statusArray[val];
}

/**
 * 初始化各输入框宽度
 */
function initInputWidth(){

	$("#name00Id").attr("style", "width:" + fixShortWidth()/2 + "px");

	$("#standby1Id").attr("style", "width:" + fixShortWidth() + "px"); 

	$("#nameId").attr("style", "width:" + fixShortWidth() + "px");
	$("#invoiceInfo_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#name_enId").attr("style", "width:" + fixShortWidth() + "px");
	$("#name_en_lastId").attr("style", "width:" + fixShortWidth() + "px");
	$("#postId").attr("style", "width:" + fixShortWidth() + "px");
	$("#companyId").attr("style", "width:" + fixShortWidth() + "px");
	$("#company_enId").attr("style", "width:" + fixShortWidth() + "px");
	$("#addressId").attr("style", "width:" + fixShortWidth() + "px");
	$("#address_enId").attr("style", "width:" + fixShortWidth() + "px");
	$("#telphoneId").attr("style", "width:" + fixShortWidth() + "px");
	$("#titleId").attr("style", "width:" + fixShortWidth() + "px");
	$("#mobileTelId").attr("style", "width:" + fixShortWidth() + "px");
	$("#phoneId").attr("style", "width:" + fixShortWidth() + "px");
	$("#emailId").attr("style", "width:" + fixShortWidth() + "px");
	$("#email_enId").attr("style", "width:" + fixShortWidth() + "px");
	$("#invoiceTitleId").attr("style", "width:" + fixShortWidth() + "px");
	$("#addressNameId").attr("style", "width:" + fixShortWidth() + "px");
	$("#telephoneId").attr("style", "width:" + fixShortWidth() + "px");
	$("#bankAddressId").attr("style", "width:" + fixShortWidth() + "px");
	$("#trTaxpayerNumId").attr("style", "width:" + fixShortWidth() + "px");
	$("#bankAccountId").attr("style", "width:" + fixShortWidth() + "px");
	$("#dcNumberId").attr("style", "width:" + fixShortWidth() + "px");
	$("#countryId").attr("style", "width:" + fixShortWidth() + "px");
	$("#invoiceInfoId").attr("style", "width:" + fixShortWidth() + "px");

	$("#invoiceTitle_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#addressName_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#telephone_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#trTaxpayerNum_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#bankAddress_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#bankAccount_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#dcNumber_Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#remarkId").attr("style", "width:" + fixShortWidth() + "px");
	$("#provinceId").attr("style", "width:" + fixShortWidth() + "px");
	$("#chengshiId").attr("style", "width:" + fixShortWidth() + "px");
	$("#districtId").attr("style", "width:" + fixShortWidth() + "px");

	$("#appreciationTaxType_Id").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});
	$("#invoiceType_Id").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});
	$("#subject_Id").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});
	$("#registerStatus_Id").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});
	$("#taxIssue_id").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});

	$("#payTime_Id").datebox({
		width : fixShortWidth()+4,
		valueField : 'id',
		panelHeight:'auto',
		textField : 'text'
	});
	$("#taxIssue_Id").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});

	$("#city_enId").combobox({
		url : '/admin/metadataManage/getMetadatasList?dataType=encityType',
        valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#cityId").combobox({
		url : '/admin/metadataManage/getMetadatasList?dataType=cityType',
		valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#genderId").combobox({
		valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#gender_enId").combobox({
		valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#invoiceTypeId").combobox({
		url : '/admin/metadataManage/getMetadatasList?dataType=invoiceType',
		valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#genderId").combobox({
		url : '/admin/metadataManage/getMetadatasList?dataType=gender',
		valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#gender_enId").combobox({
		url : '/admin/metadataManage/getMetadatasList?dataType=engender',
		valueField : 'id',
        textField : 'text',
        panelHeight:'auto',
        width : fixShortWidth()+4
	});
	$("#payTimeId").datebox({
		width : fixShortWidth()+4,
		valueField : 'id',
		panelHeight:'auto',
        textField : 'text'
	});
	$("#date1").datebox({
		width : fixShortWidth()+4,
		valueField : 'id',
        textField : 'text'
	});
	$("#date2").datebox({
		width : fixShortWidth()+4,
		valueField : 'id',
        textField : 'text'
	});
	$("#appreciationTaxTypeId").combobox({
		width : fixShortWidth()+4,
		panelHeight:'auto'
	});
	$("#registerStatusId").combobox({
		width : fixShortWidth()+4,
		data:JD.statusJson,
        valueField : 'dataNameId',
        textField : 'dataName',
		panelHeight:'auto',
	});
	$("#registerStatusId00").combobox({
		width : fixShortWidth()+4,
		data:JD.statusJson,
        valueField : 'dataNameId',
        textField : 'dataName',
		panelHeight:'auto',
	});
	$("#registerStatus00Id").combobox({
		width : (fixShortWidth()+4)/2,
		data:JD.statusJson,
        valueField : 'dataNameId',
        textField : 'dataName',
		panelHeight:'auto',
	});
	$("#phaseId").combobox({
		data:JD.phases,
        valueField:'id',
        textField:'name',
        panelHeight:'auto',
        width : (fixShortWidth()+4)/2
	})
	$("#subjectId").combobox({
		data:JD.subjectJson,
        valueField : 'dataNameId',
        textField : 'dataName',
        panelHeight:'auto',
        width : fixShortWidth()+4,
	})
	$("#subject00Id").combobox({
		data:JD.subjectJson,
        valueField : 'dataNameId',
        textField : 'dataName',
        panelHeight:'auto',
        width : (fixShortWidth()+4)
	})

	$("#emailSenderId").combobox({
		 width : fixLongWidth()+4,
		 panelHeight:'auto',
	})
	$("#emailTitleId").attr("style","width:" + (fixLongWidth()) + "px");
	$("#emailContentId").attr("style", "width:" + (fixLongWidth()) + "px");
}
 
//查询
function queryData() {
	var name = $("#name00Id").val();
	var status = $("#registerStatus00Id").combobox('getValue');
    var phase = $("#phaseId").combobox('getValue');
    var subject = $("#subject00Id").combobox('getValue');
    var queryParams = null;
	queryData2(name,status,phase,subject);
    $("#registersTable").datagrid({
        queryParams : {
            name : name,
            phase:phase,
            status:status,
            subject:subject
        }
    });

}
function queryData2(name,status,phase,subject){
	$("#subjectTable").datagrid({
		queryParams : {
			name : name,
			phase:phase,
			status:status,
			subject:subject
		}
	});
}


//导出为excel
function exportExl(){
    var name = $("#name00Id").val();
    var status = $("#registerStatus00Id").combobox('getValue');
    var phase = $("#phaseId").combobox('getValue');
    var subject = $("#subject00Id").combobox('getValue');
    var queryParams = null;

    var out ='';
    $(".dropable .block").each(function(){
        if ($(this).find("input").get(0).checked) {
            if (out.length>0) {
                out+=",";
            };
            out+=$(this).find(".ele-name").attr("val");
        };
    });
    name = encodeURI(encodeURI(name));
    location.href="/admin/business/registerManage/exporgExcel2?name="+name+"&status="+status+"&phase="+phase+"&subject="+subject
    +"&fields="+out;
    $("#exportWindow").window('close');
}
//清空查询条件
function clearQueryForm(){
	$("#queryFormId").form('clear');
}

//打开修改状态窗口
function openEditStatusWin(){
	rows = $('#registersTable').datagrid('getSelections');
	if(rows.length != 0){
		$("#editStatusWinId").window({
			title : '修改报名状态',
			width : addWinWidth/1.5,
			height : addWinHeight/3.5, 
			left : (dataGridWidth - addWinWidth),
			top : (bodyHeight - addWinHeight)*1.5,
			closable : true, // 否显示关闭按钮
			collapsible : false, // 是否显示折叠按钮
			minimizable : false, // 是否显示最小化按钮
			maximizable : false, // 是否显示最大化按钮
			close : true, // 初始是否关闭
			modal : true 
		});  
		$("#buttonId03").show();
		$("#buttonId04").show();
		$("#editStatusForm").form('clear');
		$("#editStatusWinId").window('open');
	}else {
		$.messager.alert('提示', '请选中要修改的记录', 'info');
	}
	
} 
//保存修改报名状态
function saveData2(){
	var jsonIds = arrayTojson(rows);
	var registerStatus=$('#registerStatusId00').combobox('getValue');
	var postUrl = '/admin/business/registerManage/updateStatus';
	$.post(postUrl,{"ids":jsonIds,"registerStatus":registerStatus},function(result){
		if (result.flag){
			$.messager.alert('提示',"修改成功", 'ok');
			closeWindow2();
			initDataTable();
		} else {
			$.messager.alert("提示","修改失败",'error');
			return;
		}
	},'json');
} 
//打开新增窗口
function openAddWin(){
	$(".certificatePhoto").attr("src","");
    $('#newsForm')[0].reset();

    operation = "add";
	$("#main_area_01").height(addWinHeight);
	$("#addNewsWinId").window({
		title : '新增报名信息',
		width : addWinWidth,
		height : addWinHeight, 
		left : (dataGridWidth - addWinWidth)/2,
		top : (bodyHeight - addWinHeight)/2,
		closable : true, // 否显示关闭按钮
		collapsible : false, // 是否显示折叠按钮
		minimizable : false, // 是否显示最小化按钮
		maximizable : false, // 是否显示最大化按钮
		close : true, // 初始是否关闭
		modal : true 
	});  
	imagePath = ""; 
	$("#newsForm").form('clear');
	$("#saveButton01").show();
	$("#addNewsWinId").window('open');
}
//打开导出窗口
function openExportWin(){
    $("#exportWindow").window({
        title : '选择导出信息',
        left : "10%",
        top : "10%",
        height : addWinHeight/5*4,
        width : addWinWidth*0.3,
        closable : true, // 否显示关闭按钮
        collapsible : false, // 是否显示折叠按钮
        minimizable : false, // 是否显示最小化按钮
        maximizable : false, // 是否显示最大化按钮
        close : true, // 初始是否关闭
        modal : true
    });
    $("#exportWindow").window('open');
}

function formatDateField(selector,val){
    if(val!=''&&val!=undefined&&val!='undefined'){
        var d = new Date(parseInt(val));
        d.setTime(d.getTime() + 60*60*1000);
        $(selector).val(d.format("yyyy-MM-dd"));
        $(selector).datebox("setValue",d.format("yyyy-MM-dd"));
    }
}

//打开报名信息修改窗口
function openEditWin(){
	$(".certificatePhoto").attr("src","");
	operation = "edit";
	var rows = $('#registersTable').datagrid('getSelections');
	if(rows.length == 1){
		var row = $('#registersTable').datagrid('getSelected');
		$.getJSON("/admin/business/registerManage/queryRegisterInfoById?registerInfo=" + row.id, function(data) {
			$("#newsForm").form('load', data);
			$("#img1").attr("src",data.img1);
	    	$("#img2").attr("src",data.img2);
	    	$("#img3").attr("src",data.img3);
            formatDateField("#date1",data.birthday);
            formatDateField("#date2",data.dateOfBirth);
            formatDateField("#payTimeId",data.payTime);
            $("#main_area_01").height((bodyHeight/5)*4);
		});
        $("#addNewsWinId").window({
            title : '修改报名信息',
            width : addWinWidth,
            height : addWinHeight,
            left : (dataGridWidth - addWinWidth)/2,
            top : (bodyHeight - addWinHeight)/2,
            closable : true, // 否显示关闭按钮
            collapsible : false, // 是否显示折叠按钮
            minimizable : false, // 是否显示最小化按钮
            maximizable : false, // 是否显示最大化按钮
            close : true, // 初始是否关闭
            modal : true
        });
        $("#saveButton01").show();
        $("#addNewsWinId").window('open');
	}else {
		$.messager.alert('提示', '请选中一条记录', 'info');
	}
}

//打开报名科目修改窗口
function open_subject_win(){
    $(".certificatePhoto").attr("src","");
    operation = "edit";
    var rows = $('#subjectTable').datagrid('getSelections');
    if(rows.length == 1){
        var row = $('#subjectTable').datagrid('getSelected');
        $.getJSON("/admin/business/registerManage/querySubjectById?id=" + row.id, function(data) {
            $("#subjectForm").form('load', data);
            $("#img_1").attr("src",data.img1);
            $("#img_2").attr("src",data.img2);
            $("#img_3").attr("src",data.img3);
            formatDateField("#payTime_Id",data.payTime);
            $("#main_area_05").height((bodyHeight/5)*4);
        });
        $("#subjectWinId").window({
            title : '修改报名科目信息',
            width : addWinWidth,
            height : addWinHeight,
            left : (dataGridWidth - addWinWidth)/2,
            top : (bodyHeight - addWinHeight)/2,
            closable : true, // 否显示关闭按钮
            collapsible : false, // 是否显示折叠按钮
            minimizable : false, // 是否显示最小化按钮
            maximizable : false, // 是否显示最大化按钮
            close : true, // 初始是否关闭
            modal : true
        });
        $("#saveButton01").show();
        $("#subjectWinId").window('open');
    }else {
        $.messager.alert('提示', '请选中一条记录', 'info');
    }
}

function closeSubjectWindow() {
	$("#subjectWinId").window('close');
	$("#subjectForm").form('clear');
}

function saveSubjcetData() {
	var row = $('#subjectTable').datagrid('getSelected');
	url = "/admin/business/registerManage/updateSubject?id=" + row.id;
	$('#subjectForm').form('submit',{
		url: url,
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(data){
			var data = eval("("+data+")");
			if (data.flag){
				$.messager.alert('提示',data.info, 'ok');
				closeWindow3();
				queryData();
			} else {
				$.messager.alert("提示",data.info,'error');
			}
		}
	});
}

//打开查看窗口
function viewData(id){ 
	$(".certificatePhoto").attr("src","");
    $.getJSON("/admin/business/registerManage/queryRegisterInfoById?registerInfo=" + id, function(data) {
    	$("#newsForm").form('load', data);
    	$("#img1").attr("src",data.img1);
    	$("#img2").attr("src",data.img2);
    	$("#img3").attr("src",data.img3);
        formatDateField("#date1",data.birthday);
        formatDateField("#date2",data.dateOfBirth);
        formatDateField("#payTimeId",data.payTime);
	});
	$("#main_area_01").height((bodyHeight/5)*4);
	$("#addNewsWinId").window({
		title : '查看',
		width : addWinWidth,
		height : addWinHeight, 
		left : (dataGridWidth - addWinWidth)/2,
		top : (bodyHeight - addWinHeight)/2,
		closable : true, // 否显示关闭按钮
		collapsible : false, // 是否显示折叠按钮
		minimizable : false, // 是否显示最小化按钮
		maximizable : false, // 是否显示最大化按钮
		close : true, // 初始是否关闭
		modal : true 
	});
	$("#saveButton01").hide();
	$("#addNewsWinId").window('open');
}

//保存数据
function saveData(){
	var url = "";
	if('edit' == operation){
		var row = $('#registersTable').datagrid('getSelected');
		url = "/admin/business/registerManage/addOrUpdateRegisterInfo?operate="+ operation+"&id="+row.id;
		$('#newsForm').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(data){
				var data = eval("("+data+")");
				if (data.flag){
					$.messager.alert('提示',data.info, 'ok');
					closeWindow();
                    queryData();
				} else {
					$.messager.alert("提示",data.info,'error');
				}
			}
		}); 
	}
} 

//删除
function deleteNews(){
	var rows = $('#registersTable').datagrid('getSelections');
	if (rows.length !=0 ){
		var jsonIds = arrayTojson(rows);
		$.messager.confirm("删除","确定删除？",function(r){
			if (r){
				var postUrl = '/admin/business/registerManage/deleteByIds';
				$.post(postUrl,{
					"ids":jsonIds},function(result){
					if (result.flag){
						$.messager.alert('提示',"数据删除成功！",'ok');
						initDataTable(); 
					} else {
						$.messager.alert('提示',"数据删除失败！",'error');
						initDataTable();
						return;
					}
				},'json');
			}
		});
	}else{ 
		$.messager.alert('提示', '请选中一条要删除记录', 'info');
	}
}

//删除科目信息
function delete_subject(){
    var rows = $('#subjectTable').datagrid('getSelected');
    if (rows.length !=0 ){
        $.messager.confirm("删除","确定删除？",function(r){
            if (r){
                var postUrl = '/admin/business/registerManage/delete_subject_ById';
                $.post(postUrl,{
                    "id":rows.id},function(result){
                    if (result.flag){
                        $.messager.alert('提示',"数据删除成功！",'ok');
                        initDataTable();
                    } else {
                        $.messager.alert('提示',"数据删除失败！",'error');
                        initDataTable();
                        return;
                    }
                },'json');
            }
        });
    }else{
        $.messager.alert('提示', '请选中一条要删除记录', 'info');
    }
}

 
/**
 * id数组转换为json字符串
 */
function arrayTojson(arr) { 
	var jsonIds = "[";
	for ( var i = 0; i < arr.length; i++) {
		if (i == arr.length - 1) {
			jsonIds += arr[i].id;
		} else {
			jsonIds += arr[i].id + ",";
		}
	}
	jsonIds += "]";

	return jsonIds; 
}

//打开邮件发送窗口
function openEmailWin() {
	rows = $('#registersTable').datagrid('getSelections');
	if (rows.length !=0 ){
		$("#main_area_02").height(addWinHeight);
		$("#addEmailInfoWinId").window({
			title : '新增邮件信息',
			resizable : false,
			width : addWinWidth,
			height : addWinHeight / 2,
			left : (dataGridWidth - addWinWidth) / 2,
			top : (bodyHeight - addWinHeight / 2) / 2,
			closable : true, // 否显示关闭按钮
			collapsible : false, // 是否显示折叠按钮
			minimizable : false, // 是否显示最小化按钮
			maximizable : false, // 是否显示最大化按钮
			close : true, // 初始是否关闭
			modal : true
		});
		$("#buttonSaveForAdd").show();
		$("#buttonCancelForAdd").show();
		$("#emailInfoForm").form('clear');
		$("#addEmailInfoWinId").window('open');
	}else{ 
		$.messager.alert('提示', '请选中发送邮件的对象', 'info');
	}
}

//发送邮件
function sendEmail() {
	$("#addEmailInfoWinId").window('close');
	$("#emailSendWinId").dialog('open');
	var jsonIds = arrayTojson(rows);
	$.ajax({
		url : '/base/emailSendManage/sendEmail?jsonIds=' + jsonIds,
		type : 'post',
		data : $("#emailInfoForm").serialize(),
		success : function(data) {
			if (data.flag) {
				$.messager.alert('提示', data.info, 'ok');
			} else {
				$.messager.alert("提示", data.info, 'error');
			}
			$("#emailSendWinId").window('close');
		}
	});
}

//设置 long 宽度自适应   相对窗口
function fixLongWidth(){ 
	return addWinWidth*0.76;        
} 
           
//设置 short 宽度自适应
function fixShortWidth(){    
	return addWinWidth*0.284;            
} 
  
//关闭窗口
function closeWindow(){
	$("#addNewsWinId").window('close');
}
function closeWindow2(){
	$("#editStatusWinId").window('close');
}

//关闭窗口
function closeWindow3() {
	$("#subjectWinId").window('close');
}
