contract CPayCrowdsale is owned, usingOraclize  {
    
    address public wallet1 = address(0x9740Cd40296744a0aE2aFD115933a791Da4c2AD8);
    address public wallet2 = address(0x370a68F41978088376E299A74d26674da269A476);
    
    uint public amountRaised;
    uint public priceEUR;
    CPayCoin public tokenReward;
    bool public saleFinished = false;


    event FundTransfer(address backer, uint amount, bool isContribution);
    event updatedEtherPrice(string price);
    event updatingViaOracle(string description);

    modifier onlyCrowdsalePeriod() { 
        require ( !saleFinished ) ;
        _; 
    }

    /**
     * Constrctor function
     *
     * Setup the owner
     */
    function CPayCrowdsale(
        CPayCoin addressOfTokenUsedAsReward
    ) {
        tokenReward = addressOfTokenUsedAsReward;
    }
    
    // low level token purchase function
    function buyTokens(address beneficiary) payable {
        
        updatePrice();
        uint amount = msg.value;
        amountRaised += amount;
        tokenReward.transfer(beneficiary, amount*priceEUR);
        FundTransfer(beneficiary, amount, true);

    }
    
    function withdrawEthers() payable onlyOwner {
        require (this.balance > 0);
        uint thisbalance = this.balance;
        wallet1.send(thisbalance/2);
        wallet2.send(thisbalance/2);
    }
    
    function withdrawTokens() onlyOwner {
        tokenReward.transfer(msg.sender, tokenReward.balanceOf(this));
        FundTransfer(msg.sender, tokenReward.balanceOf(this), false);
    }
    
    /**
     * Fallback function
     *
     * The function without name is the default function that is called whenever anyone sends funds to a contract
     */
    function () payable onlyCrowdsalePeriod {
        buyTokens(msg.sender);
    }
    
    function finishCrowdsale() onlyOwner {
        saleFinished = true;
    }

    function __callback(bytes32 myid, string result) {
        require (msg.sender == oraclize_cbAddress());
        updatedEtherPrice(result);
        priceEUR = parseInt(result, 0);
    }

    function updatePrice() payable  {
        require(msg.value >= 5 finney);
        if (oraclize_getPrice("URL") > this.balance) {
            updatingViaOracle("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
        } else {
            updatingViaOracle("Oraclize query was sent, standing by for the answer..");
            oraclize_query("URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHEUR).result.XETHZEUR.c.[0]");
        }
    }


}
function CPayCrowdsale(
    CPayCoin addressOfTokenUsedAsReward
) {
    tokenReward = addressOfTokenUsedAsReward;
}

/**
 * Constrctor function
 *
 * Setup the owner
 */