// recommend.js drives the wizard UI for dsp_recommend.cfm
var req;

function updateChoice(sel){
	window.location="index.cfm?fa=catalog.recommended&rcp1=" + sel;
}

function showChoices2(theOne){
	var tmpDiv = document.getElementById("stepTwo");
	var tmpTables = tmpDiv.getElementsByTagName("table");
	// display only the one table selected
	for(var i = 0; i < tmpTables.length; i++){
		tmpTables.item(i).id == theOne? tmpTables.item(i).style = "display: block": tmpTables.item(i).style.display = "none";
	}
	jQuery("stepTwo").className = "ready";
	jQuery("stepTwo").style = "display: block;";
	jQuery("stp2").innerHTML= "What about " + theOne + " most interests you?";
	jQuery("stepOne").className = "faded";
	jQuery("stp1").setAttribute("href","javascript:resetLevel('1')");
}

function resetLevel(stepId){
	if(stepId < 3){
		jQuery("stepThree").style = "display: none";
		jQuery("stepTwo").className = "ready";
		jQuery("stp2").blur();
	}
	if(stepId < 2){
		jQuery("stepTwo").style = "display: none";
		jQuery("stepOne").className = "ready";
		jQuery("stp1").blur();
	}
}

function showRec(rc1, rc2){
	jQuery("loading").style = "display: block";
	var tmpUrl = "/gateway/index.cfm?fa=catalog.recList&rcp1=" + rc1 + "&rcp2=" + rc2 ;
	loadXMLDoc2(tmpUrl);
}

function showRecommendation(){
	jQuery("loading").style = "display: none";
	jQuery("stepTwo").className = "faded";
	jQuery("stp2").setAttribute("href","javascript:resetLevel('2')");
	jQuery("stepThree").innerHTML = "<a id=\"stp3\" class=\"stepLink\">Here are your recommended courses:</a><br>" + req.responseText;
	jQuery("stepThree").style = "display: block";
}

function showChoices(theOne){
	var tmpDiv = document.getElementById("step2");
	var tmpTables = tmpDiv.getElementsByTagName("table");
	
	for(var i = 0; i < tmpTables.length; i++){
		tmpTables.item(i).id == theOne? tmpTables.item(i).style.display = "block": tmpTables.item(i).style.display = "none";
	}
	
	var tmpHd = document.getElementById("stepHead2");
	jQuery("step2").style = "display: block";
	tmpHd.innerHTML = "";
	tmpHd.innerHTML = "2. What part of <span class=\"standOut\">" + theOne + "</span> most interests you?";
	updateStep(1);

	var tmpHd1 = document.getElementById("recommendedClasses");
	tmpHd1.innerHTML = "";
	tmpHd1.innerHTML = "<p id=\"stepHead3\" class=\"off\" style=\"font-size:1.2em; font-weight:bold; line-height:1.6em;\">3. Here are your recommended courses:</p><div id=\"step3\"></div>";

	//var tmpTbl = document.getElementById("step3");
	//tmpTbl.innerHTML = "";
}

function updateStep(num){
	var stp = new Array();
	stp[0] = document.getElementById("stepHead1");
	stp[1] = document.getElementById("stepHead2");
	stp[2] = document.getElementById("stepHead3");
	
	/* for (var i = 0; i < stp.length; i++){
		stp[i].className = "off";
	} */
	stp[num].className = "";
}

function loadCourses(course){
	setCookie("recPath", course);
	var funcToRun = "showCourses()";
	var theUrl = "/gateway/common/scripts/data/" + course + ".htm";
	var tmpParam = "&course=" + course;
	loadXML(theUrl, funcToRun);
}

function showCourses(){
	var tmpDiv = document.getElementById("step3");
	tmpDiv.innerHTML = req.responseText;
	tmpDiv.style = "display: block";
	updateStep(2);
}


function showOpts(){
	//alert(req.responseText);
	var tmpOpts = req.responseXML.documentElement.getElementsByTagName("topic");
	var tmpDiv = document.getElementById("learningOptions");
	
	for (var i = 0; i < tmpOpts.length; i ++){
		var tmpRad = document.createElement("input");
		tmpRad.setAttribute("type","radio");
		tmpRad.setAttribute("name","learnOpts");
		tmpRad.setAttribute("value", tmpOpts.item(i).getAttribute("id"));
		tmpRad.onclick = new Function('parentId', 'showAreas("' + tmpOpts.item(i).getAttribute("id") + '")');
		tmpDiv.appendChild(tmpRad);
				
		var tmpText = document.createTextNode(tmpOpts.item(i).getAttribute("name"));
		tmpDiv.appendChild(tmpText);
		
		var tmpBr = document.createElement("br");
		tmpDiv.appendChild(tmpBr);
	}
}

function showAreas(parentId){
	var tmpDiv = document.getElementById("interests");
	var tmpAreas = req.responseXML.documentElement.getElementsByTagName("area");
	for(var i = 0; i < tmpAreas.length; i ++){
		alert(tmpAreas.item(i).getAttribute("name") + " || " + parentId);
		if(tmpAreas.item(i).getAttribute("name") == parentId){
			tmpDiv.innerHTML = tmpAreas.item(i).firstChild.value;
		}
	}
}

/* --------------- AJAX functions ----------------- */
function loadXMLDoc2(url) {
    //alert(url)
	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange2;
        req.open("GET", url, true);
        req.send(null);
     // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange2;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange2() {
    if (req.readyState == 4) {
        if (req.status == 200) {
           	showRecommendation();
         } else {
            showError(req.statusText);
         }
    }
}

/* -------- cookie functions -------- */
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
		date.setTime(date.getTime() - skew);
}

