/**
*  This script contains stuff that should be defined on all pages.
*/

/**
*  Define the name-scoping object
*/
if ( ! KH )  var KH = {};

/**
*  The default directory for the "KH.include()" method will be the
*  URL specified by the global "includeUrl" variable, or if it's not
*  defined, the directory where this script (khclient.js) is located.
*  (NOTE: on FF, the result seems to always be an absolute pathname,
*  but on IE it seems to be relative, so the effect on IE depends on
*  where the script that uses "include()" is located!  So that puts
*  some constraints on the use of "include()", described below).
*/
if ( typeof(includeUrl) == "undefined" ) {
    KH.list = document.getElementsByTagName( "script" );
    includeUrl = "";
    for ( KH.i=0; KH.i<KH.list.length; KH.i++ ) {
        KH.src = KH.list[KH.i].src;
        if ( (KH.p=KH.src.indexOf("/khclient.js")) >= 0 ) {
            // if ( p>2 && KH.src.substring(p-2,p)=="kh" )  p -= 2;
	    includeUrl = KH.src.substring(0,KH.p+1);
	    break;
        }
    }
}
if ( includeUrl.charAt(includeUrl.length-1) != "/" )  includeUrl += "/";

/**
*  A function to "include" other scripts.
*
*  Since this function is writing to the page, it must NOT be used after
*  the page is loaded!  The ONLY safe use is within scripts that are
*  being sourced in the <HEAD> !!!!  E.g., this one!
*
*  NOTE: since the default directory is relative on IE, to be portable
*  if you are using this method from a file which is NOT in the same
*  place as "khclient.js" (e.g. from a <script> that's in an HTML file,
*  or from a js-file that's in a sub-directory), you either need to
*  use the "dir" argument, or else put appropriate relative prefixing
*  on "name".
*/
KH.include = function( name, dir ) {
	if ( !dir )  dir = includeUrl;
	name = dir + name;
	document.write( "<script type='text/javascript' src='" );
	document.write( name );
	document.writeln( "'><\/script>" );
}

/**
* A mechanism to set up a list of page initialization functions.
* Some tools may require that this be replaced by their own similar
* thing, or you could also use the IE or standard event stacker.
*/
KH.initList = [];
KH.addInitMethod = function( func ) {
    for ( var i=0; i<KH.initList.length; i++ ) {
        if ( KH.initList[i] == func )  return;
    }
    KH.initList.push( func );
}
window.onload = function() {
    for ( var i=0; i<KH.initList.length; i++ )  KH.initList[i]();
}

//  A few utility methods that don't fit in anywhere else

//  A "trim" function is handy for XML and plain-text processing!
//  First method below adds it to the prototype for "String" so all
//  strings will have this method.
//  
String.prototype.trim = function() {
	return this.replace(/^\s+/,"").replace(/\s+$/,"");
}
KH.trim = function( s ) {
	return s.replace(/^\s+/,"").replace(/\s+$/,"");
}

//  Get a particular named value from the query parameters
KH.getQueryParameter = function( key ) {
    var self = KH.getQueryParameter;
    //  first time called, convert the query string to a map
    if ( ! self.paramMap ) {
        self.paramMap = {};
	var q = document.location.search;
	if ( q && q.charAt(0) == "?" )  q = q.substring(1);
	var a = q.split( "&" );
	for ( var i=0; i<a.length; i++ ) {
	    var w = a[i].split("=");
	    if ( w.length<2 )  w[1] = true;
	    self.paramMap[ w[0] ] = w[1];
	}
    }
    return ( self.paramMap[key] );
}
//
//  Get/Set the value on a <select> specified as an id string or as a
//  reference to the element.  If you try to set a value that's
//  not one of the options, a new option will be added for it!
KH.setSelectValue = function( el, val ) {
    if ( el instanceof String ) el = document.getElementById( el );
    if ( !el )  throw "KH.setSelectValue: specified <select> not found";
    var opts = el.options;
    // find the specified value in the the options
    for ( var i=0; i<opts.length; i++ ) {
        if ( opts[i].value == val )  break;
    }
    // if not found, create a new option for it
    if ( i >= opts.length ) {
        opts[i=opts.length] = new Option("Value '"+val+"'",val );
    }
    el.selectedIndex = i;
}
KH.getSelectValue = function( el ) {
    if ( el instanceof String ) el = document.getElementById( el );
    if ( !el )  throw "KH.getSelectValue: specified <select> not found";
    return ( el.options[el.selectedIndex].value );
}


/**
*  Here's the set of components we want to include.  Keep in mind the
*  warnings about the use of directory names with "include()".
*/
KH.include( "open/prototype.js" );
KH.include( "open/scriptaculous.js?load=effects" );
KH.include( "kh/Logger.js" );

