/*
File:		GLOBAL.JS
Author:		Lighthouse Information Systems, Inc.	www.lhinfo.com
Updated:	$Id: global.js,v 1.4 2008/09/12 15:57:57 kharris Exp $
Notes:		JavaScript document for global functions only
*/

/* -------------------- */
/*
include
Allows javascript to include other javascript file.
*/
function include(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    if ( html_doc != null )
    {
	    var js = document.createElement('script');
	    js.setAttribute('language', 'javascript');
	    js.setAttribute('type', 'text/javascript');
	    js.setAttribute('src', script_filename);
	    html_doc.appendChild(js);
    }
    return false;
}

/*
checkEnter
Checks to see if <ENTER> key was pressed
*/
function checkEnter(e)
{
	var obj = null;
	if ( e.srcElement != null )
	{
		obj = e.srcElement;
	}
	else
	{
		obj = e.target;
	}
	if ( obj != null )
	{
		//e is event object passed from function invocation
		var characterCode // literal character code will be stored in this variable
		if(e && e.which)
		{
			//if which property of event object is supported (NN4)
			e = e;
			characterCode = e.which; //character code is contained in NN4's which property
		}
		else
		{
			//e = window.event;
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}
		
		if(characterCode == 13)
		{
			return true;
		}
		else
		{
			return false
		}
	}
}

/*
clearArea
 Clear an HTML area by ID.
*/
function clearArea ( areaId )
{
	var myId = (areaId) ? areaId : "listDiv";
	var myArea = document.getElementById ( myId );
	if ( myArea != null )
	{
		myArea.innerHTML = '';
		while ( myArea.childNodes.length > 0 )
		{
			myArea.removeChild ( myArea.childNodes[0] );
		}
	}
}

/*
createParmsString
 extract just the parameters/query string from an href element.
*/
function createParmsString ( hrefString, laction )
{
	var newString = "";
	if ( hrefString != null )
	{
		if ( laction != null )
		{
			newString = "?laction=" + laction;
		}
		var tmpArray = new Array;
		var pos = 0;
		if (( pos = hrefString.indexOf ( "?" )) != -1 )
		{
			newString += (newString =="" ? "?" : "&") + hrefString.substr ( pos+1 );
			//var tmpArray = newString.split ( /&/ );
		}
	}
	return newString;
}

function getFileName ()
{
	var myPath = document.location.pathname;
	return myPath.substr (  myPath.lastIndexOf ( '/' ) + 1)
}
function getBasePath ()
{
	var pathList = new Array ( 'admin', 'school' );
	var myPath = document.location.pathname;
	var lastSlash = myPath.lastIndexOf ( '/' );
	myPath = myPath.substr ( 0, lastSlash );
	var tstDir = '';
	while ((tstDir = pathList.shift()) != null )
	{
		var myIdx = -1;
		if (( myIdx = myPath.indexOf ( "/" + tstDir )) != -1 )
		{
			myPath = myPath.substr ( 0, myIdx );
			break;
		}
	}
	return myPath;
}
/*
getMyPath
 Get the document path and strip the file name from it.
*/
function getMyPath ()
{
	var myPath = document.location.pathname;
	var lastSlash = myPath.lastIndexOf ( '/' );
	return ( myPath.substr ( 0, lastSlash ));
}

/*
addEvent
Platform independent method for adding events
*/
function addEvent ( obj, evType, fn, useCapture )
{
	if ( obj.addEventListener )
	{
		// stop gap measure to prevent Firefox from ignoring 'on' events when returning false
		if ( evType == 'submit' )
		{
			obj['on' + evType] = fn;
		}
		obj.addEventListener ( evType, fn, useCapture );
	}
	else if ( obj.attachEvent )
	{
		return obj.attachEvent ( 'on' + evType, fn );
	}
	else
	{
		obj['on' + evType] = fn;
	}
	return false;
}

/*
removeEvent
Platform independent method for removing events
*/
function removeEvent(obj, evType, fn, useCapture )
{
	if ( obj.removeEventListener )
	{
		// stop gap measure to prevent Firefox from ignoring 'on' events when returning false
		if ( evType == 'submit' )
		{
			obj['on' + evType] = null;
		}
		obj.removeEventListener(evType,fn,useCapture);
		return false;
	}
	else if ( obj.detachEvent )
	{
		obj.detachEvent( 'on' + evType, fn );
	}
	else
	{
		obj['on' + evType] = null;
	}
		
}
/*
 * stopEvent
 * Platform independent method for stopping events
 */
function stopEvent (e)
{
	if (!e)
	{
		var e = window.event;
	}
	e.cancelBubble = true;
	if (e.stopPropagation)
	{
		e.stopPropagation();
	}
	if (e.preventDefault)
	{
		e.preventDefault();
	}
	else
	{
		window.event.returnValue=false;
	}
	return false;
}

/*
returnFalse
stop gap measure to prevent Firefox from ignoring 'on' events when returning false
*/
function returnFalse (e)
{
	stopEvent (e);
	return false;
}

