var isNN = (navigator.appName.indexOf('Netscape') != -1);

function autoTab(input, len, e)
{
   var keyCode = isNN ? e.which : e.keyCode;
   var filter = isNN ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

   if (input.value.length >= len && !containsElement(filter, keyCode))
   {
      input.value = input.value.slice(0, len);
      input.form[(getIndex(input) + 1) % input.form.length].focus();
   }

   return true;
}

function containsElement(arr, ele)
{
   var found = false, index = 0;

   while(!found && index < arr.length)
      if (arr[index] == ele)
         found = true;
      else
         index++;

   return found;
}
   
function getIndex(input)
{
   var index = -1, i = 0, found = false;

   while (i < input.form.length && index == -1)
      if (input.form[i] == input)
         index = i;
      else
         i++;

   return index;
}

function getArrayIndexBySelectedMessageId(messages, id)
{
   for (var i = 0; i < messages.length; i++)
      if (messages[i][0] == id)
         return i;
   return -1;
}

function getRateIcons(table, id, grade)
{
   return '<a href="#" onmouseover="window.status=\'Rate message\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickRateIcon(\'' + table + '\', ' + id + ', ' + 5 + ');"><img src="images/smiley'+(grade > 0 ? '' : '_dim')+'.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Below average"></a>' +
   '<a href="#" onmouseover="window.status=\'Rate message\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickRateIcon(\'' + table + '\', ' + id + ', ' + 4 + ');"><img src="images/smiley'+(grade > 1 ? '' : '_dim')+'.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Average"></a>' +
   '<a href="#" onmouseover="window.status=\'Rate message\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickRateIcon(\'' + table + '\', ' + id + ', ' + 3 + ');"><img src="images/smiley'+(grade > 2 ? '' : '_dim')+'.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Above average"></a>' +
   '<a href="#" onmouseover="window.status=\'Rate message\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickRateIcon(\'' + table + '\', ' + id + ', ' + 2 + ');"><img src="images/smiley'+(grade > 3 ? '' : '_dim')+'.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Excellent"></a>' +
   '<a href="#" onmouseover="window.status=\'Rate message\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickRateIcon(\'' + table + '\', ' + id + ', ' + 1 + ');"><img src="images/smiley'+(grade > 4 ? '' : '_dim')+'.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Superior"></a>';
}

