﻿function CObject(id, refresh) {
 this.id = id;
 this.refresh = refresh;
 this.failcount = 0;
 this.time = 0;
 this.content;
}

function CImage(id, refresh, src) {
 this.id = id;
 this.refresh = refresh;
 this.time = 0;
 var im = new Image();
 im.src = src;
 this.i = im;
 this.done = 0;
 this.width = 0;
 this.height = 0;
 this.link = "";
 this.alt = "";
}

function CDialog(id, x, y) {
 this.id = id;
 this.x = x;
 this.y = y;
 this.offsetx = 0;
 this.offsety = 0;
 this.active;
}
var contr = new Array();
var imgs = new Array();
var dialogs = new Array();
var sinc = 0;
window.settingMessage = false;
window.message = "";
window.lastmessage = "";
window.requestPending = 0;
window.norequest = 0;
var maxscroll = 0;
function refresh(element) {
 sendMessage("refresh#" + element);
}

function sendMessage(param) {
 if (window.message.indexOf(param) != -1) return;
 window.settingMessage = true;
 if (window.message.length != 0) window.message += "&";
 window.message += param;
 window.settingMessage = false;
}


function gotopage(element) {
 var lang = 'lang%23' + document.getElementById("lang").value + "%26";
 if (document.getElementById("lang").value == "1") lang = ""
 setHash("?command=" + lang + "gotopage%23" + element);
 sendMessage(lang + "gotopage#" + element);
}

function getScroll() {
 var i = 0;
 if (window.navigator.appName.indexOf("Internet") == -1) {
  window.scrolly = window.pageYOffset;
  window.scrollx = window.pageXOffset;
  window.height = window.innerHeight;
  window.width = window.innerWidth;
 } else {
  window.scrolly = 0;
  window.scrollx = 0;
  var ob = document.body;
  if (ob.scrollTop != 0) window.scrolly = ob.scrollTop;
  if (ob.scrollLeft != 0) window.scrollx = ob.scrollLeft;
  window.height = ob.offsetHeight;
  window.width = ob.offsetWidth;
  var ob = window.htmlTag;
  if (ob.scrollTop != 0) window.scrolly = ob.scrollTop;
  if (ob.scrollLeft != 0) window.scrollx = ob.scrollLeft;
  window.height = ob.offsetHeight;
  window.width = ob.offsetWidth;
 }
}

function scrollEvent() {
 var diatable = document.getElementById("diatable");
 if (diatable != null) {
  var dial = document.getElementById("dialog");
  var sw = window.width;
  var sh = window.height;
  var w = dial.offsetWidth;
  var h = dial.offsetHeight;
  dial.style.left = (sw / 2 - w / 2 + window.scrollx) + "px";
  dial.style.top = (sh / 2 - h / 2 + window.scrolly) + "px";

  diatable.style.top = window.scrolly + "px";
  diatable.style.left = window.scrollx + "px";
  diatable.style.width = sw + "px";
  diatable.style.height = sh + "px";
 }
 var diatable1 = document.getElementById("diatable1");
 if (diatable1 != null) {
  var dialog1 = document.getElementById("dialog1");
  var sw = window.width;
  var sh = window.height;
  var w = dialog1.offsetWidth;
  var h = dialog1.offsetHeight;
  dialog1.style.left = (sw / 2 - w / 2 + window.scrollx) + "px";
  dialog1.style.top = (sh / 2 - h / 2 + window.scrolly) + "px";
  diatable1.style.top = window.scrolly + "px";
  diatable1.style.left = window.scrollx + "px";
  diatable1.style.width = sw + "px";
  diatable1.style.height = sh + "px";
 }
 var popup = document.getElementById("popupdialog");
 if (popup.innerHTML != "") {
  var sw = window.width;
  var sh = window.height;
  var w = popup.offsetWidth;
  var h = popup.offsetHeight;
  popup.style.left = (sw - w - 40 + window.scrollx) + "px";
  popup.style.top = (40 + window.scrolly) + "px";
 }
}

function gotoarticle(element) {
 var lang = 'lang%23' + document.getElementById("lang").value + "%26";
 if (document.getElementById("lang").value=="1") lang=""
 setHash("?command=" + lang + "gotoarticle%23" + element);
 sendMessage(lang + "gotoarticle#" + element);
}

