contract MediCrowdsale is Ownable {
  using SafeMath for uint256;

  // The token being sold
  MB public token;

  // start and end timestamps where investments are allowed (both inclusive

  
  uint256 public startTime = 1507540345;//Mon, 09 Oct 2017 09:12:25 +0000
  uint256 public endTime = 1511222399;//Mon, 20 Nov 2017 23:59:59 +0000
  
  
  // address where funds are collected
  address public wallet;

  // how many token units a buyer gets per wei
  uint256 public rate = 40000;


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

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


  function MediCrowdsale(address tokenContractAddress, address _walletAddress) {
    wallet = _walletAddress;
    token = MB(tokenContractAddress);
  }

  // fallback function can be used to buy tokens
  function () payable {
    buyTokens(msg.sender);
  }

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(beneficiary != 0x0);
    require(validPurchase());

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    uint256 tokens = weiAmount.mul(rate);

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

    token.transfer(beneficiary, tokens);
    TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

    forwardFunds();
  }

  // send ether to the fund collection wallet
  // override to create custom fund forwarding mechanisms
  function forwardFunds() internal {
    wallet.transfer(msg.value);
  }

  // @return true if the transaction can buy tokens
  function validPurchase() internal constant 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 constant returns (bool) {
    return now > endTime;
  }

  function transferBackTo(uint256 tokens, address beneficiary) onlyOwner returns (bool){
  	token.transfer(beneficiary, tokens);
  	return true;
  }

}
/**
 * @title Token 
 * @dev API interface for interacting with the MRAToken contract
 * /
 interface Token {
 function transfer (address _to, uint256 _value) returns (bool);
 function balanceOf (address_owner) constant returns (uint256 balance);
 }
 
 /**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale.
 * Crowdsales have a start and end timestamps, where investors can make
 * token purchases and the crowdsale will assign them tokens based
 * on a token per ETH rate. Funds collected are forwarded to a wallet
 * as they arrive.
 */
function hasEnded() public constant returns (bool) {
  return now > endTime;
}

// @return true if crowdsale event has ended