#!/usr/bin/perl

use CGI;
use DBI;

my $data='dbi:Sybase:OMG_PROD';
my $db='omg_main';
my $uname = 'omgvisitor';
my $pword = 'mgNXW70uwbn9';
my $query = new CGI;
my $bprint = 1;
print "Content-type:text\/html\n\n";


$header_file = "/home/httpd/borders/iic-search-members.htm";

# Get header from file.
open(HEADER,"<$header_file");
while ($line=<HEADER>) {
if ($bprint eq 1){

if (index($line,'START BODY HERE') > -1){ $bprint = 0; }


    print $line;
}
}

close(HEADER);

#DHS hidden by request from Sharyn 9-6-2017


my $MEMB_LEVEL = $query->param("MEMB_LEVEL");
my $PRIMARY_BUSINESS = $query->param("PRIMARY_BUSINESS");
my $namesearch = $query->param("namesearch");


##$PRIMARY_BUSINESS =~ s/[^a-zA-Z0-9 _-]//g;
$PRIMARY_BUSINESS =~ s/[^\/a-zA-Z0-9 _-]//g;


if ($PRIMARY_BUSINESS eq "Computer SoftwareInformation Technology"){
$PRIMARY_BUSINESS = 'Computer Software/Information Technology';
}

if ($PRIMARY_BUSINESS eq "NetworkingTelecommunications"){
$PRIMARY_BUSINESS = 'Networking/Telecommunications';
}

#print "HERE $PRIMARY_BUSINESS ";



print<<EOF;

<br>
<TABLE BORDER="0" bgcolor=#ffffff  CELLPADDING="5" CELLSPACING="5" WIDTH="400">
<TR>
<TD VALIGN="top" ALIGN="left">
</TD>

<form method="post" action="iicmembersearch.pl" enctype="multipart/form-data">
<TABLE BORDER="0" bgcolor=#ffffff width=400>
<TR><TD><b>Select membership type</b></TD></TR>
<TR><TD>
<select name="MEMB_LEVEL" >
<option value="All">All Members</option>
<option value="Contributing">Contributing</option>
<option value="Founder">Founder</option>
<option value="Large Industry">Large Industry</option>
<option value="Startup">Startup</option>
<option value="Small Industry">Small Industry</option>
<option value="Nonprofit">Academia/nonprofit</option>
<option value="Government">Government</option>
</select>
</TD></TR><TR><td valign="bottom">
<tr><td colspan="4"></td></tr></TABLE>

<TABLE BORDER="0" bgcolor=#ffffff width=400>
<TR><TD><b>Select Industry type</b></TD></TR>
<TR><TD>
<select name="PRIMARY_BUSINESS" >
<option value="" SELECTED>All Industries</option>
<option value="Computer Software/Information Technology">Computer Software/Information Technology</option>
<option value="Consumer Electronics">Consumer Electronics</option>
<option value="Data Automation">Data Automation</option>
<option value="Energy & Utilities">Energy & Utilities</option>
<option value="Finance">Finance</option>
<option value="Government">Government</option>
<option value="Healthcare">Healthcare</option>
<option value="Industrial Automation">Industrial Automation</option>
<option value="Industry Organizations">Industry Organizations</option>
<option value="Manufacturing">Manufacturing</option>
<option value="Mining & Metals">Mining & Metals</option>
<option value="Networking/Telecommunications">Networking/Telecommunications</option>
<option value="Research & Development">Research & Development</option>
<option value="Services">Services</option>
<option value="Transportation">Transportation</option>
<option value="Academic Institutions">Academic Institutions</option>
<option value="Other">Other</option>
</select>
</TD></TR><TR><td valign="bottom">
<tr><td colspan="4"></td></tr></TABLE>





<p>
<table border="0" bgcolor=#fffff6 width=400><TR><TD align="bottom"><b>Search for a company by name</b></TD></TR>
<TR><TD valign="bottom"><input type="text" name="namesearch"  size="50" /><br /></td></tr>
<tr><td></td></tr></table>


<input type="submit" name="submit" value="Search" />

</form>
EOF


#if (length($MEMB_LEVEL) < 1){ 
if ( 0 < 1 ){ 

#}else{

print '<BR><BR>';

my $i;
my $j;

my $dbh=DBI->connect($data, $uname, $pword) or die "Can't connect to the database: DBI:errstr";

my $rv = $dbh->do("use $db");

my $extraquery = '';





if ($MEMB_LEVEL eq 'Founder'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Founder" ';
}

if ($MEMB_LEVEL eq 'Contributing'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Contributing" ';
}

if ($MEMB_LEVEL eq 'Large Industry'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Large Industry" ';
}

if ($MEMB_LEVEL eq 'Small Industry'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Small Industry" ';
}

if ($MEMB_LEVEL eq 'Government'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Government" ';
}

if ($MEMB_LEVEL eq 'Startup'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Startup" ';
}

if ($MEMB_LEVEL eq 'Nonprofit'){
$extraquery = 'and MEMBERSHIP.MEMB_LEVEL = "Nonprofit" ';
}

if (length($PRIMARY_BUSINESS) > 2){ 
$extraquery .= 'and COMPANY.PRIMARY_BUSINESS = "' . $PRIMARY_BUSINESS . '" ';
}





my $dbquery = 'SELECT DISTINCT COMPANY.COMP_NAME AS COMPANY, COMPANY.URL, PRIMARY_BUSINESS FROM
COMPANY, MEMBERSHIP, BILLING
WHERE MEMBERSHIP.COMP_NAME = COMPANY.COMP_NAME
and BILLING.COMP_NAME = COMPANY.COMP_NAME
and BILLING.ORGANIZATION = "IIC"
and COMPANY.COMP_NAME != "DHS"
and MEMBERSHIP.ORGANIZATION = "IIC"
and BILLING.ON_HOLD = "No"
and MEMBERSHIP.ACTIVE_MEMBER in ("Yes")
and COMPANY.DELETED = "No"
and COMPANY.COMP_NAME <> "Industrial Internet Consortium" ' . $extraquery . '
order by lower(COMPANY.COMP_NAME)';

#,"PND")



#print $dbquery;

my $unt = $dbh->prepare($dbquery) or &dienicer("Can't select from table: ",$dbh->errmsg);
$unt->execute() || die $unt->errstr;



while (@row = $unt->fetchrow_array) {

$i = 0;

if (index(lc($row[0]),lc($namesearch)) eq -1){
$i = 1;
}


if ($i eq 0){

if (index($row[1],'http') > -1){
print '<A HREF="' . $row[1] . '">';
}

print $row[0] . '<BR>';

if (index($row[1],'http') > -1){
print '</A>';
}



}

}



print '<BR><FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';


}

print '<BR><BR>';


$footer_file = "/home/httpd/borders/iic-search-members.htm";
open(FOOTER,"<$footer_file");

while ($line=<FOOTER>) {
if (index($line,'END BODY HERE') > -1){ $bprint = 1; }

if ($bprint eq 1){



    print $line;


}
}
close(FOOTER);



