function $(id) {
  return document.getElementById(id);
}

function get_XHR()	{ 
  if(window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    alert("Votre navigateur ne supporte pas la technologie AJAX(XMLHttpRequest).");
    return null;
  }
}

// ********************
// HOME
// ********************

function change_news(offset) {
  var xhr = get_XHR();
  var data = "offset="+offset+"&category_filter="+$("category_filter").value+"&popular_filter="+$("popular_filter").checked+"&news_search="+$("news_search").value;
  xhr.open("post", "pages/news_home.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("news_page").innerHTML = xhr.responseText;
    }
  };
}

// ********************
// MORNING
// ********************

function change_morning_comments(offset) {
  var xhr = get_XHR();
  var data = "morning_id="+$("morning_id").value+"&offset="+offset;
  xhr.open("post", "pages/morning_comments.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("morning_comments").innerHTML = xhr.responseText;
    }
  };
}

function add_morning_comment() {
  var xhr = get_XHR();
  var data = $('add_morning_comment_form').serialize();
  xhr.open("post", "pages/add_morning_comment.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  $('add_morning_comment_form').innerHTML = '';
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      change_morning_comments(0);
      window.location.href="#Koommentaires";
    }
  };
}

function edit_morning_comment(comment_id) {
  var xhr = get_XHR();
  var data = "comment_id="+comment_id+"&"+$('edit_morning_comment_form-'+comment_id).serialize();
  xhr.open("post", "pages/edit_morning_comment.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      if (xhr.responseText != '') {
	$('morning_comment_text-'+comment_id).innerHTML = xhr.responseText;
      }
    }
  };
}

function delete_morning_comment(comment_id) {
  var xhr = get_XHR();
  var data = "comment_id="+comment_id;
  xhr.open("post", "pages/delete_morning_comment.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      change_morning_comments(0);
      window.location.href="#Koommentaires";
    }
  };
}

// ********************
// NEWS
// ********************

function change_news_comments(offset) {
  var xhr = get_XHR();
  var data = "news_id="+$('news_id').value+"&offset="+offset;
  xhr.open("post", "pages/news_comments.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("news_comments").innerHTML = xhr.responseText;
    }
  };
}

function add_news_comment() {
  var xhr = get_XHR();
  var data = "news_id="+$('news_id').value+"&"+$('add_news_comment_form').serialize();
  xhr.open("post", "pages/add_news_comment.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  $('textarea-0').value = '';
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      change_news_comments(0);
      window.location.href="#Koommentaires";
    }
  };
}

function edit_news_comment(comment_id) {
  var xhr = get_XHR();
  var data = "comment_id="+comment_id+"&"+$('edit_news_comment_form-'+comment_id).serialize();
  xhr.open("post", "pages/edit_news_comment.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      if (xhr.responseText != '') {
	$('news_comment_text-'+comment_id).innerHTML = xhr.responseText;
      }
    }
  };
}

function delete_news_comment(comment_id) {
  var xhr = get_XHR();
  var data = "comment_id="+comment_id;
  xhr.open("post", "pages/delete_news_comment.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      change_news_comments(0);
      window.location.href="#Koommentaires";
    }
  };
}

function vote_news() {
  var xhr = get_XHR();
  var data = "news_id="+$('news_id').value;
  xhr.open("post", "pages/vote_news.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      if (xhr.responseText != '') {
	$('vote_news').innerHTML = xhr.responseText;
      }
    }
  };
}

function up_news(news_id) {
  var xhr = get_XHR();
  var data = "news_id="+news_id;
  xhr.open("post", "pages/up_news.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      change_news(0);
    }
  };
}

// ********************
// MEMBER
// ********************

function friends() {
  var xhr = get_XHR();
  var data = "member_id="+$('member_id').value;
  xhr.open("post", "pages/friends.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $('friends_xhr').innerHTML = xhr.responseText;
    }
  };
}

function friends_pending_to() {
  var xhr = get_XHR();
  xhr.open("post", "pages/friends_pending_to.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send();
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $('friends_pending_to_xhr').innerHTML = xhr.responseText;
    }
  };
}

function friends_pending_from() {
  var xhr = get_XHR();
  xhr.open("post", "pages/friends_pending_from.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send();
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $('friends_pending_from_xhr').innerHTML = xhr.responseText;
    }
  };
}

function accept_friendship(member_id) {
  var xhr = get_XHR();
  var data = "member_id="+member_id;
  xhr.open("post", "pages/accept_friendship.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      friends_pending_to();
      friends();
    }
  };
}

function delete_friendship(member_id) {
  var xhr = get_XHR();
  var data = "member_id="+member_id;
  xhr.open("post", "pages/delete_friendship.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      friends();
    }
  };
}

