<!doctype>
<html>
<title>JevCoin</title>
<head>
<script type="text/javascript" src="../ext/bignumber.min.js"></script>
<script type="text/javascript" src="../ext/ethereum.js/dist/ethereum-light.min.js"></script>
</head>
<body>

<h1>JevCoin <code id="contract_addr"></code></h1>
<div>
	<strong>Balance</strong>
	<span id="balance"></strong>
</div>

<div>
	<span>Address:</span>
	<input type="text" id="address" style="width:200px">
	<span>Amount:</span>
	<input type="text" id="amount" style="width:200px">
	<button onclick="transact()">Send</button>
	<span id="message"></span>
</div>

<hr>

<table width="100%" id="table">
	<tr><td style="width:40%;">Address</td><td>Balance</td></tr>
	<tbody id="table_body"></tbody>
</table>

</body>

<script type="text/javascript">
	var web3 = require('ethereum.js');
	var eth = web3.eth;

	web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
	var desc = [{
		"name": "balance(address)",
		"type": "function",
		"inputs": [{
			"name": "who",
			"type": "address"
		}],
		"constant": true,
		"outputs": [{
			"name": "value",
			"type": "uint256"
		}]
	}, {
		"name": "send(address,uint256)",
		"type": "function",
		"inputs": [{
			"name": "to",
			"type": "address"
		}, {
			"name": "value",
			"type": "uint256"
		}],
		"outputs": []
	}, {
		"name":"Changed",
		"type":"event",
		"inputs": [
			{"name":"from","type":"address","indexed":true},
			{"name":"amount","type":"uint256","indexed":true},
		],
	}];

	var address = localStorage.getItem("address");
	// deploy if not exist
	if(address === null) {
		var code = "0x60056013565b61014f8061003a6000396000f35b620f42406000600033600160a060020a0316815260200190815260200160002081905550560060e060020a600035048063d0679d3414610020578063e3d670d71461003457005b61002e600435602435610049565b60006000f35b61003f600435610129565b8060005260206000f35b806000600033600160a060020a03168152602001908152602001600020541061007157610076565b610125565b806000600033600160a060020a03168152602001908152602001600020908154039081905550806000600084600160a060020a031681526020019081526020016000209081540190819055508033600160a060020a03167fb52dda022b6c1a1f40905a85f257f689aa5d69d850e49cf939d688fbe5af594660006000a38082600160a060020a03167fb52dda022b6c1a1f40905a85f257f689aa5d69d850e49cf939d688fbe5af594660006000a35b5050565b60006000600083600160a060020a0316815260200190815260200160002054905091905056";
		address = web3.eth.sendTransaction({from: eth.accounts[0], data: code, gas: "1000000"});
        localStorage.setItem("address", address);
	}
	document.querySelector("#contract_addr").innerHTML = address;

	var Contract = web3.eth.contract(desc);
	contract = new Contract(address);
	var filter = contract.Changed({from: eth.accounts[0]})
	filter.watch(function(logs) {
		console.log(logs);
		refresh();
	});
window.filter = filter;

	function refresh() {
        document.querySelector("#balance").innerHTML = contract.balance(eth.coinbase);
	}

	function transact() {
		var to = document.querySelector("#address");
		if( to.value.length == 0 ) {
			to = "0x4205b06c2cfa0e30359edcab94543266cb6fa1d3";
		} else {
			if (to.value.substr(0,2) != "0x")
				to.value = "0x"+to.value;
		}

		var value = document.querySelector("#amount");
		var amount = parseInt( value.value );
		console.log("transact: ", to.value, " => ", amount)

		contract.sendTransaction({from: eth.accounts[0]}).send( to.value, amount );

		to.value = "";
		value.value = "";

		var message = document.querySelector("#message")
		message.innerHTML = "Submitted";
		setTimeout(function() {
			message.innerHTML = "";
		}, 1000);
	}

	refresh();
</script>
</html>

<!--
contract JevCoin {
    function JevCoin()
    {
        balances[msg.sender] = 1000000;
    }
 
    event Changed(address indexed from, uint indexed amount);
    function send(address to, uint value)
    {
        if( balances[msg.sender] < value ) return;
 
        balances[msg.sender] -= value;
        balances[to] += value;
 
        Changed(msg.sender, value);
        Changed(to, value);
    }
 
    function balance(address who) constant returns(uint t)
    {
        t = balances[who];
    }
 
    mapping(address => uint256) balances;
 }
-!>