//******************************************************************************
//
//
//
//******************************************************************************

var netscape = false;

if (navigator.appName == 'Netscape') {

  netscape = true;

}

//******************************************************************************
//
//
//
//******************************************************************************

var mouse_X = 0;
var mouse_Y = 0;

document.onmousemove=MoveHandler;

if (netscape) {

  window.captureEvents(Event.MOUSEMOVE);
  window.onMouseMove = MoveHandler;

}

function MoveHandler(e) {

  mouse_X = (netscape) ? e.pageX : event.x;
  mouse_Y = (netscape) ? e.pageY : event.y + document.body.scrollTop;

  var str = 'MPos = ';

  str = str + mouse_X + ':' + mouse_Y;

  el=document.getElementById('d_test');

  if  (el) {

    el.innerHTML = str;

  }
}

var PpOwn = null;

timerProc = setInterval("check_popup()", 300);

function mouse_inside(elem) {

  var ElPos = getElPos(elem);

  var r = ElPos.l + ElPos.w;
  var b = ElPos.t + ElPos.h;

  if (mouse_X >= ElPos.l && mouse_X <= r && mouse_Y >= ElPos.t && mouse_Y <= b) {

    return true;

  }

  return false;
}

function check_margins(elem) {

  var ElPos = getElPos(elem);

  var r = ElPos.l + ElPos.w;
  var b = ElPos.t + ElPos.h;

  var hor  = r - mouse_X;
  var vert = b - mouse_Y;

  return {"hor":hor, "vert":vert};
  
}

function on_PopUp() {

  pp_content = get_pp_content(PpOwn);

  if (!pp_content) { return; }

  elPpc = document.getElementById('rds_popup_cntt');

  elPpc.innerHTML = pp_content;

  elPp = document.getElementById('rds_popup');

  elPp.style.display = 'block';

  var mm = check_margins(PpOwn);

//alert(mm.hor + ' / ' + mm.vert);

  var X_margin = -6;
  if (X_margin > mm.hor)
  {
	  X_margin = mm.hor - 1;
  }
  
  var Y_margin = 12; // 64
  if (Y_margin > mm.vert)
  {
	  Y_margin = mm.vert - 1;
  }
  
  elPp.style.left = mouse_X + X_margin;
  elPp.style.top  = mouse_Y + Y_margin;
}

function check_popup() {

  elPp = document.getElementById('rds_popup');

  if (!elPp) {

    return;

  }

  if (elPp.style.display == 'none') {

    if (PpOwn == null) {
      return;
    }

    if (mouse_inside(PpOwn)) {
      on_PopUp();
      return;
    }

  } else {

    if (mouse_inside('rds_popup')) {

     return;

    }

    if (PpOwn == null) {

      elPp.style.display ='none;'
      return;

    } else {

      if (mouse_inside(PpOwn) == false) {

        elPp.style.display ='none';
        return;
      }
    }
  }
}

function pp_mOvr(owner) {    // alert('debug');

  if (PpOwn == owner) {

    return;

  } else {

    elPp = document.getElementById('rds_popup'); if (!elPp) { return;  }

    if (elPp.style.display != 'none') {

      elPp.style.display = 'none';

    }
  }

  if (timerProc != null) {

    clearInterval(timerProc);

  }

  PpOwn = owner;

  timerProc = setInterval("check_popup()", 300);
}

//******************************************************************************
//
// 'text_selector' functions...
//
//******************************************************************************

var ts_ID = '';
var ts_IDClick = '';
var ts_tltmr = null;
var ts_val_prior = '';
var ts_val = '';
var ts_hidden = false;
var ts_OFF_OPERATION = false;
var ts_anchors = new Array;
var ts_cur_line = -1;

function ts_onFocus() {
  
  ts_tltmr=window.setInterval('upd_text_selector()',1000);

}

function ts_onBlur() {
  
  ts_OFF_OPERATION = true;
  
}


function text_selector_hilight(shift) {

  if (ts_cur_line != -1) {
  
    elAnchor = document.getElementById(ts_anchors[ts_cur_line]);

	if (elAnchor) {
	  elAnchor.className='a_text_selector'; 
	}
  }
  
  ts_cur_line += shift;

  var max_row = ts_anchors.length - 1;

  if (ts_cur_line < 0) {
  
    ts_cur_line = 0;
  }
  
  if (ts_cur_line > max_row) {
  
    ts_cur_line = max_row;
  }
  
  for (i = 0; i <= max_row; i++) {
    
	elAnchor = document.getElementById(ts_anchors[i]);

	if (!elAnchor) {
	  
	  continue;
	
	}
    
	if (ts_cur_line == i) {
      
	  elInput=document.getElementById(ts_ID);
	  
	  if (elInput) {
	    
        ts_val = elAnchor.alt;
        ts_val_prior = ts_val;
		elInput.value = ts_val;
	  
	    elAnchor.className='a_text_selector_h'; 
	  }
	}
  }
}

function text_selector_key_up() { text_selector_hilight(-1) }

function text_selector_key_dn() { text_selector_hilight(1) }

fun_key_up = text_selector_key_up;
fun_key_dn = text_selector_key_dn;

function move_text_selector() {

  elTs = document.getElementById('text_selector');
  
  if (!elTs) { return;  }

  elTs.style.display = 'block';

  pos = getElPos(ts_ID)
  
  var elTs_x = pos.l;
  
  var elTs_y = pos.t + pos.h;

  elTs_x--;
  
  elTs_y--;
  
  elTs.style.width = pos.w;
  
  elTs.style.left  = elTs_x;
  
  elTs.style.top   = elTs_y;
}	

function upd_text_selector() {
  
  if (ts_OFF_OPERATION) {
  
    window.clearInterval(ts_tltmr);
  
    ts_tltmr = null;
  
    elTs = document.getElementById('text_selector');
  
    if(elTs) { elTs.style.display='none'; }
  }
  
  ts_OFF_OPERATION = false;
  
  if (ts_hidden) { 
  
    return false;
  
  }
  
  el=document.getElementById(ts_ID);
  
  ts_val = el.value;
  
  if (ts_val.length > 2 != 0 && ts_val != ts_val_prior) {
    
	var cmd_line='upd_input_selector(' + ts_ID + ',' + ts_val + ',' + ts_IDClick + ')';

	call_submit('frm_rds','rds','rds|rds',cmd_line);
    
	move_text_selector();
	
	ts_val_prior = ts_val;

	ts_cur_line = -1;
	
	// text_selector_hilight(0);
  } 
  
  return false;
}

//******************************************************************************
//
//
//
//******************************************************************************

tips = new Array();

function off_all_tips() {
  
  for (i = 0; i < tips.length; i++) {
    
	el=document.getElementById(tips[i]);

    if  (el) {
	  
	  el.style.display='none';
    }
  }
} /* off_all_tips() */

function on_tip(tip,master,width) {
  
  off_all_tips();

  elTip=document.getElementById(tip);

  if (!elTip) {
    
	return;  
  
  }
  
  var ps_master = getElPos(master);

  elTip.style.display='block';
  
  var tip_h = getElPos(tip).h;
  
  elTip.style.width=width;
  
  elTip.style.left = ps_master.l + 5;
  
  elTip.style.top  = ps_master.t - tip_h - 3;

} /* on_tip(tip,master) */
