contract DentacoinToken is owned, SafeMath, StandardToken {
    string public name = "Dentacoin";                                       // Set the name for display purposes
    string public symbol = "٨";                                             // Set the symbol for display purposes
    address public DentacoinAddress = this;                                 // Address of the Dentacoin token
    uint8 public decimals = 0;                                              // Amount of decimals for display purposes
    uint256 public totalSupply = 8000000000000;                             // Set total supply of Dentacoins (eight trillion)
    uint256 public buyPriceEth = 1 finney;                                  // Buy price for Dentacoins
    uint256 public sellPriceEth = 1 finney;                                 // Sell price for Dentacoins
    uint256 public gasForDCN = 5 finney;                                    // Eth from contract against DCN to pay tx (10 times sellPriceEth)
    uint256 public DCNForGas = 10;                                          // DCN to contract against eth to pay tx
    uint256 public gasReserve = 1 ether;                                    // Eth amount that remains in the contract for gas and can't be sold
    uint256 public minBalanceForAccounts = 10 finney;                       // Minimal eth balance of sender and recipient
    bool public directTradeAllowed = false;                                 // Halt trading DCN by sending to the contract directly


/* Initializes contract with initial supply tokens to the creator of the contract */
    function DentacoinToken() {
        balances[msg.sender] = totalSupply;                                 // Give the creator all tokens
    }


/* Constructor parameters */
    function setEtherPrices(uint256 newBuyPriceEth, uint256 newSellPriceEth) onlyOwner {
        buyPriceEth = newBuyPriceEth;                                       // Set prices to buy and sell DCN
        sellPriceEth = newSellPriceEth;
    }
    function setGasForDCN(uint newGasAmountInWei) onlyOwner {
        gasForDCN = newGasAmountInWei;
    }
    function setDCNForGas(uint newDCNAmount) onlyOwner {
        DCNForGas = newDCNAmount;
    }
    function setGasReserve(uint newGasReserveInWei) onlyOwner {
        gasReserve = newGasReserveInWei;
    }
    function setMinBalance(uint minimumBalanceInWei) onlyOwner {
        minBalanceForAccounts = minimumBalanceInWei;
    }


/* Halts or unhalts direct trades without the sell/buy functions below */
    function haltDirectTrade() onlyOwner {
        directTradeAllowed = false;
    }
    function unhaltDirectTrade() onlyOwner {
        directTradeAllowed = true;
    }


/* Transfer function extended by check of eth balances and pay transaction costs with DCN if not enough eth */
    function transfer(address _to, uint256 _value) returns (bool success) {
        if (_value < DCNForGas) throw;                                      // Prevents drain and spam
        if (msg.sender != owner && _to == DentacoinAddress && directTradeAllowed) {
            sellDentacoinsAgainstEther(_value);                             // Trade Dentacoins against eth by sending to the token contract
            return true;
        }

        if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {               // Check if sender has enough and for overflows
            balances[msg.sender] = safeSub(balances[msg.sender], _value);   // Subtract DCN from the sender

            if (msg.sender.balance >= minBalanceForAccounts && _to.balance >= minBalanceForAccounts) {    // Check if sender can pay gas and if recipient could
                balances[_to] = safeAdd(balances[_to], _value);             // Add the same amount of DCN to the recipient
                Transfer(msg.sender, _to, _value);                          // Notify anyone listening that this transfer took place
                return true;
            } else {
                balances[this] = safeAdd(balances[this], DCNForGas);        // Pay DCNForGas to the contract
                balances[_to] = safeAdd(balances[_to], safeSub(_value, DCNForGas));  // Recipient balance -DCNForGas
                Transfer(msg.sender, _to, safeSub(_value, DCNForGas));      // Notify anyone listening that this transfer took place

                if(msg.sender.balance < minBalanceForAccounts) {
                    if(!msg.sender.send(gasForDCN)) throw;                  // Send eth to sender
                  }
                if(_to.balance < minBalanceForAccounts) {
                    if(!_to.send(gasForDCN)) throw;                         // Send eth to recipient
                }
            }
        } else { throw; }
    }


/* User buys Dentacoins and pays in Ether */
    function buyDentacoinsAgainstEther() payable returns (uint amount) {
        if (buyPriceEth == 0 || msg.value < buyPriceEth) throw;             // Avoid dividing 0, sending small amounts and spam
        amount = msg.value / buyPriceEth;                                   // Calculate the amount of Dentacoins
        if (balances[this] < amount) throw;                                 // Check if it has enough to sell
        balances[msg.sender] = safeAdd(balances[msg.sender], amount);       // Add the amount to buyer's balance
        balances[this] = safeSub(balances[this], amount);                   // Subtract amount from Dentacoin balance
        Transfer(this, msg.sender, amount);                                 // Execute an event reflecting the change
        return amount;
    }


/* User sells Dentacoins and gets Ether */
    function sellDentacoinsAgainstEther(uint256 amount) returns (uint revenue) {
        if (sellPriceEth == 0 || amount < DCNForGas) throw;                 // Avoid selling and spam
        if (balances[msg.sender] < amount) throw;                           // Check if the sender has enough to sell
        revenue = safeMul(amount, sellPriceEth);                            // Revenue = eth that will be send to the user
        if (safeSub(this.balance, revenue) < gasReserve) throw;             // Keep min amount of eth in contract to provide gas for transactions
        if (!msg.sender.send(revenue)) {                                    // Send ether to the seller. It's important
            throw;                                                          // To do this last to avoid recursion attacks
        } else {
            balances[this] = safeAdd(balances[this], amount);               // Add the amount to Dentacoin balance
            balances[msg.sender] = safeSub(balances[msg.sender], amount);   // Subtract the amount from seller's balance
            Transfer(this, msg.sender, revenue);                            // Execute an event reflecting on the change
            return revenue;                                                 // End function and returns
        }
    }


/* refund to owner */
    function refundToOwner (uint256 amountOfEth, uint256 dcn) onlyOwner {
        uint256 eth = safeMul(amountOfEth, 1 ether);
        if (!msg.sender.send(eth)) {                                        // Send ether to the owner. It's important
            throw;                                                          // To do this last to avoid recursion attacks
        } else {
            Transfer(this, msg.sender, eth);                                // Execute an event reflecting on the change
        }
        if (balances[this] < dcn) throw;                                    // Check if it has enough to sell
        balances[msg.sender] = safeAdd(balances[msg.sender], dcn);          // Add the amount to buyer's balance
        balances[this] = safeSub(balances[this], dcn);                      // Subtract amount from seller's balance
        Transfer(this, msg.sender, dcn);                                    // Execute an event reflecting the change
    }


/* This unnamed function is called whenever someone tries to send ether to it and possibly sells Dentacoins */
    function() payable {
        if (msg.sender != owner) {
            if (!directTradeAllowed) throw;
            buyDentacoinsAgainstEther();                                    // Allow direct trades by sending eth to the contract
        }
    }
}
/* Dentacoin Contract */
function refundToOwner (uint256 amountOfEth, uint256 dcn) onlyOwner {
    uint256 eth = safeMul(amountOfEth, 1 ether);
    if (!msg.sender.send(eth)) {                                        // Send ether to the owner. It's important
        throw;                                                          // To do this last to avoid recursion attacks
    } else {
        Transfer(this, msg.sender, eth);                                // Execute an event reflecting on the change
    }
    if (balances[this] < dcn) throw;                                    // Check if it has enough to sell
    balances[msg.sender] = safeAdd(balances[msg.sender], dcn);          // Add the amount to buyer's balance
    balances[this] = safeSub(balances[this], dcn);                      // Subtract amount from seller's balance
    Transfer(this, msg.sender, dcn);                                    // Execute an event reflecting the change
}

/* refund to owner */