%
% valid=validatetag(tagDef, tagTable)
%
% version 0.1 (1999-12-01) 
% Moze sprawic klopty ??
%

function valid=validatetag(tagDef, tagTable)

PagCount=sum(tagDef(:,1)==1);
BlkCount=sum(tagDef(:,1)==2);
ChnCount=sum(tagDef(:,1)==3);

%
% Podstawowe testy poprawnosci tagDef i tagTable
% (mozne jeszcze troche dolozyc np. testy znaku itp.
% 

if PagCount>0
   Tags=tagDef(find(tagDef(:,1)==1),2);
   numTags=size(Tags,1);
   for i=1:numTags
      if (sum(Tags==Tags(i))>1)
         valid=-1;
         return;
      end
   end
end

if BlkCount>0
   Tags=tagDef(find(tagDef(:,1)==2),2);
   numTags=size(Tags,1);
   for i=1:numTags
      if (sum(Tags==Tags(i))>1)
         valid=-2;
         return;
      end
   end
   
   BlkPP=tagDef(1,3);
   Tags=tagTable(find(tagTable(:,1)==2), 4);
   if sum(Tags<1 | Tags>BlkPP)>0
      valid=-5;
      return;
   end
end

Channels=tagDef(1,4);
if (ChnCount>0) & (Channels>=1)
   Tags=tagDef( find(tagDef(:,1)==3), 2);
   numTags=size(Tags,1);

   for i=1:numTags
      if (sum(Tags==Tags(i))>1)
         valid=-3;
         return;
      end
   end

   Tags=tagTable(find(tagTable(:,1)==3),2);
   if (sum(Tags<0 | Tags>Channels)>0)
      valid=-4;
      return;
   end
end

%
% OK.
%

valid=1;