function gotousermanagement(userID) {
 sendMessage("gotousermanagement#" + userID);
}

function gotonewuser() {
 sendMessage("gotonewuser");
}

//  function uploadImageNewUser()
//  {
//   window.afterimagedone=doImageNewUser;
//   uploadimage();
//  } 
function onafterfiledone(name, imagename, imageUrl) {
 if (document.getElementById(name + "_div") != null) { document.getElementById(name + "_div").innerHTML = "<img src=\"" + imageUrl + "\" />"; }
}

function gotoforum() {
 setHash("?command=gotoforum%23");
 sendMessage("gotoforum#");
}

function gotosubforum(forum) {
 setHash("?command=gotosubforum%23" + forum);
 sendMessage("gotosubforum#" + forum);
}

function gotothread(thread) {
 setHash("?command=gotothread%23" + thread);
 sendMessage("gotothread#" + thread);
}

function gotomessage(mess) {
 setHash("?command=gotomessage%23" + mess);
 sendMessage("gotomessage#" + mess);
}


function gotousermanagementlist() {
 sendMessage("gotousermanagementlist#");
}

function getHash(str) {
 if (str == null) return 0;
 var out = 0;
 var k = 0;
 for (k = 0; k < str.length; k++) {
  out += str.charCodeAt(k);
 }
 return out;
}

