<?PHP

// ---------------------------------------------------------------
//
// -----------------------------------------------------
// copyright 2000 by voxeo corporation. (see LGPL.txt)
//
// US Postal Codes database, Copyright (c) 2000 CD Light
//                           All Rights Reserved.
// -----------------------------------------------------
//
// v1.0 Coded in PHP 4.0
// v1.0 Coded by Stephen J. Lewis
//
// 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, PERL, 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
//     database-win32.inc    <---- abstracted ODBC database routines     
//
// 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.
//
//----------------------------------------------------------------
  
//-----------------------------------------------------------------------------------------

function Initialize() {
  global $HTTP_GET_VARS;
  
  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 
  echo "<callxml>";
  
  //------------------------------------------------------------------------
  // 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.
  //------------------------------------------------------------------------

  if ($HTTP_GET_VARS["cachekiller"] == "") {
    echo "<assign var=\"cachekiller\" value=\"1\"/>";
  }  // if statement
  else {
    $HTTP_GET_VARS["cachekiller"]++;
	echo "<assign var=\"cachekiller\" value=\"" . $HTTP_GET_VARS["cachekiller"] . "\"/>";
  }  // else statement
  
}  // initialize function
  
//-------------------------------------------------------------------------------------


function Closure() {

  echo "</callxml>";

}  // closure function

//----------------------------------------------------------------------------------------

function FindZipCode($ZipCode) {
  $TempArray      = Array();
  $TempConnection = OpenDatabase("USPostalCodes");
  $TempSQL        = "select * from PostalCodes where Zipcode = '" . $ZipCode . "'";
  $TempDatabase   = ReadDatabase($TempConnection, $TempSQL);
  $Loopy          = 0;
  
  while ( $TempRow = FetchRowFromDatabase( $TempDatabase ) ) {
    $Loopy++;
    $TempArray["City" . $Loopy]  = GetResultFromRow( "CITY",   $TempRow, $TempDatabase );
    $TempArray["State" . $Loopy] = GetResultFromRow( "STATE",  $TempRow, $TempDatabase );
  }  // while statement
  
  if ($Loopy == 0) {
    $TempArray["City1"]  = "NOT FOUND";
	$TempArray["State1"] = "NOT FOUND";
	$Loopy               = 1;
  }  // else statement
  
  CloseDatabase;
  
  $TempArray["NumberOfCities"] = $Loopy;
  
  return $TempArray;
}  // function FindZipCode

//----------------------------------------------------------------------------------------

function ZipCodeMenu($ZipCode) {
  $CityAndState = Array();
  $CityAndState = FindZipCode($ZipCode); 
  
  echo "<block>";  
  
  //-------------------------------------------
  // We need to play the appropriate prompt for 
  // no matches, 1 match, or multiple matches.
  //-------------------------------------------
  
  if ( ($CityAndState["City1"]  == "NOT FOUND") ||
       ($CityAndState["State1"] == "NOT FOUND") ) {
    echo "<playaudio format=\"audio/wav\" value=\"NotFound.wav\" />";    
  }  // if statement
  elseif ($CityAndState["NumberOfCities"] == "1") {
    echo "<playaudio format=\"audio/wav\" value=\"Results.wav\" />";
  }  // elseif statement
  else {
    echo "<playaudio format=\"audio/wav\" value=\"MultipleResults.wav\" />";
  }  // else statement
   
  //---------------------------------------------------------
  // Check to make sure there was both a city and state found
  //
  // Note that the AGS seems to recognize two capital letters
  // in a row as a State name.  Thus, "FL" reads as "Florida."
  //---------------------------------------------------------
  
  for ($Loopy = 1; $Loopy <= $CityAndState["NumberOfCities"]; $Loopy++) {
    if ( ($CityAndState["City" . $Loopy]  != "NOT FOUND") ||
         ($CityAndState["State" . $Loopy] != "NOT FOUND") ) {
      //-----------------------------------------------------
      // We have to check for certain states because the
	  // callxml TTS engine will not pronounce them correctly
      //-----------------------------------------------------
      
	  switch ($CityAndState["State" . $Loopy]) {
	    Case "AL": $CityAndState["State" . $Loopy] = "Alabama";       break;
	    Case "CT": $CityAndState["State" . $Loopy] = "Connecticut";   break;
	    Case "CO": $CityAndState["State" . $Loopy] = "Colorado";      break;
	    Case "DE": $CityAndState["State" . $Loopy] = "Delaware";      break;
	    Case "HI": $CityAndState["State" . $Loopy] = "Hawaii";        break;
	    Case "ID": $CityAndState["State" . $Loopy] = "Idaho";         break;
	    Case "IN": $CityAndState["State" . $Loopy] = "Indiana";       break;
	    Case "LA": $CityAndState["State" . $Loopy] = "Louisiana";     break;
	    Case "MA": $CityAndState["State" . $Loopy] = "Massachusetts"; break;
	    Case "MD": $CityAndState["State" . $Loopy] = "Maryland";      break;
	    Case "ME": $CityAndState["State" . $Loopy] = "Maine";         break;
	    Case "MI": $CityAndState["State" . $Loopy] = "Michigan";      break;
	    Case "MO": $CityAndState["State" . $Loopy] = "Missouri";      break;
	    Case "MS": $CityAndState["State" . $Loopy] = "Mississippi";   break;
	    Case "MT": $CityAndState["State" . $Loopy] = "Montana";       break;
	    Case "NE": $CityAndState["State" . $Loopy] = "Nebraska";      break;
	    Case "OH": $CityAndState["State" . $Loopy] = "Ohio";          break;
	    Case "OK": $CityAndState["State" . $Loopy] = "Oklahoma";      break;
	    Case "OR": $CityAndState["State" . $Loopy] = "Oregon";        break;
	    Case "PA": $CityAndState["State" . $Loopy] = "Pennsylvania";  break;
	    Case "WA": $CityAndState["State" . $Loopy] = "Washington";    break;
	    Case "WY": $CityAndState["State" . $Loopy] = "Wyoming";       break;
      }  // switch statement
	  
	  //----------------------------------------------------
	  // Throw in the text-to-speech for the city and state.
	  //----------------------------------------------------
	
      echo "<text>";
      echo $CityAndState["City" . $Loopy] . "," . $CityAndState["State" . $Loopy];
      echo "</text>";
    }  // else statement
  }  // for loop
  
  //------------------------------------------------------
  // We want to pause for 2 seconds, clear out the zipcode 
  // variable, and then return to the main menu.
  //------------------------------------------------------
  
  echo "<wait value=\"2s\"/>";
  echo "<clear var=\"ZipCode\"/>";
  echo "<goto value=\"" . $PathString . "zipcode.php\" submit=\"*\" method=\"get\" />"; 
  echo "</block>";
}  // function ZipCodeMenu

//-------------------------------------------------------------------------------------

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

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

//-------------------------------------------------------------------------------------

$PathString = "http://myserver.com/MyDirectory/";

include('database-win32.inc');

Initialize();

$ZipCode = $HTTP_GET_VARS["zipcode"];

//---------------------------------------------------
// 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 != "") { ZipCodeMenu($ZipCode); }
else 			    { MainMenu();            }

Closure();

?>