contract Crowdsale {
    
    using SafeMath for uint256;
    address fundsWallet;
    RTCoin public token;
    address public owner;
	bool public open = false;
    uint256 public tokenLimit;
    
    uint256 public rate = 20000; //
    
   
    function Crowdsale(address _fundsWallet, address tokenAddress, 
                       uint256 _rate, uint256 _tokenLimit) public {
        fundsWallet = _fundsWallet;
        token = RTCoin(tokenAddress);
        rate = _rate;
        owner = msg.sender;
        tokenLimit = _tokenLimit * (uint256(10) ** token.decimals());
    }
    
    
    function() external isOpen payable {
        require(tokenLimit>0);
        fundsWallet.transfer(msg.value);
        uint256 tokens = calculateTokenAmount(msg.value);
        token.transfer(msg.sender, tokens);
        tokenLimit = tokenLimit.sub(tokens);
    }
  
    
    function changeFundAddress(address newAddress) public onlyOwner {
        require(newAddress != address(0));
        fundsWallet = newAddress;
	}
	
	
    function changeRate(uint256 newRate) public onlyOwner {
        require(newRate>0);
        rate = newRate;
    }
    
   
    function calculateTokenAmount(uint256 weiAmount) public constant returns(uint256) {
        if (token.decimals()!=18){
            uint256 tokenAmount = weiAmount.mul(rate).div(uint256(10) ** (18-token.decimals())); 
            return tokenAmount;
        }
        else return weiAmount.mul(rate);
    }
    
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }
    
    
    function allowSale() public onlyOwner {
        open = true;
    }
    
    
    function disallowSale() public onlyOwner {
        open = false;
    }
    
    modifier isOpen() {
        require(open == true);
        _;
    }
}
function Crowdsale(address _fundsWallet, address tokenAddress, 
                       uint256 _rate, uint256 _tokenLimit) public {
        fundsWallet = _fundsWallet;
        token = RTCoin(tokenAddress);
        rate = _rate;
        owner = msg.sender;
        tokenLimit = _tokenLimit * (uint256(10) ** token.decimals());
    }
//