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

# ---------------------------------------------------------------
#
# -----------------------------------------------------
# copyright 2000 by voxeo corporation. (see LGPL.txt)
#
# US Postal Codes database, Copyright (c) 2000 CD Light
#                           All Rights Reserved.
# -----------------------------------------------------
#
# v1.0 Coded in PERL
# v1.0 Coded by Stephen J. Lewis and RJ Burnham
#
# This application is prompts users over the phone for a 5 digit
# zipcode, then accesses a database containing the US Postal Code
# information for the year 2000 to find cities (and states) that
# match the zipcode.  It will read the results to the caller,
# then ask for another zipcode.  The same application has been
# written in ASP, JSP, PHP, Cold Fusion, and as a Java Servlet (q.v.) 
# as sample code on how to use CallXML and backend scripting languages. 
#
# This file is associated with the following files:				 
#     US Postal Codes.mdb   <---- The actual database file itself 
#
# Requires:
#     CGI.pm
#     DBI.pm and a database driver
#
# Several audio files are associated with this application:
#           MainMenu.wav
#           Results.wav
#           MultipleResults.wav
#           NotFound.wav
#           YouHaveTakenTooLong.wav
#
# All audio files are PCM format, 8-bit, mono, unsigned.
#
#----------------------------------------------------------------

#-----------------------------------------------------------------------------------------

sub Initialize() {
  print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 
  print "<callxml>";
  
  #------------------------------
  # Create a new cgi query module
  #------------------------------
  
  $query = new CGI;
  
  #------------------------------------------------------------------------
  # This annoying little variable is simply incremented and passed along to
  # avoid the AGS's caching.  This helps to ensure that a different URL
  # query string will be passed.
  #------------------------------------------------------------------------
  
  $CacheKiller = $query->param('cachekiller');
  $Zipcode     = $query->param('zipcode');  
  
  if ($CacheKiller lt "1") { print "<assign var=\"cachekiller\" value=\"1\"/>";  }
  else                     { print "<assign var=\"cachekiller\" value=\"" . 
                                   ($CacheKiller + 1) . "\"/>";                  }
  
}  # initialize subroutine
  
#-------------------------------------------------------------------------------------


sub Closure() {

  print "</callxml>";

}  # closure subroutine

#----------------------------------------------------------------------------------------

