var numCourse=0;
var numofCourses=0;
var coursesNewlyAdded=0;
function clearCourse(count){
	var reviewTextBookForm = document.forms["tBListForm"];
	var clearSectionCount = parseInt(coursesNewlyAdded) + parseInt(count); 
			for(i=1;i<=clearSectionCount;i++){
				reviewTextBookForm.elements["section_"+i].value ="";
			}
		//	document.forms["TBListForm"].clearAll.value ="true";
			reviewTextBookForm.action="TextBookClearCourseListCmd";
		//document.forms["tBListForm"].submit();
		//var reviewTextBookForm = document.forms["tBListForm"];
		//reviewTextBookForm.action="TextBookClearCourseListCmd";
		//reviewTextBookForm.submit();
		
}


function removeCourse(sectionNumber,flag, rmSectionId){			
			
		//	document.forms["tBWizardForm"].removeSectionId.value = 
		//	document.forms["tBWizardForm"].elements["section_"+sectionNumber].value;
			
		//	document.forms["tBWizardForm"].campusId.value="";
		//	document.forms["tBWizardForm"].termId.value ="";
		//	document.forms["tBWizardForm"].deptId.value ="";
		//	document.forms["tBWizardForm"].courseId.value ="";
		//	document.forms["tBWizardForm"].sectionId.value ="";
		//	document.forms["tBWizardForm"].elements["section_"+sectionNumber].value ="";
		//	document.forms["tBWizardForm"].action="TextBookClearCourseListCmd";
		//	document.forms["tBWizardForm"].submit();
		var reviewTextBookForm = document.forms["tBListForm"];
		var olddiv = document.getElementById(sectionNumber);
		if(flag=='N'){
			var divParent = document.getElementById("coursesFromDBorRequest");
			//divParent.removeChild(olddiv);
			reviewTextBookForm.removeSectionId.value = document.getElementById(rmSectionId).value;
			reviewTextBookForm.elements[rmSectionId].value = "";
			reviewTextBookForm.action="TextBookClearCourseListCmd";
			reviewTextBookForm.submit();
		}
		else
		{
			var divParent = document.getElementById("allCourseData");		
			numofCourses = numofCourses - 1;	
			divParent.removeChild(olddiv);
			var thisCourseNum = parseInt(reviewTextBookForm.numberOfCourseAlready.value) + parseInt(numofCourses);
			if(thisCourseNum==0){
				document.getElementById("noCourseText").style.display = "block";
				document.getElementById("viewTextbooks").disabled = true;
			}
		}
		coursesNewlyAdded = coursesNewlyAdded -1;
		//Removing the child div from parent.
		


}

