contract JACSToken is ERC20Interface, Owned, Lock {
    using SafeMath for uint;
    string public symbol;
    string public  name;
    uint8 public decimals;
    uint256 public tokensForTokenSale; 
    address public tokenSaleAddress;                    // address of TokenSale contract
    uint256 public totalAllocatedTokens;                // variable to regulate the funds allocation
    uint256 public ownerHold; 
    uint internal _totalSupply;
    mapping(address => uint) public balances;
    mapping(address => mapping(address => uint)) public allowed;
    
        /**
     * @dev Error messages for require statements
     */
    string internal constant AMOUNT_ZERO = "Amount can not be 0";
    string internal constant ONLY_OWNER = "Only owner has the right to perform this action";
    address public owner = msg.sender;
    
    constructor( address _tokensaleContract) public {
           symbol = "JACS";
           name = "JACS Token";
           decimals = 18;
           tokenSaleAddress = _tokensaleContract;
           _totalSupply = 536870912 * 10**uint(decimals);
           
           tokensForTokenSale = 268435456 * 10**uint(decimals);
           ownerHold = 268435456 * 10**uint(decimals);
           
            balances[tokenSaleAddress] = tokensForTokenSale;
            balances[owner] = ownerHold;
            
            emit Transfer(address(0), tokenSaleAddress, tokensForTokenSale);
            emit Transfer(address(0), owner, ownerHold);
         }
         // ------------------------------------------------------------------
        // Total supply
        // -------------------------------------------------------------------
        function totalSupply() public constant returns (uint) {
             return _totalSupply.sub(balances[address(0)]);
        }
     // ------------------------------------------------------------------
    // modifier
    // ------------------------------------------------------------------
      modifier onlyTokenSale() {
        require(msg.sender == tokenSaleAddress);
    _;
  }
  
    modifier onlyOwner {
        require(msg.sender == owner, ONLY_OWNER);
        _;
    }
    
       /**lock
     * @dev Transfers and Locks a specified amount of tokens,
     *      for a specified reason and time
     * @param _to adress to which tokens are to be transfered
     * @param _reason The reason to lock tokens
     * @param _amount Number of tokens to be transfered and locked
     * @param _days Number of days for locked token
     */
    function transferWithLock(address _to, bytes32 _reason, uint256 _amount, uint256 _days)
    public
    onlyOwner 
    returns(bool) {
        uint256 validUntil = now.add(_days.mul(86400)); //solhint-disable-line
        require(_amount != 0, AMOUNT_ZERO);

        if (locked[_to][_reason].amount == 0)
            lockReason[_to].push(_reason);

        transfer(address(this), _amount);

        locked[_to][_reason] = LockToken(_amount, validUntil, false);

        emit Locked(_to, _reason, _amount, validUntil);
        return true;
    }

    /**
     * @dev Returns tokens locked for a specified address for a
     *      specified reason
     * @param _of The address whose tokens are locked
     * @param _reason The reason to query the lock tokens for
     */
    function tokensLocked(address _of, bytes32 _reason)
    public
    view
    returns(uint256 amount) {
        if (!locked[_of][_reason].claimed)
            amount = locked[_of][_reason].amount;
    }

    /**
     * @dev Returns unlockable tokens for a specified address for a specified reason
     * @param _of The address to query the the unlockable token count of
     * @param _reason The reason to query the unlockable tokens for
     */
    function tokensUnlockable(address _of, bytes32 _reason)
    public
    view
    returns(uint256 amount) {
        if (locked[_of][_reason].validity <= now && !locked[_of][_reason].claimed) //solhint-disable-line
            amount = locked[_of][_reason].amount;
    }

    /**
     * @dev Unlocks the unlockable tokens of a specified address
     * @param _of Address of user, claiming back unlockable tokens 
     */
    function unlock(address _of)
    public
    returns(uint256 unlockableTokens) {
        uint256 lockedTokens;

        for (uint256 i = 0; i < lockReason[_of].length; i++) {
            lockedTokens = tokensUnlockable(_of, lockReason[_of][i]);
            if (lockedTokens > 0) {
                unlockableTokens = unlockableTokens.add(lockedTokens);
                locked[_of][lockReason[_of][i]].claimed = true;
                emit Unlocked(_of, lockReason[_of][i], lockedTokens); 
                
                if (unlockableTokens > 0){
                    this.transfer(_of, unlockableTokens);
                }
            }
        }
    }

    /**
     *  @dev Internal function that burns an amount of the token
     * @param tokens The amount that will be burnt.
     */

    function burn(uint256 tokens) public onlyOwner  returns(bool) {
        _burn(owner, tokens);
        return true;
    }
    
   /**
   * @dev Internal function that burns an amount of the token of a given
   * account.
   * @param account The account whose tokens will be burnt.
   * @param amount The amount that will be burnt.
   */
  function _burn(address account, uint256 amount) internal {
    require(account != 0);
    require(amount <= balances[account]);

    _totalSupply = _totalSupply.sub(amount);
    balances[account] = balances[account].sub(amount);
    emit Transfer(account, address(0), amount);
  }
  
 /**
      @dev function used to change the supply of total Allocated tokens in the market , it only called by TokenSale
      @param _amount amount is the token quantity added in token supply
  
   */
  function totalAllocatedTokens(uint256 _amount) public onlyTokenSale {
    totalAllocatedTokens += _amount;
  }
  
     // ------------------------------------------------------------------
    // Get the token balance for account `tokenOwner`
    // -------------------------------------------------------------------
     function balanceOf(address tokenOwner) public constant returns (uint balance) {
         return balances[tokenOwner];
    }
     // ------------------------------------------------------------------
     // Transfer the balance from token owner's account to `to` account
    // - Owner's account must have sufficient balance to transfer
     // - 0 value transfers are allowed
     // ------------------------------------------------------------------
     function transfer(address to, uint tokens) public returns (bool success) {
        balances[msg.sender] = balances[msg.sender].sub(tokens);
        balances[to] = balances[to].add(tokens);
        emit Transfer(msg.sender, to, tokens);
         return true;
     }
     // ------------------------------------------------------------------
    // Token owner can approve for `spender` to transferFrom(...) `tokens`
     // from the token owner's account
  // recommends that there are no checks for the approval double-spend
 // attack
 // as this should be implemented in user interfaces
     // ------------------------------------------------------------------
    function approve(address spender, uint tokens) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
         return true;
     }
  // -------------------------------------------------------------------
  // Transfer `tokens` from the `from` account to the `to` account
  //
  // The calling account must already have sufficient tokens
 // approve(...)-d
 // for spending from the `from` account and
  // - From account must have sufficient balance to transfer
  // - Spender must have sufficient allowance to transfer
   // - 0 value transfers are allowed
  // -------------------------------------------------------------------
    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        balances[from] = balances[from].sub(tokens);
         allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
         balances[to] = balances[to].add(tokens);
         emit Transfer(from, to, tokens);
         return true;
     }
    // -------------------------------------------------------------------
     // transferred to the spender's account
     // ------------------------------------------------------------------
     function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
         return allowed[tokenOwner][spender];
    }
     // ------------------------------------------------------------------
    // Token owner can approve for `spender` to transferFrom(...) `tokens`
    // from the token owner's account. The `spender` contract function
     // `receiveApproval(...)` is then executed
     // ------------------------------------------------------------------
     function approveAndCall(address spender, uint tokens, bytes memory data) public returns (bool success) {
         allowed[msg.sender][spender] = tokens;
         emit Approval(msg.sender, spender, tokens);
         ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, address(this), data);
         return true;
     }
    // -------------------------------------------------------------------
     // Don't accept ETH
    // -------------------------------------------------------------------
     function () external payable {
         revert();
     }
// -------------------------------------------------------------------
     // Owner can transfer out any accidentally sent ERC20 tokens
    // -------------------------------------------------------------------
    function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
        return ERC20Interface(tokenAddress).transfer(owner, tokens);
    }
}
function burn(uint256 tokens) public onlyOwner  returns(bool) {
    _burn(owner, tokens);
    return true;
}

/**
 *  @dev Internal function that burns an amount of the token
 * @param tokens The amount that will be burnt.
 */