function delete_friend_pending_to(member_id) {
  var xhr = get_XHR();
  var data = "member_id="+member_id;
  xhr.open("post", "pages/delete_friendship.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      friends_pending_to();
    }
  };
}

function delete_friend_pending_from(member_id) {
  var xhr = get_XHR();
  var data = "member_id="+member_id;
  xhr.open("post", "pages/delete_friendship.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      friends_pending_from();
    }
  };
}

function news_member(offset) {
  var xhr = get_XHR();
  var data = "member_id="+$('member_id').value+"&offset="+offset;
  xhr.open("post", "pages/news_member.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("news_member").innerHTML = xhr.responseText;
    }
  };
}

// ********************
// PRIVATE MESSAGES
// ********************

function private_messages(offset) {
  var xhr = get_XHR();
  var data = "offset="+offset;
  xhr.open("post", "pages/private_messages.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("private_messages").innerHTML = xhr.responseText;
    }
  };
}

function delete_private_message(pm_id) {
  var xhr = get_XHR();
  var data = "pm_id="+pm_id;
  xhr.open("post", "pages/delete_private_message.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      private_messages(0);
    }
  };
}

function display_private_message(id) {
  if ($("pm_text-"+id).style.display == "none") {
    $("pm_text-"+id).style.display = "block";
  } else {
    $("pm_text-"+id).style.display = "none";
  }
}

// ********************
// CHAT
// ********************

function chat() {
  chat_messages();
  chat_online();
}

function chat_messages() {
  var xhr = get_XHR();
  xhr.open("post", "pages/chat_messages.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send();
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("chat_messages").innerHTML = xhr.responseText;
    }
  };
}

var last_action = 0;
function add_chat_message() {
  if ((new Date()).getTime() > last_action + 5000) {
    var xhr = get_XHR();
    var data = $('add_chat_message_form').serialize();
    xhr.open("post", "pages/add_chat_message.php", true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send(data);
    $('add_chat_message_form').reset();
    last_action = (new Date()).getTime();
    xhr.onreadystatechange = function () {
      if(xhr.readyState == 4 && xhr.status == 200) {
	chat_messages();
      }
    };
  } else {
    alert('Flood...');
  }
}

function delete_chat_message(message_id) {
  var xhr = get_XHR();
  var data = "message_id="+message_id;
  xhr.open("post", "pages/delete_chat_message.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      chat_messages();
    }
  };
}

function chat_online() {
  var xhr = get_XHR();
  xhr.open("post", "pages/chat_online.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send();
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $("chat_online").innerHTML = xhr.responseText;
    }
  };
}

function edit_chat_status() {
  var xhr = get_XHR();
  var data = "chat_status="+$("chat_status").value;
  xhr.open("post", "pages/edit_chat_status.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      chat_online();
    }
  };
}

function add_smiley(id, smiley) {
  var textarea = $('textarea-'+id);
  var old_value = textarea.value;
  textarea.value = old_value.substring(0, textarea.selectionStart)+smiley+old_value.substring(textarea.selectionEnd, textarea.textLength);
  textarea.setSelectionRange(textarea.selectionStart + smiley.length, textarea.selectionStart + smiley.length);
  textarea.focus();
}

function add_chat_action(action, to_id) {
  var xhr = get_XHR();
  var data = "action="+action+"&to_id="+to_id;
  xhr.open("post", "pages/add_chat_action.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      chat_messages();
    }
  };
}

function action_box(id) {
  if ($("action_box-"+id).style.display == "none") {
    $("action_box-"+id).style.display = "block";
  } else {
    $("action_box-"+id).style.display = "none";
  }
}

function chat_message_to(pseudo) {
  $('textarea-0').value = pseudo+'> '+$('textarea-0').value;
  $('textarea-0').focus();
}

function chat_spoil(message_id) {
  var xhr = get_XHR();
  var data = "message_id="+message_id;
  xhr.open("post", "pages/chat_spoil.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      chat_messages();
    }
  };
}

function chat_quiz(action) {
  var xhr = get_XHR();
  var data = "action="+action+"&"+$('chat_quiz').serialize();
  xhr.open("post", "pages/chat_quiz.php", true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if(xhr.readyState == 4 && xhr.status == 200) {
      $('chat_quiz').reset();
      chat_messages();
    }
  };
}



function edit_comment(id) {
  if ($("edit_comment_text-"+id).style.display == "none") {
    $("read_comment_text-"+id).style.display = "none";
    $("edit_comment_text-"+id).style.display = "block";
  } else {
    $("edit_comment_text-"+id).style.display = "none";
    $("read_comment_text-"+id).style.display = "block";
  }
}
