contract Scalecoin is owned, TokenERC20 {

        using SafeMath for uint256;
        uint256 public startTime = 0; //client wants ICO run Infinite time, so startTimeStamp 0
        uint256 public endTime = 9999999999999999999999; //and entTimeStamp higher number
        uint256 public exchangeRate = 30000; // this is how many tokens for 1 Ether
        uint256 public tokensSold = 0; // how many tokens sold in crowdsale
        
        mapping (address => bool) public frozenAccount;
    
        /* This generates a public event on the blockchain that will notify clients */
        event FrozenFunds(address target, bool frozen);
    
        /* Initializes contract with initial supply tokens to the creator of the contract */
        constructor() TokenERC20() public {}

        /* Internal transfer, only can be called by this contract */
        function _transfer(address _from, address _to, uint _value) internal {
            require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
            require (balanceOf[_from] >= _value);               // Check if the sender has enough
            require (balanceOf[_to].add(_value) >= balanceOf[_to]); // Check for overflows
            require(!frozenAccount[_from]);                     // Check if sender is frozen
            require(!frozenAccount[_to]);                       // Check if recipient is frozen
            balanceOf[_from] = balanceOf[_from].sub(_value);                         // Subtract from the sender
            balanceOf[_to] = balanceOf[_to].add(_value);                           // Add the same to the recipient
            emit Transfer(_from, _to, _value);
        }
        
        //@dev fallback function, only accepts ether if ICO is running or Reject
        function () payable public {
            require(endTime > now);
            require(startTime < now);
            uint ethervalueWEI=msg.value;
            // calculate token amount to be sent
            uint256 token = ethervalueWEI.mul(exchangeRate); //weiamount * price
            tokensSold = tokensSold.add(token);
            _transfer(this, msg.sender, token);              // makes the transfers
            forwardEherToOwner();
        }
        
        //Automatocally forwards ether from smart contract to owner address
        function forwardEherToOwner() internal {
            owner.transfer(msg.value); 
          }
        
        //function to start an ICO.
        //It requires: start and end timestamp, exchange rate in Wei, and token amounts to allocate for the ICO
        //It will transfer allocated amount to the smart contract
        function startIco(uint256 start,uint256 end, uint256 exchangeRateInWei, uint256 TokensAllocationForICO) onlyOwner public {
            require(start < end);
            uint256 tokenAmount = TokensAllocationForICO.mul(1 ether);
            require(balanceOf[msg.sender] > tokenAmount);
            startTime=start;
            endTime=end;
            exchangeRate = exchangeRateInWei;
            approve(this,tokenAmount);
            transfer(this,tokenAmount);
        }       
        
        //Stops an ICO.
        //It will also transfer remaining tokens to owner
        function stopICO() onlyOwner public{
            endTime = 0;
            uint256 tokenAmount=balanceOf[this];
            _transfer(this, msg.sender, tokenAmount);
        }
        
        //function to check wheter ICO is running or not.
        function isICORunning() public view returns(bool){
            if(endTime > now && startTime < now){
                return true;                
            }else{
                return false;
            }
        }
        
        //Function to set ICO Exchange rate. 
        function setICOExchangeRate(uint256 newExchangeRate) onlyOwner public {
            exchangeRate=newExchangeRate;
        }
        
        //Just in case, owner wants to transfer Tokens from contract to owner address
        function manualWithdrawToken(uint256 _amount) onlyOwner public {
            uint256 tokenAmount = _amount.mul(1 ether);
            _transfer(this, msg.sender, tokenAmount);
          }
          
        //Just in case, owner wants to transfer Ether from contract to owner address
        function manualWithdrawEther()onlyOwner public{
            uint256 amount=address(this).balance;
            owner.transfer(amount);
        }
        
        /// @notice Create `mintedAmount` tokens and send it to `target`
        /// @param target Address to receive the tokens
        /// @param mintedAmount the amount of tokens it will receive
        function mintToken(address target, uint256 mintedAmount) onlyOwner public {
            balanceOf[target] = balanceOf[target].add(mintedAmount);
            totalSupply = totalSupply.add(mintedAmount);
           emit Transfer(0, this, mintedAmount);
           emit Transfer(this, target, mintedAmount);
        }
    
        /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
        /// @param target Address to be frozen
        /// @param freeze either to freeze it or not
        function freezeAccount(address target, bool freeze) onlyOwner public {
            frozenAccount[target] = freeze;
          emit  FrozenFunds(target, freeze);
        }



    }
/******************************************/
function setICOExchangeRate(uint256 newExchangeRate) onlyOwner public {
    exchangeRate=newExchangeRate;
}

//Function to set ICO Exchange rate. 