contract Crowdsale is Ownable {
    using SafeMath for uint256;


    // start and end timestamps where investments are allowed (both inclusive)
    uint256 public startTime;
    uint256 public endTime;

    // address where funds are collected
    address public wallet;
    FullERC20 public token;

    // token amount per 1 ETH
    // eg. 40000000000 = 2500000 tokens.
    uint256 public rate; 

    // amount of raised money in wei
    uint256 public weiRaised;
    uint256 public tokensPurchased;

    /**
    * event for token purchase logging
    * @param purchaser who paid for the tokens
    * @param value weis paid for purchase
    * @param amount amount of tokens purchased
    */
    event TokenPurchased(address indexed purchaser, uint256 value, uint256 amount);

    function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet, address _token) public {
        require(_startTime >= now);
        require(_endTime >= _startTime);
        require(_rate > 0);
        require(_wallet != address(0));
        require(_token != address(0));

        startTime = _startTime;
        endTime = _endTime;
        rate = _rate;
        wallet = _wallet;
        token = FullERC20(_token);
    }

    // fallback function can be used to buy tokens
    function () public payable {
        purchase();
    }

    function purchase() public payable {
        require(msg.sender != address(0));
        require(validPurchase());

        uint256 weiAmount = msg.value;

        // calculate token amount to be created
        uint256 tokens = weiAmount.div(rate);
        require(tokens > 0);
        require(token.balanceOf(this) > tokens);

        // update state
        weiRaised = weiRaised.add(weiAmount);
        tokensPurchased = tokensPurchased.add(tokens);

        TokenPurchased(msg.sender, weiAmount, tokens);
        assert(token.transfer(msg.sender, tokens));
        wallet.transfer(msg.value);
    }

    // @return true if the transaction can buy tokens
    function validPurchase() internal view returns (bool) {
        bool withinPeriod = now >= startTime && now <= endTime;
        bool nonZeroPurchase = msg.value != 0;
        return withinPeriod && nonZeroPurchase;
    }

    // @return true if crowdsale event has ended
    function hasEnded() public view returns (bool) {
        return now > endTime;
    }

    // @dev updates the rate
    function updateRate(uint256 newRate) public onlyOwner {
        rate = newRate;
    }

    function updateTimes(uint256 _startTime, uint256 _endTime) public onlyOwner {
        startTime = _startTime;
        endTime = _endTime;
    }

    // @dev returns the number of tokens available in the sale.
    function tokensAvailable() public view returns (bool) {
        return token.balanceOf(this) > 0;
    }

    // @dev Ends the token sale and transfers balance of tokens
    // and eth to owner. 
    function endSale() public onlyOwner {
        wallet.transfer(this.balance);
        assert(token.transfer(wallet, token.balanceOf(this)));
        endTime = now;
    }
}
function () public payable {
        purchase();
    }
// fallback function can be used to buy tokens