#!/usr/bin/perl -w
#
# index.cgi
#

use strict;
use DBI;
use CGI qw(:cgi-lib :standard);
use CGI::Carp qw/fatalsToBrowser/;

require( "web.pl" );
require( "subs.pl" );
require( "jscript.pl" );

my $cgi = new CGI;
my %formdata = $cgi->Vars();

my $dbh;
my $sql;
my $record;
my $sth;

my $cw = 20;
my $bgc = "#A5A5C5";
my $fc = "#000000";

my $shaded = "#D0A308";

my @month = ();
my @year = ();
my @monthname = ();

my @days = ();

my $month;
my $year;

######################### First Month ##############################
$month[0] = `/bin/date '+%m'`;
$year[0] = `/bin/date '+%Y'`;
chop ($month[0]);
chop ($year[0]);

if ($month[0] < 9 && $year[0] == 2007) {
    $month[0] = "09";
    $year[0] = "2007";
}

#$month[0] = "09";                     # Testing Variable
#$year[0] = "2007";                    # Testing Variable

######################### Second Month #############################
$month[1] = $month[0] + 1;
$year[1] = $year[0];
if ($month[1] > 12) { $month[1] = $month[1] - 12; $year[1]++; }
######################### Third Month ##############################
$month[2] = $month[0] + 2;
$year[2] = $year[0];
if ($month[2] > 12) { $month[2] = $month[2] - 12; $year[2]++; }
######################### Fourth Month #############################
$month[3] = $month[0] + 3;
$year[3] = $year[0];
if ($month[3] > 12) { $month[3] = $month[3] - 12; $year[3]++; }
####################################################################

$month[1] = sprintf ("%02d",$month[1]);
$month[2] = sprintf ("%02d",$month[2]);
$month[3] = sprintf ("%02d",$month[3]);

$days[0] = getdays ($month[0],$year[0]);
$days[1] = getdays ($month[1],$year[1]);
$days[2] = getdays ($month[2],$year[2]);
$days[3] = getdays ($month[3],$year[3]);

$monthname[0] = getmonth ($month[0]);
$monthname[1] = getmonth ($month[1]);
$monthname[2] = getmonth ($month[2]);
$monthname[3] = getmonth ($month[3]);

my $firstday;
my $skip;
my $a;
my $b;
my $w;
my $calrows = 0;

my $avail;
my $checkdate;
my $alt_checkdate;

my $today_dt = `/bin/date '+%Y-%m-%d'`;
chop ($today_dt);

#$today_dt = "2007-09-20";             # Testing Variable

my $firstvisit = &getfirstvisit( $today_dt );

################################################################################################

print "Content-type: text/html \n\n";

&top_html_1();

&print_jscript( $today_dt );

print "<style>\n";
print "table {\n";
print "    spacing: 0px\;\n";
print "    padding: 0px\;\n";
print "}\n";
print ".table-style-cv TABLE { margin-bottom: 0px\; }\n";
print ".table-style-cv TBODY { border-top: 0px\; }\n";
print ".table-style-cv TBODY TR.odd { background: rgb(238,238,238)\; }\n";
print ".table-style-cv TBODY TD { padding: 3px 10px 3px 10px !important\; }\n";
print ".article TBODY { border-top: 0px\; }\n";
print ".frmButton {\n";
print "    background: url(http://w4.stern.nyu.edu/emba/classvisit/images/submit.png) top left no-repeat\;\n";
print "    width: 83px\;\n";
print "    height: 25px\;\n";
print "    text-indent: -9999px\;\n";
print "    border:0px\;\n";
print "    cursor:pointer\;\n";
print "    margin-top:10px\;\n";
print "}\n";
print ".frmButton:hover {\n";
print "    background-position: bottom left\;\n";
print "}\n";
print "</style>\n";

&top_html_2();

print <<HTMLCODE;
<h1 class="large">Class Visits</h1>

<p>Request a Class Visit to experience the classroom dynamics firsthand, and meet some of our current students and faculty.</p>

