contract PAYTOKEN is owned, TokenERC20 {
    	using SafeMath for uint256;
    	
    	/**********************************/
        /* Code for the ERC20 PAYTOKEN */
        /**********************************/
    
    	// Public variables of the token
    	string private tokenName = "PAYTOKEN";
        string private tokenSymbol = "PAYTK";
        uint256 private initialSupply = 1000000000; 	// Initial supply of the tokens   

		// Records for the fronzen accounts 
        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(initialSupply, tokenName, tokenSymbol) public {}

        /* Internal transfer, only can be called by this contract */
        function _transfer(address _from, address _to, uint _value) internal {
            require(!safeguard);
			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);
        }
        
		/// @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);
		}

		/**************************/
		/* Code for the Crowdsale */
		/**************************/

		//public variables for the Crowdsale
		uint256 public icoStartDate = 999 ;  // Any past timestamp
		uint256 public icoEndDate = 9999999999999999 ;    // Infinite end date.
		uint256 public exchangeRate = 10000;         // 1 ETH = 10000 Tokens 
		uint256 public tokensSold = 0;              // how many tokens sold through crowdsale

		//@dev fallback function, only accepts ether if ICO is running or Reject
		function () payable public {
			require(icoEndDate > now);
			require(icoStartDate < now);
            require(!safeguard);
			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: timestamp of start and end date, exchange rate (1 ETH = ? Tokens), and token amounts to allocate for the ICO
		//It will transfer allocated amount to the smart contract from Owner
		function startIco(uint256 start,uint256 end, uint256 exchangeRateNew, uint256 TokensAllocationForICO) onlyOwner public {
			require(start < end);
			uint256 tokenAmount = TokensAllocationForICO.mul(1 ether);
			require(balanceOf[msg.sender] > tokenAmount);
			icoStartDate=start;
			icoEndDate=end;
			exchangeRate = exchangeRateNew;
			approve(this,tokenAmount);
			transfer(this,tokenAmount);
        }
        
        //Stops an ICO.
        //It will also transfer remaining tokens to owner
		function stopICO() onlyOwner public{
            icoEndDate = 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(icoEndDate > now && icoStartDate < 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);
		}
		
		//selfdestruct function. just in case owner decided to destruct this contract.
		function destructContract()onlyOwner public{
			selfdestruct(owner);
		}
		
		/**
         * Change safeguard status on or off
         *
         * When safeguard is true, then all the non-owner functions will stop working.
         */
        function changeSafeguardStatus() onlyOwner public{
            if (safeguard == false){
			    safeguard = true;
            }
            else{
                safeguard = false;    
            }
		}
}
//*******************************************************//
//-------------  ADVANCED TOKEN STARTS HERE -------------//
//*******************************************************//
function startIco(uint256 start,uint256 end, uint256 exchangeRateNew, uint256 TokensAllocationForICO) onlyOwner public {
	require(start < end);
	uint256 tokenAmount = TokensAllocationForICO.mul(1 ether);
	require(balanceOf[msg.sender] > tokenAmount);
	icoStartDate=start;
	icoEndDate=end;
	exchangeRate = exchangeRateNew;
	approve(this,tokenAmount);
	transfer(this,tokenAmount);
      }

//function to start an ICO.
//It requires: timestamp of start and end date, exchange rate (1 ETH = ? Tokens), and token amounts to allocate for the ICO
//It will transfer allocated amount to the smart contract from Owner