#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#                                                                     #
# THIS MODULE CONTAINS VARIOUS FUNCTIONS USED BY DODO.                #
# THIS MODULE CONTAINS  THE FUNCTIONA REQUIRED TO DISPLAY             #
# A MEANS OF SELECTIVELY DISPALYING DATABASE INFORMATION              #
#                                                                     #
#######################################################################
package Selector;
use Tk;
use vars qw(@ISA %SIG);
use dodo_DB_P001;
use NewBrowseEntry;
use Checkbarcode;

@ISA = qw(Database);


sub new 
{
my $pkg  = shift;
my $setup = {'List','Browse','Query1','Query2','Pages','Currentpage','Selection','Data','Pages'};

bless $setup,$pkg;
return $setup;
}

sub Test
{
my $pkg = shift;
my $chk;
$pkg->{Query1} = "Human (22)";
# Try forking something else !!



}


sub T2
{
my ($pkg, $chk) = @_;
$pkg->{Search} = "Busy";
return 1;
}


sub Addtobrowse
{

my $pkg = shift ;
my $entries;

foreach(@{$pkg->{List}})
	{$_->configure(-text => "",-fg => 'black',-bg => 'lightblue');}
	
# release previous lock
$pkg->{dbh}->commit;
my $ref = $pkg->Querydb($pkg->{ListSQL});

$pkg->{Browse}->delete(0,'end');
#if((!$pkg->{Query1})||($pkg->{Query1} =~ /^No Clones/)||($pkg->{Query1} =~ /^Another/)||($pkg->{Query1} =~ /^Error/))
#	{$pkg->{Query1} = $ENV{'DODO'};}


my $sc = ' ';

if(@{$ref})
{
	foreach(@$ref)
	{$pkg->{Browse}->insert('end',"$_->[0] ($_->[1])");
	$sc = "$sc$_->[0] ($_->[1])";
	}
	$pkg->{Browse}->insert('end',"ALL");
	$sc = "$sc ALL";
	
	if(!$pkg->{Query1})
	{
		#$pkg->{Query1} = $pkg->{Browse}->get(0);
	}
	else
	{
	$sc = index $sc, $pkg->{Query1},1;
		if($sc < 0)
			{$pkg->{Query1} = $pkg->{Browse}->get(0);}
	}
	
	
	
	
	
	
	#$pkg->Addtoresults();



}
else
{
	$pkg->{Query1} = 'No Clones Available';
	$pkg->{Pagestatus} = '';
}

	$pkg->{Sort}->delete(0,'end');
	$pkg->{Sort}->insert('end','Default');
	foreach my $s (@{$pkg->{Column}}){$pkg->{Sort}->insert('end',$s->[2]);}

}


