var activity_type_obj = null ;

$(document).ready(function() 
{ 
	$('#btnActivityTypeClear').button().bind('click',clearActivityType) ;
	$('#btnActivityTypeUpdate').button().bind('click',updateActivityType) ;
	$('#btnActivityTypeAdd').button().bind('click',addActivityType) ;
	$('#btnActivityTypePrint').button().bind('click',printActivityType) ;
	$('#activity-type-tabs').tabs() ;
	$('#txtActivityTypeDesc').focus() ;
	$(window).resize(resizeActivityTypeGrid) ;
	resizeActivityTypeGrid() ;
	clearActivityType();
}) ;
function resizeActivityTypeGrid() {
	var h = $("#sbg-center-panel").outerHeight() - $("#sbg-activity-type-entry").outerHeight() - 55;
	$("div#sbg-activity-type-data").css("height", h +'px') ;		
}
function clearActivityType() {
	$('#txtActivityTypeId').val("Auto") ;
	$('#txtActivityTypeDesc').val("") ;
	$('#txtActivityTypeCode').val("") ;
	$('#activity-type-tabs').tabs("option", "active", 0) ;
	$('#txtActivityTypeDesc').focus() ;
	$('#btnActivityTypeUpdate').prop('disabled','disabled') ;
	$('#activity_type_err_mesg').text('') ;
	$('#activity_type_err_desc').hide() ;
	$('#activity_type_err_code').hide() ;
	if (activity_type_obj != null && activity_type_obj != undefined)
	{
		$($(activity_type_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	activity_type_obj = null;
}
function updateActivityType() {
	saveActivityType(C_UPDATE) ;
}
function addActivityType() {
	saveActivityType(C_ADD) ;
}
function saveActivityType(type) {
	if (!validateActivityType()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtActivityTypeId').val(), 
                    "desc": $('#txtActivityTypeDesc').val(), 
                    "code": $('#txtActivityTypeCode').val()};
	var url = "request.pzx?c=" + activity_type_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onActivityTypeResponse,activity_type_obj) ;
}

function editActivityType(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + activity_type_url + "&d=" + new Date().getTime();		
	clearActivityType();
	activity_type_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showActivityType,obj) ;
}

function deleteActivityType(id,obj) {
	if (confirm("Confirm you want to delete Expense Item id : " + id + "?") == false) {
		return false;
	}
	
	var data = {"type": C_DELETE,"id": id} ;
	var url = "request.pzx?c=" + activity_type_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onActivityTypeResponse,obj) ;
}
function showActivityType(obj,resp) {
	if (resp.status != C_OK) {
		showDialog("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtActivityTypeId').val(resp.data.id) ;
	$('#txtActivityTypeDesc').val(resp.data.desc) ;
	$('#txtActivityTypeCode').val(resp.data.code) ;
	$('#btnActivityTypeUpdate').prop('disabled','') ;
	$('#txtActivityTypeDesc').focus() ;
}
function printActivityType() {
	var url = "report.pzx?c=" + activity_type_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onActivityTypeResponse(obj,resp) {
	if (resp.status != C_OK) {
		showDialog("Error",resp.mesg) ;
		return;
	}
	if (resp.type == C_ADD) {
		$('#sbg-activity-type-table tr:first').after("<tr><td>" + resp.data + "</td>" + 
			"<td>" + $('#txtActivityTypeCode').val() + "</td>" + 
			"<td>" + $('#txtActivityTypeDesc').val() + "</td>" + 
			"<td style='text-align:center'><a href='javascript:' onclick='editActivityType(" + resp.data + ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" +
			"<td style='text-align:center'><a href='javascript:' onclick='deleteActivityType(" + resp.data + ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" +
			"</tr>"); 
	} else if (resp.type == C_DELETE) {
		$($(obj).closest("tr")).remove() ;
	} else if (resp.type == C_UPDATE) {
		$($(obj).closest('tr')).children('td:eq(1)').html($('#txtActivityTypeCode').val()) ;
		$($(obj).closest('tr')).children('td:eq(2)').html($('#txtActivityTypeDesc').val()) ;
	}
	$('#sbg-popup-box').html(resp.mesg) ;
	$('#sbg-popup-box').show() ;
	$('#sbg-popup-box').fadeOut(5000) ;
	clearActivityType() ;
}
function validateActivityType() {
	$('#activity_type_err_mesg').text('') ;
	if ($('#txtActivityTypeDesc').blank())
	{
		$('#activity_type_err_desc').show() ;
		$('#txtActivityTypeDesc').focus() ;
		$('#activity_type_err_mesg').text("Expense Item description can not be blank.") ;
		return false ;
	}
	else 
		$('#activity_type_err_desc').hide() ;
    
	if ($('#txtActivityTypeCode').blank())
	{
		$('#activity_type_err_code').show() ;
		$('#txtActivityTypeCode').focus() ;
		$('#activity_type_err_mesg').text("Expense Item code can not be blank.") ;
		return false ;
	}
	else 
		$('#activity_type_err_code').hide() ;
			
	return true ;
}