/*

  ForteDotNet

  (c) 2007, Development for the Masses
  http://www.dev4masses.com

  Project JavaScript library, Public

*/

// Run search from CPUB main
function cpub_srch( fld, url ) {

  if( document.forms.length < 1 )
    return;

  var frm = document.forms[ 0 ];
  if( isnull( frm ) )
    return;

  el = frm.elements[ fld ];
  if( isnull( el ) )
    return;

  if( is_defined( el.value ) && window.RegExp && window.encodeURIComponent ) {
    var expr = el.value.replace( /\//g, ' ').
               replace( /\\/g, ' ' ).
               replace( /\+/g, ' ' ).
               replace( /\#/g, ' ' ).
               replace( /\&/g, ' ' ).
               replace( /^\s*/, '' ).
               replace( /\s*$/, '' );

    if( expr.length > 2 ) {
      url = url + "?" + encodeURIComponent( expr );
      window.location = url;
      return;
    } // if length ok

    el.value = expr;
  } // if value ok

  el.focus();
} // cpub_srch


// Set theme
function theme_set( theme ) {

  //alert( "Switch to theme: " + theme );

  if( document.forms.length < 1 )
    return;

  var frm = document.forms[ 0 ];
  if( isnull( frm ) )
    return;

  el = frm.elements[ "theme" ];
  if( isnull( el ) )
    return;

  el.value = theme;

  frm.submit();
} // theme_set


// -----------------------------------------------
// 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 ) + '_off.gif';
  else
    img.src = img.src.substring( 0, img.src.length - 8 ) + '_on.gif';
}

function img_on( nm ) {
  img_over( nm, true );
	return true;
}

function img_off( nm ) {
  img_over( nm, false );
	return true;
}

// Clear GB fields
function gb_clear_all( fld_ttl, fld_body, fld_author ) {

  if( document.forms.length < 1 )
    return;

  var frm = document.forms[ 0 ];
  if( isnull( frm ) )
    return;

  var ttl = frm.elements[ fld_ttl ];
  if( isnull( ttl ) )
    return;

  var body = frm.elements[ fld_body ];
  if( isnull( body ) )
    return;

  ttl.value = '';
  body.value = '';

  var author = frm.elements[ fld_author ];
  if( isnull( author ) )
    return;

  author.value = '';

} // gb_clear_all

// 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 = 'id';
  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( 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 = 'id';
  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;
  }
}

// Print contents of current page
function cpub_print() {
  if( window.print ) {
    self.print();
  } else {
    if( navigator.appName.indexOf( "Microsoft" ) !=-1 )
      VBPrint()
  }
} // cpub_print