sub Addtoresults
{
my $pkg = shift;
my $chk;



if($pkg->{Query1} eq ""){return;}
my ($species, $chromosome) = split /[(]|[)]/,$pkg->{Query1};
my $entries;
my $wait;
my $sql;

chop $species;

if ($species eq 'AL')
{
	$species = '%';
	$chromosome = '%';
}

if(!$chromosome)
	{return;}



undef $pkg->{Selection};
foreach(@{$pkg->{List}})
	{$_->configure(-text => "",-fg => 'black',-bg => 'lightblue');}
eval{
$sql = $pkg->{ResultSQL};
$sql =~ s/VAR1/$chromosome/g;
$sql =~ s/VAR2/$species/g;


# release previous lock
$pkg->{dbh}->commit;
my $results = $pkg->Querydb($sql);



$pkg->{Data} = $results;



$pkg->{Data} = $results;

# hack to point out hih preps to user
if($pkg->{Button1Label}	eq 'Streak Clones')
{
	foreach (@{$pkg->{Data}})
	{
	
	my $ref = $pkg->Querydb("SELECT 'x' FROM DUAL 
				WHERE EXISTS (	SELECT 	'X' 
						FROM 	DNAPREP 
						WHERE 	CLONENAME 	= '$_->[0]' 
						AND 	PREP_NUMBER 	> 2		)");
	
	if($ref->[0][0])
	{$_->[8] = "$_->[8] ! HIGH PREP !";}
	
	}
}


$pkg->{Currentpage} = 1;
$pkg->{Pages} = int(@{$results}/12);

if (((@{$results}/12) - $pkg->{Pages}) > 0)
{$pkg->{Pages}++;}

};if($@ =~ /ORA-00054:/)
{my $chk;
	$pkg->Message("LOCK OUT\nData Currently Being Accessed By another user\nContact Bug If Problem persists",\$chk);
	$pkg->{Mainwin}->waitVariable(\$chk);
	$pkg->{Query1} = "Another User Is Accessing $pkg->{Query1} Try Again Later";
	return;}
	
if($@)
{
	$pkg->{Query1} = "ERROR ERROR ERROR ERROR ERROR ERROR";
	print STDERR "$@ \n"; 
	return;
}
else
{
	$pkg->{Sort_by} = 'Default';
# Disabled because parallel server running so pants
#	$pkg->Getlock();
# reenabled for cloneplate (detect resends)
if(!$pkg->{Button1Label})
	{$pkg->Getlock();}
	$pkg->Fillpage();	
}

}





sub Setchoice
{
my ($evnt,$pkg,$wdgt) = @_;
my $f;
my $b;
my $c = 0;
my @position = $wdgt->gridInfo();
my $i = ($pkg->{Currentpage} - 1) * 12;
$i +=  ($position[5] - 4);

$b = $wdgt->cget(-bg);

if($b eq 'green')
{	
	$wdgt->configure( -bg => 'lightblue');
	undef $pkg->{Selection}[$i];
	
	foreach (@{$pkg->{Selection}})
	{if($_->[0]){$c++;}}
	$pkg->{Selectionstatus} = "                  Number Of Entries Selected = $c";
}
elsif($wdgt->cget(-text))
{
	$pkg->{Selection}[$i] = $pkg->{Data}[$i];
	$wdgt->configure( -bg => 'green');
	foreach (@{$pkg->{Selection}})
	{if($_->[0]){$c++;}}
	
	$pkg->{Selectionstatus} = "                  Number Of Entries Selected = $c";
}
else
{
	return;
}


}







sub Fillpage
{
my $pkg = shift;
my $i;
my $j;
my $f;
my $b;
my $pad;

my $d = 0;


if($pkg->{'Data'}->[0])
	{$d = scalar @{$pkg->{'Data'}};}


if (@_)
{my $p = shift;
 
	if (($p > 0)&&($p <= $pkg->{Pages}))
	{$pkg->{Currentpage} = $p;}
	for $j (0..11)
	{
	$pkg->{List}[$j]->configure(	-text => '', 
					-fg => 'black', 
					-bg => 'lightblue');
	}

}

$b = "lightblue";
$f = "black";
$max = 12;
if(($pkg->{Currentpage} == $pkg->{Pages})&&($d%12 != 0)){$max = $d%12;}

if($pkg->{'Data'}->[0])
{


for($j = 0; $j < $max;$j++)
{
;
$i = $j + (12 * ($pkg->{Currentpage}-1));

# pad clone name to max
$pad = '';
my $limit = 0;
foreach (@{$pkg->{Column}})
{	
	my $sub = substr($pkg->{Data}[$i][$_->[0]],0,$_->[1]-2);
	$pad = "$pad$sub";
	$limit = $limit + $_->[1] + 2;
	
	while(length($pad) < $limit)
	{$pad = "$pad ";}
	
	$pad = "$pad | ";
	
	
}

	my $l = scalar @{$pkg->{Data}[$i]};
	$l--;
	# Use pattern match to test for numerical field
	# Priority must be the penultimate element of record array
	$f = 'black';
	
	if($pkg->{Data}[$i][$l-2] !~ /\D/)
		{if($pkg->{Data}[$i][$l-2] > 0){$f = 'red';}}
		
	
	if($pkg->{Selection}[$i][0])
		{$b = "green";}
	else
		{$b = "lightblue";}
		
	
	$pkg->{List}[$j]->configure(-text => $pad, -bg => $b, -fg => $f);

}
$pkg->{Pagestatus}= "Page $pkg->{Currentpage} of $pkg->{Pages}          Total Number Of Entries = $d";
}

}







sub Display
{
my ($pkg,$window) = @_;	
my $row;	

				
my $frame2 = $window->Frame(		-width 			=> 500,
					-bg 			=> "gray85",
					-relief 		=> 'flat'
			)->grid(	-row 			=> 1,
					-column 		=> 1,
					-columnspan 		=> 2);
					
my $frame3 = $frame2->Frame(		-width 			=> -1,
					-bg 			=> "gray85",
					-borderwidth		=> 2,
					-relief 		=> 'groove'
			)->grid(	-row 			=> 4,
					-column 		=> 1,
					-columnspan 		=> 2);
					
my $frame4 = $frame2->Frame(		-width 			=> -1,
					-bg 			=> "gray85",
					-borderwidth		=> 2,
					-relief 		=> 'groove'
			)->grid(	-row 			=> 17,
					-column 		=> 1,
					-columnspan 		=> 2,
					-sticky			=> 'we');
					
$pkg->{Pages} = 1;						
$pkg->{Currentpage} = 1;
@{$pkg->{List}} = (0..11);

$frame2->Label(				-fg 			=> 'blue1',
					-font 			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
					-bg 			=> 'grey85',
					-height			=> -1,
					-width 			=> 70,
					-textvariable 		=> \$pkg->{Columnheaders},
					-anchor 		=> 'nw',
					-borderwidth		=> 2,
					-relief			=> 'groove'
			)->grid(	-row 			=> 3, 
					-column 		=> 1, 
					-columnspan 		=> 2,
					-sticky 		=> 'we');
					
$pkg->{Selectionstatus} = "                  Number Of Entries Selected = 0";	
				
$frame4->Label(				-fg 			=> 'blue1',
					-font 			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
					-height			=> -1,
					-width			=> 55,
					-textvariable 		=> \$pkg->{Selectionstatus},
					-anchor 		=> 'nw',
			)->grid(	-row 			=> 1, 
					-column 		=> 1, 
					-sticky 		=>'w');
					
$frame4->Label(				-fg 			=> 'blue1',
					-font 			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
					-height			=> -1,
					-width			=> 55,
					-textvariable 		=> \$pkg->{Pagestatus},
					-anchor 		=> 'nw',
			)->grid(	-row 			=> 2, 
					-column 		=> 1, 
					-sticky 		=>'w');
					
					
					
$row = 4;

foreach(@{$pkg->{List}})
{

$_ =  $frame3->Label(			-fg 			=> 'black',
					-bg 			=> 'lightblue',
					-font			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
					-height			=> -1,
					-width 			=> 100,
					-anchor 		=> 'w',
					-borderwidth		=> 1,
					-relief			=> 'groove'
					
			)->grid(	-row 			=> $row, 
					-column 		=> 1, 
					-columnspan 		=> 2,
					-sticky 		=> 'we');

$_->bind('<ButtonPress-1>', 	[\&Setchoice, 		$pkg,$_		]);
$_->bind('<Shift-Enter>', 	[\&Setchoice, 		$pkg,$_		]);


$row++;
}

my $but1 = $frame4->Button(		-width			=> -1,
					-command 		=> sub{$pkg->Fillpage($pkg->{Currentpage} - 1);}
			)->grid(	-row 			=> 1, 
					-column 		=> 2,
					-rowspan		=> 2,
					-pady			=> 1,
					-sticky 		=> 'e');
					
my $but2 = $frame4->Button(		-width 			=> -1,
					-command 		=> sub{$pkg->Fillpage($pkg->{Currentpage} + 1);}
			)->grid(	-row 			=> 1, 
					-column 		=> 3,
					-rowspan		=> 2, 
					-pady			=> 1,
					-sticky 		=> 'w');
					
$frame4->Button(			-text			=> "Toggle Selection",
					-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1',
					-width 			=> -1,
					-bg			=> 'darkgreen',
					-fg			=> 'lemonchiffon1',
					-activebackground	=> 'lemonchiffon1',
					-activeforeground	=> 'darkgreen',
					-command 		=> sub{$pkg->Selectall();}
			)->grid(	-row 			=> 1,
					-rowspan		=> 2, 
					-column 		=> 4, 
					-pady			=> 1,
					-padx			=> 5,
					-sticky 		=> 'w');
my $col = -1;



					
my $p = "$ENV{'BADGER'}/lib/dodo";
$image = $but1->Photo(-file => "$p/dodo_uarrw.gif");
$but1->configure(-image => $image);	



$image = $but2->Photo(-file => "$p/dodo_darrw.gif");
$but2->configure(-image => $image);	
			

$pkg->{Mainwin}->bind('<Key-Down>',	sub{$pkg->Fillpage($pkg->{Currentpage} + 1);});
$pkg->{Mainwin}->bind('<Key-Up>',	sub{$pkg->Fillpage($pkg->{Currentpage} - 1);});


$frame2->Label(				-fg 			=> 'black',
					-bg 			=> 'grey85',
					-width 			=> 55,
					-text 			=> "Retrieve Clones By :- Species (Chromosome)",
					-font 			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',,
					-anchor 		=> 'e',
					-height 		=> -1,
					-relief 		=> 'flat'
			)->grid(	-row 			=> 1, 
					-column 		=> 1, 
					-sticky 		=>'e');

my $e;
$pkg->{Sort} = $frame2->BrowseEntry(	-width 			=> 35,
					-fg 			=> 'blue1',
					-listcmd		=> sub{$pkg->{Old_sort} = $pkg->{Sort_by};},
					-browsecmd 		=> sub{if($pkg->{Old_sort} ne $pkg->{Sort_by})
									{$pkg->Customsort();}
									$e->focus
									},
					-font			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
					-state 			=> 'readonly',
					-variable 		=> \$pkg->{Sort_by}
			)->grid(	-row 			=> 2,
					-column 		=> 2,
					-columnspan		=> 1,
					-sticky			=> 'w');
					
$pkg->{Sort}->Subwidget("slistbox")->Subwidget("listbox")->configure(	-font 	=>'-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
									-fg 	=> 'blue1');



									
$frame2->Label(				-fg 			=> 'black',
					-bg 			=> 'grey85',
					-width 			=> 55,
					-text 			=> "Sort By",
					-font 			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',,
					-anchor 		=> 'e',
					-height 		=> -1,
					-relief 		=> 'flat'
			)->grid(	-row 			=> 2, 
					-pady			=> 5,
					-column 		=> 1,
					-sticky 		=>'e');




$pkg->{Query1} = "";
$pkg->{Query2} = 1;
				
$pkg->{Browse}  = $frame2->BrowseEntry(	-width 			=> 35,
					-fg 			=> 'blue1',
					-browsecmd 		=> sub{$pkg->Addtoresults();$e->focus},
					-font			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
					-state 			=> 'readonly',
					-variable 		=> \$pkg->{Query1}
			)->grid(	-row 			=> 1,
					-column 		=> 2,
					-columnspan		=> 1,
					-sticky			=> 'w');
					
$pkg->{Browse}->Subwidget("slistbox")->Subwidget("listbox")->configure(	-font 	=>'-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
									-fg 	=> 'blue1');
									
$frame2->Label(				-fg 			=> 'black',
					-bg 			=> 'grey85',
					-width 			=> 55,
					-text 			=> "Search Retrieved Data For Clone ",
					-font 			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',,
					-anchor 		=> 'e',
					-height 		=> -1,
					-relief 		=> 'flat'
			)->grid(	-row 			=> 16, 
					-pady			=> 5,
					-column 		=> 1,
					-sticky 		=>'e');
					
$e = $frame2->Entry(			-width 			=> 35,
					-fg 			=> 'black',
					-bg			=> 'lemonchiffon1',
					-font			=> '-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',,
					-textvariable 		=> \$pkg->{Search},
			)->grid(	-row 			=> 16,
					-columnspan		=> 1,
					-column 		=> 2,
					-padx			=> 6,
					-sticky 		=> 'w');
$e->bind('<Key-Return>', sub{$pkg->Search(\$e);});

$e->bind('<Button-1>', sub {$e->icursor('end');});
$e->bind("<Key>",[sub {	my ($e,$x,$k) = @_;
				if(($k == 102)||($k == 104))
				{
				my $p = $e->index('insert');
				$pkg->{Search} = $$x;
				$e->delete($p-1,$p);
				$e->icursor($p-1);
				}
				$$x = $pkg->{Search};
				if(($k == 90)||($k == 101)){$pkg->Search(\$e);}
				;}, \$delete, Ev('k')]);







$e->focus;

#Call in toplevel program
#$pkg->Addtobrowse();

}

sub Selectall
{
my $pkg = shift;
my $i;
my $c = 0;
for($i = 0; $i < @{$pkg->{Data}}; $i++)
{

if($pkg->{Selection}[$i][0]){undef $pkg->{Selection}[$i];}
else{$pkg->{Selection}[$i] = $pkg->{Data}[$i];}
}
foreach (@{$pkg->{Selection}})
	{if($_->[0]){$c++;}}
	$pkg->{Selectionstatus} = "                  Number Of Entries Selected = $c";

$pkg->Fillpage();

}

sub Search
{
my ($pkg,$e) = @_;
my $i;
my $chk;
my $c = 0;
my $h;
my $prefix = "DR";

if($pkg->{Button1Label}) 
{
	if($pkg->{Button1Label} eq 'Prepare Clones'){$prefix = "DS";}
}
my $hash = Verifynumber($pkg->{Search},$prefix);
$h = $hash->{Whole};
if($hash->{Process} eq 'Good')
{
	for($i = 0; $i < @{$pkg->{Data}}; $i++)
	{

	if(!$pkg->{Duplicatecheck}){$pkg->{Duplicatecheck} = 0;}
	
	if($pkg->{Data}[$i][$pkg->{Duplicatecheck}] == $hash->{Number})
	{
		undef $h;
		$pkg->{Selection}[$i] = $pkg->{Data}[$i];
		
	
		my $p = int($i/12)+1;
		$pkg->Fillpage($p);
		$pkg->Edititem(0);
		#$i = 0;
		last;
	}

	}

}
elsif(($hash->{Process} eq 'Bad')&&($pkg->{Search} =~ /\D/g))
{
$h = $pkg->{Search};
	my %pattern = ('*' => '.*','?' => '.', '[' => '[', ']' => ']');

	$pkg->{Search} =~ s{(.)} {$pattern{$1}|| "\Q$1"}ge;

	$pkg->{Search} = uc($pkg->{Search});
	$pkg->{Browse}->focus;
	if($pkg->{Search})
	{
	for($i = 0; $i < @{$pkg->{Data}}; $i++)
	{

	if(uc($pkg->{Data}[$i][0]) =~ /$pkg->{Search}/)
	{
		$h = 1;
		$pkg->{Selection}[$i] = $pkg->{Data}[$i];
		undef $h;
	
		my $p = int($i/12)+1;
		$pkg->Fillpage($p);
		#$i = 0;
		#last;
	}

	}#for loop
	}	
}	
	if($h)
	{
	$pkg->{Search} ='';
	$pkg->Message("Sorry, Cannot Find Entry\n\n$h\n\nPlease Check Spelling and Case",\$chk);
	$pkg->{Mainwin}->waitVariable(\$chk);
	}

	


foreach (@{$pkg->{Selection}})
	{if($_->[0]){$c++;}}
	$pkg->{Selectionstatus} = "                   Number Of Entries Selected = $c";


$pkg->{Search} = '';
$$e->focus;


}
sub Getchoice
{
my $pkg = shift;
my @data;
my $i = 0;
my $f;
my $b;
my $c = 0;

$pkg->{Choice} =["","",""];

foreach (@{$pkg->{Selection}})
{

	if($_->[0])
	{
		$pkg->{Choice} = $_;
		undef $_;

		if(($i < ($pkg->{Currentpage} * 12))&&($i >= ($pkg->{Currentpage} * 12)-12))
		{
			$j = $i - (12 * ($pkg->{Currentpage}-1));
			$pkg->{List}[$j]->configure(-bg => 'lightblue');
		}

		last;
	}
$i++;
}

foreach (@{$pkg->{Selection}})
	{if($_->[0]){$c++;}}
	$pkg->{Selectionstatus} = "                  Number Of Entries Selected = $c";

}


sub Customsort
{

my $pkg = shift;
my $c = 0;

if($pkg->{Sort_by} =~ /Default/)
{
$pkg->Addtoresults();


}else
{
	foreach (@{$pkg->{Column}})
	{
	if($_->[2] eq $pkg->{Sort_by})
	{
	my @sorted;
	
	if($_->[2] =~ /Date/)
	{
		
		@sorted = sort {
		
	
		my %mon =(	'JAN' 	=> 35,
				'FEB'	=> 70,
				'MAR'	=> 105,
				'APR'	=> 140,
				'MAY'	=> 175,
				'JUN'	=> 210,
				'JUL'	=> 245,
				'AUG'	=> 280,
				'SEP'	=> 315,
				'OCT'	=> 350,
				'NOV'	=> 385,
				'DEC'	=> 420);
	
		my ($ad,$am,$ay) = split /[-]/,$a->[$_->[0]];
		my ($bd,$bm,$by) = split /[-]/,$b->[$_->[0]];
		
	
		$q = ($ay * 455) + $mon{$am} + $ad;
		$z = ($by * 455) + $mon{$bm} + $bd;
		if($z > $q){return -1;}
		if($z < $q){return  1;}
		if($z == $q){return 0;}
		} @{$pkg->{Data}};# date sort end
	}else
	{
		@sorted = sort {
		my $A;
		my $B;
		my $num_a;
		my $num_b;
		$A = $a->[$_->[0]];
		$B = $b->[$_->[0]];
		my $i;
		
		$A =~ /(\d+).(\d+)/g;
		my $a1 = $1;
		my $a2 = $2;
		
		
		
		$B =~ /(\d+).(\d+)/g;
		my $b1 = $1;
		my $b2 = $2;
		
		
		if(($a1)&&($b1))
		{
			$num = 0;
			if($a1 > $b1){$num = 1;}
			elsif($a1 < $b1){$num = -1;}
			else
			{
				if(($a2)&&($b2))
				{
					if($a2 > $b2){$num = 1;}
					elsif($a2 < $b2){$num = -1;}
				}
				elsif($a2){$num = 1;}
				elsif($b2){$num = -1;}
				else{$num = 0;}
			}
		}
		elsif($a1){$num = 1;}
		elsif($b1){$num = -1;}
		else{$num = 0;}
	
		#if($A =~ m/(\d+)/g){$num_a = $1;}
		#$num_a = $A;
		#$num_a =~ s/\D//g;
		
		#if($B =~ m/(\d+)/g){$num_b = $1;}
		#$num_b = $B;
		#$num_b =~ s/\D//g;
		
		#if(($num_a)&&($num_b))
		#{
		#$num = 0;
		#if($num_a > $num_b){$num = 1;}
		#if($num_a < $num_b){$num = -1;}
		#}
		#elsif($num_a){$num = 1;}
		#elsif($num_b){$num = -1;}
		#else{$num = 0;}
		
		$l = length($a->[$_->[0]]);
		
		if(length($b->[$_->[0]]) < $l){$l = length($b->[$_->[0]]);}
		
		for $i(0..$l)
		{
		$A = uc(substr($a->[$_->[0]],$i,1));
		$B = uc(substr($b->[$_->[0]],$i,1));
		
		if($A ne $B)
		{
		
			if(($A =~ m/^\d/)&&($B !~ m/^\d/))
			{	
				$B = ord($B);
				$A = $B + 1;
				
			}elsif(($A !~ m/^\d/)&&($B =~ m/^\d/))
			{	
				$A = ord($A);
				$B = $A + 1;
				
			}elsif(($A =~ m/^\d/)&&($B =~ m/^\d/))
			{	
				
				$A = ord($A);
				$B = ord($B);
				
				if($num == -1){$B = $A + 1;}
				if($num == 1){$A = $B + 1;}
			}else
			{
				
				$A = ord($A);
				$B = ord($B);
			
			}
		
		last;
		}
		}
		
	
		return ($A <=> $B);
		
		} @{$pkg->{Data}};
	}
	$pkg->{Data} = \@sorted;
	last;
	
	
	
	
	}
	}
	undef $pkg->{Selection};
	foreach (@{$pkg->{Selection}})
	{if($_->[0]){$c++;}}
	$pkg->{Selectionstatus} = "                  Number Of Entries Selected = $c";
	$pkg->Fillpage();
}
}


1;