function GetFromServer(sadrzaj, context) {
 var filled = false;
 var strings = sadrzaj.split("###")
 var i = 0;
 var last = -1;
 while (i < strings.length) {
  if (last == i) {
   alert(sadrzaj);
   sinc = 0;
   requestPending = 0;
   message = oldmessage + "&" + message;
   return;
  }
  last = i;
  if (strings[i] == "empty") {
   i++;
   continue;
  }
  if (strings[i] == "message") {
   i++;
   alert(strings[i]);
   i++;
   continue;
  }
  if (strings[i] == "settitle") {
   i++;
   var title = strings[i];
   if (window.IE) {
    var k = 0
    title += '   ';
    for (k = 0; title.length < 200; k++) {
     title += String.fromCharCode(171);
    }
   }
   document.title = title;
   i++;
   continue;
  }
  if (strings[i] == "fill") {
   i++;
   if (document.getElementById(strings[i]) != null) {
    var c = getControl(strings[i]);
    var refr = false;
    var myhash = getHash(strings[i + 1]);
    if (c == null) refr = true;
    else {
     if (c.content != myhash || strings[i + 1] == "") refr = true;
    }
    if (refr) {
     lastscroll = -1;
     filled = true;
     if (c != null) c.content = myhash;
     strings[i + 1] = strings[i + 1].replace("<#rand#>", "rand=" + Math.random());
     document.getElementById(strings[i]).innerHTML = strings[i + 1];
     if (strings[i + 1] == "") {
      document.getElementById(strings[i]).style.height = "0px";
      document.getElementById(strings[i]).style.width = "0px";
      document.getElementById(strings[i]).style.position = "absolute";
     }
     else {
      document.getElementById(strings[i]).style.position = "static";
      document.getElementById(strings[i]).style.height = "auto";
      document.getElementById(strings[i]).style.width = "auto";
     }
    }
   } else if (strings[i] != "MainContent") {
    sendMessage("delete#" + strings[i]);
    removeControl(strings[i]);
   }
   if (strings[i] == "MainContent") {
    window.visibleHeight = -2;
    scrollheight = -1;
   }
   i += 2;
   continue;
  }

  if (strings[i] == "append") {
   i++;
   if (document.getElementById(strings[i]) != null) {
    document.getElementById(strings[i]).innerHTML += strings[i + 1];
   } else {
    sendMessage("delete#" + strings[i]);
    removeControl(strings[i]);
   }
   i += 2;
   continue;
  }

  if (strings[i] == "dialog") {
   i++;
   getScroll();
   var sw = window.width;
   var sh = window.height;
   document.getElementById("dialog").innerHTML = "<table id=\"diatable\" width=100% height=" + sh + "px background=\"images/forum/back.gif\" cellpadding=0 cellspacing=0 border=0><tr valign=middle><td align=center>" + strings[i] + "</td></tr></table>";
   document.getElementById("dialog").style.visibility = "visible";
   scrollEvent();
   i++;
   continue;
  }

  if (strings[i] == "dialog1") {
   getScroll();
   i++;
   showdialog1(strings[i]);
   scrollEvent();
   i++;
   continue;
  }

  if (strings[i] == "searchdialog") {
   i++;
   showsearchdialog(strings[i]);
   i++;
   continue;
  }



  if (strings[i] == "popup") {
   window.popuptime = 0;
   i++;
   document.getElementById("popupdialog").innerHTML = strings[i];
   var sw = window.width;
   var sh = window.height;
   var w = document.getElementById("popupdialog").offsetWidth;
   var h = document.getElementById("popupdialog").offsetHeight;
   document.getElementById("popupdialog").style.left = (sw - w - 40 + window.scrollx) + "px";
   document.getElementById("popupdialog").style.top = (40 + window.scrolly) + "px";
   document.getElementById("popupdialog").style.visibility = "visible";
   i++;
   continue;
  }
  if (strings[i] == "closedialog") {
   i++;
   closedialog();
   continue;
  }
  if (strings[i] == "closedial") {
   i++;
   closedial(strings[i + 1]);
   i++;
   continue;
  }

  if (strings[i] == "closedialog1") {
   i++;
   closedialog1();
   continue;
  }

  if (strings[i] == "filldialog") {
   window.popuptime = 0;
   i++;
   document.getElementById(strings[i]).innerHTML = strings[i + 1];
   var sw = window.width;
   var sh = window.height;
   var w = document.getElementById(strings[i]).offsetWidth;
   var h = document.getElementById(strings[i]).offsetHeight;
   var posx = parseInt(strings[i + 2]);
   if (posx < 0) posx = (sw - w - posx + window.scrollx);
   var posy = parseInt(strings[i + 3]);
   if (posy < 0) posy = (sh - h - posx + window.scrolly);
   if (posx != 0) document.getElementById(strings[i]).style.left = posx + "px";
   if (posy != 0) document.getElementById(strings[i]).style.top = posy + "px";
   document.getElementById(strings[i]).style.visibility = "visible";
   document.getElementById(strings[i]).style.position = "absolute";
   i += 4;
   continue;
  }


  if (strings[i] == "refreshall") {
   i++;
   var j = 0;
   for (j = 0; j < contr.length; j++) {
    if (contr[j].id.indexOf("Template") == -1) contr[j].time = 2000000;
   }
   continue;
  }

  if (strings[i] == "refresh") {
   i++;
   var j = 0;
   for (j = 0; j < contr.length; j++) {
    if (contr[j].id == strings[i]) contr[j].time = 2000000;
   }
   i++;
   continue;
  }

  if (strings[i] == "fileuploaddone") {
   i++;
   onafterfiledone(strings[i], strings[i + 1], strings[i + 2]);
   i += 3;
   continue;
  }

  if (strings[i] == "documentuploaddone") {
   i++;
   window.afterdocumentdone(strings[i], strings[i + 1]);
   i += 2;
   continue;
  }


  if (strings[i] == "playsound") {
   i++;
   document.getElementById("sound1").Play();
   i++;
   continue;
  }



  if (strings[i] == "register") {
   i++;
   if (!controlExists(strings[i])) {
    var c = new CObject(strings[i], strings[i + 1]);
    c.time = 2000000;
    contr[contr.length] = c;
   }
   else {
    getControl(strings[i]).time = 2000000;
   }
   i += 2;
   continue;
  }
  if (strings[i] == "image") {
   i++;
   var io = new CImage(strings[i], 10000, strings[i + 1]);
   io.alt = strings[i + 2];
   io.width = strings[i + 3];
   io.height = strings[i + 4];
   io.link = strings[i + 5];
   imgs[imgs.length] = io;
   i += 6;
   continue;
  }
  if (strings[i] == "sethash") {
   i++;

   setHash(strings[i]);
   i++;
   continue;
  }
  if (strings[i] == "loadmenu") {
   i++;
   eval(strings[i]);
   i++;
   continue;
  }
  if (strings[i] == "runscript") {
   i++;
   eval(strings[i]);
   i++;
   continue;
  }
  if (strings[i] == "changetemplate") {
   i++;
   window.triedToWriteMenus = false;
   contr = new Array();
   imgs = new Array();
   var c = new CObject(strings[i], 1900000);
   c.time = 2000000;
   contr[contr.length] = c;
   i++;
   continue;
  }



 } //end while
 oldmessage = "";
 if (filled) {
  getScroll();
  lastscroll = -1;
  window.scrollTo(0, window.scrolly);
 }
 sinc = 0;
 requestPending = 0;
}
var skip = 0;
var scrolldone = true;

