var project_obj = null ;

$(document).ready(function() 
{ 
	$('#btnProjectClear').button().bind('click',clearProject) ;
	$('#btnProjectUpdate').button().bind('click',updateProject) ;
	$('#btnProjectAdd').button().bind('click',addProject) ;
	$('#btnProjectPrint').button().bind('click',printProject) ;
	$('#project-tabs').tabs() ;
	$('#txtProjectTitle').focus() ;
	$(window).resize(resizeProjectGrid) ;
	resizeProjectGrid() ;
	clearProject();
}) ;
function resizeProjectGrid() {
	var h = $("#sbg-center-panel").outerHeight() - $("#sbg-project-entry").outerHeight() - 55;
	$("div#sbg-project-data").css("height", h +'px') ;		
}
function clearProject() {
	$('#txtProjectId').val("Auto") ;
	$('#txtProjectDesc').val("") ;
	$('#txtProjectTitle').val("") ;
	$('#txtProjectCode').val("") ;
	$('#txtProjectCust').val(0) ;
	$('#project-tabs').tabs("option", "active", 0) ;
	$('#txtProjectTitle').focus() ;
	$('#btnProjectUpdate').prop('disabled','disabled') ;
	$('#project_err_mesg').text('') ;
	$('#project_err_desc').hide() ;
	$('#project_err_title').hide() ;
	$('#project_err_code').hide() ;
	if (project_obj != null && project_obj != undefined)
	{
		$($(project_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	project_obj = null;
}
function updateProject() {
	saveProject(C_UPDATE) ;
}
function addProject() {
	saveProject(C_ADD) ;
}
function saveProject(type) {
	if (!validateProject()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtProjectId').val(), 
                    "title": $('#txtProjectTitle').val(), 
                    "desc": $('#txtProjectDesc').val(), 
                    "code": $('#txtProjectCode').val(),
                    "customer": $('#txtProjectCust').val()};
	var url = "request.pzx?c=" + project_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onProjectResponse,project_obj) ;
}

function editProject(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + project_url + "&d=" + new Date().getTime();		
	clearProject();
	project_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showProject,obj) ;
}

function deleteProject(id,obj) {
	if (confirm("Confirm you want to delete Project id : " + id + "?") == false) {
		return false;
	}
	
	var data = {"type": C_DELETE,"id": id} ;
	var url = "request.pzx?c=" + project_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onProjectResponse,obj) ;
}
function showProject(obj,resp) {
	if (resp.status != C_OK) {
		showDialog("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtProjectId').val(resp.data.id) ;
	$('#txtProjectTitle').val(resp.data.title) ;
	$('#txtProjectDesc').val(resp.data.desc) ;
	$('#txtProjectCode').val(resp.data.code) ;
	$('#txtProjectCust').val(resp.data.customer) ;
	$('#btnProjectUpdate').prop('disabled','') ;
	$('#txtProjectTitle').focus() ;
}
function printProject() {
	var url = "report.pzx?c=" + project_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onProjectResponse(obj,resp) {
	if (resp.status != C_OK) {
		showDialog("Error",resp.mesg) ;
		return;
	}
	if (resp.type == C_ADD) {
		var customer = ($('#txtProjectCust').val()==0)?'-':$('#txtProjectCust :selected').text();
		$('#sbg-project-table tr:first').after("<tr><td>" + resp.data + "</td>" + 
			"<td>" + $('#txtProjectCode').val() + "</td>" +
			"<td>" + $('#txtProjectTitle').val() + "</td>" +  
			"<td>" + $('#txtProjectDesc').val() + "</td>" + 
			"<td>" + customer + "</td>" +
			"<td style='text-align:center'><a href='javascript:' onclick='editProject(" + resp.data + ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" +
			"<td style='text-align:center'><a href='javascript:' onclick='deleteProject(" + 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) {
		var customer = ($('#txtProjectCust').val()==0)?'-':$('#txtProjectCust :selected').text();
		$($(obj).closest('tr')).children('td:eq(1)').html($('#txtProjectCode').val()) ;
		$($(obj).closest('tr')).children('td:eq(2)').html($('#txtProjectTitle').val()) ;
		$($(obj).closest('tr')).children('td:eq(3)').html($('#txtProjectDesc').val()) ;
		$($(obj).closest('tr')).children('td:eq(4)').html(customer) ;
	}
	$('#sbg-popup-box').html(resp.mesg) ;
	$('#sbg-popup-box').show() ;
	$('#sbg-popup-box').fadeOut(5000) ;
	clearProject() ;
}
function validateProject() {
	$('#project_err_mesg').text('') ;
	if ($('#txtProjectTitle').blank())
	{
		$('#project_err_title').show() ;
		$('#txtProjectTitle').focus() ;
		$('#project_err_mesg').text("Project title can not be blank.") ;
		return false ;
	}
	else 
		$('#project_err_title').hide() ;

	if ($('#txtProjectDesc').blank())
	{
		$('#project_err_desc').show() ;
		$('#txtProjectDesc').focus() ;
		$('#project_err_mesg').text("Project description can not be blank.") ;
		return false ;
	}
	else 
		$('#project_err_desc').hide() ;
    
	if ($('#txtProjectCode').blank())
	{
		$('#project_err_code').show() ;
		$('#txtProjectCode').focus() ;
		$('#project_err_mesg').text("Project code can not be blank.") ;
		return false ;
	}
	else 
		$('#project_err_code').hide() ;
			
	return true ;
}