/* JavaScript for ECS by Afrow UK 2007 */

function open_win(url, handle, scrollbars)
{
  if (typeof scrollbars == "undefined")
    scrollbars = 1;
  return window.open(url, handle, 'width=400,height=400,menubar=0,toolbar=0,resizable=1,scrollbars=' + scrollbars + ',status=1');
}

function trim(text)
{
  return text.replace(/^\s+|\s+$/g, "");
}

function open_playersearch(box_id, box_add)
{
  var search_string = trim(document.getElementById(box_id).value);
  if (search_string.indexOf("\n") != -1)
    search_string = '';
  var new_window = open_win('player_search.php?box=' + box_id + '&string=' + search_string + (box_add ? '&add=1' : ''), 'player_search');
  if (new_window.opener == null) new_window.opener = self;
  new_window.focus();
}

function open_playergroupsearch(box_id, box_add)
{
  var search_string = trim(document.getElementById(box_id).value);
  if (search_string.indexOf("\n") != -1)
    search_string = '';
  var new_window = open_win('player_group_search.php?box=' + box_id + '&string=' + search_string + (box_add ? '&add=1' : ''), 'player_search');
  if (new_window.opener == null) new_window.opener = self;
  new_window.focus();
}

function insertAtCaret(obj, string)
{
  obj.focus();

  if (typeof(document.selection) != 'undefined')
  {
    var range = document.selection.createRange();
    //var start = obj.caretPos;

    if (range.parentElement() != obj)
      return;
    range.text = string;
    range.select();
    //range.move('character', 1);

    /*if (typeof(state) == 'undefined')
      range.move('character', string.length);
    else
      obj.caretPos = start + string.length;*/
  }
  else if (typeof(obj.selectionStart) != 'undefined')
  {
    var start = obj.selectionStart;

    obj.value = obj.value.substr(0, start) 
              + string 
              + obj.value.substr(obj.selectionEnd, obj.value.length);

    start += string.length;
    obj.setSelectionRange(start, start);
  }
  else
    obj.value += string;

  obj.focus();
}
