/*
	============================================================================

	Maintenance Log

	Date       Who Synopsis
	---------- --- -------------------------------------------------------------
	2006/12/15 DAG Last version turned over to DAG.

	2007/04/30 DAG 1) Align code & change argument names to improve legibility.

	               2) Add function ApplyNewStyle, to apply a new style to a list
				      of objects.

				   3) Add functions DisableControls and EnableControls, to
				      disable and enable controls on forms.

	============================================================================
*/

function urlparam ( pName , pValue )
{
	return '&' + pName + '=' + escape ( pValue ) ;
}

// Use a regular expression to trim leading and trailing spaces from a string.
function trim ( pValue )
{
   var temp = pValue;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if ( obj.test ( temp ) ) { temp = temp.replace ( obj , '$2' ) ; }
   return temp ;
}

// Add an option to a given form select control.
function selectAddOption ( pCtrlName )
{
	ctrl	= $(pCtrlName) ;
	v		= prompt ( 'Enter a new value' , '' ) ;
	if ( !(v==null ) )
	{
		n					= ctrl.options.length;
		ctrl.options[n]		= new Option(v);
		ctrl.selectedIndex	= n;
	}
}

// Returns the given object by name. Uses DOM method,
// not older IE/NS/form methods.
//function $(n) {
//	return document.getElementById(n);
//	}

// Attempt to set a hidden form field called "Command" to the
// specified value, then submit the form called "MainForm".
function submitMainForm ( pCmd ) {
	window.document.forms.MainForm.Command.value = pCmd;
	if ( window.document.forms.MainForm.onsubmit ) window.document.forms.MainForm.onsubmit();
	window.document.forms.MainForm.submit();
}
// Event handler for trapping textarea Tabs to allow actual
// tabs within textarea.
function handleTAB ( pEvent , pField )
{
	pEvent = window.event ? event : pEvent && pEvent.keyCode ? pEvent : null ;
	if ( pEvent && pEvent.keyCode == 9 ) { pField.value += '\t' ; return false ; }
	return true;
}

// This block doesn't appear to be part of any function.

if (typeof DOMParser == "undefined") {
   DOMParser = function () {}
   DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
         var d = new ActiveXObject("MSXML.DomDocument");
         d.loadXML(str);
         return d;
      } else if (typeof XMLHttpRequest != "undefined") {
         var req = new XMLHttpRequest;
         req.open("GET", "data:" + (contentType || "application/xml") +
                         ";charset=utf-8," + encodeURIComponent(str), false);
         if (req.overrideMimeType) {
            req.overrideMimeType(contentType);
         }
         req.send(null);
         return req.responseXML;
      }
   }
}

function isMSIE ( )
{
	return ( navigator.userAgent.indexOf ( "MSIE" ) >= 0 ) ;
}

function checkBrowser ( )
{
	var brows = "" ;
	var agt=navigator.userAgent.toLowerCase ( ) ;
	var is_major	= parseInt ( navigator.appVersion ) ;
    var is_minor	= parseFloat ( navigator.appVersion ) ;
	
	var is_nav		= ( ( agt.indexOf ( 'mozilla' ) != -1 ) && ( agt.indexOf ( 'spoofer ' ) == -1 )
                      && ( agt.indexOf ( 'compatible' ) == -1 ) && ( agt.indexOf ( 'opera' ) == -1 )
                      && ( agt.indexOf ( 'webtv' ) == -1 ) && ( agt.indexOf ( 'hotjava' ) == -1 ) ) ;
	var is_nav6		= ( is_nav && ( is_major == 5 ) ) ;
	
	var is_ie		= ( ( agt.indexOf ( "msie" ) != -1 ) && ( agt.indexOf ( "opera" ) == -1 ) ) ;
	var is_ie3		= ( is_ie && (is_major < 4 ) ) ;
    var is_ie4		= ( is_ie && (is_major == 4 ) && ( agt.indexOf ( "msie 4" ) != - 1 ) ) ;
    var is_ie4up	= ( is_ie && (is_major >= 4 ) ) ;
    var is_ie5		= ( is_ie && (is_major == 4 ) && ( agt.indexOf ( "msie 5.0" ) !=-1 ) ) ;
    var is_ie5_5	= ( is_ie && (is_major == 4 ) && ( agt.indexOf ( "msie 5.5" ) !=-1 ) ) ;
    var is_ie5up	= ( is_ie && !is_ie3 && !is_ie4 ) ;
    var is_ie5_5up	= ( is_ie && !is_ie3 && !is_ie4 && !is_ie5 ) ;
    var is_ie6		= ( is_ie && (is_major == 4 ) && (agt.indexOf ( "msie 6." ) != -1 ) ) ;
    var is_ie6up	= ( is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5 ) ;
	
	if ( is_ie5up | is_ie5_5up )
	{
		brows = "IE" ;
	} else if ( is_nav6 )
	{
		brows = "NS" ;
	}
	if ( brows == "IE" | brows == "NS" )
	{
		return brows ;
	} else {
		alert ( "Your browser may not be compatible.  You must install IE 5 or higher or Netscape 6.2 or higher." ) ;
		return brows ;
	}
}

