/**
 * onArcade 2.0.0
 * Copyright © 2006-2007 Hans Mäesalu & Eveterm OÜ, All Rights Reserved
 **/

var xmlHttp = xmlHttpRequest();

function report_broken_submit(file_id) {
  if (xmlHttp == null) {
    return;
  }
  xmlHttp.open('POST', siteurl + '/file.php?a=report_broken&f=' + file_id, true);
  xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("report_broken").innerHTML = xmlHttp.responseText;
    } 
  }
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.send('comment=' + escape(document.getElementById("report_broken_comment").value));
}

function make_favourite(file_id, favourite_action) {
  if (xmlHttp == null) {
    return;
  }
  if (favourite_action == 'make') {
	xmlHttp.open('GET', siteurl + '/file.php?a=make_favourite&f=' + file_id, true);
	document.getElementById("favourite_message").style.display = "block";
  } else {
    xmlHttp.open('GET', siteurl + '/file.php?a=remove_favourite&f=' + file_id, true);
	document.getElementById("favourite_message").style.display = "none";
  }
  xmlHttp.onreadystatechange = function () {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("make_favourite").innerHTML = xmlHttp.responseText;
    } 
};
  xmlHttp.send(null);
}

function edit_score_comment(score_id) {
  if (xmlHttp == null) {
    return;
  }
  xmlHttp.open('POST', siteurl + '/scores.php?a=edit_comment&s=' + score_id, true);
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
      document.getElementById("edit_comment_" + score_id).innerHTML = xmlHttp.responseText;
    } 
  }
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.send('edit_comment=' + escape(document.getElementById("edit_score_comment").value));
}

function display_comments(file_id, comment_page) {
  if (xmlHttp == null) {
    return;
  }
  
  xmlHttp.open('GET', siteurl + '/file.php?f=' + file_id +'&a=comments&p=' + comment_page, true);
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("file_comments").innerHTML = xmlHttp.responseText;
    } 
  }
  xmlHttp.send(null);
}

function display_shouts() {
  if (xmlHttp == null) {
    return;
  }
  xmlHttp.open('GET', siteurl + '/shout.php?a=shouts', true);
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("shouts").innerHTML = xmlHttp.responseText;
    } 
  }
  xmlHttp.send(null);
}

function submit_comment(file_id) {
  if (xmlHttp == null) {
    return;
  }
  xmlHttp.open('POST', siteurl + '/file.php?a=submit_comment&f=' + file_id, true);
  xmlHttp.onreadystatechange = function() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("comment_error").innerHTML = xmlHttp.responseText;
      display_comments(file_id, 1);
      document.getElementById("comment_message").value = '';
    } 
  }
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.send('message=' + escape(document.getElementById("comment_message").value));
}

function submit_shout() {
  if (xmlHttp == null) {
	return;
  }
  xmlHttp.open('POST', siteurl + '/shout.php', true);
  xmlHttp.onreadystatechange = function() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("shout_error").innerHTML = xmlHttp.responseText;
	  if (document.getElementById("shout_message").value != '') {
      	display_shouts();
	  }
      document.getElementById("shout_message").value = '';
    } 
  }
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.send('shout_message=' + escape(document.getElementById("shout_message").value));
}

function rate_file(file_id) {
  if (xmlHttp == null) {
    return;
  }
  
  xmlHttp.open('GET', siteurl + '/file.php?f=' + file_id +'&a=rate&r=' + document.getElementById("file_rating_select").value, true);
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
      document.getElementById("rate_file").innerHTML = thank_you;
      document.getElementById("file_rating").src = siteurl + '/images/stars' + xmlHttp.responseText + '.gif';
    } 
  }
  xmlHttp.send(null);
}

function xmlHttpRequest() {
  var xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}