// 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.',
    'notitle' : 'Untitled document'
    };
}, false);


// The "master mind" of the VoiceXML form. This takes the semantic
// interpretation 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)
    // we pass this to the VXML form
    return si;
  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;
}