// Return a reference to an object in a way that is browser neutral.
function switchDom ( pLoc , pObjName )
{
	// alert( 'checkBrowser = ' + checkBrowser ( ) ) ;
	if ( checkBrowser ( ) == "NS" ) {
		// alert ( 'pObjName = ' + pObjName ) ;
		return eval(pLoc).document.getElementById(pObjName) ;
	} else if ( checkBrowser ( ) == "IE" ) {
		// alert ( 'pObjName = ' + pObjName ) ;
		return eval(pLoc).document.all(pObjName) ;
	}
}

// Hide/open apprnumberdiv on course page.
function showHide ( pobj , pbtn , phideobj )
/*
	All calls in existing code pass only two arguments.
	The third argument, phideobj, is unreferenced.
*/
{
	var thisObj = switchDom ( this , pobj ).style ;
	var thisBtn = switchDom ( this , pbtn ) ;
	//alert(oldBtnVal);
	if ( thisObj.display == "none" ) {
		thisObj.display		= "inline" ;
		thisBtn.value		= thisBtn.value2 ;
	} else {
		thisObj.display		= "none" ;
		thisBtn.value		= thisBtn.value1 ;
	}
}

// Confirm the deletion of a record.
function confirmDel ( pfrm )
{
	var execute = confirm ( "Delete record? Click OK to delete or click Cancel to leave the record." ) ;
	if ( execute ) {
		var frmObj			= switchDom ( this , pfrm ) ;
		frmObj.Action.value	= "Delete" ;
		frmObj.submit() ;
	}
}

function ApplyNewStyle ( pStyleName , pApplyTo )
/*
	Name:		ApplyNewStyle

	Synopsis:	Apply a new style to a list of objects.

	Arguments:	pStyleName	= New style to apply to objects.

				pApplyTo	= Semicolon delimited list of objects to which style
							  is to be applied

	Author:		David A. Gray of Simple Soft Services, Inc., d/b/a WizardWrx
				on behalf of The Edit House, Inc., d/b/a The Learning Agency.

	Created:	Thursday, 26 April 2007 and Friday, 27 April 2007.

	Copyright:	2007, The Edit House, Inc., d/b/a The Learning Agency.
				All rights reserved world wide.

	References:	http://www.w3schools.com/jsref/jsref_split.asp
				http://www.tizag.com/javascriptT/javascript-string-split.php
				http://www.w3schools.com/jsref/jsref_length_string.asp
				http://www.w3schools.com/jsref/jsref_obj_array.asp
				http://www.w3schools.com/jsref/jsref_length_array.asp
				http://www.wizardwrx.com/p6frmlib.js

	Modification history:

	Date	   Who Synopsis
	---------- --- -------------------------------------------------------------
	2007/04/27 DAG Initial releae.
	============================================================================
	indexOf
*/
{
	if ( pStyleName.length && pApplyTo.length ) {
		var aToChange	= pApplyTo.split ( ";" ) ;
		var NTargets	= aToChange.length ;
		var ItemIndex	= 0 ;
		var ThisTarget
		for ( ItemIndex = 0 ; ItemIndex < NTargets ; ItemIndex++ ) {
			switchDom ( this , aToChange [ ItemIndex ] ).style = pStyleName ;
	    }
		return true ;
	} else {
		return false ;
	}
}

