#!@@_perl_root_@@/bin/perl.exe 
package ColleagueGeneRegistryUserPage;

##########################################################
#                                                        #
# dictyBase Extension of ColleagueGeneRegistryUserPage   #
#                                                        #
##########################################################

use ColleagueGeneRegistryUserPage_base;

BEGIN { %ColleagueGeneRegistryUserPage:: = %ColleagueGeneRegistryUserPage_base:: }



#
#   took out link to dictlabs 
#
########################################################################
sub associate4pi {
########################################################################
    my ($self, $collObject) = @_;
    my @associate;
    my $return;
    if ($collObject) {
	my $associate4piRef = $collObject->associate4piRef; 
	foreach my $rowRef (@$associate4piRef) { 
	    my($colleagueNo, $lname, $fname, $suffix) = @$rowRef;
	    push(@associate, $lname .", ".$fname);
	}
    }

    my $row = Tr(td({-valign=>'top',
	             -rowspan=>'2',
		     -width=>'140',
		     -bgcolor=>'#ACBDCC'},
		    br.br.br.br." Enter the names of your students, post-docs, technicians ").
		 td(font({-size=>'+2'}, u("Option 1").": Complete if you ".u("are")." a Supervisor, Advisor, Lab Head, ".br."Group Leader, or P.I.")));
#.br.
#	font({-color=>'red'}, i("NOTE: You ".b("must")." enter at least one name here to appear on the ".
#				a({-onclick=>"open_win()",
#				   -href=>$configUrl->dictyBaseCGIRoot."$dblink/colleague/DictyosteliumLabs/DictyosteliumLabs.pl",
#				   -target=>'infowin'}, 
#				  "dictyBase Dictyostelium Labs page").".".p)))
#);

    my $box = Tr(th({-align=>'left'}, "Last name, First name").
		 th({-align=>'left'}, "Last name, First name"));
    for (my $i = 1; $i <= 10; $i += 2) {
	my $j = $i+1;
	$box .= Tr(td(textfield(-name=>"associate$i",
				   -value=>"$associate[$i-1]",
				   -size=>'30')).
		      td(textfield(-name=>"associate$j",
				   -value=>"$associate[$j-1]",
				   -size=>'30')));
    }
    $row .= Tr(td(table($box)));
    my $return = p.table($row);

    # if this is not a new colleague, we want to check to see if this 
    #   entry has '~Generic ~Associate' as one of its lab members.  If yes,
    #   then this record is a PIs without any students, post-docs or 
    #   technicians, and we need to preserve this information.
    if ($self->colleagueNo && $collObject->isPIWithoutAssocs) {
	$return .= input({-name=>"pi",
			  -value=>"true",
			  -type=>"hidden"});
    }

    return $return;

}

#
#
#
########################################################################
sub dictyBasecontact {
########################################################################
    ## added by KD
    my ($self, $collObject) = @_;
    my $return = "Would you like to subscribe to dictyNews? (you will receive dictyNews through your email): ";
    my $isContact;
    if ($collObject) { $isContact = $collObject->is_subscribed; }
    else { $isContact = $self->{'_dictyBasecontact'}; }
    if (!$isContact) {$isContact = 'Y';}
    $return .= radio_group(-name=>"dictyBasecontact",
			 -values=>['Y','N'],
			 -default=>$isContact).p."\n";	

    return $return;
}

#
# took out reference section
#
########################################################################
sub displayVerificationPage {
########################################################################
    my ($self) = @_;
    if (param('registerType')) {
	$self->displayGeneInfo;
#	$self->displayGeneReferenceInfo;
	$self->displayGeneCommentInfo;
    }
    if (!param('reserveAnotherGene')) {
	$self->displayContactInfo;
	$self->displayAssociateInfo;
	$self->displayInterestKeywordInfo;
	$self->displayCommentInfo;
    }
    $self->submitReset;
    print p, b(" Please note: this information will not be available until an dictyBase curator processes your submission."), br;
}

