// gateway test course and section setting routines
// version 2.13
// (c) 2001-09 Gavin LaRose, University of Michigan Math Department/
//    Regents of the University of Michigan
// last change 27 Jan 2009

function setCrs( f ) {
// set the course options for the course selector in form f
// expected form variables are 
//    f.course

// set course selector; we know this is for proctored tests
    f.course.options.length = 1;
    var optLen = 1;
    for ( var i=0; i<indexToCourse.length; i++ ) {
	var testNames = testIsOpen(i, true)
	if ( testNames ) {
	    f.course.options[optLen] = new Option("Math "+indexToCourse[i],i);
	    optLen = optLen + 1.*1;
	}
    }
    if ( f.course.options.length == 1 ) {
	f.course.options[1] = new Option("No tests are active", -1);
    }

    return 1;
}

function setSec( f, courseIndex, showall ) {
// set section options for the class-section selector in form f
// expected form variables are
//    f.section  - the section selector to set
//    f.testType - the type of test to take, "example", "practice", 
//		   "proctored", or "instructor", corresponding to 
//		   "Example", "Practice", "Proctored", or "InstrProctored"
//		   alternately, it may give the name of a test, as 
//		   given in gwdata.js, in which case the test is 
//		   chosen to be the given practice test, if it is open
//    f.course   - optional; if f.testType is "proctored", this 
//                 selector should give the course being used
// 
// courseIndex gives the index for the course we're using
// if showall is true, the section list is set even if no tests are open
//
// requires and sets the form element
//     f.section (selector, which allows selection of course-section)

// a bit of clean-up
    if ( showall == null ) {
	showall = false;
    }

    if ( courseIndex < 0 || courseIndex >= indexToCourse.length ) {
	f.section.options.length = 1;
	f.section.options[0] = new Option("section selector", -1);
	f.section.options[1] = new Option("none", -1);
	return 0;
    } else {
	course = indexToCourse[courseIndex];
    }

// are we giving a proctored test?
    if ( f.testType == null ) {
	f.section.options.length = 1;
	f.section.options[0] = new Option("section selector", -1);
	f.section.options[1] = new Option("none", -1);
	alert("Invalid test type given.");
	return 0;
    } else {
	var testType = readFormValue( f.testType );
	if ( testType == "proctored" || testType == "instructor" ) {
	    var isProctored = true;
	} else {
	    var isProctored = false;
	}
    }

// construct the section list, if we're doing a section-by-section
//    course
    if ( isProctored ) {
	openTests = testIsOpen( courseIndex, true );
	if ( openTests || showall ) {
	    // sxnBySxn = true if we need to display a list of 
	    //    sections in the section selector
	    var sxnBySxn = true;

	    if ( sectionedCourse[courseIndex] ) {
		f.section.options.length = 0;
		f.section.options[0] = new Option( "select section", "-1" );
		for ( var i=0; i<sections[courseIndex].length; i++ ) {
		    var name="Ma" + course + "-" + sections[courseIndex][i];
		    var value = course + "-" + sections[courseIndex][i];
		    f.section.options[i+1.*1] = new Option(name, value);
		}
	    } else {
		f.section.options.length = 0;
		var cName = indexToCourse[courseIndex]
		f.section.options[0] = new Option("all sections",
						  cName + "-all");
	    }
	} else {
	    // else, we just leave the uninitialized section selector
	    
	}
    } else { // unproctored tests
	var someOpen = false;

	for ( var i=0; i<testNames[courseIndex].length; i++ ) {
	    if ( isOpen( courseIndex, testNames[courseIndex][i] ) ) {
		someOpen = true;
	    }
	}

	if ( someOpen || showall ) {
	    if ( sectionedCourse[courseIndex] ) {
		f.section.options.length = 0;
		f.section.options[0] = new Option( "select section", "-1" );
		for ( var i=0; i<sections[courseIndex].length; i++ ) {
		    var name="Ma" + course + "-" + sections[courseIndex][i];
		    var value = course + "-" + sections[courseIndex][i];
		    f.section.options[i+1.*1] = new Option(name, value);
		}
	    } else {
		f.section.options.length = 0;
		var cName = indexToCourse[courseIndex]
		f.section.options[0] = new Option("all sections",
						  cName + "-all");
	    }
	}
    }

    return 1;
}


