var IMG_PLUS  = '/i/plus.gif';
var IMG_MINUS = '/i/minus.gif';

function is_empty( x ) {
  return x == null || typeof( x ) == 'undefined';
}

// function _dbg( el ) {
//  alert( "Name: " + el.nodeName );
// }

function _up_to( fr, tag ) {
var el;

  el = fr;
  while( true ) {
    if( is_empty( el ) )
      break;
    if( el.tagName.toLowerCase() == tag.toLowerCase() )
      break;
    el = el.parentNode;
  }

  return el;
} // _up_to

function hlp_sect() {
  this.agt = navigator.userAgent.toLowerCase();
  this.is_opera  = (this.agt.indexOf("opera") != -1);
  this.is_ie	   = ((this.agt.indexOf("msie") != -1) && (false == this.is_opera ));
  this.is_mac	   = (this.agt.indexOf("mac") != -1);
  this.is_mac_ie = (this.is_ie && this.is_mac);
  this.is_win_ie = (this.is_ie && !this.is_mac);
  this.is_gecko  = (navigator.product == "Gecko");
}

var hlp = new hlp_sect();

var state = new Array();
var bk = new Array();

function _switch_row( id, tr, is_bk ) {

  if( hlp.is_gecko ) {
    if( is_empty( bk[ id ] ) ) {
      if( is_bk )
        bk[ id ] = new Array();
      else
        return;
    }

    if( is_bk ) {
      bk[ id ][ tr.rowIndex ] = tr.innerHTML;
      tr.innerHTML = '';
    } else {
      tr.innerHTML = bk[ id ][ tr.rowIndex ];
      bk[ id ][ tr.rowIndex ] = null;
    }
  } else {
    tr.style.display = is_bk ? "none" : "block";
  }
} // _switch_row

function tbl_switch( span, is_border, idx_fr, idx_to ) {
var table, trs, tr, id;

  table = _up_to( span, 'table' );
  if( is_empty( table ) )
    return;

  id = table.id;
  if( is_empty( state[ id ] ) )
    is_bk = true;
  else
    is_bk = state[ id ];

  trs = table.rows;
  if( is_empty( trs ) )
    return;

  if( is_empty( idx_fr ) ) idx_fr = 1;
  if( is_empty( idx_to ) ) idx_to = trs.length - 1;
  if( is_empty( is_border ) ) is_border = true;

  for( i = idx_fr; i <= idx_to; i++ ) {
    tr = trs[ i ];
    if( is_empty( tr ) || tr.tagName != 'TR' )
      continue;
    _switch_row( id, tr, is_bk );
  } // for

  state[ id ] = !is_bk;

  span.src = is_bk ? IMG_PLUS : IMG_MINUS;

  if( is_border )
    table.style.borderBottom = "solid 1px white";
}

function sch_data_switch( span, is_border ) {
  var ctrl = document.getElementById( span );
  if( is_empty( ctrl ) )
    return;
  tbl_switch( ctrl, is_border, 0, null );
}

function do_switch( span, rb_no_id, rb_yes_id, rb_val, is_yes ) {
var RB_NO_CTRL, RB_YES_CTRL, RB_VAL;
var old_val;

  RB_VAL = document.getElementById( rb_val );
  RB_NO_CTRL = document.getElementById( rb_no_id );
	RB_YES_CTRL = document.getElementById( rb_yes_id );

  // get old value stored in hidden
  old_val = RB_VAL.value == "1";

  // switch radios anyway
  if( true == is_yes ) {
    RB_YES_CTRL.checked = true;
    RB_NO_CTRL.checked = false;
  } else {
    RB_NO_CTRL.checked = true;
    RB_YES_CTRL.checked = false;
  }

//  // debug
//  alert( 'Old  ' + old_val + "\n" +
//         'No?  ' + RB_NO_CTRL.checked + "\n" +
//         'Yes? ' + RB_YES_CTRL.checked );

  // if need switch to same state as before,
  // skip hide/unhide
  if( is_yes == old_val )
    return;

  // hide areas
  sch_data_switch( span, false );

  // store new values
  if( true == is_yes ) {
    RB_VAL.value = "1";
  } else {
    RB_VAL.value = "0";
  }
} // do_switch

function anch_on( img ) {
  img.style.cursor = 'pointer';
}

function anch_off( img ) {
  img.style.cursor = 'auto';
}