function setscrollto(val) {
 if (scrolldone) {
  scrolldone = false;
  window.scrolltoval = val;
  setTimeout("doautoscroll()", 10);
 }
}

function doautoscroll() {
 var dist = Math.abs(window.scrolly - window.scrolltoval);
 if (dist < 20) {
  window.scrollTo(window.scrollx, window.scrolly - dist / 10);
  scrolldone = true;
  return;
 }
 window.scrollTo(window.scrollx, window.scrolly - dist / 10);
 setTimeout("doautoscroll()", 10);
}
var lastscroll = -1;
var sevent = false;
var scrollheight = -1;


function ParseControls() {
 if (scrollheight != h) {
  var mc = document.getElementById("MainContent");
  if (mc != null) {
   var h = mc.offsetTop + mc.offsetHeight + 500;
   if (scrollheight == h) {
    window.visibleHeight = h;
    lastscroll = -1;
   }
   if (scrollheight < h) {
    scrollheight = h;
   }
  }
 }

 if (lastscroll != window.scrolly) {
  scrollEvent();
  lastscroll = window.scrolly;
  if (window.visibleHeight < window.scrolly && scrollheight != -1 && window.visibleHeight != -2) {
   var sval = window.visibleHeight - 600;
   if (sval < 0) sval = 0;
   setscrollto(sval)
  }
  if (maxscroll < document.body.scrollHeight) {
   maxscroll = document.body.scrollHeight + 50;
   window.scrolldiv.style.width = "2px";
   window.scrolldiv.style.height = maxscroll + "px";
  }
 }

 skip++;
 var i = 0;
 var coneffectcount = 0;
 for (i = 0; i < contr.length; i++) {
  if (document.getElementById(contr[i].id) == null) {
   if (contr[i].failcount > skip * 5) {
    sendMessage("delete#" + contr[i].id);
    removeControl(contr[i].id);
   }
   else {
    contr[i].failcount += 1;
   }
  }
  else {
   if (contr[i].time > contr[i].refresh) {
    if (sinc == 0 && Math.random() > -1) {
     if (coneffectcount > 10) {
      sinc = 1;
      coneffectcount = 0;
     }
     coneffectcount += 1;
     refresh(contr[i].id);
     contr[i].time = 0;
    }
   }
   if (skip > 5) {
    contr[i].time += 1;
   }
  }
 }
 if (skip > 5) {
  skip = 0;
  window.popuptime += 1;
  if (window.popuptime == 10) closepopup();
 }
 var loaded = 0;
 var j = 0;
 for (j = 0; j < imgs.length; j++) {
  if (document.getElementById(imgs[j].id) == null) {
   removeimage(imgs[j].id);
   j--;
   continue;
  }
  if (imgs[j].done == 0) {
   if (imgs[j].i != null) {
    if (imgs[j].i.complete) {
     var wh = "";
     if (imgs[j].width != 0) wh += " width=" + imgs[j].width;
     if (imgs[j].height != 0) wh += " height=" + imgs[j].height;
     var out = "<img src=" + imgs[j].i.src + wh + " alt=\"" + imgs[j].alt + "\" border=\"0\" />";
     if (imgs[j].link != "") {
      out = "<a href=\"" + imgs[j].link + "\">" + out + "</a>";
     }
     document.getElementById(imgs[j].id).innerHTML = out;
     imgs[j].done = 1;
    }
   }
  }
 }
 window.norequest++;
 if (window.settingMessage == false && window.message.length != 0) {
  var ins = document.getElementById('pageInstance').value;
  UseCallback(ins + "?" + window.message);
 }
 if (window.message.length == 0 && window.norequest > 20) {
  window.norequest = 0;
  var ins = document.getElementById('pageInstance').value;
  UseCallback(ins + "?" + "empty");
 }
 var hsh = "";
 if (window.IE) {
  if (document.getElementById('fakeframe').readyState != 'loading') {
   hsh = window.location.href;
   if (hsh.indexOf('#?') != -1) hsh = "#" + hsh.substr(hsh.indexOf('#?') + 1); else hsh = '';
   if (hsh != "" && hsh != window.lastHash) {
    setHash(hsh.substr(1));
    sendMessage(hsh.substr(hsh.indexOf('#?') + 2 + 8).replace("%23", "#"));
   } else {
    hsh = document.getElementById('fakeframe').contentWindow.location.href;
    if (hsh.indexOf('?') != -1) {
     hsh = "#" + hsh.substr(hsh.indexOf('?'));
    }
    else hsh = '';
   }
  }
  else hsh = window.lastHash;
 }
 else {
  hsh = window.location.href;
  if (hsh.indexOf('#?') != -1) hsh = "#" + hsh.substr(hsh.indexOf('#?') + 1); else hsh = '';
 }
 if (hsh != '' && window.lastHash != hsh) {
  setHash(hsh.substr(1));
  sendMessage(hsh.substr(hsh.indexOf('#?') + 2 + 8).replace("%23", "#"));
 }
 setTimeout("ParseControls();", 200);
}
function starttimer(mode) {
 scrollheight = -1;
 window.scrolldiv = document.getElementById("scrolldiv");
 window.htmlTag = document.getElementsByTagName("HTML")[0];
 window.bodyTag = document.getElementsByTagName("BODY")[0];
 getScroll();
 scrollEvent();
 if (mode == 0) setTimeout("ParseControls();", 50);
}

