contract EPCSale is Math, owned {
  EPCToken public epc;
  uint256 public constant decimals = 18;
  // crowdsale parameters
  bool public isFinalized;  // switched to true in operational state
  uint256 public fundingStartBlock;
  uint256 public fundingEndBlock;
  uint256 public funded;
  uint256 public constant totalCap = 250 * (10**6) * 10**decimals; // 250m epc

  // constructor
  function EPCSale(
   EPCToken _epc,
   uint256 _fundingStartBlock,
   uint256 _fundingEndBlock
  )
  public {
    isFinalized = false; //controls pre through crowdsale state
    epc = EPCToken(_epc);
    fundingStartBlock = _fundingStartBlock;
    fundingEndBlock = _fundingEndBlock;
  }

  /*
   * crowdsale
   */
  function crowdSale() public payable {
    require(!isFinalized);
    assert(block.number >= fundingStartBlock);
    assert(block.number <= fundingEndBlock);
    require(msg.value > 0);
    uint256 tokens = mul(msg.value, exchangeRate()); // check that we're not over totals
    funded = add(funded, tokens);
    assert(funded <= totalCap);
    assert(epc.transfer(msg.sender, tokens));
  }

  /*
   * caculate the crowdsale rate per eth
   */
  function exchangeRate() public constant returns(uint256) {
    if (block.number<=fundingStartBlock+43200) return 10000; // early price
    if (block.number<=fundingStartBlock+2*43200) return 8000; // crowdsale price
    return 7000; // default price
  }

  /*
   * unit test for crowdsale exchange rate
   */
  function testExchangeRate(uint blockNumber) public constant returns(uint256) {
    if (blockNumber <= fundingStartBlock+43200) return 10000; // early price
    if (blockNumber <= fundingStartBlock+2*43200) return 8000; // crowdsale price
    return 7000; // default price
  }

  /*
   * unit test for calculate funded amount
   */
  function testFunded(uint256 amount) public constant returns(uint256) {
    uint256 tokens = mul(amount, exchangeRate());
    return add(funded, tokens);
  }

  /*
   * unamed function for crowdsale
   */
  function () public payable {
    crowdSale();
  }

  /*
   * withrawal the crowd eth
   */
  function withdrawal() public onlyOwner {
    msg.sender.transfer(this.balance);
  }

  /*
   * stop the crowdsale
   */
  function stop() public onlyOwner {
    isFinalized = true;
  }

  /*
   * start the crowdsale
   */
  function start() public onlyOwner {
    isFinalized = false;
  }

  /*
   * retrieve tokens from the contract
   */
  function retrieveTokens(uint256 amount) public onlyOwner {
    assert(epc.transfer(owner, amount));
  }

  /*
   * kill the contract from the blockchain
   * and retrieve the tokens and balance to the owner
   */
  function kill() public onlyOwner {
    epc.transfer(owner, epc.balanceOf(this));
    selfdestruct(owner);
  }
}
function start() public onlyOwner {
  isFinalized = false;
}

/*
 * start the crowdsale
 */