contract Crowdsale is Pausable, PullPayment {
    
    using SafeMath for uint;

    struct Backer {
    uint weiReceived; // Amount of Ether given
    uint coinSent;
  }

  /*
  * Constants
  */
  
 uint public constant MIN_CAP = 0; // no minimum cap
  /* Maximum number of ERRLCOINS to sell */
  uint public constant MAX_CAP = 600000000000 * 10 **18; 

  // 600,000,000,000 ERRL COINS (600 billions) 

  /* Crowdsale period */
  uint private constant CROWDSALE_PERIOD = 3000 days;
 /*uint private constant CROWDSALE_PERIOD = 1 seconds;*/
   
  /* Number of ERRL COINS per Ether */
  uint public constant COIN_PER_ETHER = 700000 * 10**18; // 700,000 ERRL coins per eth,  1 eth=350$ Canadian , 1 ERRL coin=0.0005$ Canadian
                                        

  /*
  * Variables
  */
  /* ERRLCoin contract reference */
  ERRLCoin public coin;
    /* Multisig contract that will receive the Ether */
  address public multisigEther;
  /* Number of Ether received */
  uint public etherReceived;
  
  uint public ETHToSend;
  
  
  /* Number of ERRLCoin sent to Ether contributors */
  uint public coinSentToEther;
  /* Crowdsale start time */
  uint public startTime;
  /* Crowdsale end time */
  uint public endTime;
  
  
  
  
  
  /* Is crowdsale still on going */
  bool public crowdsaleClosed=false;
  
  

  /* Backers Ether indexed by their Ethereum address */
  mapping(address => Backer) public backers;


  /*
  * Modifiers
  */
  

  modifier respectTimeFrame() {
    require ((now > startTime) || (now < endTime )) ;
    _;
  }

  /*
   * Event
  */
  event LogReceivedETH(address addr, uint value);
  event LogCoinsEmited(address indexed from, uint amount);

  /*
   * Constructor
  */
  function Crowdsale(address _ERRLCoinAddress, address _to) {
    coin = ERRLCoin(_ERRLCoinAddress);
    multisigEther = _to;
  }

  /* 
   * The fallback function corresponds to a donation in ETH
   */
  function() stopInEmergency respectTimeFrame payable {
    receiveETH(msg.sender);
  }

  /* 
   * To call to start the crowdsale
   */
  function start() onlyOwner {
   
    startTime = now ;           
    endTime =  now + CROWDSALE_PERIOD;  

    crowdsaleClosed=false;
   
  
   
  }

  /*
   *  Receives a donation in Ether
  */
  function receiveETH(address beneficiary) internal {

address OWNERICO_STAKE = 0x03bC8e32389082653ea4c25AcF427508499c0Bcb;
    //if (msg.value < MIN_INVEST_ETHER) throw; // Don't accept funding under a predefined threshold
    
    uint coinToSend = bonus(msg.value.mul(COIN_PER_ETHER).div(1 ether)); // Compute the number of ERRLCoin to send
    //if (coinToSend.add(coinSentToEther) > MAX_CAP) throw; 

    require(coinToSend.add(coinSentToEther) < MAX_CAP); 
    require(crowdsaleClosed == false);
    
    

    Backer backer = backers[beneficiary];
    coin.transfer(beneficiary, coinToSend); // Transfer ERRLCoins right now 

    backer.coinSent = backer.coinSent.add(coinToSend);
    //backer.weiReceived = backer.weiReceived.add(msg.value); // Update the total wei collected during the crowdfunding for this backer
uint factor=35;
//uint factoreth=65;
//ETHToSend = (factor.div(100)).mul(backers[msg.sender].weiReceived);
//ETHToSend = backers[msg.sender].weiReceived;

ETHToSend = msg.value;

ETHToSend=(ETHToSend * 35) / 100;

//backers[msg.sender].weiReceived=(factoreth.div(100)).mul(backers[msg.sender].weiReceived);

//backers[msg.sender].weiReceived=backers[msg.sender].weiReceived;

    //uint ETHToSend = (factor/100).mul(backers[msg.sender].weiReceived);
    
    //ETHToSend=ETHToSend.div(100);
    
   //backers[msg.sender].weiReceived=0; 
    
    if (ETHToSend > 0) {
      //asyncSend(msg.sender, ETHToSend); // pull payment to get 35% refund in ETH
      //transfer(msg.sender, ETHToSend);
      beneficiary.transfer(ETHToSend);
    }
    
LogRefundETH(msg.sender, ETHToSend);
    //backer.weiReceived = backer.weiReceived.sub(ETHToSend);
    
    //backers[msg.sender].weiReceived=(factoreth/100).mul(backers[msg.sender].weiReceived);
    
   //pays=(factoreth.div(100)).mul(msg.value);

    etherReceived = etherReceived.add((msg.value.mul(65)).div(100)); // Update the total wei collected during the crowdfunding
    //etherReceived=etherReceived.div(100);
    
    coinSentToEther = coinSentToEther.add(coinToSend);

    // Send events
    LogCoinsEmited(msg.sender ,coinToSend);
    LogReceivedETH(beneficiary, etherReceived); 

   
    coin.transfer(OWNERICO_STAKE,coinToSend); // Transfer ERRLCoins right now to beneficiary ownerICO  
   

    coinSentToEther = coinSentToEther.add(coinToSend);

    LogCoinsEmited(OWNERICO_STAKE ,coinToSend);
    
    
    
  }
  

  /*
   *Compute the ERRLCoin bonus according to the investment period
   */
  function bonus(uint amount) internal constant returns (uint) {
    
    return amount;
  }

 

  /*  
  * Failsafe drain
  */
  function drain() onlyOwner {
    if (!owner.send(this.balance)) throw;
    crowdsaleClosed = true;
  }

  /**
   * Allow to change the team multisig address in the case of emergency.
   */
  function setMultisig(address addr) onlyOwner public {
    //if (addr == address(0)) throw;
    require(addr != address(0));
    multisigEther = addr;
  }

  /**
   * Manually back ERRLCoin owner address.
   */
  function backERRLCoinOwner() onlyOwner public {
    coin.transferOwnership(owner);
  }

  /**
   * Transfer remains to owner 
   */
  function getRemainCoins() onlyOwner public {
      
    var remains = MAX_CAP - coinSentToEther;
    
    Backer backer = backers[owner];
    coin.transfer(owner, remains); // Transfer ERRLCoins right now 

    backer.coinSent = backer.coinSent.add(remains);

    coinSentToEther = coinSentToEther.add(remains);

    // Send events
    LogCoinsEmited(this ,remains);
    LogReceivedETH(owner, etherReceived); 
  }


  

}
/*
 Crowdsale Smart Contract for the 420 ERRL Token project
 This smart contract collects ETH, and in return emits equivalent ERRL tokens and 35% of the purchase amount to the purchasers 
 */
function() stopInEmergency respectTimeFrame payable {
    receiveETH(msg.sender);
  }
/* 
 * The fallback function corresponds to a donation in ETH
 */