function uploadAvatar(userID) {
 document.getElementById("avatarstatus" + userID).innerHTML = "0%";
 i++;
 uploadavatar(userID);
 var c = new CObject("avatarstatus" + userID, 1);
 c.time = 2000000;
 contr[contr.length] = c;
}

function uploadavatar(userID) {
 document.getElementById("formavatar" + userID).submit();
}


function removeControl(id) {
 var i = 0;
 for (i = 0; i < contr.length; i++) {
  if (contr[i].id == id) {
   contr.splice(i, 1);
   return;
  }
 }
}

function removeimage(id) {
 var i = 0;
 for (i = 0; i < imgs.length; i++) {
  if (imgs[i].id == id) {
   imgs.splice(i, 1);
   return;
  }
 }
}

function controlExists(id) {
 var i = 0;
 for (i = 0; i < contr.length; i++) {
  if (contr[i].id == id) {
   return true;
  }
 }
 return false;
}

function getControl(id) {
 var i = 0;
 for (i = 0; i < contr.length; i++) {
  if (contr[i].id == id) {
   return contr[i];
  }
 }
 return null;
}


// function updateUser(userID)
// {
//  var s="updateuser#";

//  s += userID + "#";
//  s += document.getElementById("fname" + userID).value + "#";
//  s += document.getElementById("lname" + userID).value + "#";
//  s += document.getElementById("username" + userID).value + "#";
//  s += document.getElementById("password" + userID).value + "#";
//  s += document.getElementById("banday" + userID).value + "#";
//  s += document.getElementById("email" + userID).value + "#";
//  s += document.getElementById("userpermits" + userID).value + "#";
//  s += document.getElementById("description" + userID).value + "#";
//  s += document.getElementById("webpage" + userID).value + "#";
//  s += document.getElementById("signature" + userID).value ;
//  sendMessage(s);
// }

// function newUser()
// {
//  var s="newuser#";
//  if (document.getElementById("fname").value=="") {alert("Please enter first name!");return;}
//  s += document.getElementById("fname").value + "#";
//  if (document.getElementById("lname").value=="") {alert("Please enter last name!");return;}
//  s += document.getElementById("lname").value + "#";
//  if (document.getElementById("username").value=="") {alert("Please enter username(nick)!");return;}   
//  if (document.getElementById("username").value.length>15) {alert("Username(nick) to long, 15 chars max!");return;}   
//  s += document.getElementById("username").value + "#";
//  if (document.getElementById("password").value=="") {alert("Please enter password!");return;}   
//  s += document.getElementById("password").value + "#";
//  if (document.getElementById("email").value=="") {alert("Please enter email!");return;}   
//  s += document.getElementById("email").value + "#";
//  s += document.getElementById("description").value + "#";
//  s += document.getElementById("webpage").value + "#";
//  if (document.getElementById("signature")!=null) {s += document.getElementById("signature").value ;}
//  sendMessage(s);
// }


