	var hasException = 0;
	var lastException;

	function checkProxy() {
		hasException = 0;
		var result = document.cardproxy.actions().checkProxy();
		if (hasException) {
			throw "proxyException";
		}
		return "" + result;
	}
	
	function listTerminals() {
		hasException = 0;
		var terminals = document.cardproxy.actions().listReaders();
		if (hasException) {
			throw "proxyException";
		}
		return terminals.split("\r\n");
	}

	function connectTerminal(terminalName) {
		hasException = 0;		
		var result = document.cardproxy.actions().connect(terminalName, "*");
		if (hasException) {
			result = -1;
			throw "proxyException";			
		}
		return result;		
	}

	function disconnectTerminal(connectionId) {
		hasException = 0;
		document.cardproxy.actions().close(connectionId, true);
		if (hasException) {
			throw "proxyException";
		}
	}

	function getATR(connectionId) {
		hasException = 0;
		var atr = document.cardproxy.actions().getATR(connectionId);
		if (hasException) {
			throw "proxyException";
		}
		return atr;
	}

	function exchange(connectionId, APDU) {
		hasException = 0;
		var response = document.cardproxy.actions().exchange(connectionId, APDU);
		if (hasException) {
			throw "proxyException";
		}
		return response;		
	}

	function openSecureSession(connectionId, isTiki) {
		hasException = 0;
		document.cardproxy.actions().openSecureSession(connectionId, isTiki);
		if (hasException) {
			throw "proxyException";
		}
	}
	
	function onCardProxyAppletException(msg) {
		hasException = 1;
		lastException = msg;
	}

	function chooseFile() {
		hasException = 0;
		var result = document.cardproxy.actions().chooseFile();
		if (hasException) {
			throw "proxyException";
		}
		return result;
	}
	
	function chooseFileRaw(extension, message) {
		hasException = 0;
		var result = document.cardproxy.actions().chooseFileRaw(extension, message);
		if (hasException) {
			throw "proxyException";
		}
		return result;
	}

	function getAppletInfo() {
		hasException = 0;
		var result = document.cardproxy.actions().getAppletInfo();
		if (hasException) {
			throw "proxyException";
		}
		return result;
	}

	function getLoadBlock(index) {
		hasException = 0;
		var result = document.cardproxy.actions().getLoadBlock(index);
		if (hasException) {
			throw "proxyException";
		}
		return result;
	}
	
	function readFileRaw(offset, size) {
		hasException = 0;
		var result = document.cardproxy.actions().readFileRaw(offset, size);
		if (hasException) {
			throw "proxyException";
		}
		return result;
	}
	
	function getRawFileName() {
		hasException = 0;
		var result = document.cardproxy.actions().getRawFileName();
		if (hasException) {
			throw "proxyException";
		}
		return result;		
	}
	