// -------------------------------------------------------------------------------------------
// SCRIPTS.JS
// Internet Management System
// 	(c) Copyright HEC Software, 2003
// 	Programmed by Kevin Owens
//	Updated by Aaron Hess
//
// This file contains function definitions used commonly in ADD_STUDENT.ASP and
// EDIT_STUDENT.ASP.
// -------------------------------------------------------------------------------------------

var iVer = 0;

//--------------------------------------------------------------------------------------------

function back() {
	history.back();
}

//--------------------------------------------------------------------------------------------

function check(index, elmAccType, totalLessons) {
// This function executes when the user clicks on one of my pseudo-checkboxes.  It will
// override the box if it's not already overridden, and clear it if otherwise.

	var form = document.addform;

	debug("We're running the function check();");

	if (form.AllBelowSelected.value == 1) {
		debug("DEBUG: check() is calling checkAllBelow().");
		checkAllBelow(form, index);
	}
	else if (form.Lsn[index-1].value < 98) {
		setBookmark(form, index, 98);
	}
	else {
		debug("check() is setting Lesson "+index+" to zero.");

		form.Lsn[index-1].value = 0;

	}

	form.Img[index-1].src = getBmkColor(form.Lsn[index-1].value);

// 	// We act a bit differently if we've selected "override all below selected"
// 	if (form.AllBelowSelected.value == 1) {
// 		debug("DEBUG: check() is calling checkAllBelow().");
// 		checkAllBelow(form, index);
// 	}

	debug("check() is calling setNextLesson(0).");
	setNextLesson(0, form, 0, elmAccType, totalLessons);
}

//--------------------------------------------------------------------------------------------

function checkAllBelow(form, lesson) {
// This will set all boxes below and equal to lesson to a bookmark of 98.

	var totalLessons;

	if (iVer == 1)
		totalLessons = 30;
	else if (iVer == 2)
		totalLessons = 75;
	else
		alert("ERROR in checkAllBelow(), in SCRIPTS.JS.");

	// Make sure my input is correct.  If it's not correct, adjust it here.
	if (lesson > totalLessons)
		lesson = totalLessons;
	else if (lesson < 0)
		lesson = getLsnVal(0-lesson);

	debug("DEBUG: We're trying to override all below "+lesson);

	// Update the bookmarks along the way, including the one we've clicked on.
	for (i = 1; i <= lesson; i++) {

		// Update this lesson to an override bookmark.
		setBookmark(form, i, 98);

		// If we are a lesson, and we're just after an IT, mark that IT as 98.
		// getITVal() will return which IT # is immediately after parameter lesson.
		if (iVer == 1)
			setITBookmark(form, getITVal(i-1), 98);
	}

	setNextLesson(0, form, 0, iVer, totalLessons); // yellow the next lesson
}

//--------------------------------------------------------------------------------------------

function clearAfterNext(myForm, totalLessons) {
// The purpose of this function is to reset certain yellow lessons to white, if they are ahead
// of the next lesson and their bookmarks equal 1.  Lessons with a bookmark of 1 will be set
// to zero, excepting the next lesson itself.

	for (i=0; i<totalLessons; i++)
	{
		if (myForm.Lsn[i].value <= 1 && myForm.NextLesson.value != (i+1))
		{
			myForm.Lsn[i].value = 0;
			myForm.Img[i].src = "graphics/check1.gif";
		}
	}

	if (iVer == 1)
	{
		if (myForm.NextLesson.value != "IT1" && myForm.IT1.value < 95)
		{
			myForm.IT1.value = 0;
			myForm.ImgIT1.src = "graphics/check1.gif";
		}
		if (myForm.NextLesson.value != "IT2" && myForm.IT2.value < 95)
		{
			myForm.IT2.value = 0;
			myForm.ImgIT2.src = "graphics/check1.gif";
		}
		if (myForm.NextLesson.value != "IT3" && myForm.IT3.value < 95)
		{
			myForm.IT3.value = 0;
			myForm.ImgIT3.src = "graphics/check1.gif";
		}
		if (myForm.NextLesson.value != "IT4" && myForm.IT4.value < 95)
		{
			myForm.IT4.value = 0;
			myForm.ImgIT4.src = "graphics/check1.gif";
		}
	}
	else if (iVer == 2)
	{
		if (myForm.NextLesson.value != "Summary 1" && myForm.Sum1.value <= 1)
		{
			myForm.Sum1.value = 0;
			myForm.ImageSum1.src = "graphics/check1.gif";
		}
		else if (myForm.NextLesson.value != "Summary 2" && myForm.Sum2.value <= 1)
		{
			myForm.Sum2.value = 0;
			myForm.ImageSum2.src = "graphics/check1.gif";
		}
	}
}