// function deleteUser(userID)
// {
//  var s="deleteuser#";
//  s += userID ;
//  if (window.confirm("Are you sure?"))
//  {
//   sendMessage(s);
//  }
// }

function sendDeleteMessage(ID) {
 var out = "submitcommand#" + ID + "#delete";
 if (window.confirm("Are you sure?")) {
  sendMessage(out);
 }
}

function showlogindialog() {
 sendMessage("showlogindialog")
}

function showaddforumdialog(source) {
 sendMessage("showaddforumdialog#" + source)
}
function showaddforumcategorydialog(source) {
 sendMessage("showaddforumcategorydialog#" + source)
}

function closedialog(ID) {
 document.getElementById("dialog").innerHTML = "";
 document.getElementById("dialog").style.visibility = "hidden";
 sendMessage("delete#" + ID);
}

function closedial(id) {
 if (document.getElementById(id) != null) {
  document.getElementById(id).innerHTML = "";
  document.getElementById(id).style.visibility = "hidden";
  sendMessage("delete#" + id);
 }
}


function closedialog1(ID) {
 document.getElementById("dialog1").innerHTML = "";
 document.getElementById("dialog1").style.visibility = "hidden";
 sendMessage("delete#" + ID);
}

function closesearchdialog(ID) {
 document.getElementById("searchdialog").innerHTML = "";
 document.getElementById("searchdialog").style.visibility = "hidden";
 sendMessage("delete#" + ID);

}


function showdialog1(str) {
 var sw = window.width;
 var sh = window.height;
 document.getElementById("dialog1").innerHTML = "<table id=\"diatable1\" width=100% height=" + sh + "px background=\"images/forum/back.gif\" cellpadding=0 cellspacing=0 border=0><tr valign=middle><td align=center>" + str + "</td></tr></table>";
 //var w=document.getElementById("dialog1").offsetWidth;
 //var h=document.getElementById("dialog1").offsetHeight;
 //document.getElementById("dialog1").style.left=(sw/2-w/2 + window.scrollx) + "px";
 //document.getElementById("dialog1").style.top=(sh/2-h/2 + window.scrolly) + "px";
 document.getElementById("dialog1").style.visibility = "visible";
}

function showsearchdialog(str) {
 document.getElementById("searchdialog").innerHTML = "&nbsp;<br>" + str;
 var sw = window.width;
 var sh = window.height;
 var w = document.getElementById("searchdialog").offsetWidth;
 var h = document.getElementById("searchdialog").offsetHeight;
 var x = (sw - w - 40 + window.scrollx);
 var y = (200 + window.scrolly);
 var dialog = getDialog("searchdialog");
 if (dialog != null) {
  x = parseInt(dialog.offsetx);
  y = parseInt(dialog.offsety);
 }
 document.getElementById("searchdialog").style.left = x + "px";
 document.getElementById("searchdialog").style.top = y + "px";
 document.getElementById("searchdialog").style.visibility = "visible";
}



function closepopup() {
 document.getElementById("popupdialog").innerHTML = "";
 document.getElementById("popupdialog").style.visibility = "hidden";

}


// function dologin()
// {
//  sendMessage("login#" + document.getElementById("username").value + "#" + document.getElementById("password").value);
// }
// 
function logout() {
 sendMessage("logout");
 //document.getElementById("logindiv").innerHTML = "<a href=\"javascript:showlogindialog();\">Log in</a>";
 //document.getElementById("myprofile").innerHTML = "<a href=\"javascript:gotonewuser();\">Sign in</a>";
 //if (document.getElementById("adminlinks") != null) document.getElementById("adminlinks").innerHTML = "";
 //if (document.getElementById("mymessages") != null) document.getElementById("mymessages").innerHTML = "";
}

function addforum(cat) {
 sendMessage("addforum#" + document.getElementById("forumname").value + "#" + document.getElementById("forumdescription").value + "#" + cat);
}

function deleteforum(forum) {
 if (window.confirm("All messages and subforums will be deleted, are you sure?")) {
  removeControl("CForumRepeater" + forum);
  sendMessage("deleteforum#" + forum);
 }
}