function DisableControls ( pApplyTo )
/*
	Name:		DisableControls

	Synopsis:	Disable a control on a form.

	Arguments:	pApplyTo	= Semicolon delimited list of objects to which style
							  is to be applied

	Author:		David A. Gray of Simple Soft Services, Inc., d/b/a WizardWrx
				on behalf of The Edit House, Inc., d/b/a The Learning Agency.

	Created:	Friday, 27 April 2007 through Monday, 30 April 2007.

	Copyright:	2007, The Edit House, Inc., d/b/a The Learning Agency.
				All rights reserved world wide.

	References:	http://www.tek-tips.com/viewthread.cfm?qid=1349566
				
				CLASS Attribute | className Property
				http://msdn2.microsoft.com/en-us/library/ms533560.aspx


				DHTML Properties: Attributes/Properties
				http://msdn2.microsoft.com/en-us/library/ms533055.aspx
				(This is the master index of DHTML DOM properties provided by
				Microsoft, and is on the new MSDN Web site that has the latest
				material on it.)

				Document Object Model (DOM) Level 1 Specification (Second Edition)
				Version 1.0, W3C Working Draft 29 September, 2000
				http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/
				(This is the technical documentation of the Document Object
				Model, published by the W3C. Most of it is not of much practical
				use.)

				Appendix D: ECMA Script Language Binding
				http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/ecma-script-language-binding.html
				(This part of the DOM specification is useful for the official
				names of the various properites contained therein. For practical
				purposes, however, I suspect that the Microsoft references are
				entirely adequate.)



	Modification history:

	Date	   Who Synopsis
	---------- --- -------------------------------------------------------------
	2007/04/30 DAG Initial releae.
	============================================================================
*/
{
	//alert ( 'pApplyTo = ' + pApplyTo ) ;
	if ( pApplyTo.length ) {
		//alert ( 'pApplyTo.length = ' + pApplyTo.length ) ;
		var aToChange	= pApplyTo.split ( ";" ) ;
		//alert ( 'aToChange = ' + aToChange ) ;
		var NTargets	= aToChange.length ;
		//alert ( 'NTargets = ' + NTargets ) ;
		var ItemIndex	= 0 ;
		var ThisTarget
		for ( ItemIndex = 0 ; ItemIndex < NTargets ; ItemIndex++ ) {
			//alert ( 'ItemIndex = ' + ItemIndex ) ;
			//alert ( 'aToChange [ ItemIndex ] = ' + aToChange [ ItemIndex ] ) ;
			switchDom ( this , aToChange [ ItemIndex ] ).className	= 'disabled' ;
			switchDom ( this , aToChange [ ItemIndex ] ).disabled	= true ;
			switchDom ( this , aToChange [ ItemIndex ] ).readonly	= true ;
	    }
		return true ;
	} else {
		return false ;
	}
}

function EnableControls ( pApplyTo , pStyle )
/*
	Name:		EnableControls

	Synopsis:	Enable a control on a form.

	Arguments:	pApplyTo	= Semicolon delimited list of objects to which style
							  is to be applied

				pStyle		= Name of style to apply, replacing disabled style.

	Author:		David A. Gray of Simple Soft Services, Inc., d/b/a WizardWrx
				on behalf of The Edit House, Inc., d/b/a The Learning Agency.

	Created:	Friday, 27 April 2007 through Monday, 30 April 2007.

	Copyright:	2007, The Edit House, Inc., d/b/a The Learning Agency.
				All rights reserved world wide.

	References:	http://www.tek-tips.com/viewthread.cfm?qid=1349566

	Modification history:

	Date	   Who Synopsis
	---------- --- -------------------------------------------------------------
	2007/04/30 DAG Initial releae.
	============================================================================
*/
{
	//alert(pApplyTo);
	if ( pApplyTo.length && pStyle.length ) {
		var aToChange	= pApplyTo.split ( ";" ) ;
		var NTargets	= aToChange.length ;
		var ItemIndex	= 0 ;
		var ThisTarget
		for ( ItemIndex = 0 ; ItemIndex < NTargets ; ItemIndex++ ) {
			switchDom ( this , aToChange [ ItemIndex ] ).className		= pStyle ;
			switchDom ( this , aToChange [ ItemIndex ] ).disabled	= false ;
			switchDom ( this , aToChange [ ItemIndex ] ).readonly	= false ;
	    }
		return true ;
	} else {
		return false ;
	}
}

