/*
(c) Copyright 2006 ROBO Design
http://www.robodesign.ro/
*/


// On page load, we set the document.voice_msg object to contain several messages we use in the VoiceXML form.
if(document.addEventListener)
	document.addEventListener('load', function ()
{
	document.voice_msg = {
		'help' : 'You can say: speak page, speak navigation, speak content.',
		'nomatch' : 'Try again.',
		'noinput' : 'If you need help, ask for help.',
		'prompt' : 'Please input your command.'
		};
}, false);


// The "master mind" of the VoiceXML form. This takes the semantic interpretation of the user input from the VoiceXML form and does what is neccesary.
function voice_done(val)
{
	// The value/object returned by this function goes to the VoiceXML form. The form will also process it, for taking further actions, for example read parts of the page.

	if(!val || !val.interpretation)
		return 'event-nomatch';

	var si = val.interpretation;

	if(si.action == 'load-page')
	{
		document.location = si.page;

		// This is to allow ending the execution of the VoiceXML form.
		return '';
	} else if(si.action == 'prompt-element' && si.src)
		return si;
	else if(si.action == 'load-news')
	{
		// Action 'load-news' is not in the tutorial. This gives the user the possibility to ask the site to load Nth news shown as a headline on the front page.
		// For example just say "news 1" to go to the latest news, or "news 2" for the second latest, etc.
		// Take a look at the grammar file also.

		// This code takes the number from the semantic interpretation of the utterance and searches the page for the Nth news link. If found, the page will be loaded, otherwise "nomatch" event will be thrown.

		var news = document.getElementById('news');
		if(!news || !si.nr)
			return 'event-nomatch';
		var list = news.getElementsByTagName('a');
		if(!list)
			return 'event-nomatch';
		var elem = list[si.nr-1];
		if(!elem || !elem.href)
			return 'event-nomatch';
		document.location = elem.href;
		return '';
	} else
		return 'event-nomatch';
}

/*
We use this method as a workaround for the limitation in the (X)HTML standard that does not allow an ID attribute on the <title> element. Thus we cannot use <prompt xv:src="#element-id" /> and still have a valid document.

document.title is not used here, because if the page has no title, the browser sets the page location as the title. Reading long and complex URLs is very unpleasant.
*/
function voice_ptitle()
{
  var elem = document.getElementsByTagName('title')[0];
  if(!elem || !elem.firstChild)
    return document.voice_msg.notitle;
  else
    return elem.firstChild.data;
}
