var language_obj = null ;

$(document).ready(function() 
{ 
	$('#btnLanguageClear').button().bind('click',clearLanguage) ;
	$('#btnLanguageAdd').button().bind('click',addLanguage) ;
	$('#btnLanguageUpdate').button().bind('click',updateLanguage) ;
	$('#btnLanguagePrint').button().bind('click',printLanguage) ;
	$('#language-tabs').tabs() ;
	$('#txtLanguageDesc').focus() ;
	var t;
	window.onresize = function(){
		clearTimeout(t);
		t = setTimeout(function(){
			resizeLanguageGrid() ;
		},100);
		
	}
	resizeLanguageGrid() ;
	clearLanguage() ;
}) ;
function resizeLanguageGrid() {
	var h = $("#sbg-center-panel").outerHeight() - $("#sbg-language-entry").outerHeight() - 20 - $("#sbg-language-header").outerHeight();
	$("div#sbg-language-data").css("height", h +'px') ;		

	if($('#sbg-center-panel').width() < '620'){
		$('#sbg-language-table').css('width','100%'); 	
	}else{
		$('#sbg-language-table').css('width','620px');
	}
	
	$('#sbg-language-header').css({'width':$('#sbg-language-table').outerWidth()}); 
}
function clearLanguage() {
	$('#txtLanguageId').val("Auto") ;
	$('#txtLanguageDesc').val("") ;
	$('#txtLanguageDisplay').val("") ;
	$('#language-tabs').tabs("option", "active", 0) ;
	$('#txtLanguageDesc').focus() ;
	$('#btnLanguageUpdate').prop('disabled','disabled') ;
	$('#btnLanguageAdd').prop('disabled','') ;
	$('#language_err_mesg').text('') ;
	$('#language_err_desc').hide() ;
	if (language_obj != null && language_obj != undefined)
	{
		$($(language_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	language_obj = null;
}
function updateLanguage() {
	saveLanguage(C_UPDATE) ;
}
function addLanguage() {
	saveLanguage(C_ADD) ;
}
function saveLanguage(type) {
	if (validateLanguage()) {
		var data = { "type": type, "id": $('#txtLanguageId').val(), "desc": $('#txtLanguageDesc').val(),
			"display": $('#txtLanguageDisplay').val(), "ref": ""};
		var url = "request.pzx?c=" + language_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onLanguageResponse,language_obj) ;
	}
}
function printLanguage() {
	var url = "report.pzx?c=" + language_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function editLanguage(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + language_url + "&d=" + new Date().getTime();	
	clearLanguage();
	language_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showLanguage,obj) ;
}

function deleteLanguage(id,obj) {
	if (confirm("Confirm you want to delete Language Type id : " + id + "?")) {
		var data = {"type": C_DELETE,"id": id} ;
		var url = "request.pzx?c=" + language_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onLanguageResponse,obj) ;
	}
}
function showLanguage(obj,resp) {
	if (resp.status == C_OK) {
		$('#txtLanguageId').val(resp.data.id) ;
		$('#txtLanguageDesc').val(resp.data.desc) ;
		$('#txtLanguageDisplay').val(resp.data.display) ;
		$('#btnLanguageUpdate').prop('disabled','') ;
		$('#btnLanguageAdd').prop('disabled','disabled') ;
		$('#txtLanguageDesc').focus() ;
	}
	else {
		showDialog("System Message",resp.mesg) ;
	}
}
function onLanguageResponse(obj,resp) {
	if (resp.status == C_OK) {
		if (resp.type == C_ADD) {
                    
			$('#sbg-language-table tr:first').after("<tr><td>" + resp.data + "</td>" + 
				"<td>" + $('#txtLanguageDesc').val() + "</td>" + 
				"<td>" + $('#txtLanguageDisplay').val() + "</td>" + 
				"<td style='text-align:center'><a href='javascript:' onclick='editLanguage(" + resp.data + ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" +
				"<td style='text-align:center'><a href='javascript:' onclick='deleteLanguage(" + resp.data + ",this)'><img src='image/remove_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($('#txtLanguageDesc').val()) ;
			$($(obj).closest('tr')).children('td:eq(2)').html($('#txtLanguageDisplay').val()) ;
		}
		$('#sbg-popup-box').html(resp.mesg) ;
		$('#sbg-popup-box').show() ;
		$('#sbg-popup-box').fadeOut(5000) ;
		clearLanguage() ;
	}
	else 
		showDialog("Error",resp.mesg) ;
}
function validateLanguage() {
	$('#language_err_mesg').text('') ;
	if ($('#txtLanguageDesc').blank())
	{
		$('#language_err_desc').show() ;
		$('#txtLanguageDesc').focus() ;
		$('#language_err_mesg').text("Language Type description can not be blank.") ;
		return false ;
	}
	else 
		$('#language_err_desc').hide() ;
		
	return true ;
}