#
# took out orfname
#
#########################################################################
sub printGeneInfoSection {
#########################################################################
    my ($self) = @_;

    my $locusObj;
    if (param('registerType') =~ /update/i && param('gene')) {
	$locusObj = Locus->new(dbh=>$dbh,
			       locus_name=>param('gene'));
	$self->{'_locus_no'} = $locusObj->locus_no;
    }
    print table({-border=>'0',
		 -cellpadding=>'4',
		 -cellspacing=>'3',
		 -width=>'650'},
		$self->geneName($locusObj).
		$self->geneDesc($locusObj).
#		$self->orfName($locusObj).
		$self->otherGeneName($locusObj).
		$self->geneProduct($locusObj).
		$self->nullPhenotype($locusObj).
		$self->otherPhenotype($locusObj)),p;
}
##
##  took out colleague edit form for gene registry
##
#######################################################################
#sub printEntryPage {
#######################################################################
#    	my ($self) = @_;
#	if (!param('registerType')) {
#	    if (!$self->colleagueNo) {
#		$self->{'_title'} = "$self->{'_database'} New Colleague Entry Form";
#	    }
#	    else {
#		$self->{'_title'} = "$self->{'_database'} Colleague Update Form";
#	    }
#	}
#	else {
#	    $self->{'_title'} = "$self->{'_database'} Gene Registry Form";
#	}	
#	&printStartPage($self->database, $self->title, $self->help);
#	$self->printTopText;
#	print start_form(-action=>"$url",
#    		         -method=>'post');
#
#	my $collObject;
#	if ($self->colleagueNo) {
#	    if ($dbh) {
#                $collObject = 
#			 Colleague->new(dbh=>$dbh,
#					colleague_no=>$self->colleagueNo);
#	    }
#	    else {
#		print "You should pass database name or database handle to this object";
#		return;
#	    }
#	    my $fullname;
#	    if (!$collObject->suffix) {
#		$fullname = $collObject->first_name ." ". 
#		    $collObject->last_name;
#	    }
#	    elsif ($collObject->suffix =~ /\./) {
#		$fullname = $collObject->first_name." ".
#		    $collObject->last_name.
#		    ", ".$collObject->suffix;
#	    }
#	    else {
#		$fullname = $collObject->first_name." ".
#		    $collObject->last_name.
#		    " ".$collObject->suffix;
#	    }
#	    if (param('registerType')) {
#		$self->geneRegistryForm;
#		print hr;
#	    }
#	    print hidden(-name=>'id',
#                         -value=>$self->colleagueNo);
#
#
#	}
#	elsif (param('registerType') =~ /Reservation/) {
#	    $self->geneRegistryForm;
#	    print hr;
#	}
#	print hidden(-name=>'continue',
#		     -value=>'1');
#	if (param('reserveAnotherGene')) {
#	    print hidden(-name=>'lname',
#			 -value=>param('lname')),
#	          hidden(-name=>'fname',
#			 -value=>param('fname')),
#	          hidden(-name=>'email',
#			 -value=>param('email')),
#	          hidden(-name=>'reserveAnotherGene',
#			 -value=>'1');
#        }
#	elsif( param('registerType') =~ /Registration/) {
#
#	    print h3("Updating Colleague Information for ".$fullname);
#	    print b(a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/colleague/colleagueSearch?id=".$self->colleagueNo."&win=1",
#		    -target=>"infowin",
#		    -onClick=>"open_win()"},
#		    "View"))." current information", p;
#
#	    print $self->subtitle("Your Address and Contact Information"),p;
#	    $self->printContactSection($collObject);
#	    print $self->subtitle("Your Group/Lab Members and Associates");
#	    print $self->associate4pi($collObject);
#	    print font({-color=>'red',
#			-size=>'+4'}, b("OR"));
#	    print $self->pi($collObject);
#	    print $self->subtitle("Your Collaborators and Associates");
#	    print $self->associate($collObject),p;
#	    print $self->subtitle("Your Research Interests");
#	    print $self->interest($collObject);
#	    print $self->researchTopics($collObject);
#	    print $self->keyword($collObject), p;
#	    if (param('registerType')) {
#		print $self->subtitle("Colleague Entry Comments");
#	    }
#	    else {
#		print $self->subtitle("Your Comments");
#	    }
#	    print $self->comment($collObject);
#	    print $self->curatorOnly($collObject);
#
#	}
#
#	$self->submitReset("Continue"), p;
#	print p, b(" Please note: this information will not be available until an dictyBase curator processes your submission."), br;
#	print end_form;
#	&printEndPage;
#}
#
#
#





