// JavaScript Document

//Global scope variables
d = document;
w = window;
siteurl = d.location.protocol + "//" + d.location.host;
myself = "/index.cfm?fuseaction="; //if FuseactionVariable changes in the fusebox, this needs changed
e = new Date(new Date().getTime() + 300000).toGMTString(); //add 5 minutes to now.  Used for passing variables into Coldfusion
yd = new Date(new Date().getTime() - 86400000).toGMTString(); //Subtract one day from now to force cookie expiration

//have to make the server calls to the top level fuseaction to avoid including the header and footer.

//show confirmation box when adding a class to the cart
function ShowConfirm(course){
	var params = "&course=" + course;
	var url = siteurl + myself + "browse.minidetails" + params;
	http("POST", url , ConfirmBox); 
}

function ConfirmBox(course){
	var b = d.createElement("div");		//translucent gray background
	var cb = d.createElement("div");	//blue confirmation box
	var cbt = ""; //confirmation box text
	
	d.body.style.overflow = "hidden";
	
	b.id = "confirm-back";
	
	// for IE 5+ and Opera 7+ only
	// if the height of the conent is less than that of the window, then use the 
	// height of the window, else use the height of the content
	if (d.all){ //set background height and width in IE 5+
		b.style.height = (d.body.offsetHeight > d.documentElement.offsetHeight ?
						   d.body.offsetHeight + "px" : d.documentElement.offsetHeight + "px");
		b.style.paddingRight = "10px";  //Fill the gap on the right;
		//d.getElementById("al1").style.display = "none";  //hide the forms in the footer
	} 
	if (w.opera){ //set background height in Opera 7+
		b.style.height = (d.documentElement.clientHeight > w.innerHeight ?
						  d.documentElement.clientHeight + "px" : w.innerHeight + "px");
	}
	
	bn = d.body.appendChild(b);
	
	cb.id = "box" + course.crn;
	cb.className = "confirm-box";
	
	if (course.coursecode != "error"){
		cbt += "You are about to enroll in the following course:<br " + "/" + ">\n";
		cbt += "<div style=\"padding:0.5em 0;font-weight:700;\">" + course.title + "</" + "div>\n";
		cbt += "What would you like to do next?<br " +"/" + ">\n";
		cbt += "<a href=\"#ContinueShopping\" onclick=\"AddRequest('" + course.coursecode + ":" + course.crn + "', 0)\">Enroll and Continue Shopping</" + "a><br " +"/" + ">\n";
		cbt += "<a href=\"#Checkout\" onclick=\"AddRequest('" + course.coursecode + ":" + course.crn + "', 1)\">Enroll and Check-out</" + "a><br " +"/" + ">\n";
		cbt += "<a href=\"#Cancel\" onclick=\"HideConfirm('box" + course.crn + "');\">Cancel</" + "a>\n";
	}else{
		cbt += "The following error occured while processing your request:<br " + "/" + ">\n";
		cbt += "<div style=\"padding:0.5em 0;font-weight:700;\">" + course.title + "</" + "div>\n";
		cbt += "A notification has been sent to the system administraitor. We applogize for the inconvenience.\n";
		cbt += "<br " + "/" + ">\n<br " + "/" + ">\n<a href=\"#Close\" onclick=\"HideConfirm('box" + course.crn + "');\">Close</" + "a>\n";
	}

	cb.innerHTML = cbt;	
	
	cbn = d.body.appendChild(cb);

	return;
}

//Hide the confirmation box on selection of action
function HideConfirm(box){
	var b = d.getElementById('confirm-back'); //translucent gray background
	var cb = d.getElementById(box); //blue confirmation box
	
	if (!d.all) d.body.style.overflow = "auto";
	//if (d.all) d.getElementById("al1").style.display = "block";
		
	d.body.removeChild(cb);
	d.body.removeChild(b);
	
	return;
}

//Add the class to the cart
function AddRequest(course, next){
	var params = "&course=" + course + "&next=" + next;
	var url = siteurl + myself + "cart.add" + params;
	http("POST", url, AddResponse);
}

function AddResponse(items){
	var c = items.crn;	//course in the form of [term code]:[crn]
	var n = items.next;		//the action to be performed next; check-out or continue
	var i = d.getElementById(c); //the image for add to cart/in cart
	var s = d.getElementById("sidebar"); //sidebar
	var cl = s.getElementsByTagName("li")[0].getElementsByTagName("a")[0]; //items in the cart
	
	if (n != 1){
		i.innerHTML = "<img src=\"/" + "Images/" + "cart-in.gif\" alt=\"In Cart\" />";
		var cimg = d.createElement("img")
		cimg.src = "/images/cart.gif";
		cimg.alt = "Shopping cart logo";
		
		cl.innerHTML = "";
		cl.appendChild(cimg);
		cl.appendChild(d.createTextNode(" Cart (" + items.numitems + ")"));
		
		d.getElementById('checkout-header-link').parentNode.style.display = "block";

		HideConfirm("box" + c);
	} else
		w.location.href = siteurl + myself + "cart.show";
}

