var useID = 0, useLayers = 0, useAll = 0, N6 = 0, MSIE = 0;

if (document.getElementById) useID = 1;
if (document.layers) useLayers = 1;
if (document.all) useAll = 1;

var MSIE = (useID && useAll);
var N6   = (useID && (! useAll) );
var NS   = (navigator.appName.indexOf('Netscape') != -1)? 1 : 0;



function getObj(obj) {
  // cross platform tool for accessing the object style
  var myObj;

  if (useID) myObj = document.getElementById(obj).style;
  else if (useAll) myObj = document.all[obj].style;
  else if (useLayers) myObj = document.layers[obj];

  return myObj;
}

function getObjCore(obj) {
  // same as getObj() except it lets you access the core object
  var myObj;
  if (useID) myObj = document.getElementById(obj);
  else if (useAll) myObj = document.all[obj];
  else if (useLayers) myObj = document.layers[obj];
  return myObj;
}


function moveObj(obj, x, y)
{
  // move the specified object to the specified x,y coordinates
  var myObj = getObj(obj);

  if (document.moveTo) {
    myObj.moveTo(x,y);
  } else {
    myObj.left = x;
    myObj.top = y;
  }
}

function setWidth (obj, width)
{
    // Change an objects width
	var myObj = getObj(obj);
	myObj.width = width;
}

function setHeight (obj, height)
{
    // Change an objects height
	var myObj = getObj(obj);
	myObj.height = height;
}

function setClipHeight(obj, height)
{
    // Sets an object's clip window
	var myObj = getObj(obj);
	myObj.clip.height = height;
}


// -- The following implement the various form functions as discussed in
// -- chapter 24 of Dynamic HTML Weekend Crash Course

var formName = "forms[0]";		// default value

function setFormName(fName)
{
  // easy shortcut for setting form name. Used with getFormObj
  formName = fName;
}

function getFormObj(elementName) {
  // cross platform tool for accessing a form object
  return( eval("document." + formName + "." + elementName) );
}

// -- end of form functions

function nudgeObj(obj, x, y)
{
  // move the object (x,y) away from where it is currently
  var myObj = getObj(obj);

  if (myObj.moveBy) {
    myObj.moveBy(x, y);
  } else if (MSIE) {
    myObj.pixelLeft += x;
    myObj.pixelTop  += y;
  } else {
    myObj.left = parseInt(myObj.left) + x;
    myObj.top = parseInt(myObj.top) + y;
  }
}

function getX(obj)
{
  // return the y coordinate of the object
  var myObj = getObj(obj);

  if (MSIE) {
    x = myObj.pixelLeft;
  } else {
    x = myObj.left;
  }
  return(parseInt(x));
}

function getY(obj)
{
  // return the y coordinate of the object
  var myObj = getObj(obj);

  if (MSIE) {
    y = myObj.pixelTop;
  } else {
    y = myObj.top;
  }
  return(parseInt(y));
}

// -- The following two let you identify the x,y location of
//    the users' mouseclick on the page. Only use it with an
//    an event-based script, like:
//   <BODY OnClick="x=getEventX(event);y=getEventY(event);
//           alert('you clicked at '+x+','+y);">
//    note that 'event' is a global JavaScript variable and
//    doesn't have to be defined anywhere.

function getEventX(eventObj)
{
  // returns the x coordinate of where the user clicked.

  if (document.all && eventObj.x) return eventObj.x;
  if (eventObj.pageX) return eventObj.pageX;
  return -1;
}

function getEventY(eventObj)
{
  if (document.all && eventObj.y) return eventObj.y;
  if (eventObj.pageY) return eventObj.pageY;
  return -1;
}

// ----------------------------
function windowWidth()
{
  // return the width of the browser window.  Special note for MSIE: you
  // must have some output on the window before this test will work,
  // because the body object isn't instantiated until then. You can get
  // around this, if needed, with document.write("&nbsp;"); or similar.
  if (MSIE) {
    width = document.body.clientWidth;
  } else {
    width = window.innerWidth;
  }
  return(parseInt(width));
}

