// AJAX Routines

var url = "sidebar.php?menu="; // The server-side script

function handleHttpResponse() {
  if (httpXMLobj.readyState == 4) {
    if (httpXMLobj.responseText.indexOf('invalid') == -1) {
      var dom = document.getElementById('sidebar');
      dom.innerHTML = httpXMLobj.responseText; 
      isWorking = false;
    }
  }
}

var isWorking = false;
function updateSidebar(menu) {
  if (!isWorking && httpXMLobj) {      
    httpXMLobj.open("GET", url + escape(menu), true);
    httpXMLobj.onreadystatechange = handleHttpResponse;
    isWorking = true;
    httpXMLobj.send(null);
  }
}

var playList_url ="play_list.php";
function playlist_response() {
  if (httpXMLobj.readyState == 4) {
    if (httpXMLobj.responseText.indexOf('invalid') == -1) {
      var dom = document.getElementById('sidebar');
      document.getElementById("current_submenu_header").style.visibility = "hidden";
      document.getElementById("current_submenu").innerHTML = "Play list";
      dom.innerHTML = httpXMLobj.responseText; 
      isWorking = false;
    }
  }

}

function getPlayList() {                     
  if (!isWorking && httpXMLobj) {     
    httpXMLobj.open("GET", playList_url, true);
    httpXMLobj.onreadystatechange = playlist_response;
    isWorking = true;
    httpXMLobj.send(null);
  }
}

// Saving a Timeline uses storeTimeLine() and store_response()
var storeTL_url ="storeTL.php";

function store_response() {
  if (httpXMLobj.readyState == 4) {
    if (httpXMLobj.responseText.indexOf('invalid') == -1) {
           alert(httpXMLobj.responseText);
           document.getElementById('saveTLine').style.visibility='hidden';
           isWorking = false;
    }
  }

}

function storeTimeLine(saveTL, id) {  
  if (!isWorking && httpXMLobj) {
    var data = "id=" + escape(id) + "&data=" + escape(saveTL);
    httpXMLobj.open("POST", storeTL_url, true);
    httpXMLobj.onreadystatechange = store_response;
    httpXMLobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    isWorking = true;
    httpXMLobj.send(data);
  }
}


//
// Retrieving a saved Timeline uses process_saved_data() and get_saved_data
// getting the play lists uses getPlayList() and playlist_response()
// When a play list item (printed to sidebar) is cliked, then process_saved_data() and get_saved_data
//  come into play

function process_saved_data() {
  if (httpXMLobj.readyState == 4) {
    if (httpXMLobj.responseText.indexOf('invalid') == -1) {
//           alert(httpXMLobj.responseText);        
             TLProcessPlayList(httpXMLobj.responseText);
           isWorking = false;
    }
  }

}

function get_saved_data(file) {  
  if (!isWorking && httpXMLobj) {      
    var plURL = "get_saved_file.php?file=" + escape(file);
    httpXMLobj.open("GET", plURL, true);
    httpXMLobj.onreadystatechange = process_saved_data;
    isWorking = true;
    httpXMLobj.send(null);
  }
}



function getHTTPObject() {
  if(screen.height < 700) return false;
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var httpXMLobj = getHTTPObject(); // We create the HTTP Object