#
#  took out reference section
#
#######################################################################
sub geneRegistryForm {
#######################################################################
    my ($self) = @_;
    $self->printGeneOrfLink;
    print hidden(-name=>'registerType',
		 -value=>param('registerType'));
    print $self->subtitle("Information about the Gene");
    $self->printGeneInfoSection;
   
#    print $self->subtitle("Reference");
#    $self->printGeneReferenceInfoSection;

    print $self->subtitle("Gene Name Comments"),p;
    print $self->registryComments, p;
}
#
#  added single email requirement
#
#####################################################################
sub start {
######################################################################
    my ($self) = @_;

    $configUrl = ConfigURLdictyBase->new;
    $configPath = ConfigPathdictyBase->new;
    $dblink = $configUrl->dblink($self->database);
    $url = $ENV{"SCRIPT_NAME"};
    $url =~ s/\?.+$//;
    if ($self->colleagueNo !~ /^[0-9]+$/) {
	$self->{'_colleagueNo'} = "";
    }
    if (!param('registerType')) {
	$self->{'_help'} = "colleagueform.html";
    }
    $self->{'_pid'} = param('pid');
    if (!$self->{'_pid'}) { $self->{'_pid'} = $$; }

    if (param('pid') && param('back')) {
	# $self->{'_colleagueNo'} = param('id');
	$self->printVerificationPage;
	exit;
    }

    if (param('pid') && param('editType')) {
	if (param('SaveDisplay')) {    
	    $self->printVerificationPage;
	}
	else {
	    $self->editForm;
	}
	exit;
    }
    if (param('finalUpdate') || param('finalNewEntry')){

	for (my $i = 1; $i <= 10; $i++) {
	    if (param("ORIassociate$i") && !param("associate$i")) {
		$self->err_report("On the colleague new entry form, you typed a new associate name ".param("ORIassociate$i").". If you wish to add that person as an associate, you must select that person on the 'Colleague New Entry Verification' form. Please return to that page and select a name. Or, if you have decided that you do not wish to add that associate, please go back two pages, to the colleague new entry form, and remove that name. Thanks!");
		exit;
	    }
	}
	$self->readInfo;
	if (param('finalNewEntry')) {
	    $self->createNewEntryFile;
	}
	else {
	    $self->{'_colleagueNo'} = param('id');
	    $self->createUpdateFile;
	}
    }
    elsif ((!param('lname') && !param('fname')) || 
	   (param('reserveAnotherGene') && !param('continue'))) {
	$self->printEntryPage;
    }
    elsif (param('lname') && param('fname') && param('email') && param('institution')) {
	my $emails = param('email');
	$emails =~ s/^ *//;
	$emails =~ s/ *$//;
        $emails =~ s/[,;\t]+/ /g;
	my @emailList = split(' ', $emails);
    
    #
    # dicty added check
    #
     if (@emailList > 1) {
       $self->err_report("Please enter a single email in the format 'myaccount\@host.edu'");
	}
     foreach my $email (@emailList) {
	    if ($email) {
		my ($username, $hostname) = split('@', $email);
		my $hostvalid;
		if ( $username ne "" && $hostname ne "") {
		    $hostvalid = $self->hostcheck($hostname);
		}
		if ( !$hostvalid ) {
		    $self->err_report("Your email address '$email' is incomplete or does not contain a valid hostname. <p>Please return to the form and check that your email address is correct.");
		    exit;
	        }
	    }
	}
	$self->printVerificationPage;
    }
    elsif (param('reserveAnotherGene')) {
	$self->printVerificationPage;
    }
    else {
	$self->err_report("You must enter your last and first name(s), e-mail address and institution.<p>Please click on the BACK button to return to the form.");
	exit;
    }
}
#
#  changed example to Northwestern
#
#######################################################################
sub institution {
#######################################################################
    my ($self, $collObject) = @_;
    my $institution;
    if ($collObject) { $institution = $collObject->institution; }
    else { $institution = $self->{'_institution'}; }
    return Tr(td({-width=>'140',
		  -bgcolor=>'#ACBDCC'},
		 " Organization ".
		 font({-color=>'red',
		       -size=>'2'},
		      b("[required]"))).
	      td(textfield(-name=>'institution', 
			   -value=>"$institution", 
			   -size=>'30')).
	      td("(e.g., Northwestern University)"));
}


