************************************************************************
* This file is part of OpenMolcas.                                     *
*                                                                      *
* OpenMolcas is free software; you can redistribute it and/or modify   *
* it under the terms of the GNU Lesser General Public License, v. 2.1. *
* OpenMolcas is distributed in the hope that it will be useful, but it *
* is provided "as is" and without any express or implied warranties.   *
* For more details see the full text of the license in the file        *
* LICENSE or in <http://www.gnu.org/licenses/>.                        *
*                                                                      *
* Copyright (C) 1996-2006, Thorstein Thorsteinsson                     *
*               1996-2006, David L. Cooper                             *
************************************************************************
      subroutine ortelim_cvb(trprm,iorts,irots,sorbs,
     >  nc,npr1,norbprm,nrem)
      implicit real*8 (a-h,o-z)
#include "main_cvb.fh"
#include "optze_cvb.fh"
#include "files_cvb.fh"
#include "print_cvb.fh"

#include "WrkSpc.fh"
      dimension trprm(npr1,npr1),iorts(2,nort),irots(2,ndrot)
      dimension sorbs(norb,norb)
      dimension dum(1)

      i1 = mstackrz_cvb(norbprm*max(nc+nort+ndrot,norbprm))
      i1ff=i1-1
      do 100 i=1,nc
      call fmove_cvb(trprm(1,i),work(1+(i-1)*norbprm+i1ff),norbprm)
100   continue
      do 200 iort=1,nort
      iorb=iorts(1,iort)
      jorb=iorts(2,iort)
      do 201 korb=1,norb
      ki=korb+(iorb-1)*(norb-1)
      if(korb.gt.iorb)ki=ki-1
      kj=korb+(jorb-1)*(norb-1)
      if(korb.gt.jorb)kj=kj-1
      if(korb.ne.iorb)work(ki+(iort+nc-1)*norbprm+i1ff)=sorbs(korb,jorb)
      if(korb.ne.jorb)work(kj+(iort+nc-1)*norbprm+i1ff)=sorbs(korb,iorb)
201   continue
200   continue
      do 300 irot=1,ndrot
      iorb=irots(1,irot)
      jorb=irots(2,irot)
      do 301 korb=1,norb
      ki=korb+(iorb-1)*(norb-1)
      if(korb.gt.iorb)ki=ki-1
      kj=korb+(jorb-1)*(norb-1)
      if(korb.gt.jorb)kj=kj-1
      if(korb.ne.iorb)work(ki+(irot+nc+nort-1)*norbprm+i1ff)=
     >  sorbs(korb,jorb)
      if(korb.ne.jorb)work(kj+(irot+nc+nort-1)*norbprm+i1ff)=
     >  -sorbs(korb,iorb)
301   continue
300   continue
      call span_cvb(work(i1),nc+nort+ndrot,nrem,dum,norbprm,0)
      call compl_cvb(work(i1),nrem,norbprm)

      call fzero(trprm,npr1*npr1)
      do 400 i=1,norbprm
      call fmove_cvb(work(1+(i-1)*norbprm+i1ff),trprm(1,i),norbprm)
400   continue

      call mfreer_cvb(i1)
      return
      end