function addTestSel( f, courseIndex, isProctored ) {
// reset the location of the test selector, and options for that
//    selector
// input are f, the form, and courseIndex, a number 0-4, and a binary
//    for which test to take

    var selObj = document.getElementById( "testname" );
    if ( courseIndex == -1 ) {
	selObj.style.position = 'absolute';
	selObj.style.left = '-1500px';
    } else {
        if ( isProctored ) {
	    openTests = testIsOpen( courseIndex, true );
	} else {
	    var openTests = new Array();
	    for ( var i=0; i<testNames[courseIndex].length; i++ ) {
		if ( isOpen( courseIndex, testNames[courseIndex][i] ) ) {
		    openTests.push( testNames[courseIndex][i] );
		}
	    }
 	}
	if ( openTests ) {
	    selObj.style.position = 'relative';
	    selObj.style.left = '0';

	    if ( openTests.length > 1 ) {
		f.testname.options.length = 0;
		f.testname.options[0] = new Option("select test", "none");
		for ( var i=0; i<openTests.length; i++ ) {
		    var value = openTests[i]; // testNames[courseIndex][i];
		    if ( value == "Entr" ) {
			var name = "Entrance";
		//     } else if ( value == "cci_pre" || 
		// 	        value == "cci_post" ) {
		// 	var name = "CCI";
		    } else if ( value == "Deriv" ) {
			var name = "Derivative";
		    } else {
			var name = value;
		    }
		    f.testname.options[i+1.*1] = new Option(name, value);
		}
	    } else {
		f.testname.options.length = 0;
		var value = openTests; // testNames[courseIndex][0];
		if ( value == "Entr" ) {
		    var name = "Entrance";
		// } else if ( value == "cci_pre" || 
		// 	    value == "cci_post" ) {
		//     var name = "CCI";
		} else if ( value == "Deriv" ) {
		    var name = "Derivative";
		} else {
		    var name = value;
		}
		f.testname.options[0] = new Option(name, value);
	    }
	}
    }
}


function setLoc( f, courseIndex ) {
// send the user to the appropriate test; f is the form and courseIndex
//    the index of the course to consider
// expected form variables are
//    f.section  - the section selector, for courseIndices corresponding
//		   to 105, 115, 116 this is a select element, and for 
//		   215 a hidden element
//    f.testType - the type of test to take, "example", "practice", 
//		   "proctored", or "instructor", corresponding to 
//		   "Example", "Practice", "Proctored", or "InstrProctored"
// the location is then reset

// get the course name
    if ( courseIndex < 0 || courseIndex >= indexToCourse.length ) {
	alert("Invalid course selection.");
	return 0;
    } else {
	course = indexToCourse[courseIndex];
    }

// what test are we giving (Example, Practice, Proctored, InstrProctored)?
//   if testType is undefined, we're in trouble
    if ( f.testType == null ) { 
	alert("Invalid test selection.");
	return 0;
    } else {
	var testType = readFormValue( f.testType );

// otherwise, read the type from the variable
	if ( testType == "instructor" ) {
	    var testType = new String("InstrProctored");
	} else if ( testType.substr(0,5) == "instr" && courseIndex == 2 ) {
	    var testType = testType.substr(0,1).toUpperCase() + 
		testType.substr(1,testType.length - 1);
	} else {
	    var testType = testType.substr(0,1).toUpperCase() + 
		testType.substr(1,testType.length - 1);
	}
    }

// now find a test that we can be giving
    if ( testType == "Proctored" || testType.substr(0,14) == "InstrProctored" ) {
	var proctoredString = 'proctored_';
	if ( f.testname != null ) {
	    // var testName = f.testname.options[f.testname.options.selectedIndex].value;
	    var testName = readFormValue( f.testname );
	} else {
	    var testName = testIsOpen(courseIndex, false);
	}
    } else if ( testType == "Example" || testType == "Practice" ) {
	var proctoredString = '';
	if ( f.testname != null ) {
	    var testName = f.testname.options[f.testname.options.selectedIndex];
	} else {
	    var testName = testIsOpen(courseIndex, false);
	}
// if the testType isn't Proctored, etc., then it gives the name of
//   the test to give
    } else {
	var proctoredString = '';
	var testName = testType;
	if ( ! isOpen( courseIndex, testName ) ) {
	    alert("The test " + testName + " is not open for Ma" + 
		  indexToCourse[courseIndex]);
	    return 0;
	}
    }

// this should not happen unless the user is fiddling with things
//   that shouldn't be fiddled with
    if ( ! testName ) {
	alert("No tests are open for Math " + indexToCourse[courseIndex]);
	return 0;
    } else if ( testName == "none" ) {
	alert("Please select a test.");
	return 0;
    } else if ( testType == "Example" ) {
	testName = "";
    } else if ( testType == testName ) {
	testType = "Practice";
    }

    if ( f.section == null ) {
	alert("Invalid section selection.");
	return 0;
    }

    if ( course == 215 && testType != "Proctored" ) {
// section selector should be a hidden element with a value
	if ( f.section.type != "hidden" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = f.section.value;
	}
    } else if ( course == 215 ) {
	var crssxn = "215-000";
    } else {
	if ( f.section.type != "select-one" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = 
		f.section.options[f.section.options.selectedIndex].value;
 	}
    }
    if ( crssxn == "-1" ) {
	alert("Please select a course-section before going to the test.");
	return 0;
    } else {
	if ( course == 215 ) {
	    crssxn = new String(crssxn).substr(0,3);
	}
// FIXME: this is a fudge; I made the tests be "ProctoredIntegralGW"
// and "InstrProctoredIntegGW".  Duh.
	if ( testName == "Integral" && testType == "InstrProctored" ) {
	     testName = "Integ";
        }
// And another fudge, to catch the 115 cci
// 	if ( crssxn == "115-all" ) {
// 	    crssxn = "115-cci";
// 	    testType = "";
// 	    var gwExt = "";
// 	} else {
	    var gwExt = "GW";
// 	}
	var destination = wwURL + "ma" + crssxn + "-" + currentTerm +
	     "/" + proctoredString + "quiz_mode/" + testType + 
	     testName + gwExt;
	window.location = destination;
//	alert("destination = " + destination);
    }
}

