function f=index_builder(index,node_on_icefront,element_on_icefront)
%INDEX_BUILDER - ???
%
%   Usage:
%      f=index_builder(index,node_on_icefront,element_on_icefront)

index_icefront=[];
nel=length(index);
problematic_elements=[];
for n=1:nel
	
   if element_on_icefront(n)
      if (node_on_icefront(index(n,1)) & node_on_icefront(index(n,2)) & node_on_icefront(index(n,3))),
         problematic_elements=[problematic_elements
            n];
       else,
      
		if (node_on_icefront(index(n,1)) & node_on_icefront(index(n,2)))
			index_icefront=[index_icefront
            index(n,1) index(n,2)];
		end

		if (node_on_icefront(index(n,2)) & node_on_icefront(index(n,3)))
		   index_icefront=[index_icefront
            index(n,2) index(n,3)];
		end
		if (node_on_icefront(index(n,3)) & node_on_icefront(index(n,1)) & ...
							(1-node_on_icefront(index(n,2))))
		   index_icefront=[index_icefront
            index(n,3) index(n,1)];
		end
   	end
	end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%we deal with the problematic elements
type_prob_elem=zeros(length(problematic_elements),1);
position=zeros(length(problematic_elements),3);
for i=1:length(problematic_elements),
   for n=1:3,
      a=length(find(index_icefront == index(problematic_elements(i),n)));
      if a>=1, position(i,n)=1;
      else position(i,n)=0;
      end
      
   end
   	a=length(find(position(i,:)));
      type_prob_elem(i)=a(1);
end

%we begin with the type_prob_elem =2
index_front_spec=[];
pos=find(type_prob_elem ==2);
for j=1:length(pos),
   el=problematic_elements(pos(j));
   pos2=find(position(pos(j),:)==0);
   switch pos2,
   case 1,
      index_front_spec=[index_front_spec
         index(el,1) index(el,2)
      index(el,3) index(el,1)];
   case 2,
      index_front_spec=[index_front_spec
         index(el,2) index(el,3)
      index(el,1) index(el,2)];
   case 3,
      index_front_spec=[index_front_spec
         index(el,3) index(el,1)
      index(el,2) index(el,3)];
   end   
end

%now the type_prob_elem=1
pos=find(type_prob_elem==1);
for n=1:length(pos),
el=problematic_elements(pos(n));

pos2=find(position(pos(n),:)==0);
for m=1:length(pos2),
   a=length(find(index_front_spec==index(el,pos2(m))));
   if a>=1,
      position(pos(n),pos2(m))=1;
   end
end

pos2=find(position(pos(n),:)==0);
switch pos2,
   case 1,
      index_front_spec=[index_front_spec
         index(el,1) index(el,2)
      index(el,3) index(el,1)];
   case 2,
      index_front_spec=[index_front_spec
         index(el,2) index(el,3)
      index(el,1) index(el,2)];
   case 3,
      index_front_spec=[index_front_spec
         index(el,3) index(el,1)
      index(el,2) index(el,3)];
end   
end
   
   
%now the type_prob_elem=0
pos=find(type_prob_elem==0);
for n=1:length(pos),
el=problematic_elements(pos(n));

pos2=find(position(pos(n),:)==0);
for m=1:length(pos2),
   a=length(find(index_front_spec==index(el,pos2(m))));
   if a>=1,
      position(pos(n),pos2(m))=1;
   end
end

pos2=find(position(pos(n),:)==0);
switch pos2,
   case 1,
      index_front_spec=[index_front_spec
         index(el,1) index(el,2)
      index(el,3) index(el,1)];
   case 2,
      index_front_spec=[index_front_spec
         index(el,2) index(el,3)
      index(el,1) index(el,2)];
   case 3,
      index_front_spec=[index_front_spec
         index(el,3) index(el,1)
      index(el,2) index(el,3)];
end   
end

%now we have to eliminate the lines that appear in the index_front_spec and that are inner lines to the mesh
flag_elim=zeros(length(index_front_spec),1);
for n=1:length(index_front_spec),
   a=length(find_line(index,index_front_spec(n,:)));
   if a>=2,
      flag_elim(n)=1;
   end
end
index_front_spec=erase_lines(index_front_spec,flag_elim);
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

   
   index_icefront=[index_icefront
      index_front_spec];
   
f=index_icefront;
   
   
   