<p><b>Completion of both the <a href="https://app.applyyourself.com/?id=sternemba" target="_blank">Pre-Application</a> and the Admissions Interview is required before scheduling a Class Visit</b>.&nbsp; Request a Class Visit by selecting a date and completing the form below.</p>

<p>If you have any questions regarding your Class Visit request, please send your inquiry to <a href="mailto:executive\@stern.nyu.edu">executive\@stern.nyu.edu</a>, or call the NYU Stern Executive MBA office at (212) 998-0789.</p>

<p>
<center>

<table>
<tr>

HTMLCODE

#$dbh = DBI->connect("DBI:mysql:embaclassvisits:mysql.stern.nyu.edu","asguser", "asgpwd!");
$dbh = DBI->connect("DBI:mysql:embaclassvisits:mysql-p2.stern.nyu.edu","emba_usr", "3mb\@U3ul&");

for ($a=0; $a<4; $a++) {
    if ( $a == 2 ) {
        print "</tr>\n";
        print "<tr><td colspan=2>\&nbsp\;</td></tr>\n";
        print "<tr>\n";
    }
print <<HTMLCODE;
<td valign=top>

<table border=1 cellpadding=0 cellspacing=0>
<tr><td colspan=7 bgcolor=$bgc align=center><font color=$fc><b>$monthname[$a] $year[$a]</b></font></td>
<tr><td width=$cw align=center>SU</td>
<td width=$cw align=center>MO</td>
<td width=$cw align=center>TU</td>
<td width=$cw align=center>WE</td>
<td width=$cw align=center>TH</td>
<td width=$cw align=center>FR</td>
<td width=$cw align=center>SA</td>
<tr>
HTMLCODE

    $calrows = 0;
    $firstday = &day($month[$a],01,$year[$a]);

    if ($firstday eq "SUN") { $skip = 0; }
    elsif ($firstday eq "MON") { $skip = 1; }
    elsif ($firstday eq "TUE") { $skip = 2; }
    elsif ($firstday eq "WED") { $skip = 3; }
    elsif ($firstday eq "THU") { $skip = 4; }
    elsif ($firstday eq "FRI") { $skip = 5; }
    elsif ($firstday eq "SAT") { $skip = 6; }

    for ($b=0; $b<$skip; $b++) {
        print "<td align=center>\&nbsp\;</td>\n";
    }
    $w = $skip;

    $checkdate = sprintf ("%04d-%02d-%02d",$year[$a],$month[$a],1);
    $alt_checkdate = sprintf ("%02d-%02d-%04d",$month[$a],1,$year[$a]);

    $avail = "";

    $sql = qq(SELECT dates FROM emba_cv_dates WHERE dates = '$checkdate' and active = 'Y');
    $sth = $dbh->prepare($sql);
    $sth->execute;
    if ($record = $sth->fetchrow_hashref) {
       do { $avail = "Y";
       } while ($record = $sth->fetchrow_hashref);
    }
    $sth->finish;

    print "<td align=center";

    if ($avail eq "Y" && $checkdate ge $firstvisit) {
#      print " bgcolor=$shaded><a href=\"#\" onClick=\"document.form1.class_date.value=\'$alt_checkdate\'\; checkdate()\">";
       print " bgcolor=$shaded><a href=\"javascript:void(null)\" onMouseOver=\"window.status='Select $alt_checkdate'\; return true\" onMouseOut=\"window.status=''\" onClick=\"document.form1.class_date.value=\'$alt_checkdate\'\; checkdate()\; document.form1.class_time.focus()\;\">";
       print "1";
       print "</a>";
    } else {
       print ">1";
    }

    print "</td>\n";

    $w++;

    for ($b=2; $b<=$days[$a]; $b++) {
       $checkdate = sprintf ("%04d-%02d-%02d",$year[$a],$month[$a],$b);
       $alt_checkdate = sprintf ("%02d-%02d-%04d",$month[$a],$b,$year[$a]);
       $avail = "";

       $sql = qq(SELECT dates FROM emba_cv_dates WHERE dates = '$checkdate' and active = 'Y');
       $sth = $dbh->prepare($sql);
       $sth->execute;
       if ($record = $sth->fetchrow_hashref){
	  do { $avail = "Y";
	  } while ($record = $sth->fetchrow_hashref);
       }
       $sth->finish;

       if ($w == 7) { print "<tr>\n"; $w = 0; $calrows++; }
       print "<td align=center";

       if ($avail eq "Y" && $checkdate ge $firstvisit) {
           print " bgcolor=$shaded><a href=\"javascript:void(null)\" onMouseOver=\"window.status='Select $alt_checkdate'\; return true\" onMouseOut=\"window.status=''\" onClick=\"document.form1.class_date.value=\'$alt_checkdate\'\; checkdate()\; document.form1.class_time.focus()\;\">";
           print "$b";
           print "</a>";
       } else {
           print ">$b";
       }

       print "</td>\n";
       $w++;


    }
    for ($b=$w; $b<7; $b++) {
       print "<td align=center>\&nbsp\;</td>\n";
    }
    $calrows++;

    if ($calrows < 6) {
        print "<tr>\n";
        for ($b=0; $b<7; $b++) {
            print "<td align=center>\&nbsp\;</td>";
        }
        print "\n";
    }

    print "</td>\n";
    print "</table>\n";

}