function setInstrLoc( f, courseIndex ) {
// pre:  f is the form on the instructors' gateway page
// post: the window location is reset to get the student progress page
//       for the instructor's course

// get the course name
    if ( courseIndex < 0 || courseIndex >= indexToCourse.length ) {
	alert("Invalid course selection.");
	return 0;
    } else {
	course = indexToCourse[courseIndex];
    }

// and testtype (do we need this?)
//   if testType is undefined, we're in trouble
    if ( f.testType == null ) { 
	alert("Invalid test selection.");
	return 0;
    } else {
	var testType = readFormValue( f.testType );

// otherwise, read the type from the variable
	if ( testType == "instructor" ) {
	    var testType = new String("InstrProctored");
	} else {
	    var testType = testType.substr(0,1).toUpperCase() + 
		testType.substr(1,testType.length - 1);
	}
    }


    if ( course == 215 && testType != "Proctored" ) {
// section selector should be a hidden element with a value
	if ( f.section.type != "hidden" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = f.section.value;
	}
    } else {
	if ( f.section.type != "select-one" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = 
		f.section.options[f.section.options.selectedIndex].value;
 	}
    }
    if ( crssxn == "-1" ) {
	alert("Please select a course-section before going to the test.");
	return 0;
    } else {
	if ( course == 215 ) {
	    crssxn = new String(crssxn).substr(0,3);
	}
	var destination = wwURL + "ma" + crssxn + "-" + currentTerm +
	     "/instructor/progress/";
	window.location = destination;
//	alert("destination = " + destination);
    }
}

function setInstrLoc( f, courseIndex ) {
// pre:  f is the form on the instructors' gateway page
// post: the window location is reset to get the student progress page
//       for the instructor's course

// get the course name
    if ( courseIndex < 0 || courseIndex >= indexToCourse.length ) {
	alert("Invalid course selection.");
	return 0;
    } else {
	course = indexToCourse[courseIndex];
    }

// and testtype (do we need this?)
//   if testType is undefined, we're in trouble
    if ( f.testType == null ) { 
	alert("Invalid test selection.");
	return 0;
    } else {
	var testType = readFormValue( f.testType );

// otherwise, read the type from the variable
	if ( testType == "instructor" ) {
	    var testType = new String("InstrProctored");
	} else {
	    var testType = testType.substr(0,1).toUpperCase() + 
		testType.substr(1,testType.length - 1);
	}
    }


    if ( course == 215 && testType != "Proctored" ) {
// section selector should be a hidden element with a value
	if ( f.section.type != "hidden" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = f.section.value;
	}
    } else {
	if ( f.section.type != "select-one" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = 
		f.section.options[f.section.options.selectedIndex].value;
 	}
    }
    if ( crssxn == "-1" ) {
	alert("Please select a course-section before going to the test.");
	return 0;
    } else {
	if ( course == 215 ) {
	    crssxn = new String(crssxn).substr(0,3);
	}
	var destination = wwURL + "ma" + crssxn + "-" + currentTerm +
	     "/instructor/progress/";
	window.location = destination;
//	alert("destination = " + destination);
    }
}

function setHWLoc( f, courseIndex ) {
// pre:  f is the form on the homework page
// post: the window location is reset to get to the webwork login page
//       for the indicated course.

// get the course name
    if ( courseIndex < 0 || courseIndex >= indexToCourse.length ) {
	alert("Invalid course selection.");
	return 0;
    } else {
	course = indexToCourse[courseIndex];
    }

    if ( course == 215 ) {
// section selector should be a hidden element with a value
	if ( f.section.type != "hidden" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = f.section.value;
	}
    } else {
	if ( f.section.type != "select-one" ) {
	    alert("Invalid section selector.");
	    return 0;
	} else {
	    var crssxn = 
		f.section.options[f.section.options.selectedIndex].value;
 	}
    }

    if ( crssxn == "-1" ) {
	alert("Please select a course-section before going to the test.");
	return 0;
    } else {
	if ( course == 215 ) {
	    crssxn = new String(crssxn).substr(0,3);
	}
	var destination = wwURL + "ma" + crssxn + "-" + currentTerm + "/";
	window.location = destination;
//	alert("destination = " + destination);
    }
}

