function get_screen_width()
{
    return screen.width;
}

function get_screen_height()
{
    return screen.height;
}

function get_window_width()
{
    /*
     * See also:
     *    http://www.quirksmode.org/dom/w3c_cssom.html
     *    http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
     */
    var width = 0;
    if ( window.innerWidth != null) {
	width = window.innerWidth;
    } else if (document.documentElement.clientWidth) {
	width = document.documentElement.clientWidth;
    } else if (document.body.clientWidth != null) {
	width = document.body.clientWidth;
    }
    return width;
}
function get_window_height()
{
    /*
     * See also:
     *    http://www.quirksmode.org/dom/w3c_cssom.html
     *    http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
     */
    var height = 0;
    if ( window.innerHeight != null) {
	height = window.innerHeight;
    } else if (document.documentElement.clientHeight) {
	height = document.documentElement.clientHeight;
    } else if (document.body.clientHeight != null) {
	height = document.body.clientHeight;
    }
    return height;
}

function update_width(form)
{
    if ( form ) {
	form.screenx.value = get_screen_width();
	form.windowx.value = get_window_width();
	form.screeny.value = get_screen_height();
	form.windowy.value = get_window_height();
    }
}


// function resizeLink()
// {
//   obj = document.getElementById('viewlink');
//   if (obj) {
//     obj.href = obj.href + '&screenx='+get_screen_width() + '&windowx=' + get_window_width();
//   }
// }

function getElementByName(target)
{
    if( document.getElementById ) {
	element = document.getElementById(target);
    } else if( document.all ) {
	element = document.all[target];
    } else if( document.layers ) {
	element = document.layers[target];
    }
    return element;
}

function decToHex(dec)
{
  var hexStr = "0123456789ABCDEF";
  var low = dec % 16;
  var high = (dec - low)/16;
  hex = "" + hexStr.charAt(high) + hexStr.charAt(low);
  return hex;
}

function SecToTime(seconds)
{
  var hours = Math.floor(seconds/3600);
  var seconds = seconds%3600;
  var minutes = Math.floor(seconds/60);
  seconds = Math.round(seconds%60);

  if (hours) {
    time = hours + " " + (hours == 1 ? stringHour : stringHours) + ", ";
    time += minutes + " " + (minutes == 1 ? stringMinute : stringMinutes) + ", ";
    time += seconds + " " + (seconds == 1 ? stringSecond : stringSeconds);
  } else if (minutes) {
    time = minutes + " " + (minutes == 1 ? stringMinute : stringMinutes) + ", ";
    time += seconds + " " + (seconds == 1 ? stringSecond : stringSeconds);
  } else {
    time = seconds + " " + (seconds == 1 ? stringSecond : stringSeconds);
  }
  return time;
}

function MillisToDate(millis)
{
  var date = new Date(millis);

  var year = date.getFullYear();

  var month = date.getMonth() + 1;
  if (month < 10)
    month = "0" + month;

  var day = date.getDate();
  if (day < 10)
    day = "0" + day;

  var hours = date.getHours();
  if (hours < 10)
    hours = "0" + hours;

  var minutes = date.getMinutes();
  if (minutes < 10)
    minutes = "0" + minutes;

  var seconds = date.getSeconds();
  if (seconds < 10)
    seconds = "0" + seconds;

  return year  + "-" + month   + "-" + day + "_" +
    hours + "-" + minutes + "-" + seconds;
}

function getXMLHTTPRequest() {
  try {
    req = new XMLHttpRequest();
  } catch(err1) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (err2) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (err3) {
        req = false;
      } 
    } 
  }
  return req;
}

function useHttpResponse_startul(http_obj) {
  if (http_obj.readyState == 4) {
    if (http_obj.status == 200) { 
      // TODO: check response?
    }
  }
}

function getCurrentSec() {
  var da = new Date();
  currtime_sec = Math.round(da.getTime()/1000);
  return currtime_sec;
}

function confirmAnchor(url,message)
{
  if (!confirm(message))
    return;
  location.href=url;
}

function printError(errors)
{
  var contents = document.getElementById('contents');
  var doFade = false;

  if (document.getElementById('feedback-frame')) {
    contents.removeChild(document.getElementById('feedback-frame'));
    doFade = true;
  } else {
    var fadescript = document.createElement('script');
    fadescript.setAttribute('src', '/scripts/fade.js');
    fadescript.setAttribute('type', 'text/javascript');
    fadescript.setAttribute('language', 'javascript');
    fadescript.setAttribute('id', 'fadescript');
    contents.appendChild(fadescript);

    // This is for IE to excecute the fade after the script is loaded
    fadescript.onreadystatechange = function() {
      if (fadescript.readyState == 'loaded') {
        fadeColor("feedback-text","#FFFFFF","#FF3333",2,10,"c");
        fadeColor("feedback-text","#FF3333","#FFFFFF",2,10,"b");
      }
    }

    // This is for all other browsers to fade after script is loaded
    fadescript.onload = function() {
      fadeColor("feedback-text","#FFFFFF","#FF3333",2,10,"c");
      fadeColor("feedback-text","#FF3333","#FFFFFF",2,10,"b");
    }
  }

  var feedback_frame = document.createElement('div');
  feedback_frame.setAttribute('id', 'feedback-frame');

  var feedback_text = document.createElement('p');
  feedback_text.setAttribute('id', 'feedback-text');
  feedback_text.setAttribute('style', 'color: rgb(255, 51, 51); background-color: rgb(255, 255, 255);');
  feedback_text.innerHTML = 'Failed';

  var messages = document.createElement('ul');

  contents.insertBefore(feedback_frame, contents.firstChild);
  feedback_frame.appendChild(feedback_text);
  feedback_frame.appendChild(messages);

  for (var i = 0; i < errors.length; i++) {
    var message = document.createElement('li');
    message.innerHTML = errors[i];
    messages.appendChild(message);
  }

  if (doFade) {
    fadeColor("feedback-text","#FFFFFF","#FF3333",2,10,'c');
    fadeColor("feedback-text","#FF3333","#FFFFFF",2,10,'b');
  }
}