sub ZipCodeMenu {
  #-----------------------------------------------------------------------------
  # Initialize our databasing routines and select out all matches to the zipcode
  #-----------------------------------------------------------------------------

  my $TempDatabase = DBI->connect("dbi:ODBC:USPostalCodes", "", "");
  $TempSQL         = "select * from PostalCodes where Zipcode = '" . $Zipcode . "'";
  
  $TempRecordset = $TempDatabase->prepare($TempSQL);
  $TempRecordset->execute();
  
  print "<block>"; 
  
  $Loopy       = 0;
  $CallXMLText = "";
  
  while (($City,$State) = $TempRecordset->fetchrow_array() ) {
    $Loopy = $Loopy + 1; 
	
    #-----------------------------------------------------
    # We have to check for certain states because the
	# callxml TTS engine will not pronounce them correctly
    #-----------------------------------------------------
    
    TTSFix: {
	  $State = "Alabama",       last TTSFix if ($State eq "AL");
	  $State = "Connecticut",   last TTSFix if ($State eq "CT");
	  $State = "Colorado",      last TTSFix if ($State eq "CO");
	  $State = "Delaware",      last TTSFix if ($State eq "DE");
	  $State = "Hawaii",        last TTSFix if ($State eq "HI");
	  $State = "Idaho",         last TTSFix if ($State eq "ID");
	  $State = "Indiana",       last TTSFix if ($State eq "IN");
	  $State = "Louisiana",     last TTSFix if ($State eq "LA");
	  $State = "Massachusetts", last TTSFix if ($State eq "MA");
	  $State = "Maryland",      last TTSFix if ($State eq "MD");
	  $State = "Maine",         last TTSFix if ($State eq "ME");
	  $State = "Michigan",      last TTSFix if ($State eq "MI");
	  $State = "Missouri",      last TTSFix if ($State eq "MO");
	  $State = "Mississippi",   last TTSFix if ($State eq "MS");
	  $State = "Montana",       last TTSFix if ($State eq "MT");
	  $State = "Nebraska",      last TTSFix if ($State eq "NE");
	  $State = "Ohio",          last TTSFix if ($State eq "OH");
	  $State = "Oklahoma",      last TTSFix if ($State eq "OK");
	  $State = "Oregon",        last TTSFix if ($State eq "OR");
	  $State = "Pennsylvania",  last TTSFix if ($State eq "PA");
	  $State = "Washington",    last TTSFix if ($State eq "WA");
	  $State = "Wyoming",       last TTSFix if ($State eq "WY");
	}  # simulated switch/case statement
	
    if    ($State eq "CT") { $State = "Connecticut"; }
    elsif ($State eq "CO") { $State = "Colorado";    }
	
	$CallXMLText = $CallXMLText . "<text>" . $City . "," . $State . "</text>";
  }  # while loop
  
  #-------------------------------------------
  # We need to play the appropriate prompt for 
  # no matches, 1 match, or multiple matches.
  #-------------------------------------------  
  
  if ($Loopy eq "0") {
    print "<playaudio format=\"audio/wav\" value=\"NotFound.wav\" />";    
  }  # if statement
  elsif ($Loopy eq "1") {
    print "<playaudio format=\"audio/wav\" value=\"Results.wav\" />";
  }  # elseif statement
  else {
    print "<playaudio format=\"audio/wav\" value=\"MultipleResults.wav\" />";  
  } # else statement
  
  print $CallXMLText;
  
  #------------------------------------------------------
  # We want to pause for 2 seconds, clear out the zipcode 
  # variable, and then return to the main menu.
  #------------------------------------------------------  
  
  print "<wait value=\"2s\"/>";
  print "<clear var=\"ZipCode\"/>";
  print "<goto value=\"" . $PathString . "zipcode.pl\" submit=\"*\" method=\"get\" />"; 
  print "</block>";  
}  # subroutine ZipCodeMenu

#-------------------------------------------------------------------------------------

sub MainMenu() {
  print "<inputdigits label=\"input_selection\" " .
                    "repeat=\"3\" " .
				    "var=\"ZipCode\" " . 
				    "format=\"audio/wav\" " . 
				    "value=\"MainMenu.wav\" " . 
				    "cleardigits=\"TRUE\" " .
				    "includetermdigit=\"FALSE\" " . 
				    "maxdigits=\"5\" " . 
				    "maxtime=\"15s\" " . 
				    "maxsilence=\"5s\" >"; 

  print "<onmaxdigits>";
  print "<goto value=\"" . $PathString . "zipcode.pl\" submit=\"*\" method=\"get\" />"; 
  print "</onmaxdigits>";
  
  print "<onmaxtime>";
  print "<playaudio format=\"audio/wav\" value=\"YouHaveTakenTooLong.wav\" />";  
  print "<hangup/>";
  print "</onmaxtime>";
  
  print "<onmaxsilence>";
  print "<playaudio format=\"audio/wav\" value=\"YouHaveTakenTooLong.wav\" />";  
  print "<hangup/>";
  print "</onmaxsilence>";
  
  print "</inputdigits>";
  
  #-----------------------------------
  # One last message in case the block 
  # loops through before maxsilence or
  # maxtime has been reached.
  #-----------------------------------
  
  print "<playaudio format=\"audio/wav\" value=\"YouHaveTakenTooLong.wav\" />";    
}  # subroutine MainMenu

#-------------------------------------------------------------------------------------

#------------------------------
# Declare our global variables.
#------------------------------

$PathString = "http://myserver.com/mydirectory/";
$Zipcode = "";
$City    = "";
$State   = "";

# -----------------------------------------------------
# Initialize the ODBC databasing routines and perl libs
# -----------------------------------------------------
  
use CGI;
use DBI; 

&Initialize;

#---------------------------------------------------
# The Querystring variable zipcode determine whether 
# we need to go to the main menu, or if a caller has
# just entered in a ZipCode.
#---------------------------------------------------

if ($Zipcode ne "") { &ZipCodeMenu; }
else 			    { &MainMenu;    }

&Closure;
