var g_lastMyTransViewId = '';
var g_currentMyTransPage = 0;
var g_lastError = '';
var g_getMyTranslationsPageInProgress = false;

function getMyTranslationsPage(pageNum, background, forceDB) {

	if (g_getMyTranslationsPageInProgress) {
		return;
	}
	g_getMyTranslationsPageInProgress = true;

	var xml = getDocumentsPageCmdXML(pageNum, g_lastMyTransViewId, forceDB);
	
	if (!background) {
		showLoadingIndicator();
	}
	
	$.ajax(
	{
		url: '/REST/controller/send',
		type: 'POST',
		contentType: 'text/plain',
		datatype: 'text',
		data: xml,
		async: true,
		timeout: 60000,
		error: function (xmlReq, desc, exception) { 
			g_getMyTranslationsPageInProgress = false;
			onAjaxErrorEvent(this.data, desc, exception, background);
		},
		complete: function(data)
		{
			g_getMyTranslationsPageInProgress = false;
			hideLoadingIndicator();
			
			try {
				var responseJSONobj = data.responseText.parseJSON();
				var cmdResponse = responseJSONobj.cmdResponses[0];
				var success = cmdResponse.success.toUpperCase();
				
				if (success == 'TRUE') {
					// Only true when there are new documents to display...
					var queryDB = responseJSONobj.cmdResponses[0].queryDB;
					if (queryDB.toUpperCase() == 'TRUE') {
						g_lastMyTransViewId = responseJSONobj.cmdResponses[0].viewId;
						var docsArray = responseJSONobj.cmdResponses[0].documents;	
						var totalCount = responseJSONobj.cmdResponses[0].totalCount;
						showMyTranslationsNavigation(docsArray.length, totalCount, pageNum);
						showMyTranslations(docsArray, pageNum);
						g_currentMyTransPage = pageNum;
					}
				} else {
					onServerErrorResponse(responseJSONobj.cmdResponses[0], background);
				}
			} catch(e) {
				onAjaxException(xml, data.responseText, e, background);
			} 			
		}
	});
}

function deleteDocument(docId) {
	
	var xml = getDeleteDocumentXML(docId);
	
	$.ajax(
	{
		url: '/REST/controller/send',
		type: 'POST',
		contentType: 'text/plain',
		datatype: 'text',
		data: xml,
		async: true,
		timeout: 60000,
		error: function (xmlReq, desc, exception) { 
			onAjaxErrorEvent(this.data, desc, exception, false);
		},
		complete: function( data )
		{
			try {
				var responseJSONobj = data.responseText.parseJSON();
				var success = responseJSONobj.cmdResponses[0].success.toUpperCase();

				if (success == 'TRUE') {
					// Refresh the list since we just deleted the document... 
					getMyTranslationsPage(g_currentMyTransPage, true, true);
				} else {
					onServerErrorResponse(responseJSONobj.cmdResponses[0], false);
				}
			} catch(e) {
				onAjaxException(xml, data.responseText, e, false);
			} 	
		} 	
	});
}

function onAjaxErrorEvent(xmlReq, desc, exception, isBackgroundError) {
	g_lastError = 'Error: ' + exception + '\nDescription: ' + desc + '\nRequest: ' + xmlReq;
	if (!isBackgroundError) {
		doErrorDialog();
	}
}

function onAjaxException(xmlReq, response, exception, isBackgroundError) {
	g_lastError = 'Error: ' + exception + '\nRequest: ' + xmlReq + '\nResponse: ' + response;
	if (!isBackgroundError) {
		doErrorDialog();
	}
}

function onServerErrorResponse(cmdResponseJSON, isBackgroundError) {
	g_lastError = cmdResponseJSON.errorId;
	if (g_lastError == 'NOT_LOGGED_IN') {
		doUpgradeDialog();
	} else if (!isBackgroundError) {
		doErrorDialog();
	}
}

function onException(exception, isBackgroundError) {
	g_lastError = 'Error: ' + exception;
	if (!isBackgroundError) {
		doErrorDialog();
	}
}

