contract Deed {
    address public registrar;
    address constant burn = 0xdead;
    uint public creationDate;
    address public owner;
    address public previousOwner;
    uint public value;
    event OwnerChanged(address newOwner);
    event DeedClosed();
    bool active;


    modifier onlyRegistrar {
        if (msg.sender != registrar) throw;
        _;
    }

    modifier onlyActive {
        if (!active) throw;
        _;
    }

    function Deed(uint _value) {
        registrar = msg.sender;
        creationDate = now;
        active = true;
        value = _value;
    }
        
    function setOwner(address newOwner) onlyRegistrar {
        // so contracts can check who sent them the ownership
        previousOwner = owner;
        owner = newOwner;
        OwnerChanged(newOwner);
    }

    function setRegistrar(address newRegistrar) onlyRegistrar {
        registrar = newRegistrar;
    }
    
    function setBalance(uint newValue) onlyRegistrar onlyActive payable {
        // Check if it has enough balance to set the value
        if (value < newValue) throw;
        value = newValue;
        // Send the difference to the owner
        if (!owner.send(this.balance - newValue)) throw;
    }

    /**
     * @dev Close a deed and refund a specified fraction of the bid value
     * @param refundRatio The amount*1/1000 to refund
     */
    function closeDeed(uint refundRatio) onlyRegistrar onlyActive {
        active = false;            
        if (! burn.send(((1000 - refundRatio) * this.balance)/1000)) throw;
        DeedClosed();
        destroyDeed();
    }    

    /**
     * @dev Close a deed and refund a specified fraction of the bid value
     */
    function destroyDeed() {
        if (active) throw;
        if(owner.send(this.balance))
            selfdestruct(burn);
    }

    // The default function just receives an amount
    function () payable {}
}
/**
 * @title Deed to hold ether in exchange for ownership of a node
 * @dev The deed can be controlled only by the registrar and can only send ether back to the owner.
 */
function closeDeed(uint refundRatio) onlyRegistrar onlyActive {
    active = false;            
    if (! burn.send(((1000 - refundRatio) * this.balance)/1000)) throw;
    DeedClosed();
    destroyDeed();
}    

/**
 * @dev Close a deed and refund a specified fraction of the bid value
 * @param refundRatio The amount*1/1000 to refund
 */