//--------------------------------------------------------------------------------------------

function ClearAll(myForm, elmAccType, totalLessons) {
// This function clears all overrides for ADD and EDIT STUDENT screens.

	for (i=0; i < totalLessons; i++) {

		debug("ClearAll() is setting Lesson "+(i+1)+" to zero.");

		myForm.Lsn[i].value = 0;
		myForm.Img[i].src = "Graphics/check1.gif";
	}

	myForm.IT1.value = 0;
	myForm.ImgIT1.src = getBmkColor(myForm.IT1.value);

	myForm.IT2.value = 0;
	myForm.ImgIT2.src = getBmkColor(myForm.IT2.value);

	myForm.IT3.value = 0;
	myForm.ImgIT3.src = getBmkColor(myForm.IT3.value);

	myForm.IT4.value = 0;
	myForm.ImgIT4.src = getBmkColor(myForm.IT4.value);

	myForm.ReducedCourse.value = "";

	myForm.Sum1.value = 0;
	myForm.ImageSum1.src = getBmkColor(myForm.Sum1.value);
	myForm.Sum2.value = 0;
	myForm.ImageSum2.src = getBmkColor(myForm.Sum2.value);
	disappear(Summaries);
	reappear(Blank);

	setNextLesson(0, myForm, 0, elmAccType, totalLessons);
}

//--------------------------------------------------------------------------------------------

function debug(string) {
// THIS IS FOR MY OWN TESTING PURPOSES.  IF 1, I'LL GET DEBUGGING MESSAGES; IF ZERO, I WON'T.

	if (0)
		alert(string);
}

//--------------------------------------------------------------------------------------------

function getBmkColor(bookmark) {

	var sReturn;

	if (bookmark == 0)
		sReturn = "graphics/check1.gif"; // blank
	else if (bookmark > 0 && bookmark < 95)
		sReturn = "graphics/check_yellow.gif"; // yellow
	else if (bookmark == 95)
		sReturn = "graphics/check_red.gif"; // red
	else if (bookmark == 98)
		sReturn = "graphics/check3.gif"; // black
	else if (bookmark == 99)
		sReturn = "graphics/check_blue.gif"; // blue
	else
		sReturn = "error.gif";

	return sReturn;

}

//--------------------------------------------------------------------------------------------

function getITBmk(itNum) {
// This will return the bookmark value of interim test number itNum.

	if (itNum >= 1 && itNum <= 4) {
		switch (itNum) {

		case 1:
			return document.addform.IT1.value;
			break;

		case 2:
			return document.addform.IT2.value;
			break;

		case 3:
			return document.addform.IT3.value;
			break;

		case 4:
			return document.addform.IT4.value;
			break;

		}
	}
	else
		return 0; // no interim test found; we'll just return a zero then.

}

//--------------------------------------------------------------------------------------------

function getITVal(lesson) {
// This takes a lesson number, and returns what IT # is immediately after that lesson.  If
// no IT is immediately after that lesson, it will return 0.

	var iReturn;

	if (iVer == 1) {
	// Accelerated
		if (lesson == 9)
			iReturn = 1;
		else if (lesson == 17)
			iReturn = 2;
		else if (lesson == 24)
			iReturn = 3;
		else if (lesson == 30)
			iReturn = 4;
		else
			iReturn = 0;
	}
	else if (iVer == 2) {
	// Elementary
			iReturn = 0;
	}
	else {
	// iVer == 0
		alert("Error: we don't know what version we're running here in getITVal().");
	}

	return iReturn;

}

//--------------------------------------------------------------------------------------------
function getLsnVal(itNum) {
// THis is basically a reverse getITVal().

	var iReturn;

	if (iVer == 1) {
	// Accelerated
		if (itNum == 1)
			iReturn = 9;
		else if (itNum == 2)
			iReturn = 17;
		else if (itNum == 3)
			iReturn = 24;
		else if (itNum == 4)
			iReturn = 30;
		else
			iReturn = 0;
	}
	else if (iVer == 2) {
	// Elementary
			iReturn = 0;
	}
	else {
	// iVer == 0
		alert("Error: we don't know what version we're running here in getITVal().");
	}

	return iReturn;

}

//--------------------------------------------------------------------------------------------

