function isnull( o ) {
  return typeof( o ) == 'undefined' || o == null || !o;
} // isnull


// in registartion: display advanced fields if gender is women
function disp_women_adv( rbl_nm, block ) {
var rbl;
var frm = document.forms[0];
var i = 0;

  rbl = frm.elements[ rbl_nm ];

  if( null == block )
    return;

  i = 0;

  if( null != rbl ) {
    if( rbl[1].checked ) {
      block.style.display = "block";
      while( i < 3 ) {
        block = block.nextSibling;
        if( block.tagName == 'TR' ) {
          block.style.display = "block";
          i++;
        }
        if( i == 2 )
        break;

      }

    } else {
      block.style.display = "none";
      while( i < 3 ) {
        block = block.nextSibling;
        if( block.tagName == 'TR' ) {
          block.style.display = "none";
          i++;
        }
        if( i == 2 )
        break;

      }

    }
  }
}

// check if either one checkbox is seled on forms[ 0 ]
function check_on_seled( nm ) {
var el = null;
var i, frm = document.forms[0];
var nof = 0;
var s;

  if( !nm ) nm = '';
  nof = nm.length;
  for( i = 0; i < frm.elements.length; i++ ) {
  	el = frm.elements[i];
    if( ( el.type == 'checkbox' ) && ( el.checked==true ) ) {
      if( nof == 0 ) return true;
      s = el.name.substr( el.name.length - nof, nof );
      if( s == nm ) return true;
    }
  }
  return false;
}

// select all checkboxes on form[ 0 ]
function sel_all_chboxs( nm ) {
var frm = null;
var chk_val = 1;
var el, nof = 0, s;

  // form init
  frm = document.forms[0];
  if( frm == null ) return;

  if( !nm ) nm = '';
  nof = nm.length;
	chk_val = check_on_seled( nm );
  for( var i = 0; i < frm.elements.length; i++ ) {
    el = frm.elements[i];
    if( el.type != 'checkbox' )
      continue;
    if( nof != 0 ) {
      s = el.name.substr( el.name.length - nof, nof );
      if( s != nm )
        continue;
    }
    frm.elements[i].checked = 1 - chk_val;
  }
}

function chk_if_seled( nm ) {
var is_chked = check_on_seled( nm );

  if( !is_chked ) {
    alert( 'Maak eerst een selectie' );
    return false;
  }
  return true;
}

function confirm_rm_if_seled( msg, nm ) {
var is_chked = check_on_seled( nm );

  if( !is_chked ) {
    alert( 'Maak eerst een selectie' );
    return false;
  }
  return confirm( msg );
}

// Check if textbox is not empty
function chk_txt_val( nm, msg ) {
var el = null;
var i, frm = document.forms[ 0 ];

   	el = frm.elements[ nm ];
    if( ( el.type == 'text' || el.type == 'textarea' ) && ( el.value.length > 0 ) )
      return true;

  alert( msg );
  return false;
}

function alert_msg( msg ) {
  alert( msg );
  return false;
}

// Check if dropdownlist is not empty
function chk_ddl_val( nm, msg ) {
var el = null;
var i, frm = document.forms[ 0 ];

   	el = frm.elements[ nm ];
   	if( el != null && el.selectedIndex > 0 )
      return true;

  alert( msg );
  return false;
}

// Open Popup
function popup( url, w, h, wnd_nm ) {
  if( w > 600 ) w = 600;
  if( h > 400 ) h = 400;

  newwin = window.open( url + '&rand=' + Math.random(99999), wnd_nm,
                       "height=" + h + ",width=" + w +
                       ",status=no,toolbar=no,menubar=no,scrollbars=yes,directories=no,resizable=yes" );
  newwin.focus();
} // popup

function get_lb_seled( lb_id ) {
  var frm = document.forms[0];

  lb = frm.elements[ lb_id ];
  if( lb == null )
    return false;

  if( lb.selectedIndex < 0 )
    return false;

  return lb.options[ lb.selectedIndex ];
}

//
function btn2Popup( url, w, h, lb_id, win) {
  var item = get_lb_seled( lb_id );
  if( item == false ) {
    alert( "Nothing selected, please, select item first." );
    return false;
  }
  url += '&btnid=' + item.value + '&btntxt=' + escape(item.text);
  popup( url, w, h, win);
}

function par_wnd_submit( flag ) {
var pwnd, pfrm;

  if( !window.opener || !window.opener.document ) {
    alert( "Parent window not found." );
    return false;
  }

  if( window.opener.document.forms.length < 1 ||
      !window.opener.document.forms[0] ) {
    window.opener.location = eval( 'window.opener.location' );
  } else {

    pfrm = window.opener.document.forms[0];

  	// Flag
  	// if( flag && flag != null && pfrm.d4m_flag != null && flag != '' ) {
  	if( flag && flag != null && pfrm.d4m_flag != null ) {
  	  pfrm.d4m_flag.value = flag;
  	  // pfrm.__VIEWSTATE.value = '';
  	}

	  pfrm.submit();
	  window.close();

  } // if
}