function windowHeight()
{
  // return the width of the browser window
  if (MSIE) {
    height = document.body.clientHeight;
  } else {
    height = window.innerHeight;
  }
  return(parseInt(height));
}

function screenWidth()
{
  // return the width of the users screen
  return(parseInt(window.screen.width));
}

function screenHeight()
{
  // return the height of the users screen
  return(parseInt(window.screen.height));
}

function objHeight(obj)
{
  // return the height of the specified object
  var myObj = getObjCore(obj);

  if (myObj.offsetHeight) {
    ht = myObj.offsetHeight;
  } else {
    ht = myObj.clip.height;
  }
  return(parseInt(ht));
}

function objWidth(obj)
{
  // return the height of the specified object
  var myObj = getObjCore(obj);

  if (myObj.offsetWidth) {
    wd = myObj.offsetWidth;
  } else {
    wd = myObj.clip.width;
  }
  return(parseInt(wd));
}

function centerObj(obj)
{
  // center the object in the window (horizontally only)
  var y = getY(obj), width = windowWidth(), owidth = objWidth(obj);
  var newX = Math.floor((width - owidth) / 2);
  moveObj(obj, newX, y);
}


// -- the following are examples of how to easily set a specific
// attribute using the crashcourse.js functions. In the first case, it'd
// be easy to have a link on your page that'd let the user change a
// dark background color to a lighter one, for example, with a link
// like "OnClick="setBgColor('id', 'blue')".

function setBgColor(obj, color)
{
  // set the background color for the specified object
  var myObj = getObj(obj);
  myObj.backgroundColor = color;
}

function showObj(obj)
{
  // turn on visibility for the object
  var myObj = getObj(obj);

  myObj.visibility = "visible";
}

function hideObj(obj)
{
  // turn off visibility, hiding the object
  var myObj = getObj(obj);

  myObj.visibility = "hidden";
}




function tabber(tab, count)
{
	topTabName = "tab" + tab;
	topTab = tab;
	var topContent = "contentWindow" + tab;
	totalTabs = count +1;
	for (var i = 1; i < totalTabs; i++)
		{
		var currentWindow = "contentWindow" + i;
		myObj = getObj(currentWindow);
		myObj.zIndex = 20;
		var currentTab = "tab" + i;
		myObj = getObj(currentTab);
		myObj.borderColor = 'gray';
		hideObj(currentWindow);
		}
		showObj(topContent);
		zobj= getObj(topContent);
		zobj.zIndex = 70;
	myObj = getObj(topTabName);
	myObj.borderColor = 'blue';
}




function zchanger(obj, zvalue)
{
	myObj = getObj(obj);
	myObj.zIndex = zvalue;
}

function pushBack()
{
	totalTabs = 5;
	for (var i = 1; i < totalTabs; i++)
		{
		var currentWindow = "contentWindow" + i;
		myObj = getObj(currentWindow);
		myObj.zIndex = 20;
		var currentTab = "tab" + i;
		myObj = getObj(currentTab);
		myObj.borderColor = 'gray';
		}
}

// popup stuff
var horizontalPad = 20;
var rightPad = 50;
var verticalPad = 0;


function popUpWindow(evt, id)
{

   var myObj = getObj(id);
   var popupWidth = objWidth(id);
   var halfPopupWidth = Math.round(popupWidth / 2);

   var rightEdge = windowWidth() - popupWidth - rightPad;
   var leftEdge = 175;

   // where did the user click their mouse?
   // if (evt != null) event = evt;  // keeps Netscape happy
   x = getEventX(event);
   y = getEventY(event);

   //x -= halfPopupWidth;

   if (x > rightEdge) x = rightEdge - horizontalPad;
   if (x < leftEdge) x = leftEdge + horizontalPad;



   myObj.zIndex = 90;
   myObj.left = x;
   // myObj.bottom  =  (vpad*.9)+40 - y;
   myObj.bottom = windowHeight() - 40 - y;
   myObj.visibility = "visible";

}



function popDownWindow(id)
{
    var myObj = getObj(id);
    myObj.visibility = "hidden";
}