function getOS() {
// Return "security.aam" if Windows-based OS, "security_mac.aam" if Mac-based OS, "error.aam"
// if other.
//
// A more comprehensive version of this function can be found at:
//
//		http://www.naithai.com/webmastertool/Javascripts/Navigation/operating.htm
//		(Careful, it plays music.)

	var os = 'X';
    var v = navigator.appVersion.toUpperCase();

    if (1+v.indexOf('WIN95')
    ||  1+v.indexOf('WINDOWS 95')
    ||  1+v.indexOf('WIN32')
    ||  1+v.indexOf('WIN98')
    ||  1+v.indexOf('WINDOWS 98')
    ||  1+v.indexOf('WINNT')
    ||  1+v.indexOf('WINDOWS NT')
    ||  1+v.indexOf('WINDOWS 3.1')
    ||  1+v.indexOf('WINDOWS 3')
    ||  1+v.indexOf('WIN16'))
     	 os = 'Win';

    else if (1+v.indexOf('MAC')
    ||  1+v.indexOf('PPC')
    ||  1+v.indexOf('POWERPC'))
         os = 'Mac';

    else
     	 os = 'Other';

    // Now, determine a return value.
	if (os == 'Win')
		return "win";
	else if (os == 'Mac')
		return "mac";
	else
		return "error";

}

//--------------------------------------------------------------------------------------------

function getYellowIT() {
// This function returns the number of the highest IT that's yellow.  It returns 0 if none.

	var form = document.addform;
	var iReturn;

	if (iVer == 1) {
		if (form.IT4.value > 0 && form.IT4.value < 95)
			iReturn = 4;
		else if (form.IT3.value > 0 && form.IT3.value < 95)
			iReturn = 3;
		else if (form.IT2.value > 0 && form.IT2.value < 95)
			iReturn = 2;
		else if (form.IT1.value > 0 && form.IT1.value < 95)
			iReturn = 1;
		else
			iReturn = 0;
	}
	else
		iReturn = 0;

	return iReturn;

}

//--------------------------------------------------------------------------------------------

function ITCheck(object, image, index, myForm, elmAccType, totalLessons) {
//	This function is essentially an ITBox->OnClick().
// 		"object" refers to a hidden form member which contains a bookmark value
// 		"image" is the image that was clicked to get here
// 		"index" is the number of the test box which was clicked (1, 2, 3, or 4)

	var nextLesson;

	if (object.value >= 0 && object.value < 95) { // If we're clicking on a blank or yellow.
		object.value = 98;
		image.src = getBmkColor(object.value);
	}
	else {

		debug("ITCheck() is setting object.value to 0.");

		object.value = 0;
		image.src = getBmkColor(object.value);
	}

	// Rig it up in case "Override All Below Selected" has been set.
	if (myForm.AllBelowSelected.value == 1) {
		debug("DEBUG: ITCheck() is calling checkAllBelow().");
		checkAllBelow(myForm, 0-index);
	}

	debug("ITCheck() is calling setNextLesson(0).");
	setNextLesson(0, myForm, 1, elmAccType, totalLessons);

}

//--------------------------------------------------------------------------------------------

function popUnder(url) {

	newWindow = window.open( url );
	newWindow.blur();
	window.focus();

}

//--------------------------------------------------------------------------------------------

function restrictLength(object, size) {
// This function will make sure that a value for an object (object) is no greater than a
// specified length (size).

	if (object.value.length > size) {
		object.value = object.value.substr(0, size-1);
		alert("This field cannot exceed "+size+" characters in length.");
	}

}

//--------------------------------------------------------------------------------------------

function setBookmark(form, lesson, bookmark) {
// This function will set lesson # lesson to bookmark.  It will only do so if, in so doing,
// it would be an improvement.

	if (form.Lsn[lesson-1].value < bookmark) {
		form.Lsn[lesson-1].value = bookmark;
		form.Img[lesson-1].src = getBmkColor(bookmark);
	}

}

//--------------------------------------------------------------------------------------------

function setITBookmark(myForm, itNumber, bookmark) {
// This function will assign IT #[itNumber] to bookmark.
// If no IT is immediately after the lesson, we will simply return 1.
// It's really kind of dumb to have IT1, IT2, IT3, and IT4 instead of just IT[...].  I should
// probably go in and change these to an array sometime.  It'd make things quite simpler.

	var iReturn;

	switch (itNumber) {

	case 1:

		if (myForm.IT1.value < bookmark) {
			myForm.IT1.value = bookmark;
			myForm.ImgIT1.src = getBmkColor(bookmark);
		}

		iReturn = 0;
		break;

	case 2:

		if (myForm.IT2.value < bookmark) {
			myForm.IT2.value = bookmark;
			myForm.ImgIT2.src = getBmkColor(bookmark);
		}

		iReturn = 0;
		break;

	case 3:

		if (myForm.IT3.value < bookmark) {
			myForm.IT3.value = bookmark;
			myForm.ImgIT3.src = getBmkColor(bookmark);
		}

		iReturn = 0;
		break;

	case 4:

		if (myForm.IT4.value < bookmark) {
			myForm.IT4.value = bookmark;
			myForm.ImgIT4.src = getBmkColor(bookmark);
		}

		iReturn = 0;
		break;

	default:

		iReturn = 1; // I don't think anyone is checking against this, so it really doesn't
				  // need to be here; but if I want to check against it in the future, I
				  // thought it might be helpful to keep a return value.

		break;

	}

	return iReturn;

}