function changeforumstatus(forum) {
 sendMessage("changeforumstatus#" + forum);
}


//adding subforum
function showaddsubforumdialog(forum) {
 sendMessage("showaddsubforumdialog#" + forum)
}
function addsubforum(forum) {
 sendMessage("addsubforum#" + document.getElementById("forumname").value + "#" + document.getElementById("forumdescription").value + "#" + forum);
}

//adding thread
function showaddthreaddialog(forum) {
 sendMessage("showaddthreaddialog#" + forum)
}

// function addthread(forum)
// {
//  sendMessage("addthread#" + document.getElementById("threadname").value + "#" + document.getElementById("threaddescription").value + "#" + forum);
// }

// function addcomment(article)
// {
//  sendMessage("addcomment#" + document.getElementById("comment").value + "#" + document.getElementById("grade").value + "#" + article);
// }

function uploadimage(name) {
 showdialog1("<table cellpading=0 cellspacin=0 border=1 bordercolor=Black><tr><td><div id=\"uploadimagediv\"></div><img src=\"images/loading.gif\"></td></tr></table>")
 sendMessage("imageupload");
 document.getElementById("form" + name).submit();
}

function uploadFile(name, type) {
 showdialog1("<table cellpading=0 cellspacin=0 border=1 bordercolor=Black><tr><td><div id=\"uploadfilediv\"></div><img src=\"images/loading.gif\"></td></tr></table>")
 sendMessage("fileupload#" + name);
 document.getElementById("form" + name).submit();
}


function uploaddocument() {
 showdialog1("<table cellpading=0 cellspacin=0 border=1 bordercolor=Black><tr><td><div id=\"uploaddocumentdiv\"></div><img src=\"images/loading.gif\"></td></tr></table>")
 sendMessage("documentupload");
 document.getElementById("formdocument").submit();
}

// function gotopnum(pnum)
// {
//  sendMessage("gotopnum#" + pnum);
// }
// 
// function gotopnumnotmain(pnum,element)
// {
//  sendMessage("gotopnumnotmain#" + pnum);
//  refresh(element);
// }


function showsearch() {
 sendMessage("showsearch");
}

function doSearch(ss, pnum) {
 sendMessage("dosearch#" + ss + "#" + pnum);
}

// function sendcontact()
// {
//  var ime=document.getElementById("contactname").value;
//  var email=document.getElementById("contactemail").value;
//  var poruka=document.getElementById("contactmessage").value;
//  sendMessage("sendemail#ime#" + ime + "#email#" + email + "#poruka#" + poruka);
// }

function showcontactdialog() {
 sendMessage("showcontactdialog");
}


function movedialog(e) {
 try {
  var i = 0;
  for (i = 0; i < dialogs.length; i++) {
   if (dialogs[i].active == true) {
    var ev;
    if (e == null) ev = event; else ev = e;
    var deltax = ev.screenX - dialogs[i].x;
    var deltay = ev.screenY - dialogs[i].y;
    document.getElementById(dialogs[i].id).style.left = dialogs[i].offsetx + deltax + "px";
    document.getElementById(dialogs[i].id).style.top = dialogs[i].offsety + deltay + "px";
   }
  }
 } catch (err) { }
}
function setdialogmove(e) {
 try {
  var ev;
  if (e == null) ev = event; else ev = e;
  var el = ev.target;
  if (el == null) el = ev.srcElement;
  var id = getDiv(el).id;
  var x = ev.screenX;
  var y = ev.screenY;
  var dialog = getDialog(id);
  if (dialog == null) {
   dialog = new CDialog(id, x, y);
   dialogs[dialogs.length] = dialog;
  }
  dialog.x = x;
  dialog.y = y;
  dialog.active = true;
  var oldx = parseInt(document.getElementById(dialog.id).style.left);
  var oldy = parseInt(document.getElementById(dialog.id).style.top);
  dialog.offsetx = oldx;
  dialog.offsety = oldy;
 } catch (err) { }
}
function resetdialogmove() {
 try {
  var i = 0;
  for (i = 0; i < dialogs.length; i++) {
   dialogs[i].active = false;
  }
 } catch (err) { }
}

function getDiv(element) {
 while (element != null) {
  if (element.id != null) {
   if (element.id.indexOf("dialog") != -1) {
    return element;
   }
  }
  if (element.parentElement != null) element = element.parentElement; else element = element.parentNode;
 }
 return null;
}