function popup(text)
{
  // simplify the task of in-line pop-up links

  var myObj = getObj(text);
  var myBody = document.all(text).varBody;
  var myStyle =  document.all(text).varStyle;
  document.write  ("<SPAN OnMouseOver=\"popUpWindow(event, '" + text + "');\"");
  document.write  (" OnMouseOut=\"popDownWindow('"+text+"');\" CLASS=\"mdef\";");
  document.write  (" onClick=\"location.href='#" + text + "term'; tabber(3,3);\">" +text + "</SPAN>");


 //document.write  ("<SPAN onClick=\"location.href='#" + text + "term'; tabber(3,3);\" CLASS=\"glosword1\"; >" + text + "</SPAN>");
   document.write  ("<span class=\"" + myStyle + "\">" + myBody + "</span>" );

}
function inlinedef(text)
{
  var myBody = document.all(text).varBody;
  var myStyle =  document.all(text).varStyle;
  document.write  ("<SPAN OnMouseOver=\"popUpWindow(event, '" + text + "');\"");
  document.write  (" OnMouseOut=\"popDownWindow('"+text+"');\" CLASS=\"mdef\";");
  document.write  (" onClick=\"location.href='#" + text + "term'; tabber(3,3);\">" +text + "</SPAN>");
}

function helpup(text)
{
  // simplify the task of in-line pop-up links
  var myObj = getObj(text);
  var myBody = document.all(text).varBody;
  var myStyle =  document.all(text).varStyle;
  document.write  ("<SPAN OnMouseOver=\"popUpWindow(event, '" + text + "');\"");
  document.write  (" OnMouseOut=\"popDownWindow('"+text+"');\" ");
  document.write (" >" +text + "</SPAN>");
}

function inline(text)
{
  var myObj = getObj(text);
  var myBody = document.all(text).varBody;
  document.write (text, myBody);
}

function inlineglos(text)
{
  var myObj = getObj(text);
  var myBody = document.all(text).varBody;
  document.write ("<a name='"+text+"term'></a>" + text);
  document.write ("<font color = 'black'>" + myBody);
}

function gomanual(location, text)
{
// if (! text) text="";
// document.write ("<span OnMouseOver=\"popUpWindow(event, 'manual');\" ");
// document.write  (" OnMouseOut=\"popDownWindow('Manual');\" class =\"gomanual\" ");
// document.write (" onClick=\"tabber(1, 3); location.href='#M" + location + "'; \">");
//document.write ("<sub><img border='0' src='/images/manual_20.gif' width='15' height='15'></sub>" + text);
// document.write ("</span>  ");
document.write("");
}

function gotip(location, text)
{
if (! text) text="";
document.write ("<span OnMouseOver=\"popUpWindow(event, 'Tools and Examples');\" ");
document.write  (" OnMouseOut=\"popDownWindow('Tools and Examples');\" class =\"gotip\" ");
document.write (" onClick=\"tabber(2, 3); location.href='#T" + location + "'; \">");
document.write ("<sub><img border='0' src='/images/Wrench_20.gif'  width='15' height='15'></sub>" + text );
document.write ("</span>  ");
}

function gotest(text)
{
//updated 11 July 07 to call checkExerciseComplete, which checks that exerciseComplete is true, returns alert if not
document.write ("<span class =\"gotest\" onClick=\"if (checkExerciseComplete()) location.href='/loggedin_v2/testengine/av2starttest.aspx?DashedQuizID="+text+"';\">");
document.write ("<P align = center>Take the Quiz for this Section </span>");
}

function gofinal(text)
{

document.write ("<span class =\"gotest\" onClick=\"location.href='/loggedin_v2/testengine/av2starttest.aspx?DashedFinalID="+text+"'\">");
document.write ("<P align = center>Take the Final Exam for this Course</span>");
}

exercise_origin = "check";

function checkExerciseComplete()
{
	if (exerciseComplete) 
	{
		
		return true;
	}
	else
	{
		if (fillinblank)
		{
		exercise_origin = "exit";
		return checkblanks();
		}
		else
		{
		alert("You must complete the learning exercise associated with this lesson before you can continue. \n If you are having trouble, please contact us.")
		}
		return false;
	}
	
}