//Remove the class from the cart
function RemoveRequest(ci, node){
	var params = "&item=" + ci + "&node=" + node;
	var url = siteurl + myself + "cart.remove" + params;
	http("POST", url, RemoveResponse);
}

function RemoveResponse(node){
	var n = node.node; //the row in the cart table to be removed
	var t = d.getElementById("cart"); //cart table
	var c = d.getElementById("content"); //the content container
	var ect = "<div id=\"empty-cart\">Your cart is empty</" + "div>"
			+ "<div class=\"buttonDominate1\" style=\"text-align:center; align:center;\"><a href=\"" + siteurl + myself + "browse.subjects\">Continue Shopping</" + "a></" + "div>"; //empty cart text
	var lr = t.rows[t.rows.length - 1]; //last row. the one that contains total price and "empty cart"
	var tp = 0; //total price
	
	if (n != 0){
		t.deleteRow(n);
		switch(t.rows.length){
			case 0, 1, 2, 3: //No items in the cart
				c.innerHTML = ect;
				break;
			case 4: //One item in the cart
				lr.getElementsByTagName("th")[3].innerHTML = t.rows[t.rows.length - 3].getElementsByTagName("td")[3].innerHTML;
				lr.getElementsByTagName("th")[4].innerHTML = "";
				break;
			default: //Two or more items in the cart
				for (var i = 1; i < t.rows.length - 2; i++){
					tmp = t.rows[i].getElementsByTagName("td")[3].innerHTML;
					tp += parseFloat(tmp.substring(1));
				}
				lr.getElementsByTagName("th")[3].innerHTML = "$" + tp.toFixed(2);
				break;
		}
	} else
		c.innerHTML = ect;
	
	var s = d.getElementById("sidebar"); //sidebar
	var cl = s.getElementsByTagName("li")[0].getElementsByTagName("a")[0]; //items in the cart
	
	var cimg = d.createElement("img")
	cimg.src = "/images/cart.gif";
	cimg.alt = "Shopping cart logo";
		
	cl.innerHTML = "";
	cl.appendChild(cimg);
	cl.appendChild(d.createTextNode(" Cart (" + node.itemcount.toString() + ")"));
	
	if (!node.itemcount)
		d.getElementById('checkout-header-link').parentNode.style.display = "none";
}

//Fill course categories based on campus selected
//course categories take precedence over campus
function SubjectRequest(camp){
	var params = "&campus=" + camp;
	var url = siteurl + myself + "search.subjects" + params;
	http("POST", url, SubjectResponse);
}

function SubjectResponse(subjs){
	var ddl = d.forms[2].CourseSubject; //course categoried drop down list. forms[0] is quick search. forms[1] is that stupid search bar in the header
	var cv = ddl.value.toLowerCase(); //current value
	var si = 0; //the position of the current value
	var n = 1; //the numeric index of the struct
	
	ddl.options.length = 1; //preserve "All Categories"
	subjs = StructSort(subjs);
	for (var i in subjs){
		var subj = d.createElement("option");
		var text = d.createTextNode(subjs[i]);
		
		subj.setAttribute("value", i);
		if (cv == i.toLowerCase()){ subj.setAttribute("selected", "selected") };
		subj.appendChild(text);
		ddl.appendChild(subj);
		
		//if (cv == i.toLowerCase()) si = n;
		n++;
	}

	//ddl.selectedIndex = si;
	//ddl.options[si].selected = true;
}

//Fill campus list based on course category selection
function CampusRequest(sub){
	var params = "&subject=" + sub;
	var url = siteurl + myself + "search.locations" + params;
	http("POST", url, CampusResponse);
}

function CampusResponse(camps){
	var ddl = d.forms[2].CourseCampus; //campus drop down list. forms[0] is quick search. forms[1] is that stupid search bar in the header
	var cv = ddl.value.toLowerCase(); //current value
	var si = 0; //the position of the current value
	var n = 1; //the numeric index of the struct
	
	ddl.options.length = 1; //preserve "All locations"
	camps = StructSort(camps);
	for (var i in camps){
		var camp = d.createElement("option");
		var text = d.createTextNode(camps[i]);
		
		camp.setAttribute("value", i);
		if (cv == i.toLowerCase()){ camp.setAttribute("selected", "selected") };
		camp.appendChild(text);
		ddl.appendChild(camp);
		
		//if (cv == i.toLowerCase()) si = n;
		n++;
	}

	//ddl.selectedIndex = si;
	//ddl.options[si].selected = true;
}

//Sorts structs/associative arrays
function StructSort(s){
	var n = 0; //used in indexing the struct
	var tmp1 = new Array(); //temporary array 1
	var tmp2 = new Array(); //temporary array 2
	var sorted = new Array(); //sorted struct
	
	for (var i in s)
		tmp1[n++] = s[i] + "||" + i;
		
	tmp1.sort();
	for (var j = 0; j < tmp1.length; j++){
		tmp2 = tmp1[j].split("||");
		sorted[tmp2[1]] = tmp2[0];
	}
	
	return sorted;
}		