/*

  ForteDotNet

  (c) 2006, Development for the Masses
  http://www.dev4masses.com

  Main Javascript library

*/

var is_ie = document.all;

function is_empty( obj ) {
  return obj == null || typeof( obj ) == 'undefined';
} // is_empty

function isnull( o ) {
  return typeof( o ) == 'undefined' || o == null || !o;
} // isnull

function is_defined( o ) {
  return typeof( o ) != 'undefined';
} // is_undefined

// Lookup control by ID
function ctrl_get( id ) {
  return document.getElementById( id );
} // ctrl_get

// Redirect page
function pg_redir( url ) {
  window.location = url;
} // pg_redir

// Get Combobox selected value
function combo_seled_val( ddl ) {
  if( isnull( ddl ) || isnull( ddl.options ) || isnull( ddl.selectedIndex ) || ddl.selectedIndex < 0 )
    return null;
  return ddl.options[ ddl.selectedIndex ].value;
} // combo_seled_val

// Get physical bounds of elements
function bounds( el ) {
  var b = new Object();

  b.left = el.offsetLeft;
  b.top = el.offsetTop;

  b.width = is_ie ? el.offsetWidth : el.scrollWidth;
  b.height = is_ie ? el.offsetHeight : el.scrollHeight;

  var par = el;
  var d = 0;
  while( ! isnull( par.offsetParent ) ) {
    par = par.offsetParent;
    b.left += par.offsetLeft;
    b.top += par.offsetTop;
  }

  b.bottom = b.top + b.height;
  b.right = b.left + b.width;

  b.fld = function( l, v ) { return l + ": " + v + "\n"; }
  b.debug = function() {
    alert(
      this.fld( "width", this.width ) +
      this.fld( "height", this.height ) +
      this.fld( "top left", this.top + "," + this.left ) +
      this.fld( "bottom right", this.bottom + "," + this.right )
    );
  }

  return b;
} // bounds

// Resize child element to fit parent element by height
function fit_height( el_id, par_id, min_height ) {
  var d = document.getElementById( el_id );
  var par = document.getElementById( par_id );
  if( isnull( d ) || isnull( par ) )
    return;

  d.style.visibility = "hidden";
  d.style.height = min_height;
  b = bounds( d ); bp = bounds( par );
  //b.debug(); bp.debug();
  d.style.height = bp.bottom - b.top;
  d.style.visibility = "visible";
} // resize


// Key watch
// -----------------------------------------------

function key_watch() {

	var ua = navigator.userAgent;
  //alert( ua );
	this.isGecko = ua.indexOf('Gecko') != -1;
	this.isNS = ua.indexOf('Netscape') != -1;
	this.isIE = ua.indexOf('Explorer') != -1 || (navigator.appName == "Microsoft Internet Explorer");

//	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
//	this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
//	this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
//	this.isSafari = ua.indexOf('Safari') != -1;
//	this.isOpera = ua.indexOf('Opera') != -1;
//	this.isMac = ua.indexOf('Mac') != -1;
//	this.isNS7 = ua.indexOf('Netscape/7') != -1;
//	this.isNS71 = ua.indexOf('Netscape/7.1') != -1;
//
//	// Fake MSIE on Opera and if Opera fakes IE, Gecko or Safari cancel those
//	if( this.isOpera ) {
//		this.isMSIE = true;
//		this.isGecko = false;
//		this.isSafari = false;
//	}
//  this.flds = new Array();

  // If events already attached
  this.is_attached = false;
  this.func = null;

  this.func_att = null;
  this.el_next_id = null;
} // key_watch.ctor()


// Methods
key_watch.prototype = {

  // Turn off keyoboard sniffing
  set_off : function() {
    watcher.func = null;
    watcher.func_att = null;
    watcher.el_next_id = null;
  }, // set_off


  // Attach 'exec function' handler
  set_func : function( fnc ) {
    watcher.set_off();

    if( isnull( fnc ) )
      return;

    if( false == watcher.is_attached )
      watcher.__attach_evs();

    // set 'call' function
    watcher.func_att = fnc;
    watcher.func = watcher.__call_func;
  }, // set_func

  // attach 'Next' element handler
  set_next : function( el_next_id ) {

    watcher.set_off();

    watcher.el_next_id = el_next_id;

    if( false == watcher.is_attached )
      watcher.__attach_evs();

    watcher.func = watcher.__mv_next;
  }, // set_next

  // Call custom function
  __call_func : function() {
    var fu = watcher.func_att;
    watcher.set_off();
    fu();
  }, // __call_func

  // Move to next element
  __mv_next : function() {

    if( isnull( watcher.el_next_id ) )
      return;

    el = document.getElementById( watcher.el_next_id );
    if( isnull( el ) )
      return;

    watcher.func = null;

    el.focus();
  }, // __mv_next

  // Attach keyboard events
  __attach_evs : function() {

    if( watcher.is_attached )
      return;

    if( watcher.isNS ) {
      if( document.layers ) { // N4
      	document.captureevents( Event.KEYPRESS );
        document.onkeypress = watcher.__on_key;
        watcher.is_attached = true;
      } else if( document.getElementById ) { // N6
        document.onkeypress = watcher.__on_key;
        watcher.is_attached = true;
      }
    } else if( watcher.isIE || watcher.isGecko ) {
      document.onkeypress = watcher.__on_key;
      watcher.is_attached = true;
    }

    // alert( 'attached? ' + watcher.is_attached );
  }, // __attach_evs


  // Key event listener
  __on_key : function( e ) {
  var key_val;

    // Attached and have function?
  	if( !watcher.is_attached || isnull( watcher.func ) )
  	  return;

    // alert( 'is_gecko: ' + watcher.isGecko + ' is_ns: ' + watcher.isNS + ' is_ie: ' + watcher.isIE );

  	if( watcher.isNS || watcher.isGecko )
  	  key_val = e.which;
  	else
  	  key_val = window.event.keyCode;

    // alert( 'Key val: ' + key_val );
  	if( key_val == 13 ) {
      // alert( watcher.func );
  	  watcher.func();
  	  return false;
  	}
  } // __on_key

} // key_watch.prototype

var watcher = new key_watch();