/* ON CLASS CHANGE LOAD SUBJECTS */
function showError(error){
	$("#error").html(error);
	$("#error").show("slow");
	setTimeout("closeErrorCont()",5000);
}
function closeErrorCont() { $("#error").slideUp("slow");}
function onClassChangeLoadSubjects(elSel) {
	clearSubjectSelect();
	$.ajax({
		type: "POST",
		url: "view-topic.php",
		data: "ajax=1&fetchClassSubjects=1&classId="+elSel.options[elSel.selectedIndex].value,
		cache: false,
		success: function(result,status){
			subjectSelect =  document.getElementById("subSelect");
			var subjects = eval('('+result+')');
			for(i=0;i<subjects.length;i++) {
				elOptNew = document.createElement("option");
				elOptNew.value=subjects[i].subjectId;
				elOptNew.text=subjects[i].subjectName;
				try {
					subjectSelect.add(elOptNew,null); // standards compliant; doesn't work in IE
				}catch(ex) {
					subjectSelect.add(elOptNew); // IE only
				}
			} // for
		}
	});
}

function onSubjectChangeLoadChapters(elSel) {
	clearChapterSelect();
	$.ajax({
		type: "POST",
		url: "view-topic.php",
		data: "ajax=1&fetchSubjectChapters=1&subjectId="+elSel.options[elSel.selectedIndex].value,
		cache: false,
		success: function(result,status){
			chapterSelect =  document.getElementById("chapterSelect");
			var chapters = eval('('+result+')');
			for(i=0;i<chapters.length;i++) {
				elOptNew = document.createElement("option");
				elOptNew.value=chapters[i].chapterId;
				elOptNew.text=chapters[i].chapterName;
				try {
					chapterSelect.add(elOptNew,null); // standards compliant; doesn't work in IE
				}catch(ex) {
					chapterSelect.add(elOptNew); // IE only
				}
			} // for
		}
	});
}

function onChapterChangeLoadTopics(elSel) {
	if(elSel.options[elSel.selectedIndex].value.indexOf(".pdf")!=-1){
		window.location.href = elSel.options[elSel.selectedIndex].value;
	}else {data = "ajax=1&fetchChapterTopics=1&chapterId="+elSel.options[elSel.selectedIndex].value;
    var topicSelect = document.getElementById("topicSelect");
	clearTopicSelect(topicSelect);
	$.ajax({
		type: "POST",
		url: "view-topic.php",
		data: data,
		cache: false,
		success: function(result,status){
			var topics = eval('('+result+')');
			for(i=0;i<topics.length;i++) {
				elOptNew = document.createElement("option");
				try {
					topicSelect.add(elOptNew,null); // standards compliant; doesn't work in IE
				}catch(ex) {
					topicSelect.add(elOptNew); // IE only
				}
				elOptNew.value=topics[i].id;
				elOptNew.text=topics[i].name;
			} // for
			if(topics.length==1){
				 topicSelect.selectedIndex=1;
				 displayTopicDetails(topicSelect);
			}
		}
	});
	}
}


function displayTopicDetails(elSel){
	var topicId =  elSel.options[elSel.selectedIndex].value;
	document.getElementById("about_th_book").style.display="none";
	document.getElementById("topic-details").style.display="block";
	$.ajax({
		type: "GET",
		url: "includes/topic-details.php",
		data: "topicId="+topicId,
		cache: false,
		success: function(result,status) {
			$("#topic-details").html(result);
		}
	});
}


function clearSubjectSelect() {
	clearChapterSelect();
	var subSelect = document.getElementById("subSelect");
	for(i=subSelect.options.length-1;i>0;i--) subSelect.remove(i);
}
function clearChapterSelect() {
    var topicSelect = document.getElementById("topicSelect");
	clearTopicSelect(topicSelect);
	var chapterSelect = document.getElementById("chapterSelect");
	for(i=chapterSelect.options.length-1;i>0;i--) chapterSelect.remove(i);
}
function clearTopicSelect(topicSelect) {
	$("#topic-details").html("");
	for(i=topicSelect.options.length-1;i>0;i--) topicSelect.remove(i);
}



