var C__MAX_PLACES = 999999;
var sjedalo_hover = "";
var C__SIRINA = 28;
var enable_select = true;
var C__SEL_STATUS = "1";
var C__CONN = "default";

function selectEnabled() {
  enable_select = true;
}

function selectDisabled() {
  enable_select = false;
}

function dummy_sjedalo_over(id) {
}

function sjedalo_over(brdok) {
  sjedalo_hover = brdok;
  setTimeout('daj_detalje(\''+brdok+'\');', 1500);
}

///////////////////////////////////////////
function sjedalo_click(id) { // id je brdok
  sakrij_detalj();
  reset_sjedala();
  var i = id.indexOf(';');
  var rstu = id.substr(0, i);
  var brdok = id.substr(i+1);
  if (brdok=='') {
    return true;
  }
  var allCells = document.getElementsByTagName("td");
  for (i=0; i<allCells.length; i++) {
    var obj = allCells[i];
    if ((obj.id+"#").indexOf(';'+brdok+"#")!=-1) {
      obj.style.border = '2px solid red';
    }
  }
  daj_detalje(brdok);
}

function reset_sjedala() {
  var allCells = document.getElementById("tbl_sektor").getElementsByTagName("td");
  for (i=0; i<allCells.length; i++) {
    var obj = allCells[i];
    if (obj.id) {
      obj.style.border = '2px inset white'; ///////////////////////////
    }
  }
}

function daj_podatke(brdok) {
  divo = document.getElementById("sjedalo_podaci");
  sjedo = document.getElementById(id);
  makeRequest("sjedalo_brdok_detalji.xsql", "conn=" + C__CONN + "&priredba="+document.forms['forma'].p.value+"&sektor="+document.forms['forma'].s.value+"&rstu="+id, true, "processXML_brdok");
}


function daj_detalj(id) {
  divo = document.getElementById("sjedalo_detalji");
  var allCells = document.getElementsByTagName("td");
  for (i=0; i<allCells.length; i++) {
    var obj = allCells[i];
    if ((obj.id).indexOf(id)!=-1) {
      sjedo = obj;
    }
  }
  //sjedo = document.getElementById(id);
  divo.style.left = u_findPosX(sjedo);
  divo.style.top = u_findPosY(sjedo) + 30;
  if (id==sjedalo_hover) {
    makeRequest("sjedalo_detalji.xsql", "conn=" + C__CONN + "&priredba="+document.forms['forma'].p.value+"&sektor="+document.forms['forma'].s.value+"&rstu="+id, true, "processXML");
  }
}

function sakrij_detalj() {
  sjedalo_hover = "";
  document.getElementById("sjedalo_detalji").style.display="none";
}

function dummy_sakrij_detalj() {
}

function processXML(xmldoc) {
  var nodes = xmldoc.getElementsByTagName("DOGADJAJ");
  var tmp_data = new Array();
  var detalji = "";
  for (i=0; i<nodes.length; i++) {
    detalji += "<tr><td>"+nodes[i].getElementsByTagName("TIP_PROM").item(0).firstChild.data +
    "</td><td>" +nodes[i].getElementsByTagName("DATPRO").item(0).firstChild.data +
    "</td><td>" +nodes[i].getElementsByTagName("OPERNO").item(0).firstChild.data +
    "</td><td>" +nodes[i].getElementsByTagName("ZAVRSEN").item(0).firstChild.data +
    "</td></tr>";
  }
  document.getElementById("sjedalo_detalji").innerHTML = "<table border='1' rules='both' class='t_m'>" + detalji + "</table>";
  document.getElementById("sjedalo_detalji").style.display="block";
}

function change(id) {
  //if (!enable_select) return;
  var formaRez=document.getElementById("rstu");
  var formaRezCount=document.getElementById("rstuCount");
  if (id.className=="tablecell1") {
    if (formaRezCount.value>(C__MAX_PLACES-1))
      alert(poruka1);
    else {
      if (formaRez.value=="") {
        formaRez.value=id.id;
      } else {
        formaRez.value=formaRez.value+id.id;
      }
      formaRezCount.value++;
      id.className="tablecellrez";
    }
  } else {
    id.className="tablecell1";
    var s=formaRez.value;
    var subs=id.id;
    var poz=s.indexOf(subs);
    if (poz>-1) {
      var poz2=poz+subs.length;
      s=s.substr(0,poz)+s.substr(poz2,s.length-poz2+1);
      formaRez.value=s;
      formaRezCount.value--;
    }
  }
}


function validate_v2() {
  var formaVar,formaVar2,formaVar3,formaVar4;
  formaVar=document.getElementById("rstu");
  if (formaVar.value=="")
  {
    alert (poruka2);
    return false;
  }
  return true;
}

function sjedalo_v2(status, id, num, zavrsen, brdok) {
  formaVar=document.getElementById("rstu");
  document.writeln(
    '<td width="'+C__SIRINA+'" ' + ((formaVar.value).indexOf(id)>=0 ? ' bgcolor="red"' : '') + '>' + 
      '<table cellspacing="1" cellpadding="1" width="100%" border="0">' +
        '<tr>');
  document.writeln(
          '<td class="sjedalo' + status + '" id="' + id + ';' + brdok + '" ' + (zavrsen=='N'?'background="img/red_dot.gif" style="background-repeat: no-repeat"':'') +
    'align="right" onMouseOut="dummy_sakrij_detalj()" onMouseOver="dummy_sjedalo_over(\''+id+'\');" onClick="sjedalo_click(\''+id+';'+brdok+'\');">' +
             num + 'B' +
          '</td>');
  document.writeln(
        '</tr>' +
      '</table>' +
    '</td>');
}

function sjedalo_legenda(status, num) {
  document.writeln('<td width="'+C__SIRINA+'"><table cellspacing="1" cellpadding="1" width="100%" border="0"><tr>');
  document.writeln('<td class="sjedalo'+status+'" align="right">'+num+'</td>');
  document.writeln('</tr></table></td>');
}
function sjedalo_detalj(status, id, num) {
  formaVar=document.getElementById("rstu");
  $("#detalji_" + id).append(
    '<table bgcolor="red" cellspacing="1" cellpadding="1" width="100%" border="0">' +
      '<tr>' +
        '<td class="tablecell'+status+'" id="' + id + '" ' + 'align="right" ' + (status=='1' ? 'onClick="change(this);"' : '') + '>' + num + '</td>' +
      '</tr>' +
    '</table>');
}

function sjedalo_legenda_detalj(status, num) {
  $("#detalji_legenda_" + status).append('<td width="'+C__SIRINA+'"><table cellspacing="1" cellpadding="1" width="100%" border="0"><tr><td class="sjedalo'+status+'" align="right">'+num+'</td></tr></table></td>');
}

function prikazi_prostor() {
  var f = document.forms["forma"];
  $("#priredba").html('<img src="/img/osn/busy_m.gif"/>');
  $("#priredba").load("ctl_evt.jsp?act=prices", { "id": f.p.value });
}