function buildMessageTable(table, logged_in)
{
   var tr, td, messages;
   messages = (table == 'pop_messages') ? pop_messages : new_messages;
   if (messages.length > 0)
      for (var i = 0; i < messages.length; i++)
      {
         tr = document.getElementById(table).insertRow(-1);
         tr.style.backgroundColor = '#' + (i % 2 ? 'eef1f5' : 'f8fafd');
         td = document.createElement('TD');
         td.setAttribute('width', '127');
         td.setAttribute('height', '18');
         td.style.paddingLeft = '5px';
         td.innerHTML = '<a href="javascript:;" onmouseover="window.status=\'Listen to: ' + messages[i][1] + '\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return playMessage(\'' + table + '\', ' + messages[i][0] + ');" class="links4" title="' + messages[i][1] + '">' + messages[i][1].substring(0, 15) + (messages[i][1].length > 15 ? '...' : '') + '</a>';
         tr.appendChild(td);
         td = document.createElement('TD');
         td.id = table + '_rate_icons' + messages[i][0] + '_id';
         td.align = 'right';
         td.setAttribute('width', '74');
         td.style.paddingRight = '6px';
         td.innerHTML = getRateIcons(table, messages[i][0], messages[i][2]);
         tr.appendChild(td);
         td = document.createElement('TD');
         td.align = 'right';
         td.setAttribute('width', '40');
         td.className = 'text2';
         td.style.paddingRight = '6px';
         td.innerHTML = messages[i][3];
         tr.appendChild(td);
         td = document.createElement('TD');
         td.align = 'right';
         td.setAttribute('width', '40');
         td.className = 'text2';
         td.style.paddingRight = '5px';
         td.innerHTML = messages[i][4];
         tr.appendChild(td);
         td = document.createElement('TD');
         td.id = table + '_save_icon' + messages[i][0] + '_id';
         td.align = 'center';
         td.setAttribute('width', '18');
         td.innerHTML = logged_in ? '<a href="#" onmouseover="window.status=\'Jesus saves\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickSaveIcon(\'' + table + '\', ' + messages[i][0] + ');"><img src="images/jesus.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Save this message"></a>' : '&nbsp;';
         tr.appendChild(td);
         td = document.createElement('TD');
         td.align = 'center';
         td.setAttribute('width', '18');
         td.innerHTML = '<a href="messages.php?a=' + messages[i][6] + '"><img src="images/maker.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Hear more messages by this person"></a>';
         tr.appendChild(td);
         td = document.createElement('TD');
         td.id = table + '_block_icon' + messages[i][0] + '_id';
         td.align = 'center';
         td.setAttribute('width', '18');
         td.style.paddingRight = '3px';
         td.innerHTML = '<a href="#" onmouseover="window.status=\'Report this message as inappropriate for all audiences\'; return true;" onmouseout="window.status=\' \'; return true;" onclick="return clickBlockIcon(\'' + table + '\', ' + messages[i][0] + ');"><img src="images/block.gif" width="14" height="14" border="0" align="middle" vspace="0" title="Report this message as inappropriate for all audiences"></a>';
         tr.appendChild(td);
      }
   else
   {
      tr = document.getElementById(table).insertRow(0);
      td = document.createElement('TD');
      td.setAttribute('height', '18');
      td.style.backgroundColor = '#fefef2';
      td.style.paddingLeft = '5px';
      td.className = 'text2';
      td.innerHTML = 'No messages are currently available.';
      tr.appendChild(td);
   }
}

function showWimpy()
{
   var messages = pop_messages.length > 0 ? pop_messages : new_messages;
   var playlist = 'server/playlist.php%3Fname%3D' + (messages.length > 0 ? escape(messages[0][1]) : '') + '%26location%3D' + (messages.length > 0 ? escape(messages[0][5].replace(':', ';')) : '') + '%26j%3Dj.xml';
   document.getElementById('wimpy').innerHTML = makeWimpyPlayer(playlist, 'scripts/skin_bar.xml', '170', '16', false, false);
}

function playMessage(table, id)
{
   var messages = table == 'pop_messages' ? pop_messages : new_messages;
   var id = getArrayIndexBySelectedMessageId(messages, id);
   message_name = messages[id][1];
   message_location = messages[id][5];
   if (flash_enabled)
   {
      flashProxy.call('js_wimpy_stop');
      window.setTimeout("flashProxy.call('js_wimpy_appendPlaylist', 'track', message_location, '', message_name, '', '');", 400);
      window.setTimeout("flashProxy.call('js_wimpy_next');", 500);
   }
   else
      location.href = message_location;
   return false;
}

function clickRateIcon(table, id, rating)
{
   document.getElementById(table + '_rate_icons' + id + '_id').innerHTML = '<div style="padding-right:1px"><img src="images/check_mark.gif" width="13" height="14" align="top" vspace="0"></div>';
   request('action=rate_message&id=' + id + '&rating=' + rating, 'Rating message', NORMAL_WAITING_TIME);
   return false;
}

function clickSaveIcon(table, id)
{
   document.getElementById(table + '_save_icon' + id + '_id').innerHTML = '<img src="images/check_mark.gif" width="13" height="14" align="top" vspace="0">';
   request('action=save_message&id=' + id, 'Saving message', NORMAL_WAITING_TIME);
   return false;
}

function clickBlockIcon(table, id)
{
   document.getElementById(table + '_block_icon' + id + '_id').innerHTML = '<img src="images/check_mark.gif" width="13" height="14" align="top" vspace="0">';
   request('action=block_message&id=' + id, 'Reporting message', NORMAL_WAITING_TIME);
   return false;
}