function getDialog(id) {
 var i = 0;
 for (i = 0; i < dialogs.length; i++) {
  if (dialogs[i].id == id) {
   return dialogs[i];
  }
 }
 return null;
}
function onEnter(e, fname) {
 var ev;
 if (e == null) ev = event; else ev = e;
 if (ev.keyCode == 13) {
  eval(fname);
  return true;
 }
 return false;
}

function doLoad() {
}

function showaddcommentdialog(art) {
 sendMessage("showaddcommentdialog#" + art)
}


function submitDialogCommand(ID, command, par) {
 var out = "submitcommand#" + ID + "#" + command;
 var params = par.split("^");
 var i = 0;
 for (i = 0; i < params.length; i++) {
  if (document.getElementById(params[i]) != null) out += "#" + document.getElementById(params[i]).value; else out += "#";
 }
 sendMessage(out);
}

function submitCommandValue(ID, command, value) {
 var out = "submitcommand#" + ID + "#" + command + "#" + value;
 sendMessage(out);
}


function setHash(str) {
 var title = document.title;
 var hsh = "";
 if (window.IE) {
  hsh = document.getElementById('fakeframe').contentWindow.location.href;
  if (hsh.indexOf('?') != -1) hsh = "#" + hsh.substr(hsh.indexOf('?')); else hsh = '';
 }
 else {
  hsh = window.location.href;
  if (hsh.indexOf('#?') != -1) hsh = "#" + hsh.substr(hsh.indexOf('#?') + 1); else hsh = '';
 }

 window.location.hash = str.replace('#', '%23');
 window.lastHash = "#" + str;
 if (window.IE) {
  var myElement = document.getElementById('fakeframe');
  myElement.contentWindow.location = "blank.html" + str;
 }

 if (window.IE) {
  var k = 0
  title += '   ';
  for (k = 0; title.length < 200; k++) {
   title += String.fromCharCode(171);
  }
 }
 document.title = title;
}

function setLang(langID) {
  document.getElementById("lang").value = langID;
  var lang = 'lang%23' + document.getElementById("lang").value + "%26";
  //if (document.getElementById("lang").value == "1") lang = ""

  var hsh = window.lastHash.substr(window.lastHash.indexOf('?command=') + 9);
  var langid = hsh.indexOf("lang%23");
  if (langid != -1) {
   hsh = hsh.substr(langid + 11);
  }
  setHash("?command=" + lang + hsh);
  sendMessage(lang + hsh);
 }

 function startExchange(id,period) {
 try{
  var imagelist = document.getElementById(id + "imageList").value.split("|");
  var i = 0;
  var current = parseInt(document.getElementById(id + "currentImage").value, 10);
  var lastImage = imagelist[current];
  current += 1;
  if (current >= imagelist.length) { current = 0; }
  var newImage = imagelist[current];
  document.getElementById(id + "currentImage").value = current;
  document.getElementById(id + "fadebackground").style.backgroundImage = "url('" + lastImage + "')";
  changeOpac(0, id + "fadeimage");
  document.getElementById(id + "fadeimage").src = newImage;
  opacity(id + "fadeimage", 0, 100, 2000);
  setTimeout("startExchange('" + id + "'," + period + ");", period);
  }
  catch(e)
  {}
 }

 function opacity(id, opacStart, opacEnd, millisec) {
 try{
  //speed for each frame
  var speed = Math.round(millisec / 100);
  var timer = 0;

  //determine the direction for the blending, if start and end are the same nothing happens
  if (opacStart > opacEnd) {
   for (i = opacStart; i >= opacEnd; i--) {
    setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
    timer++;
   }
  } else if (opacStart < opacEnd) {
   for (i = opacStart; i <= opacEnd; i++) {
    setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
    timer++;
   }
  }
    }catch(e)
    {}

 }

 //change the opacity for different browsers
 function changeOpac(opacity, id) {
  try {
   var object = document.getElementById(id).style;
   object.opacity = (opacity / 100);
   object.MozOpacity = (opacity / 100);
   object.KhtmlOpacity = (opacity / 100);
   object.filter = "alpha(opacity=" + opacity + ")";
  } catch (e) { }
 } 
