// --- ---
function trim(str)
{
  ok = false;
  while (!ok)
  {
    //ind_prob = str.indexOf(' ');
    t_char = str.substring(0,1);
    if (t_char == ' ') { str = str.substring(1); }
    else { ok = true; }
  }
  ok = false;
  while (!ok)
  {
    l = str.length;
    t_char = str.substring(l-1,l);
    if (t_char == ' ') { str = str.substring(0,l-1); }
    else { ok = true; }
  }
  return str;
}

// --- ---
function getMouseXY(e) {
   try {
      x = event.clientX + document.body.scrollLeft;
      y = event.clientY + document.body.scrollTop;
   } catch(except) {
      x = e.clientX + window.scrollX;
      y = e.clientY +  window.scrollY;
   }
   var out = new Array(x,y);
   return out;
}

// --- ---
function openFileSelect(type,idArea,ev)
{
  XY = getMouseXY(ev)
  var widthW = 200;
  var heightW = 350;
  //var top=(screen.height-width)/2;
  //var left=(screen.width-height)/2;
  var top=XY[1];
  var left=XY[0];
  try { usersWindow.close(); } catch(e) {}
  usersWindow = window.open('selectfile.php?type=' + type + '&idArea=' + idArea,'','top='+top+', left='+left+', width='+widthW+', height='+heightW+', directories=0, scrollbars=1, menubar=0, status=0, resizable=1, location=0');
}

function addtoeditor()
{
  var inp_el0 = document.getElementById('userSelectArea1');
  var inp_el1 = document.getElementById('userSelectArea2');
  var inp_el2 = document.getElementById('userSelectArea3');
  var editor = document.getElementById('elm2');
  if (inp_el0 && inp_el1 && editor)
  {
    image = trim(inp_el0.value);
    video = trim(inp_el1.value);
    v_length = inp_el2.checked;
    if ((image != '') && (video != ''))
    {
      editor.value += '[' + image + ']={' + video + ':' + v_length + '};\n';
      inp_el0.value = image;
      inp_el1.value = '';
      inp_el2.value = '';
      return true;
    }
  }
  return false;
}

function openCalendar(ev)
{
  XY = getMouseXY(ev)
  var widthW = 170;
  var heightW = 200;
  //var top=(screen.height-width)/2;
  //var left=(screen.width-height)/2;
  var top=XY[1];
  var left=XY[0];
  try { calWindow.close(); } catch(e) {}
  calWindow = window.open('calendar.php','','top='+top+', left='+left+', width='+widthW+', height='+heightW+', directories=0, scrollbars=1, menubar=0, status=0, resizable=1, location=0');
}
