var transaction_purge_obj = null ;
var m_transaction_purge_emp_idx = 0;
$(document).ready(function() 
{ 
	$('#transaction-purge-tabs').tabs() ;
	
	$('input').css('marginTop', 0); 
	$('input').css('marginBottom',0);
	$('select').css('marginTop',0) ;
	$('select').css('marginBottom',0) ;	
	$('#btnTransactionPurge').button().bind('click',collectTransactionPurge) ;
	
	var dteopt = {
		dateFormat: "dd/mm/yy",
		appendText: " - ",
		showOn: "button",
		buttonImage: "image/calendar.gif",
		buttonImageOnly: true
	};
	
	$('#txtTransactionPurgeDateFrom').datepicker(dteopt) ;
	
	var dteopt = {
		dateFormat: "dd/mm/yy",
		appendText: "dd/mm/yyyy",
		showOn: "button",
		buttonImage: "image/calendar.gif",
		buttonImageOnly: true
	};
	$('#txtTransactionPurgeDateTo').datepicker(dteopt) ;
	
	var d = new Date();
	var dd = (d.getDate() < 10 ? "0" + d.getDate() : "" + d.getDate());
	var mm = (d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : "" + (d.getMonth() + 1));
	var yyyy = d.getFullYear();
	//$('#txtTransactionPurgeDateTo').val(dd + "/" + mm + "/" + yyyy);
	//$('#txtTransactionPurgeDateFrom').val(dd + "/" + mm + "/" + yyyy);
	$('#cobTransactionPurgeDept').change(populateTransactionEmp) ;
	$('#cobTransactionPurgeResign').change(populateTransactionEmp) ;
	$('#cobTransactionPurgeCompany').change(populateTransactionEmp) ;
}) ;

function populateTransactionEmp(){
	var data = { "type": "GET_EMP",
						"dept_id": $('#cobTransactionPurgeDept').val(), 
						"company_id" : $("#cobTransactionPurgeCompany").val(), 
						"resign" : $("#cobTransactionPurgeResign").val()} ;
	var url = "request.pzx?c=" + transaction_purge_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,showTransactionPurge,transaction_purge_obj) ;
}
/*
function resizeEmpGrid(vis) {
	var h = $("#sbg-center-panel").outerHeight() - $("#emp-filter").outerHeight() - 55;
	if (vis)
		h = h - $('#sbg-emp-entry').outerHeight();
	$("div#sbg-emp-data").css("height", h +'px') ;
}*/

function collectTransactionPurge() {
	var purge_id = "";
	$('#tblTransactionPurgeEmp tr').each(function() { 
		id = $(this).find("td").eq(0).html() ;
		if (purge_id != "")
			purge_id += ",";
		purge_id += id;
	});
	if(purge_id == ""){
		$("#sbg-confirm").dialog('option', 'buttons', {
            "Cancel" : function() {
                $(this).dialog("close");
            },
            "Confirm" : function() {
                startTransactionPurge(purge_id);
                $(this).dialog("close");
            }
        });
		showConfirm('Confirm','If there is no employee selected, the transaction purge will be applied to all employees filtered by Claims Submission Date.');
	} else {
        startTransactionPurge(purge_id);
	}
}
function startTransactionPurge(purge_id) {
	if($("#txtTransactionPurgeDateFrom").val() == '' || $("#txtTransactionPurgeDateTo").val() == ''){
		showDialog("Date Error", "Please enter the date.") ;
		$("#txtTransactionPurgeDateFrom").focus();
		return;
	}
	var data = { "type": "TRANS_PURGE",
					"emp_ids"	: purge_id, 
					"date_from" : $("#txtTransactionPurgeDateFrom").val(), 
					"date_to" 	: $("#txtTransactionPurgeDateTo").val()};
	var url = "request.pzx?c=" + transaction_purge_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,showTransactionPurge,transaction_purge_obj) ;
}
function showTransactionPurge(obj,resp) {
	if (resp.status == C_OK) {
		if(resp.type == "GET_EMP"){
			$("#cobTransactionPurgeEmp").html(resp.data.empList);
		} else {
			showDialog("System Message",resp.mesg) ;
			filterEmpPurge();
		}
	}
	else {
		showDialog("System Message",resp.mesg) ;
	}
}
function addTransactionPurgeEmployee(){
	if ($('#cobTransactionPurgeEmp').val() == "") {
		alert("You must select a Employee.") ;
		return ;
	}
	m_transaction_purge_emp_idx++;
	$('#tblTransactionPurgeEmp').append("<tr id='tremp" + m_transaction_purge_emp_idx + "'>" +
		"<td style='width:50px;display:none'>"+ $('#cobTransactionPurgeEmp').val() +"</td>" +
		"<td style='width:280px;'>" + $('#cobTransactionPurgeEmp option:selected').text() + "</td>" +
		"<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeTransactionPurgeEmp(" + m_transaction_purge_emp_idx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
}
function removeTransactionPurgeEmp(id){
	$('#tremp'+id).remove() ;
	
}

