var localTerm_obj = null ;

$(document).ready(function() 
{ 
	$('input').css('marginTop', 0); 
	$('input').css('marginBottom',0);
	$('select').css('marginTop',0) ;
	$('select').css('marginBottom',0) ;
	
	$('#btnLocalTermUpdate').button().bind('click',updateLocalTerm) ;
	$('#btnLocalTermPrint').button().bind('click',printLocalTerm) ;
	$('#btnLocalTermExport').button().bind('click',exportLocalTerm) ;
	
	$('#btnGetDetail').button().bind('click',getListItem) ;
	$('#localTerm-tabs').tabs() ;
	$('#txtLocalTermDesc').focus() ;
	
	clearLocalTerm();
}) ;


function clearLocalTerm() {
	$('#errLocalLangTermType').hide() ;
	$('#errLang').hide() ;
	$("tr#term-rows").remove() ;
}

function updateLocalTerm() {
	saveLocalTerm(C_UPDATE) ;
}

function saveLocalTerm(type) {
	if(validateTerms() == false)
		return false;
	
	$finalArr = new Array();
	
	$("tr.term-rows").each(function() {
		$rowInfo = new Array();
		$(this).find('td').each(function() {
			tdClass 	= $(this).attr('class');
			if(tdClass == "termid" || tdClass == "termKey") {
				$rowInfo.push($(this).html());
			} else if (tdClass == "inputValue") {
				$rowInfo.push($(this).find('input').attr('id'));
				$rowInfo.push($(this).find('input').val());
			}
		});
		$finalArr.push($rowInfo);
	});
	
	var data = { "type": type, 
                            "infos" : $finalArr, 
                            "termType": $('#cobLocalLangTermType').val(), 
                            "appType": $('#cobLocalLangAppType').val(), 
                            "language": $('#cobLang').val()};
	var url = "request.pzx?c=" + localTerm_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onLocalTermResponse,localTerm_obj) ;
}

function exportLocalTerm() {
	if(validateTerms() == false)
		return false;
	
	var url = "report.pzx?c=" + localTerm_url + "&appType=" + $('#cobLocalLangAppType').val() + "&termType=" + $('#cobLocalLangTermType').val() + "&language=" + $('#cobLang').val() + "&d=" + new Date().getTime()  + "&t=" + C_EXPORT;
	showReport(url) ;
}

function printLocalTerm() {
	if(validateTerms() == false)
		return false;
	
	var url = "report.pzx?c=" + localTerm_url + "&appType=" + $('#cobLocalLangAppType').val() + "&termType=" + $('#cobLocalLangTermType').val() + "&language=" + $('#cobLang').val() + "&d=" + new Date().getTime()  + "&t=" + C_REPORT;
	showReport(url) ;
}

function getListItem() {
	clearLocalTerm();
	if(validateTerms() == false)
		return false;
	
	var data = { "type":C_LIST,
                        "appType": $('#cobLocalLangAppType').val(), 
                        "termType": $('#cobLocalLangTermType').val(), 
                        "language": $('#cobLang').val()} ;
	var url = "request.pzx?c=" + localTerm_url + "&d=" + new Date().getTime() ;	
	callServer(url,"json",data,onLocalTermResponse,localTerm_obj) ;	
}

function onLocalTermResponse(obj,resp) {
	
	if (resp.status == C_OK) {
		if (resp.type == C_LIST){
			$("tr.term-rows").remove() ;
			$('#sbg-reason-table').html(resp.data) ;
                        
                        $('#cobLocalLangTermType').prop('readonly', true);
                        $('#cobLang').prop('readonly', true);
                        $('#cobLocalLangAppType').prop('readonly', true);
		} else if (resp.type == C_UPDATE) {
                        $('#cobLocalLangAppType').prop('readonly', false);
                        $('#cobLocalLangTermType').prop('readonly', false);
                        $('#cobLang').prop('readonly', false);
                        
			alert("Save successfully!!");
		}
		
	} else {
		alert(resp.mesg);
		
	}
	
}

function validateTerms() {
	if ($('#cobLocalLangAppType').val() == 0) {
		$('#errLocalLangAppType').show() ;
		$('#cobLocalLangAppType').focus() ;
		return false ;
	} else {
		$('#errLocalLangAppType').hide() ;
	}
        
	if ($('#cobLocalLangTermType').val() == 0) {
		$('#errLocalLangTermType').show() ;
		$('#cobLocalLangTermType').focus() ;
		return false ;
	} else {
		$('#errLocalLangTermType').hide() ;
	}
	
	if ($('#cobLang').val() == 0) {
		$('#errLang').show() ;
		$('#cobLang').focus() ;
		return false ;
	} else {
		$('#errLang').hide() ;
	}
	
	return true ;
}