document.onkeyup = KeyCheck;       

function KeyCheck(e) {
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID){
	  case 37:
      loadcombination('prev');
      break;
      case 39:
      loadcombination('next');
      break;
   }
}

Event.observe(window, 'keypress', function(e){
	if(e.keyCode == Event.KEY_LEFT) loadcombination('prev');
	if(e.keyCode == Event.KEY_RIGHT) loadcombination('next');
});

function loadstart(e) {
	/*var browser=navigator.appName;
	if (browser!="Microsoft Internet Explorer"){*/
		document.getElementById('display').className='transparent';
		$('loading').show();
	/*}*/
}

function loadend(e) {
	/*var browser=navigator.appName;
	if (browser!="Microsoft Internet Explorer"){*/
		$('loading').hide();
		document.getElementById('display').className='';
	/*}*/
}


function errorbox(a) {
	Element.toggle('errordiv');
	document.getElementById('display').className='transparent';
	document.getElementById('errorcontents').innerHTML=a;
	return false;	
}

function successbox(a) {
	Element.toggle('successdiv');
	document.getElementById('display').className='transparent';
	document.getElementById('successcontents').innerHTML=a;
	return false;	
}

function loadsidetab(a){
	var tabs = $("lefttabs");
	for(var i = 0 ; i < tabs.childNodes.length ; i++) {
		var f = tabs.childNodes[i];
		if(f.className == "currenttab")
		f.className = "tab";
	}
	document.getElementById(a).className='currenttab';
	/* Detects browser and loads the old IE compatible textbook tab if necessary
	New textbook API updated to be compatible with IE, so this isn't necessary anymore
	var browser=navigator.appName;
		if ((browser=="Microsoft Internet Explorer") && (a=='tab-books')) {var a='tab-booksie';} */
	new Ajax.Updater('display','/classifier/classifiersubpage.php', {method: 'get', parameters: {a: a},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {
	      	new Ajax.Updater('top','/classifier/classifiertoptablist.php', {method: 'get', parameters: {a: a} })
	    },
		onComplete: function() { loadend(); }
    })
}

function loadtoptabreload(a){
	var tabs = $("toptabs");
	for(var i = 0 ; i < tabs.childNodes.length ; i++) {
		var f = tabs.childNodes[i];
		if(f.className == "currenttab")
		f.className = "tab";
	}
	document.getElementById(a).className='currenttab';
	new Ajax.Updater('display','/classifier/classifiersubpage.php', {method: 'get', parameters: {a: a},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {
		    if (a!='tab-courseinfo') {
		    	Element.hide('tab-courseinfo');}
	    },
	    onComplete: function() { loadend(); }
    })
}

function loadtoptab(a){
	var tabs = $("toptabs");
	for(var i = 0 ; i < tabs.childNodes.length ; i++) {
		var f = tabs.childNodes[i];
		if(f.className == "currenttab")
		f.className = "tab";
	}
	Element.hide('tab-availabilitydiv');
	Element.hide('tab-searchcoursesdiv');
	Element.hide('tab-courseinfodiv');
	document.getElementById(a).className='currenttab';
	Element.show(a+'div');
}

function loadcombination(a){
	if (document.getElementById('currentcombo')==undefined) {
		var b=0;
		var c=0;
	}
	else {
			var b = document.getElementById('currentcombo').value;
			var c = document.getElementById('limitcombo').value;
		if (a=='next') {
			if (parseInt(b)+1!=parseInt(c)+1) {
				var newnum = parseInt(b)+1;
			}
			else {
				var newnum=0;
			}
		}
		if (a=='prev') {
			if (b!=0) {
				var newnum = parseInt(b)-1;
			}
			else {
				var newnum=c;
			}
		}
		Element.hide('tab-schedulediv'+b);
		//document.getElementById('schedulename').innerHTML='Combination '+(parseInt(newnum)+1)+' of '+(parseInt(c)+1);
		document.getElementById('currentcombo').value=newnum;
		Element.show('tab-schedulediv'+newnum);
		new Effect.Highlight(document.getElementById('combonumber'+newnum), { startcolor: '#ffff99', endcolor: '#ffffff'});
	}
}

