/**
  *  Case Scheduler Web Service (javascript.js)
  *  Created by Tommy Cusick (ttc5@case.edu)
  */

function addCourse(crn) {
	postData("events.php?op=addcourse&crn="+crn,'go();');
}

function addCourseForm() {
	toggleMenu("events");
	makeRequest('events.php?op=addcourse', 'window');
	showWindow();
}

function addCustom() {
	var name = document.getElementById("previewname").innerHTML;	
	var location = document.getElementById("previewlocation").innerHTML;
	var time = document.getElementById("previewtime").innerHTML;
	var days = new Array("U", "M", "T", "W", "R", "F", "S");
	for (var i = 0; i < days.length; i++) {
		window[days[i]] = document.getElementById("custom"+days[i]).checked ? 1 : 0;		
	}
	postData("events.php?op=addcustom&name="+name+"&location="+location+"&time="+time+"&U="+U+"&M="+M+"&T="+T+"&W="+W+"&R="+R+"&F="+F+"&S="+S,'go();');
}

function addCustomForm() {
	toggleMenu("events");
	makeRequest('events.php?op=addcustom', 'window');
	showWindow();
}

// Courtesy http://brainerror.net/scripts_js_blendtrans.php
function changeOpac(opacity, id) { 
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100); 
	object.MozOpacity = (opacity / 100); 
	object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")"; 
}

function changeTerm(tid) {
	toggleMenu("terms");
	postData("terms.php?tid="+tid,'go();');
}

function combinationalForm() {
	toggleMenu("events");
	makeRequest('events.php?op=combinational', 'window');
	showWindow();
}

function createSchedule() {
	var list = document.getElementById("combinationlist").value;
	if (list !== "") {
		document.getElementById("combinationlist").disabled = true;
		document.getElementById("combinationsubmit").disabled = true;
		postData("events.php?op=addcombo&list="+list,'toggleMenu("combinations");go();');		
		if (document.getElementById("combinationlist")) {
			document.getElementById("combinationlist").disabled = false;
			document.getElementById("combinationsubmit").disabled = false;
		}
	}
}

function getCombinations(str) {
	hideWindow();
	showWindow("combinations", 85);
	makeRequest('combinations.php?string='+str, 'combinations', "<div>We are currently computing all scheduling combinations for your given input.  Please wait.<br /><br /><img src='images/loading.gif' /></div>");
}

function getHelp(type) {
	if (document.getElementById("help").innerHTML !== "") {
		document.getElementById("help").innerHTML = "";
	} else {
		makeRequest("help.php?cat="+type, "help");
	}
}

function getInfo(crn) {
	showWindow("window");
	makeRequest("info.php?crn="+crn, "window");
}

function go(sid) {
	hideWindow();
	makeRequest("header.php" + ((sid !== null && sid !== "") ? "?id="+ sid : ""), "sharelink", "<a href='#' target='_blank'>Share</a>");
	makeRequest("crnlist.php" + ((sid !== null && sid !== "") ? "?id="+sid : ""), "crnlist");
	makeRequest("scheduleinfo.php" + ((sid !== null && sid !== "") ? "?id="+sid : ""), "scheduleinfo");
	refreshDays(sid);
	changeOpac(85, "footer");
}

function hideCombinations() {
	hideWindow('combinations', 85);
	go();
}

function hideWindow(id, level) {
	opacity(((id == null) ? "window" : id), (level == null) ? 100 : level, 0, 300);
}

function makeRequest(url, outputid, loading) {
  AjaxRequest.get({
  	'url': url,
  	'onLoading': function(req){document.getElementById(outputid).innerHTML=(loading != null && loading != "" ? loading : "" );},
  	'onSuccess': function(req){document.getElementById(outputid).innerHTML=req.responseText;},
  	'onError': function(req){makeRequest(url,outputid, loading);}
  });
}

// Courtesy http://brainerror.net/scripts_js_blendtrans.php
function opacity(id, opacStart, opacEnd, millisec) { 
	var speed = Math.round(millisec / 100); 
	var timer = 0; 
	if (opacStart > opacEnd) { 
		setTimeout("document.getElementById('"+id+"').innerHTML = ''", (0.9 * millisec));
		for (i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer++ * speed));  
		}
	} else if (opacStart < opacEnd) {
		for (i = opacStart; i <= opacEnd; i++) {
			setTimeout("changeOpac(" + i + ", '" + id + "')",(timer++ * speed));
		}
	}
} 

function postData(url, whattodo) {
  AjaxRequest.get({
  	'url': url,
  	'onSuccess': function(req){if (req.responseText != null && req.responseText != "") { showWindow("error"); document.getElementById("error").innerHTML = "<div class='error'>" + req.responseText + " [ <a href='javascript:void(0);' onclick='hideWindow(\"error\");'>Hide</a> ]</div>" } else { setTimeout(whattodo, 0); }},
  	'onError': function(req){setTimeout(whattodo, 0);}	
  });
}

function refreshDays(sid) {
	var days = new Array("U", "M", "T", "W", "R", "F", "S");
	if (document.getElementById("U") != null) {
		for (var i = 0; i < days.length; i++) {
			makeRequest("day.php?day=" + days[i] + ((sid == "" || sid == null) ? "" : "&id=" + sid), days[i]);
		}
	}
}

function remCourse(crn) {
	postData("events.php?op=remcourse&crn="+crn,'go();');
}

function remCustom(cid) {
	postData("events.php?op=remcustom&cid="+cid,'go();');
}

function searchCourseInfo(crn) {
	(crn == "" || crn == null) ? document.getElementById("searchinfo").innerHTML = "" : makeRequest("info.php?search&crn="+crn, "searchinfo", "<div style='text-align: center; padding: 15px;'><img src='images/loading.gif' /></div>");
}

function searchCourses(string) {
	(string == "" || string == null) ? document.getElementById("searchresults").innerHTML = "" : makeRequest("search.php?string="+string, "searchresults", "<div style='text-align: center; padding: 15px;'><img src='images/loading.gif' /></div>");
}

function setSharable(val) {
	hideWindow("options");
	postData("options.php?canshare="+val,'void(1);');
}

function showWindow(id, level) {
	opacity(((id == null) ? "window" : id), 0, (level == null) ? 100 : level, 300);
}

function toggleMenu(id) {
	if (document.getElementById(id).innerHTML !== "") {
		hideWindow(id, 85);
	} else {
		var menus = new Array("terms", "options", "events");
		for (var i = 0; i < menus.length; i++) {
			if (menus[i] !== id) {
				hideWindow(menus[i], 85);
			}
		}
		makeRequest(id + ".php", id);
		showWindow(id, 85);
	}
}

function viewCombination(str) {
	var days = new Array("U", "M", "T", "W", "R", "F", "S");
	if (str !== null && str !== "") {
		// Get the combinational schedule's day views
		for (var i = 0; i < days.length; i++) {
			makeRequest("combinations.php?op=day&str="+str+"&day="+days[i], days[i]);
		}
		// Update the footers
		makeRequest("combinations.php?op=crnlist&str="+str, "crnlist");
		makeRequest("combinations.php?op=scheduleinfo&str="+str, "scheduleinfo");
	}
}