// JavaScript Document


/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


function XHConn1()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

//initialize XHConn (if XHConn isn't created successfully, 
//the client doesnt' support Ajax)
var ajaxConn = new XHConn();
var ajaxConn_book = new XHConn1();


function ttsEngine(text,pagename){
//post to mypage.php with args foo and baz
 statusind = document.getElementById("playertext");
 statusind.innerHTML = 'Loading.... ';
ajaxConn.connect("/speechengine.php", "GET", 
  "text="+text+"&page="+pagename,fnWhenDone);
}
//when the server responds, javascript 
//will trigger this callback function
function fnWhenDone(XML)
{
  playTTS(XML.responseText);
}

function playTTS(snd) {
 swfobject.embedSWF("/js/playerMini.swf?autoPlay=yes&soundPath=/audtts/"+snd, "tts_player", "75", "20", "7", "#FFFFFF");
 //Effect.toggle("tts_status", 'appear', {delay : 0});
 statusind = document.getElementById("accessibility-container");
 statusind.style.backgroundImage = "url('/images/player.gif')";
 statusind = document.getElementById("playertext");
 statusind.innerHTML = 'Playing Content';
 statusind.href = 'javascript:stopTTS("'+snd+'");';
 //Effect.Fade("tts_player", {delay : 0});
 
}

function stopTTS(snd) {
 $('tts_player').replace('<span id="tts_player"></span>');
 statusind = document.getElementById("accessibility-container");
 statusind.style.backgroundImage = "url('/images/access_speech.gif')";
 statusind = document.getElementById("playertext");
 statusind.innerHTML = 'Stopped (Click to Play)';
 statusind.href = "javascript:startTTS('"+snd+"');";
 //sendEvent('playpause');
}

function startTTS(snd) {
 swfobject.embedSWF("/js/playerMini.swf?autoPlay=yes&soundPath=/audtts/"+snd, "tts_player", "75", "20", "7", "#FFFFFF");
 statusind = document.getElementById("accessibility-container");
 statusind.style.backgroundImage = "url('/images/player.gif')";
 statusind = document.getElementById("playertext");
 statusind.innerHTML = 'Playing Content';
 statusind.href = 'javascript:stopTTS("'+snd+'");';
 //sendEvent('playpause');
}

function removeDivContent(targetDiv){
var div = document.getElementById(targetDiv);
div.innerHTML = "";
}


function doAjaxPostAndReturn(url,qs){
//post to mypage.php with args foo and baz
 r = document.getElementById("search_return");
ajaxConn_book.connect("/test.php", "GET", 
  qs,fnWhenDone2);
}

function fnWhenDone2(XML)
{
  r = document.getElementById("search_return");
  r.innerHTML = XML.responseText;
}
