// Copyright (c) Microsoft Corporation.  All rights reserved. 
function Debugger( sco )
{
this.debugTag = null;
}
Debugger.prototype.SetDebugTag= function( debugTag )
{
this.debugTag = debugTag;
}
Debugger.prototype.GetResouceBasedXml= function( ID,
Level,
Args ) 
{
var AttributesString = "";
AttributesString += "StatementId=\"" + ID + "\" ";
AttributesString += "Level=\"" + Level + "\"";
return this.TopLevelTag( this.GenericTagWithAttributes( "Trace", AttributesString, this.TraceArgumentsTag( Args ) ) );
}
Debugger.prototype.GetInitialiseMessage= function( Modality, 
ScriptVersion )
{
return  this.TopLevelTag( "<Initialise Modality=\"" + Modality + "\" RuntimeScriptVersion=\"" + ScriptVersion + "\"/>" ); 
}
Debugger.prototype.GetActivationTestsStart= function()
{
return this.TopLevelTag( "<ActivationTestsStart/>" );
}
Debugger.prototype.GetActivationTestsComplete= function()
{
return this.TopLevelTag( "<ActivationTestsComplete/>" );
}
Debugger.prototype.TraceArgumentsTag= function( Args )
{
var InnerXml = "";
var i = 0;
for ( i = 0; i < Args.length; i++ )
{
InnerXml += this.TraceArgumentTag( Args[i] );
}
if ( InnerXml != "" )
{
return this.GenericTag( "Arguments", InnerXml );
}
else
{
return "";
}
}
Debugger.prototype.TraceArgumentTag= function( Arg )
{
return this.GenericTag( "Argument", Arg );
}
Debugger.prototype.GetPageSubmit= function()
{
return this.TopLevelTag( "<PageSubmit/>" );
}
Debugger.prototype.TopLevelTag= function( InnerXml )
{
return "<WebserverLog>" + InnerXml + "</WebserverLog>";
}
Debugger.prototype.GenericTag= function(Tag, 
InnerXml )
{
return "<" + Tag + ">" + InnerXml + "</" + Tag + ">";
}
Debugger.prototype.GenericTagWithAttributes= function(Tag,
Attributes,
InnerXml )
{
return "<" + Tag + " " + Attributes + ">" + InnerXml + "</" + Tag + ">";
}
function CSpeechCommon(formObj, doc, win, 
supportPQ,
isPocketPC )
{
this.Debug = new Debugger (this);
this.errorPage = "";
this.needPostback = false;
this.supportPQ = supportPQ;
this.isPocketPC = isPocketPC;
this.theForm = formObj;
this.ObjectsForOnSubmit = new Array();
this.RunSpeech = null; 
this.ClientViewState = new Object();
this.CVSHiddenField = document.all("SpeechCommon_StateField");
this.oldSubmit = formObj.submit;
formObj.submit = function(){ SpeechCommon.Submit(); };
}
CSpeechCommon.prototype.SetDebugTag= function( debugTag )
{
this.Debug.SetDebugTag(debugTag);
}
CSpeechCommon.prototype.Init= function( mode )
{
this.HookUp();
SpeechCommon.Debug.debugTag.Log(this.Debug.GetInitialiseMessage(mode, CSpeechCommon.SpeechScriptVer()));
}
CSpeechCommon.SpeechScriptVer= function()
{
return "2";
}
CSpeechCommon.prototype.SetErrorPage= function( url )
{
this.errorPage = url; 
}
CSpeechCommon.prototype.OnLoad= function()
{
this.DecodeCVS();
if( SpeechCommon.RunSpeech != null ) SpeechCommon.RunSpeech.StartUp();
}
CSpeechCommon.prototype.OnBeforeUnload= function()
{
if( SpeechCommon.RunSpeech != null ) SpeechCommon.RunSpeech.CleanUp();
}
CSpeechCommon.prototype.OnReset= function()
{
if( SpeechCommon.RunSpeech != null ) SpeechCommon.RunSpeech.Reset();
}
CSpeechCommon.prototype.HookUp= function()
{
document.body.onload = SpeechCommon.AppendFunc(document.body.onload, "SpeechCommon.OnLoad();");
document.body.onbeforeunload = SpeechCommon.InsertFunc( document.body.onbeforeunload, "SpeechCommon.OnBeforeUnload();" );
SpeechCommon.theForm.onreset = SpeechCommon.AppendFunc( SpeechCommon.theForm.onreset, "SpeechCommon.OnReset();" );
}
CSpeechCommon.prototype.GetViewState= function( key ) 
{
return this.ClientViewState[key];
}
CSpeechCommon.prototype.SetViewState= function( key, value )
{
this.ClientViewState[key] = value;
}
CSpeechCommon.prototype.InsertFunc= function(ev,  newline)
{
if (typeof(ev) == "function" )
{
ev = ev.toString();
ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
}
else
{
ev = "";
}
return new Function(newline + ev);
}
CSpeechCommon.prototype.AppendFunc= function(ev, newline)
{
if ( typeof(ev) == "function" ) 
{
ev = ev.toString();
ev = ev.substring( ev.indexOf("{") + 1, ev.lastIndexOf("}") );
}
else 
{
ev = "";
}
return new Function( ev+newline );
}
CSpeechCommon.prototype.Eval= function( expr )
{
SpeechCommon.Debug.debugTag.Log("Warning: eval causes perf degradation: " + expr);
return eval(expr);
}
CSpeechCommon.prototype.GoToErrorPage= function( status, description, reason ) 
{
if ( typeof( this.errorPage ) != "undefined" && this.errorPage != "" )
{
var QuestionMarkExists = this.errorPage.indexOf( "?" ) != -1;
this.Navigate( this.errorPage + (QuestionMarkExists ? "&" : "?") + "status=" + status + "&description=" + escape(description) + "&reason=" + escape(reason));
return true;
}
else
{
return false;
}
}
CSpeechCommon.prototype.Navigate= function( url )
{
if( url == null || url.length == 0 ) return;
window.navigate(url);
}
CSpeechCommon.prototype.MatchNode= function( smlDoc, xpath) 
{
if ( (smlDoc != null) && 
(xpath != null) && 
(xpath != "") )
{
return smlDoc.selectSingleNode( xpath );
}
return null;
}
CSpeechCommon.prototype.GetConfidence= function( smlNode ) 
{
var conf = null;
while ( (conf == null || conf == "") 
&& smlNode != null 
&& ( smlNode.baseName != null && smlNode.baseName != "" )
)
{
if ( 
smlNode.nodeType ==  1 && 
smlNode.getAttribute( "unknownConfidence" ) != "yes" )
{
conf = smlNode.getAttribute( "confidence" );
if( conf == null && smlNode.baseName == "SML" )
{
conf = smlNode.getAttribute( "utteranceConfidence" );
}
}
var parentNode = smlNode.parentNode;
if(parentNode == null)
{
parentNode = smlNode.selectSingleNode("..");
}
smlNode = parentNode;
}
if ( conf != null && conf != "" )
{
return parseFloat(conf);
}
else
{
return 0.5;
}
}
CSpeechCommon.prototype.RegisterForOnSubmit= function(obj)
{
SpeechCommon.ObjectsForOnSubmit[SpeechCommon.ObjectsForOnSubmit.length] = obj;
}
CSpeechCommon.prototype.OnSubmit= function()
{
var i;
var c = SpeechCommon.ObjectsForOnSubmit.length;
for ( i = 0; i<c; i++)
{
SpeechCommon.ObjectsForOnSubmit[i].OnSubmit();
}
SpeechCommon.EncodeCVS();
}
CSpeechCommon.prototype.Submit= function()
{
SpeechCommon.Debug.debugTag.Log(SpeechCommon.Debug.GetPageSubmit())
SpeechCommon.needPostback = false;
SpeechCommon.OnSubmit();
if( SpeechCommon.oldSubmit != null )
{
SpeechCommon.oldSubmit();
}
else
{
SpeechCommon.theForm.submit();
}
}
CSpeechCommon.prototype.EncodeCVS= function( )
{
if( this.CVSHiddenField != null )
{
var StateString = "";
for( var key in this.ClientViewState)
{
StateString += key + "," + escape (this.ClientViewState[key]) + ",";
}
if ( StateString.charAt( StateString.length - 1 ) == "," )
{
StateString = StateString.substr( 0, StateString.length - 1 );
}
this.CVSHiddenField.value = escape (StateString);
}
}
CSpeechCommon.prototype.DecodeCVS= function( )
{
if( this.CVSHiddenField != null )
{
var StateString = unescape( this.CVSHiddenField.value );
if ( StateString != null && StateString != "" )
{
this.InitViewState( StateString.split(",") );
}
}
}
CSpeechCommon.prototype.InitViewState= function( args)   
{
var cvs = new Object();    
if (args.length>1 && args.length%2 == 0)
{
for (var i=0; i<args.length; i+=2)
{
cvs[args[i]] = unescape (args[i+1]);
}
}
this.ClientViewState = cvs;
}
CSpeechCommon.prototype.GetRecoErrorDescription= function( status ) 
{
switch( status )
{
case -1:
return "A generic (speech) platform error occurred during recognition.";
case -2:
return "Failure to find a speech platform (for distributed architectures).";
case -3:
return "An illegal property/attribute setting caused a problem with the recognition result.";
case -4:
return "Failure to find a grammar resource.";
case -5:
return "Failure to load or compile a grammar resource.";
case -6:
return "Failure to activate or deactivate rules/grammars.";
case -7:
return "The period specified in the maxtimeout attribute expired before recognition was completed.";
case -8:
return "There are no grammars active when the recognition is started.";
case -9:
return "Recognition was attempted while another listen object was active.";
default:
return "Unknown error.";
}
}
CSpeechCommon.prototype.GetDtmfErrorDescription= function( status ) 
{
switch( status )
{
case -1:
return "A generic (speech) platform error occurred during DTMF collection.";
case -3:
return "An illegal property/attribute setting caused a problem with the DTMF collection request.";
case -4:
return "Failure to find a resource during the DTMF collection request.";
case -5:
return "Failure to load or compile a grammar resource.";
case -8:
return "Collection was attempted without active grammars.";
case -9:
return "Collection was attempted while another dtmf object was in execution.";
default:
return "Unknown error.";
}
}
CSpeechCommon.prototype.GetPromptErrorDescription= function( status ) 
{
switch( status )
{
case -1:
return "A generic (speech) platform error occurred during synthesis.";
case -2:
return "Failure to find a speech platform (for distributed architectures).";
case -3:
return "An illegal property/attribute/parameter setting caused a problem with the synthesis request.";
case -4:
return "Failure to resolve content during the synthesis request, for example, due to unreachable URI or malformed markup content.";
case -5:
return "Failure to pause the audio playback.";
case -6:
return "Failure to resume the audio playback.";
default:
return "Unknown error.";
}
}
CSpeechCommon.prototype.GetSmexErrorDescription= function( status ) 
{
switch( status )
{
case -1:
return "Timeout";
case -2:
return "Communication error";
default:
return "Unknown error.";
}
}
CSpeechCommon.prototype.EMPTY               = "Empty";
CSpeechCommon.prototype.NEEDCONFIRMATION    = "NeedConfirmation";
CSpeechCommon.prototype.CONFIRMED           = "Confirmed";
CSpeechCommon.prototype.VALID               = "Valid";
CSpeechCommon.prototype.INVALID             = "Invalid";
CSpeechCommon.prototype.NEUTRAL             = "Neutral";
CSpeechCommon.prototype.ACCEPT              = "Accept";
CSpeechCommon.prototype.DENY                = "Deny";
CSpeechCommon.prototype.STC                 = "ShortTimeoutConfirmation";
CSpeechCommon.prototype.PROMPTERROR         = "PromptError";
CSpeechCommon.prototype.RECO                = "";
CSpeechCommon.prototype.NORECO              = "NoReco";
CSpeechCommon.prototype.SILENCE             = "Silence";
CSpeechCommon.prototype.RECOERROR           = "RecoError";
CSpeechCommon.prototype.DTMFERROR           = "DtmfError";
function Prompt(sco, 
id,
promptTag, 
hiddenFieldID,
autoPostBack, 
onComplete )
{
this.ID = id;
this.promptTag = promptTag;
this.autoPostBack = autoPostBack;
this.onComplete = onComplete;
this.completed = false;
SpeechCommon.RegisterForOnSubmit(this);
this.hiddenField = document.all(hiddenFieldID);
if ( this.hiddenField == null )
{
SpeechCommon.GoToErrorPage( 0, "Prompt '" + this.ID + "' doens't have a valid hidden field", "");
}
}
Prompt.prototype.OnSubmit= function()
{
this.WritePostData();
}
Prompt.prototype.WritePostData= function()
{
this.hiddenField.value = this.promptTag.status + ";" + (this.completed ? "1" : "0") + ";" + this.promptTag.bookmark;
}
Prompt.prototype.SysOnComplete= function()
{
this.completed = true;
if( this.onComplete != null ) { var fp = this.onComplete; var f = fp[0]; var obj = fp[1];  if( f == null ) f = fp[0] = SpeechCommon.Eval(fp[2]); if( obj == null && fp[3] != null ) obj = fp[1] = SpeechCommon.Eval(fp[3]); f.apply(obj, new Array()); ;  };
if( this.autoPostBack )
{
SpeechCommon.Submit();
}
}
function Listen(sco, 
id, 
listenTag, 
hiddenFieldID,
autoPostBack, 
onSpeechDetected,
onSilence,
onNoReco,
onError,
onReco
)
{
this.ID = id;
this.listenTag = listenTag;
this.autoPostBack = autoPostBack;
this.onSpeechDetected = onSpeechDetected;
this.onSilence = onSilence;
this.onNoReco = onNoReco;
this.onError = onError;
this.onReco = onReco;
this.raiseServerEvent = false;
SpeechCommon.RegisterForOnSubmit(this);
this.hiddenField = document.all(hiddenFieldID);
if ( this.hiddenField == null )
{
SpeechCommon.GoToErrorPage( 0, "Listen '" + this.ID + "' doens't have a valid hidden field", "");
}
}
Listen.prototype.OnSubmit= function()
{
this.WritePostData();
}
Listen.prototype.WritePostData= function( )
{
if( this.raiseServerEvent == false )
{
this.hiddenField.value = this.listenTag.status + ";0;";
}
else
{
var smlDoc = this.listenTag.recoresult;
var sml = smlDoc==null ? "" : smlDoc.xml;
this.hiddenField.value = this.listenTag.status + ";1;" + escape(sml);
}
}
Listen.prototype.SetEventFlag= function( isReco )
{
this.raiseServerEvent = isReco;
}
Listen.prototype.SysOnSpeechDetected= function()
{
this.SetEventFlag(false);
if( this.onSpeechDetected != null ) { var fp = this.onSpeechDetected; var f = fp[0]; var obj = fp[1];  if( f == null ) f = fp[0] = SpeechCommon.Eval(fp[2]); if( obj == null && fp[3] != null ) obj = fp[1] = SpeechCommon.Eval(fp[3]); f.apply(obj, new Array()); ;  };
}
Listen.prototype.SysOnSilence= function()
{
this.SetEventFlag(false);
if( this.onSilence != null ) { var fp = this.onSilence; var f = fp[0]; var obj = fp[1];  if( f == null ) f = fp[0] = SpeechCommon.Eval(fp[2]); if( obj == null && fp[3] != null ) obj = fp[1] = SpeechCommon.Eval(fp[3]); f.apply(obj, new Array()); ;  };
}
Listen.prototype.SysOnNoReco= function()
{
this.SetEventFlag(false);
if( this.onNoReco != null ) { var fp = this.onNoReco; var f = fp[0]; var obj = fp[1];  if( f == null ) f = fp[0] = SpeechCommon.Eval(fp[2]); if( obj == null && fp[3] != null ) obj = fp[1] = SpeechCommon.Eval(fp[3]); f.apply(obj, new Array()); ;  };
}
Listen.prototype.SysOnError= function()
{
this.SetEventFlag(false);
if( this.onError != null ) { var fp = this.onError; var f = fp[0]; var obj = fp[1];  if( f == null ) f = fp[0] = SpeechCommon.Eval(fp[2]); if( obj == null && fp[3] != null ) obj = fp[1] = SpeechCommon.Eval(fp[3]); f.apply(obj, new Array()); ;  };
}
Listen.prototype.SysOnReco= function()
{
this.SetEventFlag(true);
if( this.onReco != null ) { var fp = this.onReco; var f = fp[0]; var obj = fp[1];  if( f == null ) f = fp[0] = SpeechCommon.Eval(fp[2]); if( obj == null && fp[3] != null ) obj = fp[1] = SpeechCommon.Eval(fp[3]); f.call(obj, this.listenTag.recoresult); ;  };
if( this.autoPostBack )
{
SpeechCommon.Submit();
}
}
