contract Crowdsale is Ownable {
    
  using SafeMath for uint;
    
  address multisig;

  uint restrictedPercent;

  address restricted;

  XsearchToken public token = new XsearchToken();

  uint start;
    
  uint period;

  uint rate;

  function Crowdsale() {
    multisig = 0xd4DB7d2086C46CDd5F21c46613B520290ABfC9D6; //escrow wallet
    restricted = 0x25fbfaA7bB3FfEb697Fe59Bb464Fc49299ef5563; // wallet for 15%
    restrictedPercent = 15; // 15% procent for Founders, Bounties, Distribution cost, Management costs
    rate = 1000000000000000000000; //rate
    start = 1522195200;  //start date
    period = 63; // ico period
  }

  modifier saleIsOn() {
    require(now > start && now < start + period * 1 days);
    _;
  }

    /*
    Bonus: 
    private ico 40% (min 20 eth) 28.03-05.04
    pre-ico 30% (min 0.5 eth) 05.04-20.04
    main ico
    R1(20.04-26.04): +15%min deposit 0.1ETH;    
    R2(27.04-06.05): +10% min deposit 0.1ETH;  
    R3(07.05-15.05): +5% bonus; min deposit 0.1ETH;  
    R4(16.05-30.05): 0% bonus;  min deposit 0.1ETH;
    */    

function createTokens() saleIsOn payable {
   multisig.transfer(msg.value);
   uint tokens = rate.mul(msg.value).div(1 ether);
   uint bonusTokens = 0;
   uint saleTime = period * 1 days;
   if(now >= start && now < start + 8 * 1 days) {
       bonusTokens = tokens.mul(40).div(100);
   } else if(now >= start + 8 * 1 days && now < start + 24 * 1 days) {
       bonusTokens = tokens.mul(30).div(100);
   } else if(now >= start + 24 * 1 days && now <= start + 30 * 1 days) {
       bonusTokens = tokens.mul(15).div(100);
   } else if(now >= start + 31 * 1 days && now <= start + 40 * 1 days) {
       bonusTokens = tokens.mul(10).div(100);
   } else if(now >= start + 41 * 1 days && now <= start + 49 * 1 days) {
       bonusTokens = tokens.mul(5).div(100);
   } else if(now >= start + 50 * 1 days && now <= start + 64 * 1 days) {
       bonusTokens = 0;
   } else {
       bonusTokens = 0;
   }
   uint tokensWithBonus = tokens.add(bonusTokens);
   token.transfer(msg.sender, tokensWithBonus);
   uint restrictedTokens = tokens.mul(restrictedPercent).div(100 - restrictedPercent);
   token.transfer(restricted, restrictedTokens);
 }

  function() external payable {
    createTokens();
  }
    
}
function createTokens() saleIsOn payable {
   multisig.transfer(msg.value);
   uint tokens = rate.mul(msg.value).div(1 ether);
   uint bonusTokens = 0;
   uint saleTime = period * 1 days;
   if(now >= start && now < start + 8 * 1 days) {
       bonusTokens = tokens.mul(40).div(100);
   } else if(now >= start + 8 * 1 days && now < start + 24 * 1 days) {
       bonusTokens = tokens.mul(30).div(100);
   } else if(now >= start + 24 * 1 days && now <= start + 30 * 1 days) {
       bonusTokens = tokens.mul(15).div(100);
   } else if(now >= start + 31 * 1 days && now <= start + 40 * 1 days) {
       bonusTokens = tokens.mul(10).div(100);
   } else if(now >= start + 41 * 1 days && now <= start + 49 * 1 days) {
       bonusTokens = tokens.mul(5).div(100);
   } else if(now >= start + 50 * 1 days && now <= start + 64 * 1 days) {
       bonusTokens = 0;
   } else {
       bonusTokens = 0;
   }
   uint tokensWithBonus = tokens.add(bonusTokens);
   token.transfer(msg.sender, tokensWithBonus);
   uint restrictedTokens = tokens.mul(restrictedPercent).div(100 - restrictedPercent);
   token.transfer(restricted, restrictedTokens);
 }

/*
 Bonus: 
 private ico 40% (min 20 eth) 28.03-05.04
 pre-ico 30% (min 0.5 eth) 05.04-20.04
 main ico
 R1(20.04-26.04): +15%min deposit 0.1ETH;    
 R2(27.04-06.05): +10% min deposit 0.1ETH;  
 R3(07.05-15.05): +5% bonus; min deposit 0.1ETH;  
 R4(16.05-30.05): 0% bonus;  min deposit 0.1ETH;
 */