/* Sanjalica Minichat v1.1.0
   Copyright www.sanjalica.com */

var url = "/minichat/mc.php";

var mc_focused = 20000; // 20 sekundi
var mc_blurred = 60000; // 60 sekundi
var mc_refresh = 10000;
var isWorking = false;


function getHTTPObject()
{
   var xmlhttp;
   if ( ! xmlhttp && typeof XMLHttpRequest != 'undefined')
   {
      try
      {
         xmlhttp = new XMLHttpRequest();
         xmlhttp.overrideMimeType("text/xml");
      }
      catch (e)
      {
         xmlhttp = false;
      }
   }
   return xmlhttp;
}

var http = getHTTPObject(); // create the HTTP Object

function ajaxslanje(url, divname, poruka)
{

   var params = "posalji=Posalji&poruka=" + poruka;
   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)
      {
         loadpage(http, divname)
      }
   }
   http.send(params);

}

function ajaxpage(url, containerid)
{
   http.onreadystatechange = function()
   {
      loadpage(http, containerid)
   }
   http.open('GET', url, true)
   http.send(null)
}

function loadpage(http, containerid)
{
   if (http.readyState == 4 && (http.status == 200 || window.location.href.indexOf("http") == - 1))
   document.getElementById(containerid).innerHTML = http.responseText
}

function checkMessage()
{
   http.open("GET", url, true);
   http.onreadystatechange = function()
   {
      loadpage(http, 'minichat')
   }
   isWorking = true;
   http.send(null);
   setTimeout('checkMessage()', mc_refresh);
}

function chatform()
{
   var mc_el = document.getElementById("chatform");
   if (mc_el !== null)
   {
      mc_el.innerHTML = '<form id="chat" method="post" action="">Poruka<br><textarea id="mc_poruka" name="mc_poruka" cols="15" rows="3"></textarea><input type="button" id="mc_posalji" value="Pošalji">';
      mc_b = document.getElementById("mc_posalji");
      mc_t = document.getElementById("mc_poruka");

      mc_b.onclick = function()
      {
         ajaxslanje('/minichat/mc.php', 'minichat', mc_t.value);
         mc_t.value = "";
         mc_b.disabled = true;
         mc_pauza = 5;
         // pauza;
         function foo()
         {
            if (mc_pauza == 0)
            {
               mc_b.disabled = false;
               mc_b.value = "Pošalji";
            }
            else
            {
               mc_b.value = "Pauza: " + mc_pauza + " s";
               mc_pauza -= 1;
               setTimeout(foo, 1000);
            }
         }
         foo(); // startujemo odbrojavanje
      }
   }
   else
   {
      document.write("<strong><a href='/login.php'>Ulogujte se</a><br>da pišete poruke</strong>");
   }
   checkMessage();
}
