onerror=handleErr;

var playing = 0;
var maxch   = 0;
var PlayDirectChannel_buffer = "0";
var timeoutID = 0;
var prev = "";
var playerState = "playing";


function handleErr()
{
	var ua = navigator.userAgent;
  if (ua.indexOf('Celrun') != -1)
  {
    document.location.href='error.html';
  }
  else
  {
    document.location.href='error.php';
  }
}

function hideMouse()
{
  var mse = new ActiveXObject("TVerCheck.TVersionCheck.1");
  mse.hideMouse();
}

function createCookie(name, value, days)
{
  if (days)
  {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else
    var expires = "";

  document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name)
{
  createCookie(name, "", -1);
}

function init()
{
  hideMouse();
  eraseCookie("macaddr");
  createCookie("macaddr", BOXINFOOBJ.MacAddress, 30);
}

function playChannel()
{
//alert(PlayDirectChannel_buffer);
  window.clearTimeout(timeoutID);

	if (PlayDirectChannel_buffer > maxch)
	{
	  PlayDirectChannel_buffer = "0";
	  return false;
	}

  playing = PlayDirectChannel_buffer;
  PlayDirectChannel_buffer = "0";

  init();

  if (prev == "esttv") 
  {
    document.location.href = "chest-xx.php?ch=" + playing;
  }
  else if (prev == "psttv") 
  {
    document.location.href = "chpst-xx.php?ch=" + playing;
  }
  else
  {
    document.location.href = "ch-xx.php?ch=" + playing;
  }
}

function PlayDirectChannel(id)
{
	if(PlayDirectChannel_buffer == "0" && id == 0)
	{
		return false;
	}

	if(PlayDirectChannel_buffer == "0")
	{
		PlayDirectChannel_buffer = id;
	}
	else
	{
		PlayDirectChannel_buffer = PlayDirectChannel_buffer + "" + id;
	}

	timeoutID = window.setTimeout("playChannel()", 2000);
}

function nextChannel(diff)
{
  playing = playing+diff;

  if (playing < 1)
    playing = maxch;

  if (playing > maxch)
    playing = 1;

  init();
  
  if (prev == "esttv") 
  {
    document.location.href = "chest-xx.php?ch=" + playing;
  }
  else if (prev == "psttv") 
  {
    document.location.href = "chpst-xx.php?ch=" + playing;
  }
  else
  {
    document.location.href = "ch-xx.php?ch=" + playing;
  }
}

function gotoPage(page)
{
	init();
//  document.location.href = "indexh.php";
  document.location.href = page;
}

function keyhandler()
{
	playing = parseInt(document.getElementById('playing_ch').value);
	maxch   = parseInt(document.getElementById('max_ch').value);
	prev    = document.getElementById('previous').value;

  var kc = event.keyCode;

//alert(kc);

  switch(kc)
  {
    // buttons from 1-9
    case 48:
      PlayDirectChannel(kc - 48);
      break;
    case 49:
      PlayDirectChannel(kc - 48);
      break;
    case 50:
      PlayDirectChannel(kc - 48);
      break;
    case 51:
      PlayDirectChannel(kc - 48);
      break;
    case 52:
      PlayDirectChannel(kc - 48);
      break;
    case 53:
      PlayDirectChannel(kc - 48);
      break;
    case 54:
      PlayDirectChannel(kc - 48);
      break;
    case 55:
      PlayDirectChannel(kc - 48);
      break;
    case 56:
      PlayDirectChannel(kc - 48);
      break;
    case 57:
      PlayDirectChannel(kc - 48);
      break;

    case 129: //channel up key
      nextChannel(1);
      break;
    case 130: //channel down key
      nextChannel(-1);
      break;

    case 9:   //EPG key
      if (prev == "livetv") gotoPage("livetvh.php");
      if (prev == "esttv") gotoPage("esttvh.php");
      if (prev == "psttv") gotoPage("psttvh.php");
      break;

    case 131: //play-pause key
/*
      if (playerState == "playing")
      {
        document.getElementById("MediaPlayer").pause();
        playerState = "paused";
      }
      if (playerState == "paused")
      {
        document.getElementById("MediaPlayer").play();
        playerState = "playing";
      }
*/
      break;
    case 132: //stop key
      gotoPage("indexh.php");
      break;
//    case 133: //rw key
//    case 134: //ff key


/*
    case 27: //escape key (on PC keyboard)
      window.location = './?f=Home';
      break;
//    case 199: //play key

    case 107: //channel up key
      PlayChannelUp();
      break;
    case 109: //channel down key
      PlayChannelDown();
      break;
*/

  }
}

function updateClock()
{
  var currentTime = new Date();

  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();
  var currentDay = currentTime.getDate();
  var currentMonth = currentTime.getMonth();
  var mnth = "";
  
  if (currentMonth == 0) mnth = "Jan";
  if (currentMonth == 1) mnth = "Feb";
  if (currentMonth == 2) mnth = "Mar";
  if (currentMonth == 3) mnth = "Apr";
  if (currentMonth == 4) mnth = "Maj";
  if (currentMonth == 5) mnth = "Jun";
  if (currentMonth == 6) mnth = "Jul";
  if (currentMonth == 7) mnth = "Avg";
  if (currentMonth == 8) mnth = "Sep";
  if (currentMonth == 9) mnth = "Okt";
  if (currentMonth == 10) mnth = "Nov";
  if (currentMonth == 11) mnth = "Dec";

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
//  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
  var currentTimeString = currentDay + ". " + mnth + "&nbsp;&nbsp;&nbsp;" + currentHours + ":" + currentMinutes + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").innerHTML = currentTimeString;
}