// Sets the keyboard focus to the first text element of the first form on the screen
function setFocus (e)
{
	var myForms = document.getElementsByTagName("form");
	if ( myForms != null )
	{
		if ( myForms[0] != null )
		{
			// If the qwkSearch form is availble it may be hiddent so don't set focus to it.
			if ( myForms[0].name != 'qwkSearch' )
			{
				var fldCnt = myForms[0].elements.length;
				var myField = null;
				for ( var i = 1; i < fldCnt; i++ )
				{
					myField = myForms[0].elements[i];
					if ( myField.type != 'hidden' &&
					     !myField.disabled &&
					     !myField.readonly )
					{
						myField.focus();
						//myField.select();
						break;
					}
				}
			}
		}
	}
}

/**
 * showCurrentImage
 * @param {XML Obj, String} XML, imgElementId 
 */
function showCurrentImage(XML, imgElementId)
{
	var myImgId = (imgElementId) ? imgElementId : "currentimg";
	var mySection = document.getElementById(myImgId);
	if ( mySection != null )
	{
		var myRoot  = XML.documentElement;
		var xRecords = myRoot.getElementsByTagName("records")[0];
		if ( xRecords != null )
		{
			var myForms = xRecords.getElementsByTagName("forms")[0];
			if ( myForms != null && myForms.hasChildNodes )
			{
				var myCurrentForm = myForms.firstChild;
				if ( myCurrentForm.nodeName == "#text" )
				{
					myCurrentForm = myCurrentForm.nextSibling;
				}
				if ( typeof ( fillFormById ) == "function" )
				{
					var xFormData = myCurrentForm.getElementsByTagName("formdata")[0];
					if ( xFormData != null && xFormData.firstChild != null )
					{
						var xImgData = xFormData.getElementsByTagName("currentimage")[0];
						if ( xImgData != null && xImgData.firstChild != null )
						{
							var lnkElements = (xImgData.firstChild.nodeValue).split(",");

							var dspWidth = 200;
							var dspHeight = 200;
							if ( lnkElements[2] > 200 || lnkElements[3] > 200 )
							{
								if ( lnkElements[2] != lnkElements[3] )
								{
									if ( lnkElements[2] > lnkElements[3] )
									{
										var myRatio = 200 / lnkElements[2];
										dspHeight = lnkElements[3] * myRatio;
									}
									else
									{
										var myRatio = 200 / lnkElements[2];
										dspHeight = lnkElements[3] * myRatio;
									}
								}
							}
							else
							{
								dspWidth = lnkElements[2];
								dspHeight = lnkElements[3];
							}
							var currentImage = document.createElement( "img" );
							currentImage.setAttribute ( "src", getBasePath() + "/showimage.php?id=" + lnkElements[0] );
							currentImage.setAttribute ( "alt", lnkElements[1] );
							currentImage.setAttribute ( "title", lnkElements[1] );
							currentImage.setAttribute ( "width",  dspWidth );
							currentImage.setAttribute ( "height", dspHeight );
							clearArea ( myImgId );
							mySection.appendChild ( currentImage );
						} // End of Image Data
					}
				}
			}
		}
	}	
}

/*
 * hrefIsImage
 * Checks to see if the "link" that was clicked is an image instead of text
 */
function hrefIsImage ( hrefObj )
{
	var retVal = false;
	if ( hrefObj != null )
	{
		if ( hrefObj.toString().indexOf ( "HTMLImageElement" ) != -1 )
		{
			retVal = true;
		}
		else
		{
			if ( hrefObj.href != null && ( 
				 hrefObj.href.indexOf("png") != -1 ||
				 hrefObj.href.indexOf("jpg") != -1 ||
				 hrefObj.href.indexOf("gif") != -1 ))
			{
				retVal = true;
			}
		}
	}
	return retVal;

}

/*
 * addClass
 * Adds a new class to an HTML element
 */
function addClass ( object, classname )
{
	if ( object != null )
	{
		var myClass = object.className;
		if ( myClass.indexOf ( classname ) == -1 )
		{
			myClass += ' ' + classname;
		}
		object.className = myClass;
	}
}

/*
 * removeClass
 * removes a class from an HTML element
 */
function removeClass ( object, classname )
{
	if ( object != null )
	{
		var myClass = "";
		var tmpClass = object.className;
		if ( tmpClass != null )
		{
			var myNames = tmpClass.split ( " " );
			var j = 0;
			for ( var i = 0; i < myNames.length; i++ )
			{
				if ( myNames[i] != classname )
				{
					if ( j++ == 0 )
					{
						myClass += myNames[i];
					}
					else
					{
						myClass += " " + myNames[i];
					}
				}
			}
		}
		object.className = myClass;
	}
}

/*
 * delay
 * provides a timer
 */
function delay(gap)
{ 
  /* gap is in millisecs */
  var then,now; 
  then=new Date().getTime();
  now=then;
  while((now-then)<gap)
  {
    now=new Date().getTime();
  }
}
/*
 * sleep
 * wrapper for delay
 */
function sleep(gap)
{
	delay(gap);
}
/* -------------------- */