// Submit parent form
function par_frm_submit( act, arg ) {
var pwnd, pfrm;

  if( !window.opener || !window.opener.document ) {
    alert( "Parent window not found." );
    return false;
  }

  if( window.opener.document.forms.length < 1 ||
      !window.opener.document.forms[0] ) {
    window.opener.location = eval( 'window.opener.location' );
  } else {
    pfrm = window.opener.document.forms[0];
  	if( act && act != null && pfrm.d4m_act != null && act != '' ) {
  	  pfrm.d4m_act.value = act;
  	  pfrm.__VIEWSTATE.value = '';
  	}
  	if( arg && arg != null && pfrm.d4m_arg != null && arg != '' ) {
  	  pfrm.d4m_arg.value = arg;
  	  pfrm.__VIEWSTATE.value = '';
  	}
	  pfrm.submit();
  }
}

function par_frm_submit_cmd( url, cmd, prm ) {
  pwnd = window.opener;

  if( !pwnd ) {
    alert( "Parent window not found." );
    return false;
  }

  //alert( "Old: " + pwnd.location + "\nNew: " + url + '&cmd=' + escape( cmd ) + '&prm=' + escape( prm ) );
  pwnd.location = url + '&cmd=' + escape( cmd ) + '&prm=' + escape( prm );
}

// 'focused' color
// var edt_bg_clr="#ece7f3";
var edt_bg_clr="#EEEEEE";
// 'return' color storage
var last_edt_bg_clr = "#ffffff";

function clr_on( el )  {
  last_edt_bg_clr = el.style.backgroundColor;
  el.style.backgroundColor=edt_bg_clr;
}
function clr_off( el ) {
  el.style.backgroundColor=last_edt_bg_clr;
}

/*
 * Description: Lookup for input control by ID on current page
 * Returns:     true if form found; false otherwise.
*/
function get_inp( nm ) {
var ctrl, objs, i;

  objs = document.getElementsByTagName( 'input' );
	for( i = objs.length; --i >= 0 && ( ctrl = objs[ i ] ); )
		if( 'object' == typeof( ctrl ) && nm == ctrl.name )
			return ctrl;

  return null;
} // get_inp

function ch_notify_status( id, hdn_nm ) {
var theform = null;
var hdn;

	if( window.navigator.appName.toLowerCase().indexOf( 'netscape' ) > -1 )
		theform = document.forms[ 'main' ];
	else
		theform = document.main;

	if( !theform || theform == null )
		theform = document.forms[ 0 ];

	if( typeof( theform.onsubmit ) == 'function' &&
      theform.onsubmit != null )
    theform.onsubmit();

	hdn = get_inp( hdn_nm );
  if(	null != hdn )
    hdn.value = id;

	theform.submit();

} // ch_notify_status

// -----------------------------------------------
// Image over functions

function img_over( nm, is_on ) {
var img;

  if( ! document.images )
    return;

  img = eval( 'document.' + nm );
  if( is_on == false )
    img.src = img.src.substring( 0, img.src.length - 7 ) + '.gif';
  else
    img.src = img.src.substring( 0, img.src.length - 4 ) + '_on.gif';
}

function img_on( nm ) {
  img_over( nm, true );
	return true;
}

function img_off( nm ) {
  img_over( nm, false );
	return true;
}

// -----------------------------------------------
// Search
// -----------------------------------------------
function srch_def( txt_ctrl, def_txt, is_on, color ) {

  if( is_on ) {
    if( def_txt == txt_ctrl.value )
      txt_ctrl.value = '';
    txt_ctrl.style.color = '#000000';
  } else {
    if( '' == txt_ctrl.value )
      txt_ctrl.value = def_txt;
    //txt_ctrl.style.color = '#b1a0cc';
    txt_ctrl.style.color = color;
  } // if

} // srch_def

// -----------------------------------------------
// Pin at Home
// -----------------------------------------------
function go_2_pg( pg_id, mode, args ) {
  __doPostBackD4M( 'pg_' + pg_id, '', mode, args, '', '' );
} // go_2_pg

// -----------------------------------------------
// Go to 123 Reunie
// -----------------------------------------------
function go2reunie( ctrl_nm, mode, args, tgt ) {
  __doPostBack_lbtn( 'd4m_act', 'd4m_arg', ctrl_nm, '', mode, args, tgt, true );
} // go2reunie

// -----------------------------------------------
// Go to Tell a Friend
// -----------------------------------------------
//function go2taf( ctrl_nm, mode, cmd, args, tgt ) {
//  __doPostBack_lbtn( 'd4m_act', 'd4m_arg', ctrl_nm, '', mode, '', tgt, true );
//} // go2taf
function go2taf( tgt ) {
  if( 'undefined' == tgt || null == tgt )
    return;
  location.href = tgt;
} // go2taf


// -----------------------------------------------
// Check if Rich editor in HTML mode
// -----------------------------------------------