#
#  took out directions for multiple emails
#
########################################################################
sub email {
########################################################################
    my ($self, $collObject) = @_;
    my $emails;
    if ($collObject) { 
	my $emailRef = $collObject->emailRef;
	foreach my $rowRef (@$emailRef) {
	    my ($email) = @$rowRef;
	    $emails .= ", " . $email;
	}
	$emails =~ s/^\, //;
    }
    else {
	$emails = $self->{'_email'};
    }
    return Tr(td({-width=>'140',
		  -bgcolor=>'#ACBDCC'},
		 "E-mail ".
		 font({-color=>'red',
		       -size=>'2'},
		      b("[required]"))).
	      td(textfield(-name=>'email', 
			   -value=>"$emails", 
			   -size=>'30')).
	      td("&nbsp;"));
}


########################################################################
sub subtitle {
########################################################################
    my($self, $subtitle) = @_;
    return table({-border=>'0',
		  -cellpadding=>'4',
		  -cellspacing=>'3',
		  -width=>'650'},
		 Tr(td({-width=>'100%',
			-bgcolor=>'#d8d8d8'},
		       font({-size=>'+2'},
			    b($subtitle)))));
    
}

########################################################################
sub webPage {
########################################################################
    my ($self, $collObject) = @_;
    my $urls;
    my $urlTypes;

    my $webPageString = "";

    if ($collObject) {
	my $urlRef = $collObject->urlRef;
	foreach my $rowRef (@$urlRef) {
	    my ($url, $wwwTitle, $urlType) = @$rowRef;
	    $urls .= ", ".$url;
	    $urlTypes .= ", ".$urlType;
	}
	$urls =~ s/^\, //;
	$urlTypes =~ s/^\, //;
    }
    else { 
      $urls = $self->{'_urls'}; 
      $urlTypes = $self->{'_url_type'};
    }

    my @urlTypeList = ("Please specify", "Lab", "Research summary", "Other");

    my $urlTypeList = "";

    foreach my $urlType (@urlTypeList) {
      $urlTypeList .= " ".$urlType;
    }

    my $help = div("Example of a 'Lab': ",
		a({-href=>'http://dicty.cmb.northwestern.edu/chisholm/'},
		  "http://dicty.cmb.northwestern.edu/chisholm/"), br,
		"Example of a 'Research Summary': ",
		a({-href=>'http://dicty.cmb.northwestern.edu/chisholm/overview.htm'},
		  "http://dicty.cmb.northwestern.edu/chisholm/overview.htm"), br,
		"Example of 'Other': ",
		a({-href=>'http://dictyBase.org/'},
		  "http://dictyBase.org/")
	       );

    my @urls = split(/, /, $urls);
    my @urlTypes = split(/, /, $urlTypes);

    $webPageString .= table({-border=>'0',
		     -cellpadding=>'4'},
		    Tr(td({
			   -width=>'140',
			   -bgcolor=>'#ACBDCC'},
			  " Web Pages ").
		       td("Please specify URLs and types of web pages"
			  , br, $help
			 )));

    my $webPageCells = "";

    for (my $counter = 0; $counter < $numUrls; $counter++) {
      my $url;
      my $urlType;

      if (defined $urls[$counter]) {
	$url = $urls[$counter];
	$urlType = $urlTypes[$counter];

	if ($urlTypeList !~ /$urlType/) {
	  $urlType = "Other";
	}
      }
      else {
	$url = $self->{"_url$counter"};
	$urlType = $self->{"_url_type$counter"};
      }

      my $currentRow = (td({-width=>'140'},
			   "&nbsp;").
			td({-width=>'30'},
			   textfield(-name=>"url$counter",
				     -value=>"$url",
				     -size=>'30')).
			td({-align=>'LEFT'},
			   popup_menu(-name=>"url_type$counter",
				      -value=>\@urlTypeList,
				      -default=>"$urlType"))
		       );

      $webPageCells .= Tr($currentRow);
    }
    $webPageString .= table(table({-border=>0}, $webPageCells));

    return $webPageString;

}