function showcourseinfo(a,b,c){
	document.getElementById('courseinfoname').innerHTML=c;
	Element.show('tab-courseinfo');
	new Ajax.Updater('tab-courseinfodiv','/classifier/classifiercourseinfo.php', {method: 'get', parameters: {a:a},
		onLoading: function() {Element.hide('courselist');loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {
	      	Element.show('courselist');
	      	Element.hide('tab-searchcoursesdiv');
			loadtoptab('tab-courseinfo');
	    },
	    onComplete: function() { loadend(); }
    })
}

function showcourseinfoview(a,b,c,d){
	document.getElementById('courseinfoname').innerHTML=c;
	Element.show('tab-courseinfo');
	loadtoptabreload('tab-courseinfo');
	var e='1';
	new Ajax.Updater('tab-courseinfodiv','/classifier/classifiercourseinfo.php', {method: 'get', parameters: {a:a,e:e}})
}

function searchcourses(a,b,c,d,e,f,g,h){
	new Ajax.Updater('searchresults','/classifier/classifiersearchresults.php', {method: 'get', parameters: {a:a,b:b,c:c,d:d,e:e,f:f,g:g,h:h},
		onLoading: function() {loadstart(); },
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {},
      	onComplete: function() { loadend(); }
    })
    //Effect.toggle('browsebody', 'blind', { duration: 0.5, afterFinish: function(){$('showdepts').toggle(); $('hidedepts').toggle();} })
    Effect.BlindUp('browsebody');
    $('hidedepts').hide();
    $('showdepts').show();
}

function searchcoursesadd(){
	if (document.getElementById('searchterm').value.length < 2) {
		errorbox("Please do not enter less than 2 characters.");
		return false;
	}
	if (document.getElementById('category').value=='CourseName'){document.getElementById('CourseName').value=document.getElementById('searchterm').value;}
	if (document.getElementById('category').value=='CourseShortName'){document.getElementById('CourseShortName').value=document.getElementById('searchterm').value;}
	if (document.getElementById('category').value=='Professor'){document.getElementById('Professor').value=document.getElementById('searchterm').value;}
		var days='';
		for (var loop=1; loop<8; loop++) {
            if (document.getElementById('day_'+loop).checked == true) {
            	var days=days+document.getElementById('day_'+loop).value+"-";
            }
		}
	searchcourses(document.getElementById('CourseName').value,document.getElementById('CourseShortName').value,document.getElementById('Professor').value,document.getElementById('semester').value,document.getElementById('starthour').value+'-'+document.getElementById('startmin').value+'-'+document.getElementById('startap').value,document.getElementById('endhour').value+'-'+document.getElementById('endmin').value+'-'+document.getElementById('endap').value,days,document.getElementById('DeptID').value);
}

function relatedsearchprof(a){
	document.getElementById('Professor').value=a; 
	document.getElementById('CourseName').value='';
	document.getElementById('CourseShortName').value='';
	document.getElementById('searchterm').value=a;
	document.getElementById('category').options[2].selected=true;
	document.getElementById('starthour').value='6';
	document.getElementById('startmin').value='00';
	document.getElementById('startap').options[0].selected=true;
	document.getElementById('endhour').value='11';
	document.getElementById('endmin').value='00';
	document.getElementById('endap').options[1].selected=true;
	document.getElementById('day_1').checked='checked';
	document.getElementById('day_2').checked='checked';
	document.getElementById('day_3').checked='checked';
	document.getElementById('day_4').checked='checked';
	document.getElementById('day_5').checked='checked';
	document.getElementById('day_6').checked='checked';
	document.getElementById('day_7').checked='checked';
		var days='Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday-';
	searchcourses(document.getElementById('CourseName').value,document.getElementById('CourseShortName').value,document.getElementById('Professor').value,document.getElementById('semester').value,document.getElementById('starthour').value+'-'+document.getElementById('startmin').value+'-'+document.getElementById('startap').value,document.getElementById('endhour').value+'-'+document.getElementById('endmin').value+'-'+document.getElementById('endap').value,days);
	loadtoptab('tab-searchcourses');
}

function relatedsearchdept(a){
	document.getElementById('Professor').value=''; 
	document.getElementById('CourseName').value='';
	document.getElementById('CourseShortName').value=a;
	document.getElementById('category').options[1].selected=true;
	document.getElementById('searchterm').value=a;
	document.getElementById('starthour').value='6';
	document.getElementById('startmin').value='00';
	document.getElementById('startap').options[0].selected=true;
	document.getElementById('endhour').value='11';
	document.getElementById('endmin').value='00';
	document.getElementById('endap').options[1].selected=true;
	document.getElementById('day_1').checked='checked';
	document.getElementById('day_2').checked='checked';
	document.getElementById('day_3').checked='checked';
	document.getElementById('day_4').checked='checked';
	document.getElementById('day_5').checked='checked';
	document.getElementById('day_6').checked='checked';
	document.getElementById('day_7').checked='checked';
		var days='Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday-';
	searchcourses(document.getElementById('CourseName').value,document.getElementById('CourseShortName').value,document.getElementById('Professor').value,document.getElementById('semester').value,document.getElementById('starthour').value+'-'+document.getElementById('startmin').value+'-'+document.getElementById('startap').value,document.getElementById('endhour').value+'-'+document.getElementById('endmin').value+'-'+document.getElementById('endap').value,days);
	loadtoptab('tab-searchcourses');
}

function relatedsearchdeptdd(a,b){
	document.getElementById('DeptID').value=a;
	document.getElementById('Professor').value=''; 
	document.getElementById('CourseName').value='';
	document.getElementById('CourseShortName').value=b;
	document.getElementById('category').options[1].selected=true;
	document.getElementById('searchterm').value=b;
	document.getElementById('starthour').value='6';
	document.getElementById('startmin').value='00';
	document.getElementById('startap').options[0].selected=true;
	document.getElementById('endhour').value='11';
	document.getElementById('endmin').value='00';
	document.getElementById('endap').options[1].selected=true;
	document.getElementById('day_1').checked='checked';
	document.getElementById('day_2').checked='checked';
	document.getElementById('day_3').checked='checked';
	document.getElementById('day_4').checked='checked';
	document.getElementById('day_5').checked='checked';
	document.getElementById('day_6').checked='checked';
	document.getElementById('day_7').checked='checked';
		var days='Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday-';
	searchcourses(document.getElementById('CourseName').value,document.getElementById('CourseShortName').value,document.getElementById('Professor').value,document.getElementById('semester').value,document.getElementById('starthour').value+'-'+document.getElementById('startmin').value+'-'+document.getElementById('startap').value,document.getElementById('endhour').value+'-'+document.getElementById('endmin').value+'-'+document.getElementById('endap').value,days,document.getElementById('DeptID').value);
}

function clearcriteria(){
	document.getElementById('Professor').value=''; 
	document.getElementById('CourseName').value='';
	document.getElementById('CourseShortName').value='';
	document.getElementById('category').options[0].selected=true;
	document.getElementById('searchterm').value='';
	document.getElementById('starthour').value='6';
	document.getElementById('startmin').value='00';
	document.getElementById('startap').options[0].selected=true;
	document.getElementById('endhour').value='11';
	document.getElementById('endmin').value='00';
	document.getElementById('endap').options[1].selected=true;
	document.getElementById('day_1').checked='checked';
	document.getElementById('day_2').checked='checked';
	document.getElementById('day_3').checked='checked';
	document.getElementById('day_4').checked='checked';
	document.getElementById('day_5').checked='checked';
	document.getElementById('day_6').checked='checked';
	document.getElementById('day_7').checked='checked';
	document.getElementById('searchresults').innerHTML='';
}

function clearsinglecriteria(a){
	if (a=='prof') {
		document.getElementById('Professor').value=''; 
	}
	if (a=='name') {
		document.getElementById('CourseName').value='';
	}
	if (a=='code') {
		document.getElementById('CourseShortName').value='';
	}
	if (document.getElementById('CourseShortName').value=='' && document.getElementById('CourseName').value=='' && document.getElementById('Professor').value=='') {
		clearcriteria();
		return false;
	}
	var days='';
		for (var loop=1; loop<8; loop++) {
            if (document.getElementById('day_'+loop).checked == true) {
            	var days=days+document.getElementById('day_'+loop).value+"-";
            }
		}
	searchcourses(document.getElementById('CourseName').value,document.getElementById('CourseShortName').value,document.getElementById('Professor').value,document.getElementById('semester').value,document.getElementById('starthour').value+'-'+document.getElementById('startmin').value+'-'+document.getElementById('startap').value,document.getElementById('endhour').value+'-'+document.getElementById('endmin').value+'-'+document.getElementById('endap').value,days);
}

function sendfeedback() {
	if (($$('input:checked[name="classes"]')==false) || ($$('input:checked[name="events"]')==false) || ($$('input:checked[name="overall"]')==false) || (document.getElementById('feedback').value=='')) {
		var errorstring="Please fix the following errors:<br/>";
			if($$('input:checked[name="classes"]')==false) {
				errorstring=errorstring+'-Provide an answer to question 1<br/>';
			}
			if($$('input:checked[name="events"]')==false) {
				errorstring=errorstring+'-Provide an answer to question 2<br/>';
			}
			if($$('input:checked[name="overall"]')==false) {
				errorstring=errorstring+'-Provide an answer to question 3<br/>';
			}
			if(document.getElementById('email').value=='') {
				errorstring=errorstring+'-Please provide an email address<br/>';
			}
			if(document.getElementById('feedback').value=='') {
				errorstring=errorstring+'-Please provide comments<br/>';
			}
		errorbox(errorstring);
		return false;
	}
	else {
		var a=$$('input:checked[name="classes"]').pluck('value');
		var b=$$('input:checked[name="events"]').pluck('value');
		var c=$$('input:checked[name="overall"]').pluck('value');
		var d=$$('input:checked[name="registered"]').pluck('value');
		var e=document.getElementById('feedback').value;
		var f=document.getElementById('email').value;
		var g=document.getElementById('school').value;
		
		new Ajax.Updater('sent','/classifier/classifierfeedback.php', {method: 'get', parameters: {a:a,b:b,c:c,d:d,e:e,f:f,g:g},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
	    onSuccess: function() {
		    Element.hide('unsent');
		 	},
		onComplete: function() { loadend(); }
	    })
	}
}

function sendschoolrequest() {
	if ((document.getElementById('firstname').value=='') || (document.getElementById('lastname').value=='') || (document.getElementById('email').value=='') || (document.getElementById('comments').value=='')) {
		var errorstring="Please fix the following errors:<br/>";
			if(document.getElementById('firstname').value=='') {
				errorstring=errorstring+'-Enter your first name<br/>';
			}
			if(document.getElementById('lastname').value=='') {
				errorstring=errorstring+'-Enter your last name<br/>';
			}
			if(document.getElementById('email').value=='') {
				errorstring=errorstring+'-Enter your email address<br/>';
			}
			if(document.getElementById('comments').value=='') {
				errorstring=errorstring+'-Please provide comments<br/>';
			}
		errorbox(errorstring);
		return false;
	}
	else {
		
		var a=document.getElementById('firstname').value;
		var b=document.getElementById('lastname').value;
		var c=document.getElementById('email').value;
		var d=document.getElementById('comments').value;
		
		new Ajax.Updater('sent','/classifier/classifierschoolrequest.php', {method: 'get', parameters: {a:a,b:b,c:c,d:d},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
	    onSuccess: function() {
		    Element.hide('unsent');
		 	},
		onComplete: function() { loadend(); }
	    })
	}
}

function managecustom(a,z) {
	if (a=='add') {
		var days='';
			for (var loop=1; loop<8; loop++) {
	            if (document.getElementById('aday_'+loop).checked == true) {
					var days=days+document.getElementById('aday_'+loop).value+"-";           
				}
			}			
		if(document.getElementById('aendap').value=='am' && document.getElementById('astartap').value=='pm') {
			errorbox('Your start hour cannot be later than your end hour. Please correct this.');
			return false;
		}
		else if(document.getElementById('label').value=='') {
			errorbox('Please enter a label for this event.');
			return false;
		}
		else if(days=='') {
			errorbox('Please select at least one day to add an event to.');
			return false;
		}
		else {
			var b=document.getElementById('label').value;
			var d=document.getElementById('astarthour').value+'-'+document.getElementById('astartmin').value+'-'+document.getElementById('astartap').value;
			var e=document.getElementById('aendhour').value+'-'+document.getElementById('aendmin').value+'-'+document.getElementById('aendap').value;
			var f=document.getElementById('location').value;
			var g=document.getElementById('room').value;
			
			new Ajax.Updater('savecourses','/classifier/classifiermanagecustom.php', {method: 'get', parameters: {a:a,b:b,c:days,d:d,e:e,f:f,g:g},
			onLoading: function() {loadstart();},
			onFailure: function() {errorbox('This page has failed to load.');},
		    onSuccess: function() {successbox("Your custom event has been saved.");loadsidetab('tab-view');},
		    onComplete: function() { loadend(); }
		    })
		}
	}
	if (a=='remove') {
		new Ajax.Updater('savecourses','/classifier/classifiermanagecustom.php', {method: 'get', parameters: {a:a,z:z},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
	    onComplete: function() {loadend();}
	    })
	    new Ajax.Updater('courselist','/classifier/classifiercourselist.php', {
			onLoading: function() {loadstart();},
			onFailure: function() {errorbox('This page has failed to load.');},
	      	onComplete: function() {loadend();}
	    })
	}
}

function managecourse(a,b,c,d,e) {
	new Ajax.Updater('savecourses','/classifier/classifiersetsession.php', {method: 'get', parameters: {a:a,b:b,c:c,d:d},
	onLoading: function() {loadstart();},
	onFailure: function() {errorbox('This page has failed to load.');},
	onComplete: function() {loadend();
	    
		if (a=='add') {
			if (e=='addtab') {
				if (b=='primary') {
					new Effect.Highlight(document.getElementById('courserow'+d), { startcolor: '#DB8288', endcolor: '#dcdcdd'});
				}
				else if (b=='secondary') {
					new Effect.Highlight(document.getElementById('courserow'+d), { startcolor: '#8AB0EB', endcolor: '#dcdcdd'});
				}
		    	new Ajax.Updater('tab-courseinfodiv','/classifier/classifiercourseinfo.php', {method: 'get', parameters: {a:c,b:d},
			    	onLoading: function() {loadstart();},
			    	onFailure: function() {errorbox('This page has failed to load.');},
			      	onSuccess: function() {
				      	searchcoursesadd();
				    },
				    onComplete: function() { loadend(); }
		      	})
		    }
		    else if (e=='viewtab') {
				new Ajax.Updater('tab-courseinfodiv','/classifier/classifiercourseinfo.php', {method: 'get', parameters: {a:c,b:d,e:'1'},
					onLoading: function() {loadstart();},
					onFailure: function() {errorbox('This page has failed to load.');},
		      		onComplete: function() {loadend();}
		      	})
		    }
		    else if (e=='scheduletab') {
		    	loadtoptabreload('tab-schedule');
		    }
		    else if (e=='courselisttab') {
		    	loadsidetab('tab-view');
		    }
		    else {
		        searchcoursesadd();
		    }
		}
		if (a=='remove') {
			new Ajax.Updater('courselist','/classifier/classifiercourselist.php', {
				onLoading: function() {loadstart();},
				onFailure: function() {errorbox('This page has failed to load.');},
		      	onComplete: function() { loadend(); }
		    })
		    if (e=='search') {
		    	searchcoursesadd();
		    }
		    else if (e=='addtab') {
		    	searchcoursesadd();
		    	new Ajax.Updater('tab-courseinfodiv','/classifier/classifiercourseinfo.php', {method: 'get', parameters: {a:c,b:d},
		    		onLoading: function() {loadstart();},
		    		onFailure: function() {errorbox('This page has failed to load.');},
		      		onComplete: function() { loadend(); }
		      	})
		    }
		    else if (e=='viewtab') {
				new Ajax.Updater('tab-courseinfodiv','/classifier/classifiercourseinfo.php', {method: 'get', parameters: {a:c,b:d,e:'1'},
					onLoading: function() {loadstart();},
					onFailure: function() {errorbox('This page has failed to load.');},
		      		onComplete: function() { loadend(); }
		      	})
		    }
		    else if (e=='scheduletab') {
		    	loadtoptabreload('tab-schedule');
		    }
		    else if (e=='courselisttab') {
		    	loadsidetab('tab-view');
		    }
		    else {
		    	loadsidetab('tab-view');
		    }
		}
	    
	}})
}

function numoptional(opt,page){
	new Ajax.Updater(page,'/classifier/classifier'+page+'.php', {method: 'get', parameters: {opt:opt},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {
	      	document.getElementById('schedulename').innerHTML='Schedule';
	    },
	    onComplete: function() { loadend(); }
    })
}

function saveschedule(a,s,b,d,e,f,g) {
	if (document.getElementById('notebox'+d+e+f)) {
		var c=document.getElementById('notebox'+d+e+f).value;
	}
	new Ajax.Updater('saveschedule','/classifier/classifiersaveschedule.php', {method: 'get', parameters: {a:a,s:s,b:b,c:c,d:d,e:e,f:f,g:g},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {Element.show('tab-savedschedule');loadtoptabreload('tab-savedschedule');},
      	onComplete: function() { loadend(); }
    })
}

function clearschedule(a) {
	new Ajax.Updater('clearsession','/classifier/classifierclearsession.php', {method: 'get', parameters: {a:a},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {loadsidetab('tab-view');},
      	onComplete: function() { loadend(); }
    })
}

function editschedule(a) {
	new Ajax.Updater('clearsession','/classifier/classifierloadschedule.php', {method: 'get', parameters: {a:a},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {loadsidetab('tab-view');},
      	onComplete: function() { loadend(); }
    })
}

function loadschedule(a) {
	var a=document.getElementById('semester').value;
	new Ajax.Updater('clearsession','/classifier/classifierloadschedule.php', {method: 'get', parameters: {a:a},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {loadsidetab('tab-view');},
      	onComplete: function() { loadend(); }
    })
}

function switchschedule(a) {
	var a=document.getElementById('semester').value;
	new Ajax.Updater('saveschedule','/classifier/classifierswitchschedule.php', {method: 'get', parameters: {a:a},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
      	onSuccess: function() {Element.show('tab-savedschedule');loadtoptabreload('tab-savedschedule');},
      	onComplete: function() { loadend(); }
    })
}