Server/Code/main.js: Difference between revisions

From kJams Wiki
Jump to navigation Jump to search
Created page with '<pre> kJams Project - main.js Description: Main screen functionality. (c) 2007 kJams and David M. Cotter: var timer = null; var allowBackspace = false; var inSear…'
 
No edit summary
 
Line 1: Line 1:
<pre>
<pre>
/*   kJams Project - main.js
/* kJams Project - main.js
  Description: Main screen functionality.
Description: Main screen functionality.
  (c) 2007 kJams and David M. Cotter   */
(c) 2007 kJams and David M. Cotter */


var timer = null;
var timer = null;
var lastPing = 0;
var allowBackspace = false;
var allowBackspace = false;
//checks to see if the search field it in focus. If it is, allow the user to use the Delete/Backspace key. Otherwise, set flag to trap it.
var inSearch = {
var inSearch = {
   on: function() {allowBackspace = true;},
   on: function() {allowBackspace = true;},
Line 12: Line 14:


function m_populatePlaylists() {
function m_populatePlaylists() {
  //Remove anything that was already there
//Remove anything that was already there
  divObj = document.getElementById("playlists");
divObj = document.getElementById("playlists");
 
  for (var i = 0; i < divObj.childNodes.length; i++) {
for (var i = 0; i < divObj.childNodes.length; i++) {
  divObj.removeChild(divObj.childNodes[i]);
divObj.removeChild(divObj.childNodes[i]);
  }
}
 
  list = new KJDropList();
list = new KJDropList();
  list.create(divObj);
list.create(divObj);
  playlistsList = list;
playlistsList = list;
 
  getPlaylists(url_playlists, m_playlistsLoaded);
getPlaylists(url_playlists, m_playlistsLoaded);
}
}


function m_playlistsLoaded(list) {
function m_playlistsLoaded(list) {
  listCtrl = document.getElementById("playlists").childNodes[0].parent;
listCtrl = document.getElementById("playlists").childNodes[0].parent;
 
  for (var i = 0; i < list.length; i++) {
for (var i = 0; i < list.length; i++) {
  if ((list[i]['name'] == "Library") || (list[i]['name'] == "History"))
if ((list[i]['name'] == "Library") || (list[i]['name'] == "History"))
dropTarget = false;
dropTarget = false;
  else
else
dropTarget = true;
dropTarget = true;
 
  listCtrl.addItem("p" + list[i]['id'], list[i]['name'], dropTarget, null, false);
listCtrl.addItem("p" + list[i]['id'], list[i]['name'], dropTarget, null, false);
 
  //Library and History items are not rearrangeable
//Library and History items are not rearrangeable
  if ((list[i]['name'] == "Library") || (list[i]['name'] == "History"))
if ((list[i]['name'] == "Library") || (list[i]['name'] == "History"))
document.getElementById("p" + list[i]['id']).rearrange = false;
document.getElementById("p" + list[i]['id']).rearrange = false;
  else
else
document.getElementById("p" + list[i]['id']).rearrange = true;
document.getElementById("p" + list[i]['id']).rearrange = true;
 
  //Keep track of ids for 'Tonight' and 'Favorites'
//Keep track of ids for 'Tonight' and 'Favorites'
  if (list[i]['name'] == "Tonight") {
if (list[i]['name'] == "Tonight") {
pTonightID = list[i]['id'];
pTonightID = list[i]['id'];
  }
}
 
  if (list[i]['name'] == "Favorites") {
if (list[i]['name'] == "Favorites") {
pFavoritesID = list[i]['id'];
pFavoritesID = list[i]['id'];
  }
}
 
  //Store the playlist name in the DOM object
//Store the playlist name in the DOM object
  document.getElementById("p" + list[i]['id']).name = list[i]['name'];
document.getElementById("p" + list[i]['id']).name = list[i]['name'];
  }
}
 
  listCtrl.color();
listCtrl.color();
  listCtrl.evtClick = m_populateSongs;
listCtrl.evtClick = m_populateSongs;
  listCtrl.evtDropInto = m_playlistDrop;
listCtrl.evtDropInto = m_playlistDrop;
}
}


function m_populateSongs(playlist) {
function m_populateSongs(playlist) {
  playlist = playlist.substring(1);
playlist = playlist.substring(1);
 
  //Hide
//Hide
  if (songsList) {
if (songsList) {
  songsList.hide();
songsList.hide();
 
  if (songsList.playlist != "1") {
if (songsList.playlist != "1") {
songsList.destroy();
songsList.destroy();
  }
}
  }
}
 
  //Hide "No Playlist" message, show waiting message
//Hide "No Playlist" message, show waiting message
  document.getElementById("message").style.display = 'none';
document.getElementById("message").style.display = 'none';
  document.getElementById("msg_dosearch").style.display = 'none';
document.getElementById("msg_dosearch").style.display = 'none';
  document.getElementById("msg_error").style.display = 'none';
document.getElementById("msg_error").style.display = 'none';
  document.getElementById("waiting").style.display = '';
document.getElementById("waiting").style.display = '';
 
  //If the list is already loaded, simply display it
//If the list is already loaded, simply display it
  if ((playlist == "1") && (songsLists[playlist] != undefined)) {
if ((playlist == "1") && (songsLists[playlist] != undefined)) {
  songsList = songsLists[playlist];
songsList = songsLists[playlist];
  songsList.show();
songsList.show();
  document.getElementById("waiting").style.display = 'none';
document.getElementById("waiting").style.display = 'none';
  }
}
 
  //If it's the library, request that user do a search first
//If it's the library, request that user do a search first
  else if (playlist == "1") {
else if (playlist == "1") {
  document.getElementById("waiting").style.display = 'none';
document.getElementById("waiting").style.display = 'none';
  document.getElementById("msg_dosearch").style.display = '';
document.getElementById("msg_dosearch").style.display = '';
  }
}
 
  else {
else {
  //Playlist DOM object
//Playlist DOM object
  playlistObj = document.getElementById("p" + playlist);
playlistObj = document.getElementById("p" + playlist);
 
  //Which columns are needed?
//Which columns are needed?
  var columns = Array("#", "Song Name", "Artist", "Album");
var columns = Array("#", "Song Name", "Artist", "Album");
 
  if (playlistObj.name == "Tonight") {
if (playlistObj.name == "Tonight") {
columns.push("Pitch");
columns.push("Pitch");
columns.push("Add to");
columns.push("Add to");
  }
}
 
  else if (playlistObj.name == "Favorites") {
else if (playlistObj.name == "Favorites") {
columns.push("Add to");
columns.push("Add to");
  }
}
 
  else {
else {
columns.push("Add to");
columns.push("Add to");
columns.push("Add to");
columns.push("Add to");
  }
}
 
  songsList = new KJList();
songsList = new KJList();
  songsList.rearrange = playlistObj.rearrange;
songsList.rearrange = playlistObj.rearrange;
  songsList.playlist = playlist;
songsList.playlist = playlist;
  songsList.playlistName = playlistObj.name;
songsList.playlistName = playlistObj.name;
  songsList.create(document.getElementById("songs"));
songsList.create(document.getElementById("songs"));
  songsList.setColumns(columns, 0);
songsList.setColumns(columns, 0);
  songsList.evtDrop = m_songRearrange;
songsList.evtDrop = m_songRearrange;
  songsLists[playlist] = songsList;
songsLists[playlist] = songsList;
  getSongs(url_songs, m_songsLoaded, "playlist=" + playlist);
getSongs(url_songs, m_songsLoaded, "playlist=" + playlist);
 
  if (false) {
if (false) {
songsList.evtColClick = null;
songsList.evtColClick = null;
  }
}
  else {
else {
songsList.evtColClick = m_sortSongs;
songsList.evtColClick = m_sortSongs;
  }
}
  }
}
 
  m_sessionCheckin();
m_sessionCheckin();
}
}


function m_songsLoaded(list) {
function m_songsLoaded(list) {
  //Hide waiting message
//Hide waiting message
  document.getElementById("waiting").style.display = 'none';
document.getElementById("waiting").style.display = 'none';
 
  //Rearrangeable?
//Rearrangeable?
  if (songsList.rearrange)
if (songsList.rearrange)
  var rearrange = true;
var rearrange = true;
  else
else
  var rearrange = false;
var rearrange = false;
 
  for (var i = 0; i < list.length; i++) {
for (var i = 0; i < list.length; i++) {
  //Add to favorites
//Add to favorites
  var favsButton = document.createElement("INPUT");
var favsButton = document.createElement("INPUT");
  favsButton.type = "button"
favsButton.type = "button"
  favsButton.name = songsList.playlist + "-" + list[i]['itemId'];
favsButton.name = songsList.playlist + "-" + list[i]['itemId'];
  favsButton.songId = list[i]['id'];
favsButton.songId = list[i]['id'];
  favsButton.value = "Favorites";
favsButton.value = "Favorites";
  favsButton.onmousedown = m_addToFavorites;
favsButton.onmousedown = m_addToFavorites;
 
  //Add to Tonight
//Add to Tonight
  var tonightButton = document.createElement("INPUT");
var tonightButton = document.createElement("INPUT");
  tonightButton.type = "button";
tonightButton.type = "button";
  tonightButton.name = songsList.playlist + "-" + list[i]['itemId'];
tonightButton.name = songsList.playlist + "-" + list[i]['itemId'];
  tonightButton.songId = list[i]['id'];
tonightButton.songId = list[i]['id'];  
  tonightButton.value = "Tonight";
tonightButton.value = "Tonight";
  tonightButton.onmousedown = m_addToTonight;
tonightButton.onmousedown = m_addToTonight;
 
  //Columns
//Columns
  var columns = Array(i, list[i]['name'], list[i]['artist']);
var columns = Array(i, list[i]['name'], list[i]['artist']);
 
  if (typeof(list[i]['album']) == "object") {
if (typeof(list[i]['album']) == "object") {
var dropDown = document.createElement("SELECT");
var dropDown = document.createElement("SELECT");
dropDown.name = list[i]['id'];
dropDown.name = list[i]['id'];
for (var j = 0; j < list[i]['album']['items'].length; j++) {
var optionObj = document.createElement("OPTION");
optionObj.value = list[i]['album']['items'][j];
optionObj.innerHTML = optionObj.value;
if (list[i]['album']['default'] == j) {
for (var j = 0; j < list[i]['album']['items'].length; j++) {
  optionObj.selected = true;
var optionObj = document.createElement("OPTION");
optionObj.value = list[i]['album']['items'][j];
optionObj.innerHTML = optionObj.value;
if (list[i]['album']['default'] == j) {
optionObj.selected = true;
}
dropDown.appendChild(optionObj);
}
}
dropDown.appendChild(optionObj);
columns.push(dropDown);
}
}
columns.push(dropDown);
else {
  }
columns.push(list[i]['album']);
 
}
  else {
columns.push(list[i]['album']);
if (songsList.playlistName == "Tonight") {
  }
//Drop-down
 
var options = {"+6" : 6, "+5" : 5, "+4" : 4, "+3" : 3, "+2" : 2, "+1" : 1, "0" : 0, "-1" : -1, "-2" : -2, "-3" : -3, "-4" : -4, "-5" : -5, "-6" : -6}
  if (songsList.playlistName == "Tonight") {
var dropDown = document.createElement("SELECT");
//Drop-down
dropDown.name = list[i]['itemId'];
var options = {"+6" : 6, "+5" : 5, "+4" : 4, "+3" : 3, "+2" : 2, "+1" : 1, "0" : 0, "-1" : -1, "-2" : -2, "-3" : -3, "-4" : -4, "-5" : -5, "-6" : -6}
dropDown.songId = list[i]['id'];
var dropDown = document.createElement("SELECT");
dropDown.onchange = m_changePitch;
dropDown.name = list[i]['itemId'];
for (option in options) {
dropDown.songId = list[i]['id'];
var optionObj = document.createElement("OPTION");
dropDown.onchange = m_changePitch;
optionObj.value = options[option];
for (option in options) {
optionObj.innerHTML = option;
var optionObj = document.createElement("OPTION");
optionObj.value = options[option];
if(list[i]['pitch'] == options[option]) {
optionObj.innerHTML = option;
optionObj.selected = true;
}
if(list[i]['pitch'] == options[option]) {
  optionObj.selected = true;
dropDown.appendChild(optionObj);
}
}
dropDown.appendChild(optionObj);
columns.push(dropDown);
}
columns.push(favsButton);
}
columns.push(dropDown);
columns.push(favsButton);
else if (songsList.playlistName == "Favorites") {
  }
columns.push(tonightButton);
 
}
  else if (songsList.playlistName == "Favorites") {
columns.push(tonightButton);
else {
  }
columns.push(tonightButton);
 
columns.push(favsButton);
  else {
}
columns.push(tonightButton);
columns.push(favsButton);
index = songsList.addItem(songsList.playlist + "-" + list[i]['itemId'], columns, rearrange, null, false);
  }
 
document.getElementById(songsList.getByIndex(index)).songName = list[i]['name'];
  index = songsList.addItem(songsList.playlist + "-" + list[i]['itemId'], columns, rearrange, null, false);
document.getElementById(songsList.getByIndex(index)).songId = list[i]['id'];
 
document.getElementById(songsList.getByIndex(index)).piIx = list[i]['itemId'];
  document.getElementById(songsList.getByIndex(index)).songName   = list[i]['name'];
}
  document.getElementById(songsList.getByIndex(index)).songId     = list[i]['id'];
  document.getElementById(songsList.getByIndex(index)).piIx     = list[i]['itemId'];
songsList.color();
  }
 
if (songsList.playlistName != "Tonight" && songsList.playlistName != "History") {
  songsList.color();
songsList.evtColClick = m_sortSongs;
 
}
  if (songsList.playlistName != "Tonight" && songsList.playlistName != "History") {
else {
  songsList.evtColClick = m_sortSongs;
songsList.evtColClick = null;
  }
}
  else {
  songsList.evtColClick = null;
  }
}
}


function m_songRearrange(list, song, index, oldIndex) {
function m_songRearrange(list, song, index, oldIndex) {
  sendData(url_rearrange, "playlist=" + list.playlist + "&index=" + index + "&oldIndex=" + oldIndex);
sendData(url_rearrange, "playlist=" + list.playlist + "&index=" + index + "&oldIndex=" + oldIndex);
  m_sessionCheckin();
m_sessionCheckin();
}
}


function m_playlistDrop(song, index, nodeId) {
function m_playlistDrop(song, index, nodeId) {
  playlists   = document.getElementById("playlists").childNodes[0].parent;
playlists = document.getElementById("playlists").childNodes[0].parent;
  item     = songsList.getByID(song);
item = songsList.getByID(song);
 
  if (songsLists[playlists.getByIndex(index).substring(1)]) { songsLists[playlists.getByIndex(index).substring(1)].destroy(); }
if (songsLists[playlists.getByIndex(index).substring(1)]) { songsLists[playlists.getByIndex(index).substring(1)].destroy(); }
  songsLists[playlists.getByIndex(index).substring(1)] = null;
songsLists[playlists.getByIndex(index).substring(1)] = null;
  sendData(url_drop, "playlist=" + playlists.getByIndex(index).substring(1) + "&song=" + songsList.getByID(song).songId);
sendData(url_drop, "playlist=" + playlists.getByIndex(index).substring(1) + "&song=" + songsList.getByID(song).songId);
 
  m_setStatus("Added \"" + item.songName + "\" to \"" + m_getPlaylistName(playlists.getByIndex(index)) + "\" ...");
m_setStatus("Added \"" + item.songName + "\" to \"" + m_getPlaylistName(playlists.getByIndex(index)) + "\" ...");
  m_sessionCheckin();
m_sessionCheckin();
}
}


function m_addToTonight(event) {
function m_addToTonight(event) {
  playlists = document.getElementById("playlists").childNodes[0].parent;
playlists = document.getElementById("playlists").childNodes[0].parent;
  m_playlistDrop(event.target.name, playlists.getIndex("p" + pTonightID), event.target.name);
m_playlistDrop(event.target.name, playlists.getIndex("p" + pTonightID), event.target.name);
}
}


function m_addToFavorites(event) {
function m_addToFavorites(event) {
  playlists = document.getElementById("playlists").childNodes[0].parent
playlists = document.getElementById("playlists").childNodes[0].parent
  m_playlistDrop(event.target.name, playlists.getIndex("p" + pFavoritesID), event.target.name);
m_playlistDrop(event.target.name, playlists.getIndex("p" + pFavoritesID), event.target.name);
}
}


function m_sortSongs(list, column) {
function m_sortSongs(list, column) {
  list.removeAll();
list.removeAll();
  list.setPrimaryCol(column);
list.setPrimaryCol(column);
  document.getElementById("waiting").style.display = '';
document.getElementById("waiting").style.display = '';
 
  if (document.getElementById("searchfield").value) {
if (document.getElementById("searchfield").value) {
  search = "&search=" + document.getElementById("searchfield").value;
search = "&search=" + document.getElementById("searchfield").value;
  }
}
 
  getSongs(url_sort, m_songsLoaded, "playlist=" + list.playlist + "&orderby=" + column + search);
getSongs(url_sort, m_songsLoaded, "playlist=" + list.playlist + "&orderby=" + column + search);
  m_sessionCheckin();
m_sessionCheckin();
}
}


function m_changePitch(event) {
function m_changePitch(event) {
  sendData(url_pitch, "song=" + event.target.songId + "&pitch=" + event.target.value);
sendData(url_pitch, "song=" + event.target.songId + "&pitch=" + event.target.value);
  m_sessionCheckin();
m_sessionCheckin();
}
}


function m_doSearch() {
function m_doSearch() {
  //Remove old search list if it's there
//Select "Library" in the side bar
  if (songsLists["1"]) {
playlistsList.select(playlistsList.getByID(playlistsList.getByIndex(0)));
  songsLists["1"].parentObj.removeChild(songsLists["1"].container);
  songsLists["1"].parentObj.removeChild(songsLists["1"].columnsObj);
//Remove old search list if it's there
  }
if (songsLists["1"]) {
 
songsLists["1"].parentObj.removeChild(songsLists["1"].container);
  //Hide currently loaded list
songsLists["1"].parentObj.removeChild(songsLists["1"].columnsObj);
  if (songsList) {
}
  songsList.hide();
  }
//Hide currently loaded list
 
if (songsList) {
  document.getElementById("msg_dosearch").style.display = 'none';
songsList.hide();
  document.getElementById("message").style.display = 'none';
}
  document.getElementById("msg_error").style.display = 'none';
  document.getElementById("waiting").style.display = '';
document.getElementById("msg_dosearch").style.display = 'none';
document.getElementById("message").style.display = 'none';
document.getElementById("msg_error").style.display = 'none';
document.getElementById("waiting").style.display = '';


  var columns = Array("#", "Song Name", "Artist", "Album", "Add to", "Add to");
var columns = Array("#", "Song Name", "Artist", "Album", "Add to", "Add to");
 
  songsList = new KJList();
songsList = new KJList();
  songsList.rearrange = false;
songsList.rearrange = false;
  songsList.playlist = "1";
songsList.playlist = "1";
  songsList.playlistName = "Library";
songsList.playlistName = "Library";
  songsList.create(document.getElementById("songs"));
songsList.create(document.getElementById("songs"));
  songsList.setColumns(columns, 1);
songsList.setColumns(columns, 1);
  songsLists["1"] = songsList;
songsLists["1"] = songsList;
 
  getSongs(url_search, m_songsLoaded, "search=" + document.getElementById("searchfield").value);
getSongs(url_search, m_songsLoaded, "search=" + document.getElementById("searchfield").value);
  m_sessionCheckin();
m_sessionCheckin();
}
}


function searchKeyPressed(event) {
function searchKeyPressed(event) {
  if (event.keyCode == 13) {
if (event.keyCode == 13) {
  m_doSearch();
m_doSearch();
  }
}
}
}


function m_getSongName(id) {
function m_getSongName(id) {
  songObj = document.getElementById(id);
songObj = document.getElementById(id);
  return songObj.songName;
return songObj.songName;
}
}


function m_getPlaylistName(id) {
function m_getPlaylistName(id) {
  playlistObj = document.getElementById(id);
playlistObj = document.getElementById(id);
  return playlistObj.name;
return playlistObj.name;
}
}


function kj_data_error(id, desc) {
function kj_data_error(id, desc) {
  songsLists[songsList.id] = null;
songsLists[songsList.id] = null;
  songsList.hide();
songsList.hide();
  document.getElementById("waiting").style.display = 'none';
document.getElementById("waiting").style.display = 'none';
 
  document.getElementById("msg_error").innerHTML = "Error: " + desc
document.getElementById("msg_error").innerHTML = "Error: " + desc
  document.getElementById("msg_error").style.display = '';
document.getElementById("msg_error").style.display = '';
}
}


function m_setStatus(status, revert) {
function m_setStatus(status, revert) {
  statusObj = document.getElementById("status");
statusObj = document.getElementById("status");
  statusObj.innerHTML = status;
statusObj.innerHTML = "<span style='color:#FF0000'>" + status + "</span>";
var newInnerHTML='statusObj.innerHTML = "<span style="color:#FF0000">" + status + "</span>"';
blink();
setTimeout("statusObj.innerHTML = newInnerHTML",1250);
}
 
function blink(){
blinkFlag = 1
blinkCount = 1
blinkTimer();
}
 
function blinkTimer(){
if(blinkFlag==1){
blinkSpeed=250
blinkNumber=10 // If this value is even, the text flashes then remains visible. If it is odd, it flashes, then disappears
blinkCount++;
document.getElementById("status").style.visibility='visible';
}else {
blinkCount++;
document.getElementById("status").style.visibility='hidden';  
}
}
blinkFlag^=1;
if (blinkCount < blinkNumber) {setTimeout('blinkTimer()', blinkSpeed);}
}
function trapDelete() {
function trapDelete() {
//Checks to see if the key pressed is the Delete/Backspace key, and if so, stop it from getting to the browser.
   var keyID = (window.event) ? event.keyCode : e.keyCode;
   var keyID = (window.event) ? event.keyCode : e.keyCode;
   if (keyID==8 && !allowBackspace){
   if (keyID==8 && !allowBackspace){
Line 348: Line 378:
   return true;
   return true;
}
}
function m_keyPressed(event) {
function m_keyPressed(event) {
  //This is not IE-compliant code.
//This is not IE-compliant code.
  if (event.keyCode == 46 || event.keyCode ==8) {
if (event.keyCode == 46 || event.keyCode == 8) {
  if (!songsList.selection) {
if (!songsList.selection) {
return;
return;
  }
}
 
  playlistName   = songsList.playlistName;
playlistName = songsList.playlistName;
 
if (playlistName != "Library" && playlistName != "History") {
selection = songsList.selection;
item = songsList.getByID(selection);
songsList.select(null);
sendData(url_remove, "playlist=" + songsList.playlist + "&piIx=" + item.piIx);
m_setStatus("Removed \"" + item.songName + "\" ...");
songsList.removeItem(selection);
}
}
}


  if (playlistName != "Library" && playlistName != "History") {
function m_pingSession() {
selection  = songsList.selection;
var elapsedTime = new Date().getTime()/1000.0 - lastPing;
item      = songsList.getByID(selection);
if (elapsedTime >= 5) {
songsList.select(null);
sendData(url_ping, "");
sendData(url_remove, "playlist=" + songsList.playlist + "&piIx=" + item.piIx);
m_sessionCheckin();
m_setStatus("Removed \"" + item.songName + "\" ...");
lastPing = new Date().getTime()/1000.0;
songsList.removeItem(selection);
}
  }
  }
}
}


function m_sessionCheckin() {
function m_sessionCheckin() {
  /* A true or false value is set by the server before delivering this script
/* A true or false value is set by the server before delivering this script  
  depending on preference.
depending on preference. */
  if (!{auto_logout}) {
if (!{auto_logout}) {
  return;
return;
  }
}
 
  if (timer == null) {
if (timer == null) {
  timer = setInterval("m_sessionLogout()", {timeout} * 1000);
timer = setInterval("m_sessionLogout()", {timeout} * 1000);
  }
}
  else {
else {
  clearInterval(timer);
clearInterval(timer);
  timer = null;
timer = null;
  return m_sessionCheckin();
return m_sessionCheckin();
  }
}
*/
}
}


function m_sessionLogout() {
function m_sessionLogout() {
//   window.location=url_login;
// Sets the flag to tell confirmUnload subroutine that it is OK to let the user leave this page (close the Window, etc.)
allowUnload = true;
window.location=url_login;
}
function loaded() {
document.addEventListener('touchmove', function(e){ e.preventDefault(); });
myScroll = new iScroll('songs');
}
}
//document.addEventListener('DOMContentLoaded', loaded);
</pre>
</pre>

Latest revision as of 02:23, 4 September 2012

/*	kJams Project - main.js
	Description: Main screen functionality.
	(c) 2007 kJams and David M. Cotter	*/

var timer = null;
var lastPing = 0;
var allowBackspace = false;
//checks to see if the search field it in focus. If it is, allow the user to use the Delete/Backspace key. Otherwise, set flag to trap it.
var inSearch = {
   on: function() {allowBackspace = true;},
   off: function() {allowBackspace = false;}
}

function m_populatePlaylists() {
	//Remove anything that was already there
	divObj = document.getElementById("playlists");
	
	for (var i = 0; i < divObj.childNodes.length; i++) {
		divObj.removeChild(divObj.childNodes[i]);
	}
	
	list = new KJDropList();
	list.create(divObj);
	playlistsList = list;
	
	getPlaylists(url_playlists, m_playlistsLoaded);
}

function m_playlistsLoaded(list) {
	listCtrl = document.getElementById("playlists").childNodes[0].parent;
	
	for (var i = 0; i < list.length; i++) {
		if ((list[i]['name'] == "Library") || (list[i]['name'] == "History"))
			dropTarget = false;
		else
			dropTarget = true;
		
		listCtrl.addItem("p" + list[i]['id'], list[i]['name'], dropTarget, null, false);
		
		//Library and History items are not rearrangeable
		if ((list[i]['name'] == "Library") || (list[i]['name'] == "History"))
			document.getElementById("p" + list[i]['id']).rearrange = false;
		else
			document.getElementById("p" + list[i]['id']).rearrange = true;
		
		//Keep track of ids for 'Tonight' and 'Favorites'
		if (list[i]['name'] == "Tonight") {
			pTonightID = list[i]['id'];
		}
		
		if (list[i]['name'] == "Favorites") {
			pFavoritesID = list[i]['id'];
		}
		
		//Store the playlist name in the DOM object
		document.getElementById("p" + list[i]['id']).name = list[i]['name'];
	}
	
	listCtrl.color();
	listCtrl.evtClick = m_populateSongs;
	listCtrl.evtDropInto = m_playlistDrop;
}

function m_populateSongs(playlist) {
	playlist = playlist.substring(1);
	
	//Hide
	if (songsList) {
		songsList.hide();
		
		if (songsList.playlist != "1") {
			songsList.destroy();
		}
	}
	
	//Hide "No Playlist" message, show waiting message
	document.getElementById("message").style.display = 'none';
	document.getElementById("msg_dosearch").style.display = 'none';
	document.getElementById("msg_error").style.display = 'none';
	document.getElementById("waiting").style.display = '';
	
	//If the list is already loaded, simply display it
	if ((playlist == "1") && (songsLists[playlist] != undefined)) {
		songsList = songsLists[playlist];
		songsList.show();
		document.getElementById("waiting").style.display = 'none';
	}
	
	//If it's the library, request that user do a search first
	else if (playlist == "1") {
		document.getElementById("waiting").style.display = 'none';
		document.getElementById("msg_dosearch").style.display = '';
	}
	
	else {
		//Playlist DOM object
		playlistObj = document.getElementById("p" + playlist);
		
		//Which columns are needed?
		var columns = Array("#", "Song Name", "Artist", "Album");
		
		if (playlistObj.name == "Tonight") {
			columns.push("Pitch");
			columns.push("Add to");
		}
		
		else if (playlistObj.name == "Favorites") {
			columns.push("Add to");
		}
		
		else {
			columns.push("Add to");
			columns.push("Add to");
		}
		
		songsList = new KJList();
		songsList.rearrange = playlistObj.rearrange;
		songsList.playlist = playlist;
		songsList.playlistName = playlistObj.name;
		songsList.create(document.getElementById("songs"));
		songsList.setColumns(columns, 0);
		songsList.evtDrop = m_songRearrange;
		songsLists[playlist] = songsList;
		getSongs(url_songs, m_songsLoaded, "playlist=" + playlist);
		
		if (false) {
			songsList.evtColClick = null;
		}
		else {
			songsList.evtColClick = m_sortSongs;
		}
	}
	
	m_sessionCheckin();
}

function m_songsLoaded(list) {
	//Hide waiting message
	document.getElementById("waiting").style.display = 'none';
	
	//Rearrangeable?
	if (songsList.rearrange)
		var rearrange = true;
	else
		var rearrange = false;
	
	for (var i = 0; i < list.length; i++) {
		//Add to favorites
		var favsButton = document.createElement("INPUT");
		favsButton.type = "button"
		favsButton.name = songsList.playlist + "-" + list[i]['itemId'];
		favsButton.songId = list[i]['id'];
		favsButton.value = "Favorites";
		favsButton.onmousedown = m_addToFavorites;
		
		//Add to Tonight
		var tonightButton = document.createElement("INPUT");
		tonightButton.type = "button";
		tonightButton.name = songsList.playlist + "-" + list[i]['itemId'];
		tonightButton.songId = list[i]['id']; 
		tonightButton.value = "Tonight";
		tonightButton.onmousedown = m_addToTonight;
	
		//Columns
		var columns = Array(i, list[i]['name'], list[i]['artist']);
		
		if (typeof(list[i]['album']) == "object") {
			var dropDown = document.createElement("SELECT");
			dropDown.name = list[i]['id'];
			
			for (var j = 0; j < list[i]['album']['items'].length; j++) {
				var optionObj = document.createElement("OPTION");
				optionObj.value = list[i]['album']['items'][j];
				optionObj.innerHTML = optionObj.value;
				
				if (list[i]['album']['default'] == j) {
					optionObj.selected = true;
				}
				
				dropDown.appendChild(optionObj);
			}
			
			columns.push(dropDown);
		}
		
		else {
			columns.push(list[i]['album']);
		}
		
		if (songsList.playlistName == "Tonight") {
			//Drop-down
			var options = {"+6" : 6, "+5" : 5, "+4" : 4, "+3" : 3, "+2" : 2, "+1" : 1, "0" : 0, "-1" : -1, "-2" : -2, "-3" : -3, "-4" : -4, "-5" : -5, "-6" : -6}
			var dropDown = document.createElement("SELECT");
			dropDown.name = list[i]['itemId'];
			dropDown.songId = list[i]['id'];
			dropDown.onchange = m_changePitch;
			for (option in options) {
				var optionObj = document.createElement("OPTION");
				optionObj.value = options[option];
				optionObj.innerHTML = option;
				
				if(list[i]['pitch'] == options[option]) {
					optionObj.selected = true;
				}
				
				dropDown.appendChild(optionObj);
			}
			
			columns.push(dropDown);
			columns.push(favsButton);
		}
		
		else if (songsList.playlistName == "Favorites") {
			columns.push(tonightButton);
		}
		
		else {
			columns.push(tonightButton);
			columns.push(favsButton);
		}
	
		index = songsList.addItem(songsList.playlist + "-" + list[i]['itemId'], columns, rearrange, null, false);
		
		document.getElementById(songsList.getByIndex(index)).songName	= list[i]['name'];
		document.getElementById(songsList.getByIndex(index)).songId		= list[i]['id'];
		document.getElementById(songsList.getByIndex(index)).piIx		= list[i]['itemId'];
	}
	
	songsList.color();
	
	if (songsList.playlistName != "Tonight" && songsList.playlistName != "History") {
		songsList.evtColClick = m_sortSongs;
	}
	else {
		songsList.evtColClick = null;
	}
}

function m_songRearrange(list, song, index, oldIndex) {
	sendData(url_rearrange, "playlist=" + list.playlist + "&index=" + index + "&oldIndex=" + oldIndex);
	m_sessionCheckin();
}

function m_playlistDrop(song, index, nodeId) {
	playlists	= document.getElementById("playlists").childNodes[0].parent;
	item		= songsList.getByID(song);
	
	if (songsLists[playlists.getByIndex(index).substring(1)]) { songsLists[playlists.getByIndex(index).substring(1)].destroy(); }
	songsLists[playlists.getByIndex(index).substring(1)] = null;
	sendData(url_drop, "playlist=" + playlists.getByIndex(index).substring(1) + "&song=" + songsList.getByID(song).songId);
	
	m_setStatus("Added \"" + item.songName + "\" to \"" + m_getPlaylistName(playlists.getByIndex(index)) + "\" ...");
	m_sessionCheckin();
}

function m_addToTonight(event) {
	playlists = document.getElementById("playlists").childNodes[0].parent;
	m_playlistDrop(event.target.name, playlists.getIndex("p" + pTonightID), event.target.name);
}

function m_addToFavorites(event) {
	playlists = document.getElementById("playlists").childNodes[0].parent
	m_playlistDrop(event.target.name, playlists.getIndex("p" + pFavoritesID), event.target.name);
}

function m_sortSongs(list, column) {
	list.removeAll();
	list.setPrimaryCol(column);
	document.getElementById("waiting").style.display = '';
	
	if (document.getElementById("searchfield").value) {
		search = "&search=" + document.getElementById("searchfield").value;
	}
	
	getSongs(url_sort, m_songsLoaded, "playlist=" + list.playlist + "&orderby=" + column + search);
	m_sessionCheckin();
}

function m_changePitch(event) {
	sendData(url_pitch, "song=" + event.target.songId + "&pitch=" + event.target.value);
	m_sessionCheckin();
}

function m_doSearch() {
	//Select "Library" in the side bar
	playlistsList.select(playlistsList.getByID(playlistsList.getByIndex(0)));
	
	//Remove old search list if it's there
	if (songsLists["1"]) {
		songsLists["1"].parentObj.removeChild(songsLists["1"].container);
		songsLists["1"].parentObj.removeChild(songsLists["1"].columnsObj);
	}
	
	//Hide currently loaded list
	if (songsList) {
		songsList.hide();
	}
	
	document.getElementById("msg_dosearch").style.display = 'none';
	document.getElementById("message").style.display = 'none';
	document.getElementById("msg_error").style.display = 'none';
	document.getElementById("waiting").style.display = '';

	var columns = Array("#", "Song Name", "Artist", "Album", "Add to", "Add to");
	
	songsList = new KJList();
	songsList.rearrange = false;
	songsList.playlist = "1";
	songsList.playlistName = "Library";
	songsList.create(document.getElementById("songs"));
	songsList.setColumns(columns, 1);
	songsLists["1"] = songsList;
	
	getSongs(url_search, m_songsLoaded, "search=" + document.getElementById("searchfield").value);
	m_sessionCheckin();
}

function searchKeyPressed(event) {
	if (event.keyCode == 13) {
		m_doSearch();
	}
}

function m_getSongName(id) {
	songObj = document.getElementById(id);
	return songObj.songName;
}

function m_getPlaylistName(id) {
	playlistObj = document.getElementById(id);
	return playlistObj.name;
}

function kj_data_error(id, desc) {
	songsLists[songsList.id] = null;
	songsList.hide();
	document.getElementById("waiting").style.display = 'none';
	
	document.getElementById("msg_error").innerHTML = "Error: " + desc
	document.getElementById("msg_error").style.display = '';
}

function m_setStatus(status, revert) {
	statusObj = document.getElementById("status");
	statusObj.innerHTML = "<span style='color:#FF0000'>" + status + "</span>";
	var newInnerHTML='statusObj.innerHTML = "<span style="color:#FF0000">" + status + "</span>"';
	blink();
	setTimeout("statusObj.innerHTML = newInnerHTML",1250);
}

function blink(){ 
blinkFlag = 1 
blinkCount = 1 
blinkTimer();
}

function blinkTimer(){ 
if(blinkFlag==1){
blinkSpeed=250
blinkNumber=10 // If this value is even, the text flashes then remains visible. If it is odd, it flashes, then disappears
blinkCount++; 
document.getElementById("status").style.visibility='visible'; 
}else {
blinkCount++; 
document.getElementById("status").style.visibility='hidden'; 
}
blinkFlag^=1;
if (blinkCount < blinkNumber) {setTimeout('blinkTimer()', blinkSpeed);} 
} 

function trapDelete() {
	//Checks to see if the key pressed is the Delete/Backspace key, and if so, stop it from getting to the browser.
   var keyID = (window.event) ? event.keyCode : e.keyCode;
   if (keyID==8 && !allowBackspace){
	  return false;
   }
   return true;
}

function m_keyPressed(event) {
	//This is not IE-compliant code.
	if (event.keyCode == 46 || event.keyCode == 8) {
		if (!songsList.selection) {
			return;
		}
	
		playlistName	= songsList.playlistName;

		if (playlistName != "Library" && playlistName != "History") {
			selection	= songsList.selection;
			item		= songsList.getByID(selection);
			
			songsList.select(null);
			sendData(url_remove, "playlist=" + songsList.playlist + "&piIx=" + item.piIx);
			m_setStatus("Removed \"" + item.songName + "\" ...");
			songsList.removeItem(selection);
		}
	}
}

function m_pingSession() {
	var elapsedTime = new Date().getTime()/1000.0 - lastPing;
	
	if (elapsedTime >= 5) {
		sendData(url_ping, "");
		m_sessionCheckin();
		lastPing = new Date().getTime()/1000.0;
	}
}

function m_sessionCheckin() {
	/* A true or false value is set by the server before delivering this script 
		depending on preference. */
	if (!{auto_logout}) {
		return;
	}
	
	if (timer == null) {
		timer = setInterval("m_sessionLogout()", {timeout} * 1000);
	}
	else {
		clearInterval(timer);
		timer = null;
		return m_sessionCheckin();
	}
}

function m_sessionLogout() {
	// Sets the flag to tell confirmUnload subroutine that it is OK to let the user leave this page (close the Window, etc.)
	allowUnload = true;
	window.location=url_login;
}
function loaded() {
	document.addEventListener('touchmove', function(e){ e.preventDefault(); });
	myScroll = new iScroll('songs');
}
//document.addEventListener('DOMContentLoaded', loaded);