//--------------------------------------------------------------------------------------------

function setNextLesson(lesson, myForm, flag, elmAccType, totalLessons) {
// This function sets the next lesson on an override form.

//	   	- flag, if set to 3, it means we're running this function from a change in the "Next Lesson"
// 		  text box on ADD_ or EDIT_STUDENT.ASP, which means we're going to have to override
//		  incomplete and untaken lessons up to the point of the new next lesson.
//
//		- The "totalLessons" argument comes from <%= TotalLessons %>, being either 30 or 75.

	var ITVal;
	var nextLesson = 0; // 1-30 (or 1-75)
	var nextSet = false;

	// We may not have set up iVer yet, so let's do it now, to make sure that functions we call from here will know
	// what version we are running.
	if (elmAccType == "Elm")
		iVer = 2;
	else if (elmAccType == "Acc")
		iVer = 1;

	// If we came from changing the "Next Lesson" box in the student information.
	if (flag == 3) {
		debug("Running the function checkAllBelow(myForm, "+(lesson-1)+");");
		checkAllBelow(myForm, lesson-1);
	}
	else	// Everything else comes here
	{
		// determine the next lesson that should be taken
		for (i = 0; i < totalLessons; i++)
		{
			if (myForm.Lsn[i].value < 98)
			{ // if it's not passed or overridden
				nextLesson = i + 1;
				break;
			}
			else
				nextLesson = 100;	// All the lessons are done
		}

		if (iVer == 1)
		{
			ITVal = setNextIT(myForm, nextLesson, iVer);	// if acc check if an IT should be next
			if (ITVal > 0)
			{
				setITBookmark(myForm, ITVal, 1);
				myForm.NextLesson.value = "IT"+ITVal;
				nextSet = true;
			}
		}
		else	// if elm check if Summaries need to be done
		{
			debug("Since iVer != 1, we're running the elementary code here.");
			if (nextLesson > 1 && myForm.ReducedCourse.value == "Abbreviated")
			{
				if (myForm.Sum1.value < 98)			// check Summary 1
				{
					myForm.Sum1.value = 1;
					myForm.ImageSum1.src = getBmkColor(myForm.Sum1.value);
					myForm.NextLesson.value = "Summary 1"; // Sum1
					nextSet = true;
				}
				else if (myForm.Sum2.value < 98)	// check Summary 2
				{
					myForm.Sum2.value = 1;
					myForm.ImageSum2.src = getBmkColor(myForm.Sum2.value);
					myForm.NextLesson.value = "Summary 2"; // Sum2
					nextSet = true;
				}
			}
		}

		// if the next lesson isn't set or the lessons aren't all done
		if (nextLesson != 100 && !nextSet)
		{
			debug("Since nextLesson != 100 && !nextSet, we're running this block of code.");
			debug("nextLesson-1 = "+(nextLesson-1));

			/* If we haven't already set a next lesson, we need to initially set it to 1. */
			nextLesson = 1;

			if (myForm.Lsn[nextLesson-1].value == 0)	// if next lesson is incomplete
			{
				debug("The next lesson is incomplete (myForm.Lsn["+nextLesson+"-1].value == "+myForm.Lsn[nextLesson-1].value);
				myForm.Lsn[nextLesson-1].value = 1;
				myForm.Img[nextLesson-1].src = getBmkColor(myForm.Lsn[nextLesson-1].value);
				myForm.NextLesson.value = nextLesson
				nextSet = true;
			}
			else	// if the next lesson is a failed lesson
			{
				debug("The next lesson is a failed lesson.  myForm.NextLesson.value now equals "+nextLesson+".");
				myForm.NextLesson.value = nextLesson
				nextSet = true;
			}
		}
		else if (nextLesson == 100 && !nextSet)		// everything is done
			myForm.NextLesson.value = "Done";

		debug("Running clearAfterNext("+myForm+", "+totalLessons+");");
		clearAfterNext(myForm, totalLessons);
	}
}

function setNextIT(myForm, lesson, elmAccType) {
// This sets the next IT that should be done for Accelerated

	if (elmAccType == 1) {
		if (lesson > 9 && getITBmk(1) < 98)
			return 1;
		else if (lesson > 17 && getITBmk(2) < 98)
			return 2;
		else if (lesson > 24 && getITBmk(3) < 98)
			return 3;
		else if (lesson > 30 && getITBmk(4) < 98)
			return 4;
		else
			return 0;
	}
	else
		return 0;
}
//--------------------------------------------------------------------------------------------
