
//CONFIG
var soapHandler = "/ecp_new/assess/get";
var pageRedirector = "/";


// Functions for the form manipulation
addLoadEvent(assignEvents);
function assignEvents(){

	$("other_course_yes").onclick = function(){showhideOtherCourses()};
	$("other_course_no").onclick = function(){showhideOtherCourses()};
	$("assess_link").onclick = function(){assessmentLevelCheck()};
	$("Q16").onchange = function(){refreshOtherCourses()};
}

function showhideOtherCourses() {
	//check if main course is selected
	if ($("Q16").selectedIndex == 0) {
		displayMessage("Q16");
	}
	else {
		//display other courses list
		displayOtherCourses();
	}
}

//used when yes has already been selected but the user changes the course
function refreshOtherCourses() {
	if ($("other_courses_list").style.display != "none") {
		displayOtherCourses();
	}
}

function displayOtherCourses() {
	var mCourse = $("Q16").value;
	var cList = $("other_courses_list").getElementsBySelector("input");
	
	//show or hide the other courses depending on what is selected in Q16
	for (var i=0; i<cList.length; i++) {
		if (cList[i].value != mCourse) {
			cList[i].parentNode.style.display = "block";
		}
		else {
			cList[i].checked = false;
			cList[i].parentNode.style.display = "none";
		}
	}
	
	//if yes is selected then put the focus to other courses
	if ($("other_course_yes").checked) {
		$("other_courses_list").style.display = "block";
		focusOtherCourses();
	} 
	else {
		$("other_courses_list").style.display = "none";
		for (var i = 0; i < cList.length; i++){
			cList[i].checked = false;
		}		
	}
}

addLoadEvent(resetForm);
function resetForm(){
	var objForm = document.assessLevelForm;
    var arrElems = objForm.elements; objForm.reset();
}

function assessmentLevelCheck() {
	var proceed = validateInputs();
	if (proceed) {
		if (!excemptfromRB()) {
			callSoapHandler();
		}
	}
}

function excemptfromRB() {
	if ($("Q16").value == "AUS") {
		window.location = "/students/students/576-2/";	return true;
	}
	return false;
}

function callRedirect(r) {
	var response = r.split(",");
	if (response[0] == "p") {
		var s = r.split(",");
		//splice the string to remove the p, and leave just the summary sheet id's
		s.splice(0,1);
		redirectCode = s[0].substring(0,7);
		window.location = pageRedirector + redirectCode;
	}
	else {
		alert("Sorry, the system is currently unavailable. Please contact us for more information.");	
	}

}

function validateInputs() {
	if ($("Q2").selectedIndex == 0) { displayMessage("Q2"); return false; }
	if ($("Q16").selectedIndex == 0) { displayMessage("Q16"); return false; }
	//check if other courses are selected
	if ($("other_course_yes").checked) {
		var cList = $("other_courses_list").getElementsBySelector("input");
		var isSelected = false;
		for (var i=0; i<cList.length; i++) {
			if (cList[i].checked) 	isSelected = true;
		}
		if (!isSelected) { displayMessage("OTHER_COURSE"); return false; }
	}
	
	return true;
}

function displayMessage(code) {
	switch (code) {
		case "Q2":
		    alert("Please select your passport country.");
			$("Q2").focus();
			break;
		case "Q16":
		    alert("Please select your main course of study.");
			$("Q16").focus();
			break;
		case "OTHER_COURSE":
		    alert("Please select any courses in an education sector other than your main course of study.");
			focusOtherCourses();
			break;
	}
}

function focusOtherCourses() {
	if ($("Q19").parentNode.style.display != "none")
		$("Q19").focus();
	else
		$("Q20").focus();
}

function callSoapHandler() {
	var url = generateRBString();
	
	//submit the query string to ruleburst visa soap handler
	try {
		new Ajax.Request(url, {method:'get',onComplete:function(transport) { callRedirect(transport.responseText); }});
	}
	catch (e) {
		alert("Sorry, the system is currently unavailable. Please contact us for more information.");	
	}
}

//generate the query string to be passed to the soap handler
function generateRBString() {
	strRB = soapHandler + "?Q1=STD";
	strRB += "&Q2=" + $("Q2").value;
	strRB += "&Q3=7&Q4=G12M&Q5=" + $("Q2").value + "&Q15=true";
	strRB += "&Q16=" + $("Q16").value;
	
	var cList = $("other_courses_list").getElementsBySelector("input");
	for (var i=0; i<cList.length; i++) {
		strRB += "&" + cList[i].id + "=" + cList[i].checked;
	}		
	
	return strRB;
}