#
# we're not doing this
#
#
########################################################################
sub researchTopics {
########################################################################
    my ($self, $collObject) = @_;
    return undef;
}

#
# took out research topics section
#
#
########################################################################
sub displayInterestKeywordInfo {
########################################################################
    my ($self) = @_;
    my $pid = $self->pid;

    if ($self->{'_interests'} || $self->{'_keyword1'} || $self->{'_researchTopics'}) {
	print $self->subtitle("Your Research Interests");
	print $self->{'_interests'}, p;
	print a({-href=>"$url?pid=".$self->pid."&editType=interest&id=".param('id')."&registerType=".param('registerType')},
		img({-src=>$configUrl->breadServerRoot."images/edit_white.gif"})), p;
    }

    if (($self->{'_keyword1'}) || ($self->{'_researchTopics'})) {

	print table({-border=>'0',
		     -cellpadding=>'4',
		     -cellspacing=>'3',
		     -width=>'650'},
		    Tr(td({-width=>'100%',
			   -bgcolor=>'#d8d8d8'},
			  font({-size=>'+1'},
			       b("Keywords")))));
	for(my $i = 1; $i <= 10; $i++) {
	    if (!$self->{"_keyword$i"}) { last;}
	    print $self->{"_keyword$i"},br;
	}
	print p, a({-href=>"$url?pid=".$self->pid."&editType=keyword&id=".param('id')."&registerType=".param('registerType') . "#Keywords"},
		img({-src=>$configUrl->breadServerRoot."images/edit_white.gif"})), p;
    }

}

#
# Took out research topics
#
########################################################################
sub editForm {
########################################################################
    my ($self) = @_;
    if (!param('registerType')) {
	$self->{'_title'} = "Edit Colleague Information";
    }
    else {
	$self->{'_title'} 
		= "Edit Gene Registry and Colleague Information";
    }
    &printStartPage($self->database, $self->title, $self->help); 
    print start_form(-action=>"$url",
    		     -method=>'post');
    print hidden(-name=>'SaveDisplay',
    		 -value=>'1'),
          hidden(-name=>'pid',
		 -value=>$self->pid),
          hidden(-name=>'editType',
		 -value=>param('editType')),
	  hidden(-name=>'registerType',
		 -value=>param('registerType')),
          hidden(-name=>'reserveAnotherGene',
		 -value=>param('reserveAnotherGene'));
    if ($self->colleagueNo) {
	print hidden(-name=>'id',
		     -value=>$self->colleagueNo);
    }
    if (param('editType') =~ /GeneInfo/i) {
	$self->readGeneInfo;
	print $self->subtitle("Information about the Gene");
	$self->printGeneInfoSection;
    }
    elsif (param('editType') =~ /ReferenceInfo/i) {
	$self->readGeneReferenceInfo;
	print $self->subtitle("Reference");
	$self->printGeneReferenceInfoSection;
    }
    elsif (param('editType') =~ /GeneCommentInfo/i) {
	$self->readGeneCommentInfo;
	print $self->subtitle("Gene Name Comments"),p;
	print $self->registryComments, p;
    }
    elsif (param('editType') =~ /contact/i) {
	$self->readContactInfo;
	print $self->subtitle("Your Address and Contact Information");
	$self->printContactSection;
    }
    elsif (param('editType') =~ /interest/i) {
	$self->readInterestInfo;
	print $self->subtitle("Your Research Interests");
	print $self->interest, p;
    }
    elsif (param('editType') =~ /keyword/i) {
	$self->readKeywordInfo;

	print a({-name=>"Keywords"});
	print $self->subtitle("Keywords");
	print $self->keyword, p;
    }
    else { ### comment section
	$self->readCommentInfo;
	print $self->subtitle("Your Comments");
	print $self->comment, p;
    }
    $self->submitReset("Continue");
    print end_form;
    &printEndPage;
    exit;
}
########################################################################
1;
########################################################################