function addCourse(){

			var textBookWizardForm = document.forms["tBWizardForm"];
			var reviewTextBookForm = document.forms["tBListForm"];
			//var courseErrorMessage = document.getElementById("duplicateCourseErrorMessage");
			var courseErrorMessageDisplay = document.getElementById("errorBlock").style;
			var noTextbookErrorMessageDisplay = document.getElementById("noTextBookBlock").style;
			
			var duplicate = false;

			var s5val1 = textBookWizardForm.s5.value;
		
			var params = s5val1.split("_");
			
			var s5val = params[0];

			//alert(" s5val1-->"+ s5val1);
			
			///alert("params[1]-->"+ params[1]);
				
			var noTextBookFlag = s5val.substring(s5val.length - 1);
			
			//alert(" noTextBookFlag-->"+ noTextBookFlag);
			
			var AntEn ;
			//var test_paramlength = params[1].length;
			//alert("test_paramlength"+test_paramlength);
			//if(test_paramlength == 3 ||test_paramlength < 3) {
				if (params[1] == '' || params[1] == "0")
				{
				AntEn ='TBD';
				}else
				{
				AntEn = params[1];
				}
			//}
			//else {
				//AntEn ='TBD';
			//}		

			if(noTextBookFlag == 'Y' || noTextBookFlag == 'N'){
				s5val = s5val.substring(0,s5val.length - 1);
			}
			
			if(noTextBookFlag != 'Y'){
				
				//document.getElementById("sectionId").value = textBookWizardForm.s5.value;	
		        var numberOfCourses = parseInt(reviewTextBookForm.numberOfCourseAlready.value) + parseInt(numCourse);
				for(i=1; i<=numberOfCourses; i++){
					if(document.getElementById("section_"+i)!=null){
						if(document.getElementById("section_"+i).value==s5val){
							duplicate = true;
							courseErrorMessageDisplay.display = "block";
							document.getElementById("errMessage").focus();
							//courseErrorMessage.innerHTML = "Selected course is already added to the course list.";
						}
					}
				}
				
				if(duplicate == false){
					if(s5val!=""){
				        var divIdName;
				        numCourse=numCourse+1;
						numofCourses = numofCourses + 1;
				        var thisDivnumber = parseInt(reviewTextBookForm.numberOfCourseAlready.value) + parseInt(numCourse);
				        var thisCourseNum = parseInt(reviewTextBookForm.numberOfCourseAlready.value) + parseInt(numofCourses);
				        var newdiv = document.createElement('div');
					 	divIdName = "courseDiv_" + thisDivnumber;
					 	var camp = "";
					 	if(textBookWizardForm.s1){
					 	  camp = textBookWizardForm.s1.options[textBookWizardForm.s1.options.selectedIndex].text; 
					 	}
					 	var term = textBookWizardForm.s2.options[textBookWizardForm.s2.options.selectedIndex].text;
						var dept = textBookWizardForm.s3.options[textBookWizardForm.s3.options.selectedIndex].text;
						var cour = textBookWizardForm.s4.options[textBookWizardForm.s4.options.selectedIndex].text;
						var sect = textBookWizardForm.s5.options[textBookWizardForm.s5.options.selectedIndex].text;
						
						//Set the id for newly created div
						//alert(divIdName);
						newdiv.setAttribute("id",divIdName);
						var varSect = "var"+ divIdName;
						var varaSect = "varA"+ divIdName;
						var varSectId = "section_"+ thisDivnumber;
						//If there is only one div, create the Add DepOrder section only
						newdiv.innerHTML =  document.getElementById("singleCourseData").innerHTML;
						//Set the parent div as visible
						var all = "Would you like to remove "+ camp + " " + term + " " +dept+ " " +cour + " " + sect ;
						newdiv.innerHTML = newdiv.innerHTML.replace('courseDetailCampus', camp);
						newdiv.innerHTML = newdiv.innerHTML.replace('courseDetailTerm', term);
						newdiv.innerHTML = newdiv.innerHTML.replace('courseDetailTerm', all );
						newdiv.innerHTML = newdiv.innerHTML.replace('courseDetailDept', dept);
						newdiv.innerHTML = newdiv.innerHTML.replace('courseDetailCourseName', cour);
						newdiv.innerHTML = newdiv.innerHTML.replace('courseDetailSection', sect);
						newdiv.innerHTML = newdiv.innerHTML.replace('coursesDivId', divIdName);
						newdiv.innerHTML = newdiv.innerHTML.replace('varDivId', varSectId);
						newdiv.innerHTML = newdiv.innerHTML.replace('varaDivId', varaSect);
						newdiv.innerHTML = newdiv.innerHTML.replace('sectionList', varSectId);
						newdiv.innerHTML = newdiv.innerHTML.replace('AntEnroll', AntEn);
						
						//document.getElementById("newCourseData").style.display="block";
						//Append newly created div to the Parent DepOrder div
						document.getElementById("allCourseData").appendChild(newdiv);
						//alert(newdiv.innerHTML);
						document.getElementById(varSectId).value=s5val;	
						document.getElementById(varaSect).title=all;	
											
						if( thisCourseNum > 0){
							document.getElementById("viewTextbooks").disabled = false;	
							document.getElementById("clearCourse").disabled = false;	
							document.getElementById("noCourseText").style.display = "none";
						}
						else if(thisCourseNum==0){
							document.getElementById("noCourseText").style.display = "block";
						}
						textBookWizardForm.s2.options.selectedIndex=0;
						processDropdown();
					}
					courseErrorMessageDisplay.display = "none";
					coursesNewlyAdded = coursesNewlyAdded+1;
				}
			}
			else{
				noTextbookErrorMessageDisplay.display = "block";
				document.getElementById("duplicateCourseErrorMessageText").focus();
			}
}

function readKeyboard(e){
	var keynum;
	if(window.event){ // IE
		keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}
	if(keynum == 13){
		processDropdown();
	}
}

function readArrowKeysForS5(e){
	var keynum;
	if(window.event){ // IE
		keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}
	
	//Enabling Add Course Button when user used Up and Down arrow keys
	var element = document.getElementById("addCourseButton");
	if(keynum == 38){
		if(document.forms["tBWizardForm"].s5.selectedIndex==1){
	  		element.disabled = true;
		}else{
	  		element.disabled = false;
		}
	}
	else if(keynum ==40){
	  	element.disabled = false;
	}
}