$dbh->disconnect;

print <<HTMLCODE;
</td>
</tr>
</table>

<p align=center>
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=20 bgcolor=$shaded>&nbsp;</td>
<td align="left" bgcolor="#ffffff">= Shaded dates indicate the days available for class visits</td>
</tr>
</table>
</p>

<p align=center>
<form action="http://w4.stern.nyu.edu/emba/classvisit/submit.cgi" method="POST" name="form1" onSubmit="return validateForm();">
<table width=100% border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td width="40%" align="right" valign=top>Date<span class="style3">*</span></td>
    <td width="60%" align="left" valign=top><input type="text" name="class_date" size=30 readonly=Y></td>
    </tr>
  <tr>
    <td align="right" valign=top>Time<span class="style3">*</span></td>
    <td align="left" valign=top>
        <select name="class_time" style="width: 205px">
        <option value="">&lt;Select&gt;</option>
        <option value="08:30 AM - 12:30 PM">08:30 AM - 12:30 PM</option>
        <option value="01:15 PM - 05:00 PM">01:15 PM - 05:00 PM</option>
        </select>
    </td>
    </tr>
  <tr>
    <td align="right" valign=top>First Name<span class="style3">*</span></td>
    <td align="left" valign=top><input type="text" name="first_name" size=30></td>
    </tr>
  <tr>
    <td align="right" valign=top>Last Name<span class="style3">*</span></td>
    <td align="left" valign=top><input type="text" name="last_name" size=30></td>
    </tr>
  <tr>
    <td align="right" valign=top>Interview Date<span class="style3">*</span></td>
    <td align="left" nowrap valign=top><input type="text" name="int_date" size=30 maxlength=10> &nbsp; &nbsp; (mm-dd-yyyy)</td>
    </tr>
  <tr>
    <td align="right" valign=top>E-mail Address<span class="style3">*</span></td>
    <td align="left" valign=top><input type="text" name="email" size=30></td>
    </tr>
  <tr>
    <td align="right" valign=top>Phone Number</td>
    <td align="left" valign=top><input type="text" name="phone" size=30></td>
    </tr>
  <tr>
    <td align="right" valign=top>Areas of academic interest</td>
    <td align="left" valign=top><input type="text" name="interests" size=30></td>
    </tr>
  <tr><td colspan=2>&nbsp;</td>
  <tr>
    <td align="right"><span class="style1">*required</span></td>
    <td>&nbsp;</td>
  </tr>
  <tr><td colspan=2>&nbsp;</td>
  <tr>
    <td colspan="2"><center>

      <p><input name="Submit2" class="frmButton" type="submit" value="Submit">
              </p>
        </center></td>
  </tr>
</table>
</form>
</p>

HTMLCODE

&footer();

exit;
