/**
* bbCode control by subBlue design [ www.subBlue.com ]
* Includes unixsafe colour palette selector by SHS`
*/

// Startup variables
var snelheid = 35;
var eindhoogte;
var interval;

/**
* Shows the help messages in the helpline window
*/
function insert_reply(title, reply, button, post_url)
{
  if (reply.length <= 0 || title.length <= 0) {
    return;
  }

  var params = "subject=" + title + "&message=" + reply + "&post=1";

  var html = document.getElementById('fieldset-quickreply').innerHTML;
  var htmlArray = html.split(/(\r\n|[\r\n])/);

  for (i = 0; i < htmlArray.length; i++)
  {
    if (htmlArray[i].indexOf("type=\"hidden\"") != -1)
    {
      // Only hidden types here..
      var pos = htmlArray[i].indexOf("name=\"") + 6;

      var name;
      var value;

      name = htmlArray[i].substr(pos);

      pos = name.indexOf("\" value=\"") + 9;
      value = name.substr(pos);
      name = name.slice(0, pos - 9);

      pos = value.indexOf("\" type=\"hidden\">");
      value = value.slice(0, pos);

      params += "&" + name + "=" + value;
    }
  }

  var http = new XMLHttpRequest();

  var url = post_url;

  http.open("POST", url, true);

  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");

  http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
      document.getElementById('loading-quickreply').style.display = "none";
      button.value = 'Verzonden!';

      // Slide
      slide('p-quickreply');

      document.getElementsByName('message')[0].value = '';
    }
  }
  http.send(params);

  button.disabled = true;
  button.value = 'Bezig...';

  document.getElementById('loading-quickreply').style.display = "block";

  reply = reply.replace(/(\r\n|[\r\n])/g, "<br />");

	document.getElementById('title-quickreply').innerHTML = title;
	document.getElementById('content-quickreply').innerHTML = reply;
}

function slide(div)
{
    var id = document.getElementById(div);
    id.style.overflow = 'hidden';

    if(id.style.display == "none")
    {
        clearInterval(interval);
        id.style.display = 'block';
        eindhoogte = (id.offsetHeight - 5);
        id.style.height = '1px';

        interval = setInterval('verhoog(\'' + div + '\');', 0001);
    }
    else
    {
        clearInterval(interval);
        id.style.height = id.offsetHeight + 'px';
        interval = setInterval('verlaag(\'' + div + '\');', 0001);
    }
}

function verhoog(id)
{
    id = document.getElementById(id);
    var px = parseInt(id.style.height);

    if(px < eindhoogte)
    {
        id.style.height = px + Math.ceil(eindhoogte / snelheid) + 'px';
    }
    else
    {
        id.style.height = '100%';
        clearInterval(interval);
    }
}

function verlaag(id)
{
    id = document.getElementById(id);
    var hoogte = (parseInt(id.style.height) - Math.ceil(eindhoogte / snelheid));

    if(hoogte < 1)
    {
        id.style.display = 'none';
        id.style.height = '100%';
        clearInterval(interval);
    }
    else
    {
        id.style.height = hoogte + 'px';
    }
}