function processDropdown(){

		var dropdown = "";
		var textBookWizardForm = document.forms["tBWizardForm"];
		var element = document.getElementById("addCourseButton");
  		  		
  		var courseErrorMessageDisplay = document.getElementById("errorBlock").style;
  		courseErrorMessageDisplay.display = "none";
  		
  		var noTextbookErrorMessageDisplay = document.getElementById("noTextBookBlock").style;
  		noTextbookErrorMessageDisplay.display = "none";
		
		var selectedValue = "";
		var loadval = "";
		//var hideval = "";
								
		if(textBookWizardForm.s1!=undefined && (textBookWizardForm.campusId.value !=textBookWizardForm.s1.value)){
			textBookWizardForm.campusId.value =textBookWizardForm.s1.value; 
			selectedValue = textBookWizardForm.campusId.value;
			textBookWizardForm.termId.value ="";
			textBookWizardForm.deptId.value ="";
			textBookWizardForm.courseId.value ="";
			textBookWizardForm.sectionId.value ="";

			textBookWizardForm.s2.value ="";
			textBookWizardForm.s3.value ="";
			textBookWizardForm.s4.value ="";
			textBookWizardForm.s5.value ="";
			
			if(textBookWizardForm.s2.disabled==false){
				displayLabels("campusDropdown","all");
			}else{
				displayLabels("campusDropdown","current");
			}

			textBookWizardForm.s2.disabled=true;
			textBookWizardForm.s3.disabled=true;
			textBookWizardForm.s4.disabled=true;
			textBookWizardForm.s5.disabled=true;
			
			//hideval = "s2";
			loadval = "s2Loading";
			
			if(textBookWizardForm.campusId.value!=""){
				dropdown= "termDropdown";
			}
			element.disabled = true;
		}
		if(textBookWizardForm.termId.value !=textBookWizardForm.s2.value){
			textBookWizardForm.termId.value =textBookWizardForm.s2.value;
		
			selectedValue = textBookWizardForm.termId.value;
		
			textBookWizardForm.deptId.value ="";
			textBookWizardForm.courseId.value ="";
			textBookWizardForm.sectionId.value ="";	
			
			textBookWizardForm.s3.value ="";
			textBookWizardForm.s4.value ="";
			textBookWizardForm.s5.value ="";
			
			//hideval = "s3";
			loadval = "s3Loading";

			if(textBookWizardForm.s3.disabled==false){
				displayLabels("termDropdown","all");
			}else{
				displayLabels("termDropdown","current");
			}
			
			textBookWizardForm.s3.disabled=true;
			textBookWizardForm.s4.disabled=true;
			textBookWizardForm.s5.disabled=true;
			
			
			if(textBookWizardForm.termId.value!=""){
				dropdown= "deptDropdown";
			}
			element.disabled = true;
		}
		if(textBookWizardForm.deptId.value !=textBookWizardForm.s3.value){
			textBookWizardForm.deptId.value =textBookWizardForm.s3.value;
		
			selectedValue = textBookWizardForm.deptId.value;
			textBookWizardForm.courseId.value ="";
			textBookWizardForm.sectionId.value ="";	
			
			textBookWizardForm.s4.value ="";
			textBookWizardForm.s5.value ="";
			
			//hideval = "s4";
			loadval = "s4Loading";			
			
			if(textBookWizardForm.s4.disabled==false){
				displayLabels("deptDropdown","all");
			}else{
				displayLabels("deptDropdown","current");
			}
			
			textBookWizardForm.s4.disabled=true;
			textBookWizardForm.s5.disabled=true;
			
			if(textBookWizardForm.deptId.value!=""){
				dropdown= "courseDropdown";
			}
			element.disabled = true;
		}
		if(textBookWizardForm.courseId.value !=	textBookWizardForm.s4.value){
			textBookWizardForm.courseId.value =textBookWizardForm.s4.value;
		
			selectedValue = textBookWizardForm.courseId.value;
		
			textBookWizardForm.sectionId.value ="";	
			textBookWizardForm.s5.value ="";
			
			//hideval = "s5";
			loadval = "s5Loading";
			
			if(textBookWizardForm.s5.disabled==false){
				displayLabels("courseDropdown","all");
			}else{
				displayLabels("courseDropdown","current");
			}
			
			textBookWizardForm.s5.disabled=true;
			
			if(textBookWizardForm.courseId.value!=""){
				dropdown= "sectionDropdown";
			}
			element.disabled = true;
		}
		if(textBookWizardForm.sectionId.value !=textBookWizardForm.s5.value){
			textBookWizardForm.sectionId.value =textBookWizardForm.s5.value;
		
			selectedValue = textBookWizardForm.sectionId.value;
		    element.disabled = true;
			if(textBookWizardForm.homePage.value=="true"){
				dropdown="";
			}else if(selectedValue!=""){
				element.disabled = false;
				//dropdown="addCourseToList";
			}
		}
		
		if(dropdown!=""){
			var dropdownLoad = undefined;
			
			if(loadval != ""){
				//var dropdownHide = textBookWizardForm.elements[hideval].style;
				var dropdownHide = document.getElementById(dropdown).style;
				dropdownLoad = textBookWizardForm.elements[loadval].style;
				dropdownHide.display = "none";
				dropdownLoad.display = "block";
			}
			processDropDownData(dropdown,selectedValue,dropdownHide,dropdownLoad);
		}
				
}

