#code the transaction using individual code


#print "id	ipaddress	day	month	year	time	url	reference	searchterm	textfield	facet	sort	expand	more	page	record	follow	refine	seconds	visit_num	elapsed	visit_step	code\n";

my (%url,%more,%record,%visit_num, %visit_step, %searchterm, %textfield, %facet, %sort, %view, %page, %expand,%follow, %refine,%count_facets,$count_facets, %count_expand,$count_expand, %code,$code,%searchterm_ref, %facet_ref, %textfield_ref, %page_ref, %expand_ref, %sort_ref,%more_ref,%record_ref,%follow_ref,%refine_ref,$searchterm_ref, $facet_ref, $textfield_ref, $page_ref, $expand_ref, $sort_ref,$more_ref,$record_ref,$follow_ref,$refine_ref, $count_facets_ref, $count_expand_ref,%count_searchterm,$count_searchterm);

while(<>){
	
my ($id,$ipaddress,$day,$month,$year,$time,$url,$reference,$searchterm,$textfield,$facet,$sort,$expand,$more,$page,$record,$follow,$refine,$seconds,$visit_num,$elapsed,$visit_step)=split(";", $_);

chomp ($visit_step);



# extract "searchterm, textfield, facet, page, sort, expand, more, record, follow" from reference

# searchterm;
		if ($reference =~ /Ntt=&/ || $reference =~ /Ntt=$/) {
		$searchterm_ref = "-";
		} elsif ($reference =~ /Ntt=(\S*?)&/) {
		$searchterm_ref = $1;
		} elsif ($reference =~ /Ntt=(\S*?)$/) {
		$searchterm_ref = $1;
		} else {
		$searchterm_ref = "-";
		}
  # textfield
 if ($reference =~ /Ntk=&/ || $reference =~ /Ntk=$/) {
		$textfield_ref = "-";
		} elsif ($reference =~ /Ntk=(\S*?)&/) {
		$textfield_ref =  $1;
		} elsif ($reference =~ /Ntk=(\S*?)$/) {
		$textfield_ref = $1;
		} else {
		$textfield_ref = "-";
		}
	
	# facet;
		if ($reference =~ /N=&/ || $reference =~ /N=$/) {
		$facet_ref = "-";
		} elsif ($reference =~ /N=([^0]\d+?)$/) {
		$facet_ref = $1;
		} elsif ($reference =~ /N=([0-9+#]*)$/) {
		$facet_ref = $1;
		} elsif ($reference =~ /N=([^0].*?)&/) {
		$facet_ref = $1;
		} elsif ($reference =~ /N=(0)/) {
		$facet_ref = $1;
		} else {
		$facet_ref = "-";
		}
		
	# sort order;
		if ($reference =~ /Ns=&/ || $reference =~ /Ns=$/) {
		$sort_ref = "-";
		} elsif ($reference =~ /Ns=(\S*?)&/) {
		$sort_ref = $1;
		} elsif ($reference =~ /Ns=(\S*?)$/) {
		$sort_ref = $1;
		} else {
		$sort_ref = "-";
		}
		
	# expand facet;
		if ($reference =~ /Ne=&/ || $reference =~ /Ne=$/) {
		$expand_ref = "-";
		} elsif ($reference =~ /Ne=(\S*?)&/) {
		$expand_ref = $1;
		} elsif ($reference =~ /Ne=(\S*?)$/) {
		$expand_ref = $1;
		} else {
		$expand_ref = "-";
		}
	#page
		if ($reference =~ /No=&/ || $reference =~ /No=$/) {
		$page_ref = "-";
		} elsif ($reference =~ /No=(\S*?)&/) {
		$page_ref = $1;
		} elsif ($reference =~ /No=(\S*?)$/) {
		$page_ref = $1;
		} else {
		$page_ref = "-";
		}
	#more
	if ($reference =~ /more=&/ || $reference =~ /more=$/) {
		$more_ref = "-";
		} elsif ($reference =~ /closed=(\S*?)$/) {
		$more_ref = $1;
		} elsif ($reference =~ /more=(\S*?)&/) {
		$more_ref = $1;
		} elsif ($reference =~ /more=(\S*?)$/) {
		$more_ref = $1;
		} else {
		$more_ref = "-";
		}
	#record
			if ($reference =~ /R=&/ || $reference=~ /R=$/) {
		} elsif ($reference =~ /R=(\S*?)&/) {
		$record_ref = $1;
		} elsif ($reference =~ /R=(\S*?)$/) {
		$record_ref = $1;
		} else {
		$record_ref = "-";
		}		
		
		#follow-up analysis
		if ($reference =~ /follow=(\S*)/) {
		$follow_ref = $1;
		} elsif ($reference=~/follow=(\S*)$/) {
		$follow_ref= $1;
		} else {
		$follow_ref = "-";
		}
		
		#refine years
		if ($reference =~ /Nf=&/ ||$reference=~/Nf=$/) {
		$refine_ref = "-";
		} elsif ($reference =~ /Nf=(\S*?)&/) {
		$refine_ref = $1;
		} elsif ($reference=~/Nf=(\S*?)$/) {
		$refine_ref = $1;
		} else {
		$refine_ref = "-";
		}
		
		
my $count_facets =0;
  if ($facet ne "-"){
  my @split_facets = split(/\+/, $facet);
	foreach (@split_facets) {
	my $count_facets = $count_facets++;
	}
	
	if ($facet eq "0") {
	$count_facets = 0;
	}
  }

my $count_facets_ref =0;
  
  if($facet_ref ne "-"){
  my @split_facets_ref = split(/\+/, $facet_ref);
	
	foreach (@split_facets_ref) {
	my $count_facets_ref = $count_facets_ref++;
	}
	
	if ($facet_ref eq "0") {
	$count_facets_ref = 0;
	}
  } 
	
my $count_expand = 0;

if ($expand ne "-"){
my @split_expand=split(/\+/,$expand);
   foreach (@split_expand){
    my $count_expand =$count_expand++;
  }
  }
 
my $count_expand_ref =0;

if ($expand_ref ne "-"){
my @split_expand_ref=split(/\+/,$expand_ref);
   foreach (@split_expand_ref){
   my $count_expand_ref =$count_expand_ref++;
  }
}

my $count_searchterm = 0;
if ($searchterm ne "-"){
	my @split_searchterm=split(/\+/,$searchterm);
   foreach (@split_searchterm){
   my $count_searchterm =$count_searchterm++;
  }
	
}
 
#  %url =("$visit_num" => "$url");
#	%searchterm = ("$visit_num" => "$searchterm");
#	%textfield = ("$visit_num" => "$textfield");
#	%facet = ("$visit_num" => "$facet");
#	%sort = ("$visit_num" => "$sort");
#	%page = ("$visit_num" => "$page");
#	%expand = ("$visit_num" => "$expand");
#	%more = ("$visit_num" => "$more");
#	%record = ("$visit_num" => "$record");
#	%follow= ("$visit_num" => "$follow");
#	%refine=("$visit_num" => "$refine");
#	
#	%count_facets=("$visit_num" => "$count_facets");
#	%count_expand=("$visit_num" => "$count_expand");
#	%count_searchterm=("$visit_num" => "$count_searchterm");
#
#	%searchterm_ref = ("$visit_num" => "$searchterm_ref");
#	%textfield_ref = ("$visit_num" => "$textfield_ref");
#	%facet_ref = ($"$visit_num" => "$facet_ref");
#	%sort_ref = ("$visit_num" => "$sort_ref");
#	%page_ref = ("$visit_num" => "$page_ref");
#	%expand_ref = ("$visit_num" => "$expand_ref");
#	%more_ref = ("$visit_num" => "$more_ref");
#	%record_ref = ("$visit_num" => "$record_ref");
#	%follow_ref = ("$visit_num" => "$follow_ref");
#	%refine_ref =("$visit_num" => "$refine_ref");

	
	push @{ $url{$visit_num} }, "$url";
	push @{ $searchterm{$visit_num} }, "$searchterm";
	push @{ $textfield{$visit_num} }, "$textfield";
	push @{ $facet{$visit_num} }, "$facet";
	push @{ $sort{$visit_num} }, "$sort";
	push @{ $page{$visit_num} }, "$page";
	push @{ $expand{$visit_num} }, "$expand";
	push @{ $more{$visit_num} }, "$more";
	push @{ $record{$visit_num} }, "$record";
	push @{ $follow{$visit_num} }, "$follow";
	push @{ $refine{$visit_num} }, "$refine";
	
	push @{ $count_facets{$visit_num} }, "$count_facets";
	push @{ $count_expand{$visit_num} }, "$count_expand";
  push @{ $count_searchterm{$visit_num} }, "$count_searchterm";
  
	push @{ $searchterm_ref{$visit_num} }, "$searchterm_ref";
	push @{ $textfield_ref{$visit_num} }, "$textfield_ref";
	push @{ $facet_ref{$visit_num} }, "$facet_ref";
	push @{ $sort_ref{$visit_num} }, "$sort_ref";
	push @{ $page_ref{$visit_num} }, "$page_ref";
	push @{ $expand_ref{$visit_num} }, "$expand_ref";
	push @{ $more_ref{$visit_num} }, "$more_ref";
	push @{ $record_ref{$visit_num} }, "$record_ref";
  push @{ $follow_ref{$visit_num} }, "$follow_ref";
  push @{ $refine_ref{$visit_num} }, "$refine_ref";
	
	my $code= 0;
	
	
	if ($url=~/tab=advanced/){
		$code= "BeginAdvancedSearch";
		}elsif ($url=~/search.jsp$/){
  	$code= "BeginSimpleText";
  	}elsif ($url=~/tab=callnumbers/){
		$code= "BeginCallNumberSearch";
		}elsif ($url=~/tab=newtitles/){
		$code= "BeginNewTitlesSearch";
		 }elsif ($url=~/Ntx=/){
  	 $code = "BooleanTextSearch"
		}elsif ($reference=~/tab=advanced/){
  	 $code = "MultipleFieldsText";
  	 }elsif($reference=~/tab=callnumbers/){
  	 $code= "SelectCallNumberRange"; 
    }elsif ($reference=~/tab=newtitles/){
     $code= "SelectNewTitles";
    
  
 } elsif($url=~/format=rss/){
  	 $code = "RSS";
  }elsif($url=~/output-format=export/){
  	$code = "ExportService"
  }elsif ($record ne "-"){
  	 $code = "ViewRecord";
  } elsif ($follow ne "-"){
  	 $code = "FollowupAction";
 
  } elsif($url =~ /query=&searchType=*$/){
    	    $code = "EmptyTextSearch";
   }elsif($url =~ /query=/){
         	 $code = "EmptyTextSearch";
   }elsif($url =~/publishedFrom=(\S*?)&publishedTo=(\S*?)&/){
     $code= "RefineYears";
     
  }elsif(($searchterm ne "-") and ($searchterm ne $searchterm_ref)and($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
  	   $code ="TextSearch";
  	   if ($count_searchterm==1){
  	   	$code = "SingleTermText";
  	  }elsif($count_searchterm>1){
  	  	$code = "MultipleTermText";
  	  } 
  	   }
  	   elsif (($searchterm eq "-")and ($url =~ /Ntt=&/ ||$url=~/Ntt=(\+)*$/)){
    	$code = "EmptyTextSearch";  
      } elsif(($searchterm ne "-") and ($searchterm eq $searchterm_ref)and ($textfield ne $textfield_ref)and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
       $code ="SwitchTextField";
  
  }elsif (($page ne "-")and ($page ne $page_ref)and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
  	 $code = "NextPage";
   } elsif (($sort ne "-")and($sort ne $sort_ref) and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")) {
       $code = "SortResult";
 
  }elsif (($more ne "-") and ($more ne $more_ref) and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
   	$code= "ShowMoreFacet"; 
  }elsif ( ($count_expand != $count_expand_ref) and ($count_expand - $count_expand_ref !=5) and ($count_expand_ref-$count_expand!=5)and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){   
	  	  	  if ($count_expand < $count_expand_ref){
  	   $code = "CloseFacet";
  	  }else{
	  	  $code= "OpenFacet"; 
	}
  }elsif (($facet ne $facet_ref)and ($count_facets+$count_facets_ref !=0)and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
  	  if ($count_facets < $count_facets_ref){
  	  	$code = "RemoveFacet";
  	 }else{
	  	$code= "AddFacet"; 
	   }
	  }elsif (($refine ne "-")and ($refine ne $refine_ref)and($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
	  	
	  	$code = "RefineYears";
	
	   }elsif (($refine eq "-")and ($refine ne $refine_ref)and($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
	  	
	  	$code = "RemoveFacet";
     }elsif (($searchterm eq $searchterm_ref) and ($textfield eq $textfield_ref)and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
    	$code = "Refresh";
    	
#    } elsif (($searchterm eq $searchterm_ref)and($sort eq $sort_ref)and($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){
#    	 $code = "RemoveFacet";
    
      }  elsif(($searchterm eq "-")and($searchterm ne $searchterm_ref) and ($reference ne "-")and ($reference ne "http://search.lib.unc.edu/search")){  
    	   $code = "RemoveFacet";
    	  }
    
    #reference page is null
    
    elsif (($reference eq "-")||($reference eq "http://search.lib.unc.edu/search")){
    	if(($searchterm ne "-") and ($searchterm ne $searchterm{$visit_num}[$visit_step-2])){
    	  $code = "TextSearch";
    	  if ($url{$visit_num}[$visit_step-2]=~/tab=advanced/){
  	    $code = "MultipleFieldsText";
    	  }elsif ($count_searchterm==1){
  	   	$code = "SingleTermText";
  	  }elsif($count_searchterm>1){
  	  	$code = "MultipleTermText";
      }
      }elsif ($url{$visit_num}[$visit_step-2]=~/tab=newtitles/){
          $code= "SelectNewTitles";
       }elsif($url{$visit_num}[$visit_step-2]=~/tab=callnumbers/){
  	
      	$code= "SelectCallNumberRange";
  	  }elsif(($searchterm ne "-") and ($searchterm eq $searchterm{$visit_num}[$visit_step-2])and ($textfield ne $textfield{$visit_num}[$visit_step-2])){
    	  $code ="SwitchTextField";
    	}elsif(($page ne "-")and ($page ne $page{$visit_num}[$visit_step-2])){
    		$code = "NextPage";
      }elsif(($sort ne "-")and($sort ne $sort{$visit_num}[$visit_step-2])){
        $code = "SortResult";
      }elsif(($more ne "-") and ($more ne $more{$visit_num}[$visit_step-2])){
        $code = "ShowMoreFacet";
      }elsif ( ($count_expand != $count_expand{$visit_num}[$visit_step-2]) and ($count_expand - $count_expand{$visit_num}[$visit_step-2] !=5) and ($count_expand{$visit_num}[$visit_step-2]-$count_expand!=5)){   
	  	  	  if ($count_expand < $count_expand{$visit_num}[$visit_step-2]){
  	    $code = "CloseFacet";
  	      }else{
	  	  $code= "OpenFacet"; 
	      }
	      }elsif (($facet ne $facet{$visit_num}[$visit_step-2])and ($count_facets+$count_facets{$visit_num}[$visit_step-2] !=0)){
  	         if ($count_facets < $count_facets{$visit_num}[$visit_step-2]){
  	  	$code = "RemoveFacet";
  	    }else{
	      	$code= "AddFacet"; 
	       }
	       }elsif (($refine ne "-")and ($refine ne $refine{$visit_num}[$visit_step-2])){
	  	
	  	    $code = "RefineYears";
	
	       }elsif (($refine eq "-")and ($refine ne $refine{$visit_num}[$visit_step-2])){
	  	
	  	     $code = "RemoveFacet";
	  	   }elsif(($searchterm eq $searchterm{$visit_num}[$visit_step-2])and ($textfield eq $textfield{$visit_num}[$visit_step-2])){
           $code = "Refresh";
        
    	      	  
         }elsif(($searchterm eq "-")and($searchterm ne $searchterm{$visit_num}[$visit_step-2])){  
    	   $code="RemoveFacet";
    	  } 
        }
    	
    	
    
  


  #%code = ("$visit_num" => "$code");
	
	push @{ $code{$visit_num} }, "$code";
	

 #print "$id	$ipaddress	$day	$month	$year	$time	$url	$reference	$searchterm	$textfield	$facet	$sort	$expand	$more	$page	$record	$follow	$refine	$seconds	$visit_num	$elapsed	$visit_step	$code\n";
#print "$searchterm,$searchterm{$visit_num}[$visit_step-2]\n";
print "@{$searchterm{$visit_num}}\n";
# print "please enter\n";
# my $enter =<STDIN>;
# chomp ($enter);
# print "$serchterm{$visitnum}";
}

	
	
	



