<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//VoiceXML Forum//DTD XHTML+Voice 1.2//EN"
	  "http://www.voicexml.org/specs/multimodal/x+v/12/dtd/xhtml+voice12.dtd">

<!--
Copyright 2010 MySpeechWeb Project. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, this list of
      conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice, this list
      of conditions and the following disclaimer in the documentation and/or other materials
      provided with the distribution.

THIS SOFTWARE IS PROVIDED BY MySpeechWeb Project ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MySpeechweb Project OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of MySpeechWeb Project.
-->


<!--
This XML file was written by Paul Meyer with some help from Bryan St. Amour.
The previous version was written by Richard A. Frost and Ali Karaki
-->


<!--
Speechweb Main Directory

Created by Speechweb
Fri Apr 29 15:07:00 EDT 2011

-->



<html xmlns="http://www.w3.org/1999/xhtml" xmlns:vxml="http://www.w3.org/2001/vxml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xv="http://www.voicexml.org/2002/xhtml+voice" xml:lang="en-US">
  <head>
    <title id="title" />


    <!-- the name of the speechweb application and its opening statement are specified here -->
    <script type="text/javascript">
      var appName = "Speechweb";
      var appFullName = "Speechweb Main Directory";
      var greeting = "Welcome to the Speechweb Project! For a list of application categories, say 'what are the categories'. To visit a category, say 'go to', followed by the category you wish to visit.";
    </script>


    <!-- main vxml form for handling the user/application dialogue -->
    <vxml:form id="vxml_main">
      <vxml:field name="vxml_field" modal="true">
	<vxml:grammar type="application/x-jsgf" src="Speechweb.jsgf" />

	<vxml:prompt cond="greeting.length > 0">
	  <vxml:value expr="showMessage('greeting', greeting)" />
	  <vxml:value expr="greeting" />
	  <vxml:value expr="greeting = ''" />
	</vxml:prompt>

	<vxml:nomatch>
	  I do not understand. Please try again.
	  <vxml:reprompt />
	</vxml:nomatch>

	<vxml:filled>
	  <vxml:value expr="newQuery(vxml_field)" />
	  <vxml:clear namelist="vxml_field"/>
	  <vxml:reprompt/>
	</vxml:filled>
      </vxml:field>
    </vxml:form>


    <!-- vxml form for simply saying what comes through the 'say()' function -->
    <vxml:form id="vxml_say">
      <vxml:block>
	<vxml:value expr="document.getElementById('trigger_say').value" />
      </vxml:block>
    </vxml:form>


    <!-- javascript functions -->
    <script type="text/javascript"><![CDATA[
			function init()
			{
				document.getElementById("title").innerHTML = appFullName;
				document.getElementById("header").innerHTML = appFullName;
				document.getElementById("trigger_main").click();
			}

			function manualInput()
			{
				say(newQuery(document.getElementById('textIn').value));
				document.getElementById('textIn').value = "";
			}

			function newQuery(query)
			{
				showMessage("query", query);
				if (query == "go back")
				{
				link = "../";
				window.setTimeout("window.location = link", 500);
				return "";
				}
				else if (query == "go home")
				{
					link = "/applications/";
					window.setTimeout("window.location = link", 500);
					return "";
				}

				xmlhttp = new XMLHttpRequest();
				xmlhttp.open("POST", appName + ".cgi", false);
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlhttp.send("query=" + query + "\&key=" + getKey());

				if (xmlhttp.status != 200)
				{
					showMessage("error", "An error has occured in this application. Error Code: " + xmlhttp.status + ", " + xmlhttp.statusText + ".");
					return "An error has occured in this application. Error Code: " + xmlhttp.status + ", " + xmlhttp.statusText + ".";
				}
				else if (xmlhttp.responseText == "")
				{
					showMessage("error", "An error has occured in this application. The server failed to return a response.");
					return "An error has occured in this application.  The server failed to return a response.";
				}
				else
				{
					var response = xmlhttp.responseText;
					link = response.match(/<goto.*?>.*?<\/goto>/i);
					var wait = 500;

					if (link != null)
					{
						response = response.replace(/<goto.*?>.*?<\/goto>/ig, "");
						link = link[0];
						if (link.match(/<goto wait=["']?[0-9]+["']?>/i) != null)
							wait = parseInt(link.match(/<goto wait=["']?[0-9]+["']?>/i)[0].replace(/[^0-9]/ig, ""));
						link = link.replace(/<\/?goto.*?>/ig, "");
						window.setTimeout("window.location = link", wait);
					}

					showMessage("response", toShow(response));
					return toSay(response);
				}
			}

			function say(message)
			{
				document.getElementById("trigger_say").value = message;
				document.getElementById("trigger_say").click();
				document.getElementById("trigger_main").click();
			}

			function showMessage(type, message)
			{
				var convo = document.getElementById("convo");

				convo.insertRow(0);
				convo.insertRow(0);
				convo.rows[0].style = "padding-bottom:50px";
				convo.rows[0].insertCell(0);
				convo.rows[0].insertCell(0);
				convo.rows[0].cells[0].style = "padding-right:15px; text-align:right; font-weight:bold";
				convo.rows[0].cells[1].style = "width:600px";
				convo.rows[0].cells[1].innerHTML = message;

				if (type == "query")
				{
					convo.rows[0].cells[0].style.color = "#0044CC";
					convo.rows[0].cells[0].innerHTML = "QUESTION:";
					convo.rows[1].style = "height:30px";
				}
				else if (type == "response")
				{
					convo.rows[0].cells[0].style.color = "#00AA00";
					convo.rows[0].cells[0].innerHTML = "RESPONSE:";
					convo.rows[1].style = "height:5px";
				}
				else if (type == "error")
				{
					convo.rows[0].cells[0].style.color = "#FF0000";
					convo.rows[0].cells[0].innerHTML = "ERROR:";
					convo.rows[1].style = "height:30px";
				}
				else if (type == "greeting")
				{
					convo.rows[0].cells[0].style.color = "#AA00AA";
					convo.rows[0].cells[0].innerHTML = "GREETING:";
					convo.rows[1].style = "height:0px";
				}
				else
				{
					convo.rows[0].cells[0].style.color = "#7F7F7F";
					convo.rows[0].cells[0].innerHTML = type.toUpperCase() + ":";
					convo.rows[1].style = "height:30px";
				}
			}

			function getKey()
			{
				var key = "";
				var cookie = document.cookie.match(/(^k|; k)ey=.*?([^;]$|;)/);
				if (cookie != null)
					return cookie[0].replace("key=", "");

				while (key.length < 20)
					key = key.concat(randomAlnum());
				document.cookie = "key=" + key + "; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/";
				return key;
			}

			function randomAlnum()
			{
				var char;
				var n = Math.floor(Math.random()*3);
				if (n == 0)
					return String.fromCharCode(Math.floor(Math.random()*10) + 48);
				if (n == 1)
					return String.fromCharCode(Math.floor(Math.random()*26) + 65);
				if (n == 2)
					return String.fromCharCode(Math.floor(Math.random()*26) + 97);
			}

			function toSay(response)
			{
				return response.replace(/<asay>.*?<\/asay>/igm, "").replace(/<.*?>/igm, "");
			}

			function toShow(response)
			{
				return response.replace(/<ashow>.*?<\/ashow>/igm, "").replace(/<\/?asay>/igm, "");
			}
		]]></script>
  </head>


  <body style="font-family:Verdana; text-align:center" onload="init()">
    <!-- hidden vxml triggers to activate each of the vxml forms -->
    <input type="hidden" id="trigger_say" ev:event="click" ev:handler="#vxml_say" />
    <input type="hidden" id="trigger_main" ev:event="click" ev:handler="#vxml_main" />

    <h1 id="header" />
    <b style="line-height:25px">You can speak or enter your input here:</b><br />

    <!-- a textbox which can be used to enter manual input if needed -->
    <form onsubmit="manualInput(); return false;">
      <input type="text" size="75" id="textIn" />
    </form>

    <!-- table which will contain the user/application dialogue -->
    <table style="margin-left:auto; margin-right:auto; padding-top:30px; text-align:left" id="convo" />
  </body>
</html>

