// Non-Credit Home page code for handling schedule selection drop down.
// A copy of this code actually lives in that page as otherwise current term selection wasn't
// working properly and the script code didn't behave well with the editor.

	// If you have questions about this code contact Scott Paulson.

	// Create a current schedule DIV tag
	// <div class=ItemTitle id=ScheduleTitle>Current schedule name will appear here</div>
	
	// Selection code example for what to add to your web page.
	// SELECT Control which includes Term choices.
	// <select id=termList style="WIDTH: 207px" onchange="window.open('?term=' + this.options[this.selectedIndex].value,'_top')" size=1>
	// <option selected>Select new class starting dates</option>
	// <option value=2008/100810>January - March 2008</option>
	// </select>
	
	// Links which are clickable and will use the term currently selected in the above SELECT control.
	// <a href="javascript:GoToSchedule('CEHFIT.htm');">Fitness &amp; Health</a>
	
	// In addition to creating the necessary select, anchor and div controls copy/paste the below code to the appropriate page. (wrapping it in a script tag)
	
	// function to get the currently selected term.
	// this uses the term parameter or if not found assumes the first real item in the selection box. (ignoring the "select a term" help item)
	function GetSelectedTerm()
	{
		var optCtrl = document.getElementById('termList');
		var myURL = document.location + '';
		var ret = ''
		
		// get indicated date range (via term URL parameter)
		var pi = myURL.indexOf('term=');
		var pe = myURL.substring(pi).indexOf('&');
		if (pi > 0)
		{
			if (pe > 0)
				ret = myURL.substring(pi+5,pi+pe);
			else
				ret = myURL.substring(pi+5);
		}
		if ((ret =='') || (! CheckTermValidity(ret)))
			ret = optCtrl.options[1].value;
		
		return ret;
	}
	
	// function to get the appropriate text from our selection box for the selected term value.
	function CheckTermValidity(term)
	{
		var optCtrl = document.getElementById('termList');
		var ret = false;

		var len = optCtrl.options.length;
	    if (len != undefined)
		{
	        for (var i = 0; i < len; i++)
	        {
	            if (optCtrl.options[i].value == term)
	                ret = true;
	        }
		}
		return ret;
	}
	
	// function to get the appropriate text from our selection box for the selected term value.
	function GetSelectedTermText()
	{
		var optCtrl = document.getElementById('termList');
		var curTerm = GetSelectedTerm()
		var ret = '';

		var len = optCtrl.options.length;
	    if (len != undefined)
		{
	        for (var i = 0; i < len; i++)
	        {
	            if (optCtrl.options[i].value == curTerm)
	                ret = optCtrl.options[i].text;
	        }
		}
		return ret;
	}
	
	// function for going to the appropriate schedule when a user clicks a topic link.
	function GoToSchedule(pagename)
	{
		document.location = ('https://oraweb.cocc.edu/' + GetSelectedTerm() + '/' + pagename);
	}
	
	// function for writing out the appropriate title for the approrpiate schedule.
	function SetScheduleTitle()
	{
		var schTitle = document.getElementById('ScheduleTitle');
		if (schTitle != null)
			schTitle.innerHTML = GetSelectedTermText();
	}

	// NOT CURRENTLY USED: function for writing out the appropriate title for the approrpiate schedule.
	function UpdateCurrentSchedule(selectedTerm, selectedText)
	{
		var schTitle = document.getElementById('ScheduleTitle');
		if (schTitle != null)
			schTitle.innerHTML = selectedText;
	}

	
	// Non-Credit Home page code for handling CRN search.
	
	// The related in page code for supporting CRN searching is included here in case needed.
	/*
		<A class=ItemTitle href="javascript:alert('CRN\'s are 5 digit class numbers found in the schedule.');">Find By CRN</A><BR>
                   <INPUT onkeypress="javascript:if(event.keyCode==13){FindCRN();return false;}else{return true;}" id=tbCRN type=textbox maxLength=5 size=5>&nbsp;<A href="javascript:FindCRN();"><IMG style="WIDTH: 32px; HEIGHT: 19px" alt="Find CRN" src="http://www.cocc.edu/Themes/Default/Images/btn_go3.gif" align=absMiddle border=0></A>
	*/
	function FindCRN()
	{
		var tb = document.getElementById('tbCRN');
		if (tb != null)
		{
			if ((tb.value.length == 5) && (isInt(tb.value)))
			{
				// Example crn web link:   https://oraweb.cocc.edu/2008/100820/cecourses.htm#25468
				var urlPrefix = "https://oraweb.cocc.edu/"
				var urlSuffix = "/cecourses.htm#"
				
				var crn = tb.value;
				var crnPrefix = crn.substring(0,1);
				var now = new Date()
				var mon = now.getMonth()+1; // adding 1 since the month is 0 based... this way 1=Jan and 12=Dec
				var year = now.getUTCFullYear()
				var curYear = year.toString();
				var nextYear = (year+1).toString();
				var lastYear = (year-1).toString();
				var curCETermYear = (year-1000).toString();
				var nextCETermYear = (year-1000+1).toString();
				var lastCETermYear = (year-1000-1).toString();
				var webFldr = ""
				
				switch (crnPrefix)
				{
					// Here we push people to the next schedule about a month early so we don't cause confusion by showing last year's schedule close to a new schedule release.
					case "1": // Winter schedule released at end of November
						if (mon >= 11)
							webFldr = nextYear + "/" + nextCETermYear + "10"
						else
							webFldr = curYear + "/" + curCETermYear + "10"
						break;
					case "2": // Spring schedule released at end of February
						if (mon >= 2)
							webFldr = curYear + "/" + curCETermYear + "20"
						else
							webFldr = lastYear + "/" + lastCETermYear + "20"
						break;
					case "3": // Summer schedule released at end of February
						if (mon >= 2)
							webFldr = curYear + "/" + curCETermYear + "30"
						else
							webFldr = lastYear + "/" + lastCETermYear + "30"
						break;
					case "4": // Fall schedule released at end of August
						// for fall classes the web folder is in the next year's folder so increment it.
						if (mon >= 8)
							webFldr = nextYear + "/" + curCETermYear + "40"
						else
							webFldr = curYear + "/" + lastCETermYear + "40"
						break;
					default:
						alert("The CRN entered appears to be invalid.");
						return;
						break;
				}
				document.location = urlPrefix + webFldr + urlSuffix + crn;
			}
			else
				alert("You must enter a valid CRN. (5 digit number)");
		}
	}


	function isInt(sNum)
	{
	  return (sNum!="" && !isNaN(sNum) && (sNum/1)==parseInt(sNum));
	}

	function isDecimal(sNum)
	{
	  return !isNaN(sNum!="" && sNum);
	}