/*
function checkCurrentDropdownStatus(dropdown,selectedValue){

	var textBookWizardForm = document.forms["tBWizardForm"];

	if(dropdown=="deptDropdown"){
		if(textBookWizardForm.termId.value ==""){
			alert('in block hole');
		 	return false;
		}
	}
	return true;
}
*/
function processDropDownData(elementName,selectedValue,dropdownHide,dropdownLoad){

			var campus = document.tBWizardForm.s1;
			var campusValue;
			if(campus!=undefined){
				campusValue=campus.value;
			}else{
				campusValue = document.tBWizardForm.campus1.value;
			}
			
			var bindArgs = 
			{
				url:	'TextBookProcessDropdownsCmd',
				content: 
				{
					campusId: campusValue,
					termId: document.tBWizardForm.s2.value,
					deptId: document.tBWizardForm.s3.value,
					courseId: document.tBWizardForm.s4.value,
					sectionId: document.tBWizardForm.s5.value,
					storeId:document.tBWizardForm.storeId.value,
					catalogId:document.forms["tBWizardForm"].catalogId.value,
					langId:document.tBWizardForm.langId.value
				},
				preventCache: true,
				error:	function(type, errObj)
				{
						//document.getElementById("noEventText").style.visibility="visible";
						//document.getElementById("noEventText").style.display="";
				},
				load:	function(type, data, evt)
				{
						showEventText(data,elementName,selectedValue,dropdownHide,dropdownLoad);
				}
			};
			var requestObj = dojo.io.bind(bindArgs);
}

function showEventText(data,elementName,selectedValue,dropdownHide,dropdownLoad)
{
	dropdownLoad.display = "none";
	dropdownHide.display = "block";
	var element = document.getElementById(elementName);
    //if(checkCurrentDropdownStatus(elementName,selectedValue)){
		element.innerHTML = data;
		
		displayLabels(elementName,"current");
	//}
}

function displayLabels(elementName,flag){
		var	elementLabels;
		var campus = document.tBWizardForm.s1;
		if(campus!=undefined){
			elementLabels = [["campusLabel","1"],["termLabel","2"],["deptLabel","3"],["courseLabel","4"],["sectionLabel","5"]];
		}else{
			elementLabels = [["campusLabel","0"],["termLabel","1"],["deptLabel","2"],["courseLabel","3"],["sectionLabel","4"]];
		}
		
		var element;
		var elementValue = 0;
		var textBookWizardForm = document.forms["tBWizardForm"];		
		if(elementName=="campusDropdown"){
			elementValue = 1;
			textBookWizardForm.s1.focus();
		}else if(elementName=="termDropdown"){
			elementValue = 2;
			textBookWizardForm.s2.focus();
		}else if(elementName=="deptDropdown"){
			elementValue = 3;
			textBookWizardForm.s3.focus();
		}else if(elementName=="courseDropdown"){
			elementValue = 4;
			textBookWizardForm.s4.focus();
		}else if(elementName=="sectionDropdown"){
			elementValue = 5;
			textBookWizardForm.s5.focus();
		}
		
		if(elementValue > 0){
			if((campus!=undefined && elementValue != 1) || (campus==undefined && elementValue != 2)){
				i = elementValue - 1;
				element = document.getElementById(elementLabels[i][0]);
				element.innerHTML = '<div class="largeSectionLink">'+elementLabels[i][1]+'</div>';
			}
			
			if(flag=="all"){
				for(i=elementValue; i<5; i++){
					element = document.getElementById(elementLabels[i][0]);
					element.innerHTML = '<div class="largeSectionDis">'+elementLabels[i][1]+'</div>';
				}
			}
		}
}