function redt_can_submit() {
  if( typeof( editor ) == 'undefined'
   || typeof( editor._editMode ) == 'undefined'
   || editor._editMode != "textmode" )
    return true;

  alert( 'Cannot submit data from editor cause it now in Text mode' );
  return false;
} // redt_can_submit

// -----------------------------------------------
// New buttons
// -----------------------------------------------
function sp_btn_state( btn, is_on ) {
var _l, _c, _r, _lnk;

  if( btn.id.length < 1 )
    return;

  _lnk = document.getElementById( btn.id + '_lnk' );
  _l = document.getElementById( btn.id + '_l' );
  _c = document.getElementById( btn.id + '_c' );
  _r = document.getElementById( btn.id + '_r' );

  // Link, <a> tag
  if( 'undefined' != typeof( _lnk ) ) {
    if( is_on )
      _lnk.className = _lnk.className.replace( /_off$/g, '_on' );
    else
      _lnk.className = _lnk.className.replace( /_on$/g, '_off' );
  } // if left

  // Left
  if( 'undefined' != typeof( _l ) ) {
    if( is_on )
      _l.className = _l.className.replace( /_off$/g, '_on' );
    else
      _l.className = _l.className.replace( /_on$/g, '_off' );
  } // if left

  // Center
  if( 'undefined' != typeof( _c ) ) {
    if( is_on )
      _c.className = _c.className.replace( /_off$/g, '_on' );
    else
      _c.className = _c.className.replace( /_on$/g, '_off' );
  } // if center

  // Right
  if( 'undefined' != typeof( _r ) ) {
    if( is_on )
      _r.className = _r.className.replace( /_off$/g, '_on' );
    else
      _r.className = _r.className.replace( /_on$/g, '_off' );
  } // if right

  //alert( 'Class: ' + btn.className );
}


// -------------------------------------------------------------------
// has_options(ddl)
// Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function has_options( ddl ) {
	if( !ddl || ddl == null || !ddl.options || ddl.options == null ) { return false; }
	return true;
}

// -------------------------------------------------------------------
// ddl_sort(obj)
// Pass this function a SELECT object and the options will be sorted
// by their text (display) values
// -------------------------------------------------------------------
function ddl_sort( obj, excl_id, sel_id ) {
var o = new Array();
var sel_idx = 0;

	if( !has_options( obj ) ) { return; }

	for( var i=0; i < obj.options.length; i++ ) {
		o[ o.length ] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected );
	}

	if( 0 == o.length ) { return; }
	o = o.sort(
		function(a,b) {

		  if( a.value == excl_id ) return -1;
		  if( b.value == excl_id ) return 1;
      if( a.value == excl_id && b.value == excl_id ) return 0;
//		  if( a.value == excl_id || b.value == excl_id ) return 0;
			if( (a.text + "") < (b.text + "") ) { return -1; }
			if( (a.text + "") > (b.text + "") ) { return 1; }
			return 0;
		}
	);

	for( var i = 0; i < o.length; i++ ) {
		obj.options[i] = new Option( o[i].text, o[i].value, o[i].defaultSelected, o[i].selected );
		if( typeof( sel_id ) != 'undefined' && sel_id != null && o[i].value == sel_id )
      sel_idx = i;
	}

	obj.selectedIndex = sel_idx;
  obj.disabled = false;
} // ddl_sort


// Set view
function vw_set( nm, val ) {

  if( document.forms.length < 1 )
    return;

  var frm = document.forms[ 0 ];
  if( isnull( frm ) )
    return;

  el = frm.elements[ nm ];
  if( isnull( el ) )
    return;

  el.value = val;

  frm.submit();
} // vw_set


/* Spoilers */
function hide_show_spoiler( nm ) {
var _spoiler_div;

  _spoiler_div = document.getElementById( nm );
  if( isnull( _spoiler_div ) )
    return;

  // When visible
  if( _spoiler_div.style.display == "none" )
    _spoiler_div.style.display = "block";
  else
    _spoiler_div.style.display = "none";

} // hide_show_spoiler


function spoiler_vis_set( nm, is_show ) {
var _spoiler_div;

  _spoiler_div = document.getElementById( nm );
  if( isnull( _spoiler_div ) )
    return;

  // When visible
  if( is_show )
    _spoiler_div.style.display = "block";
  else
    _spoiler_div.style.display = "none";

} // spoiler_vis_set

function lookup_spoilers( is_show ) {
var _divs;

  _divs = document.getElementsByTagName( "div" );
  if( isnull( _divs ) )
    return;

  for( var i = 0; i < _divs.length; i++ ) {
    if( _divs[ i ].id.indexOf("SPOILER_") == 0 )
      spoiler_vis_set( _divs[ i ].id, is_show );
  } // for

} // lookup_spoilers

function hide_all_spoilers() {
  lookup_spoilers( false );
} // hide_all_spoilers


function show_all_spoilers() {
  lookup_spoilers( true );
} // show_all_spoilers
