function previewReview() {
  document.getElementById('p_title').innerHTML = document.getElementById('title').value;
  var rating = getRating();
  document.getElementById('p_rating').innerHTML = rating;
  document.getElementById('p_game').innerHTML = document.getElementById('game').value;
  var body = document.getElementById('body').value;
  var game_site = document.getElementById('game_site').value;
  var publisher = document.getElementById('publisher').value;
  var publisher_site = document.getElementById('publisher_site').value;
  while (body.indexOf('\n') > -1) {
    body = body.replace('\n','<br />');
  }
  var gr = getRatingByType('gr') * 10;
  var mu = getRatingByType('mu') * 10;
  var se = getRatingByType('se') * 10;
  var cr = getRatingByType('cr') * 10;
  var lc = getRatingByType('lc') * 10;
  var ct = getRatingByType('ct') * 10;
  var cm = getRatingByType('cm') * 10;
  var ov = getRating() * 10;
  full_body  = '<div id="p_infoBox">Website: <a href="'+game_site+'" class="miscLink" target="_new"><b>Click Here</b></a><br />';
  full_body += 'Publisher: <a href="'+publisher_site+'" class="miscLink" target="_new"><b>'+publisher+'</b></a>';
  full_body += '<hr />';
  full_body += 'Graphics: <div class="ratingBar"><div class="ratingSlider" style="width: '+gr+'%;"></div></div><br />';
  full_body += 'Music: <div class="ratingBar"><div class="ratingSlider" style="width: '+mu+'%;"></div></div><br />';
  full_body += 'Sound Effects: <div class="ratingBar"><div class="ratingSlider" style="width: '+se+'%;"></div></div><br />';
  full_body += 'Controls: <div class="ratingBar"><div class="ratingSlider" style="width: '+cr+'%;"></div></div><br />';
  full_body += 'Learning Curve: <div class="ratingBar"><div class="ratingSlider" style="width: '+lc+'%;"></div></div><br />';
  full_body += 'Content: <div class="ratingBar"><div class="ratingSlider" style="width: '+ct+'%;"></div></div><br />';
  full_body += 'Community: <div class="ratingBar"><div class="ratingSlider" style="width: '+cm+'%;"></div></div><br />';
  full_body += '<hr />Overall Rating: <div class="ratingBar"><div class="ratingSlider" style="width: '+ov+'%;"></div></div>';
  full_body += '</div>'+body;

  document.getElementById('p_body').innerHTML = full_body;
  document.getElementById('preview_panel').style.display = 'block';
  document.getElementById('review_panel').style.display = 'none';
}

function showReview() {
  document.getElementById('preview_panel').style.display = 'none';
  document.getElementById('review_panel').style.display = 'block';
}

function getRating() {
  var total = 0;
  total += getRatingByType('gr');
  total += getRatingByType('mu');
  total += getRatingByType('se');
  total += getRatingByType('cr');
  total += getRatingByType('lc');
  total += getRatingByType('ct');
  total += getRatingByType('cm');
  var avg = Math.round(10*(total / 7)) / 10;
  return avg;
}

function getRatingByType(type) {
  var total = 0;
  for (var i=1,n=10;i<=n;i++) {
    var e = document.getElementById(type+'_rating'+i);
    total += (e.checked == true) ? parseInt(e.value) : 0;
  }
  return total;
}

// Show a popup
function showPopup(i,n,c) {
  // YEAAAAAAA
  var cDiv = document.getElementById('popup_container'); // container div
  var bgDiv = document.getElementById('bg_fade'); // bg div

  // Some math to get the thing in the middle of the window
  bgDiv.style.display = 'block';
  cDiv.style.display = 'block';
  // I hate IE
  if (navigator.appName == 'Microsoft Internet Explorer') {
    cDiv.style.position = 'absolute';
    bgDiv.style.position = 'absolute';
    bgDiv.style.top = document.documentElement.scrollTop + 'px';
    cDiv.style.top = (document.documentElement.scrollTop + 100) + 'px';
    cDiv.style.left = ((document.body.scrollWidth / 2) - (750 / 2)) + 'px';
  } else {
    bgDiv.style.position = 'fixed';
    cDiv.style.position = 'fixed';
    cDiv.style.top = ((window.innerHeight - 700) / 2 + document.body.scrollTop) + 'px';
    cDiv.style.left = ((window.innerWidth / 2) - (750 / 2)) + 'px';
  }
  var inBody = document.getElementById('popup_inner_body');
  inBody.innerHTML = '<img src="/images/'+i+'/'+n+'.jpg" alt="'+c+'" style="width: 720px; height: 540px; border: 1px solid black;" />';
  inBody.innerHTML += '<div style="font-family: Arial, sans-serif; text-align: center; width: 100%; font-weight: bold; font-size: 16px; padding-top: 10px;">'+c+'</div>';
}

// Hide de popup.
function closePopup() {
  var cDiv = document.getElementById('popup_container'); // container div
  var bgDiv = document.getElementById('bg_fade'); // bg div

  bgDiv.style.display = 'none';
  cDiv.style.display = 'none';
}

// I hate IE.  Did I mention that?
if (navigator.appName == 'Microsoft Internet Explorer') {
  window.onscroll = function () {
    var cDiv = document.getElementById('popup_container'); // container div
    var bgDiv = document.getElementById('bg_fade'); // bg div
    if (cDiv != null && bgDiv != null) {
      bgDiv.style.top = document.documentElement.scrollTop + 'px';
      cDiv.style.top = (document.documentElement.scrollTop + 100) + 'px';
    }
  }
}

