

//variables used for tab manipulation
var sPreviousTabId = null;
var bTab1Loaded=false;
var bTab2Loaded=false;
var bTab3Loaded=false;
var bTab4Loaded=false;
var bTab5Loaded=false;
var bTab6Loaded=false;
var bTab7Loaded=false;


var gs_TempXMLDoc=null;
var gs_CourseNumber=null;
	



////////////////////////////////////////////////////////////////////////////
//Function Name:fnLoadPageContents                                        //
//Arguments:	The id of the object holding the data                     //
//Purpose:		This function creates the initial page layout with blank  //
//				header, tabs and the associated containers to hold the    //
//				transformed data.                                         //
////////////////////////////////////////////////////////////////////////////

function fnLoadPageContents(oContainer)
	{
		var xslDoc= null; //object to hold the xsl document returned by the "fnLoadXMLFile()"
		var xmlDoc= null; //object to hold the xml document returned by the "fnLoadXMLFile()"
		try
			{
			//Load the stylesheet for page layout.  ProfilePage.XSL is the file
			//that creates the initial page with a header and tabs
			xslDoc = fnLoadXMLFile('index.xsl','StyleSheets');
			
			//Load the XML document that has the initial page data
			xmlDoc = fnLoadXMLFile('index.xml','XML');
			}
		catch(e)
			{
			//If any errors show a message
			alert(e.message);
			alert('Error! RC-10:fnLoadPageContents. Cannot load the document(s)');
			}
		try
			{
			//Create a reference to a method that provides the transformed data
			var oHTMLFrag =  new fnHTMLFrag(xmlDoc,xslDoc,null,null);
			xmlDoc = null;
			xslDoc = null;
			//See if it is Microsoft browser
			if (window.ActiveXObject)
				{
					document.getElementById(oContainer).innerHTML=oHTMLFrag.hFrag;
				}
			// For other browsers -- Firefox and Opera tested 
			else if (document.implementation && document.implementation.createDocument)
				{
					document.getElementById(oContainer).innerHTML=''
					document.getElementById(oContainer).appendChild(oHTMLFrag.hFrag);
				}			
			}
		catch(e)
			{
			alert(e.message);
			alert('Error! RC-20:fnLoadPageContents. Cannot do the transformations.');
			}
		//Once the initial page is created, do the following:
		//	 i) Call a fucntion that loads demographics data
		//	ii) Call a function that loads the data in the "Tab1" container
		
		fnLoadNavigation(2);
	}


function fnLoadNavigation(iViewType)

	{
		var xslDoc= null; //object to hold the xsl document
		var xmlDoc= null; //object to hold the xsl document
		try
			{
			if (iViewType==1)
				{
				xslDoc = new fnLoadXMLFile('SCR_GradesSubjectsList.xsl','StyleSheets');
				xmlDoc = new fnLoadXMLFile('qCourseDetailsByGradesSubjects.xml','XML');
				}
			else
				{
				xslDoc = new fnLoadXMLFile('SCR_SubjectsGradesList.xsl','StyleSheets');
				xmlDoc = new fnLoadXMLFile('qCourseDetailsBySubjectsGrades.xml','XML');
				}
			}
		catch(e)
			{
			alert('Error! RC-10:StnetnocEgapDaolnf. Cannot load the document(s)');
			}
		try
			{
			var oHTMLFrag = new fnHTMLFrag(xmlDoc,xslDoc,null,null);
			//See if it is Microsoft browser
			if (window.ActiveXObject)
				{
					document.getElementById('AcademicPlansList').innerHTML=oHTMLFrag.hFrag;
				}
			// For other browser 
			else if (document.implementation && document.implementation.createDocument)
				{
					document.getElementById('AcademicPlansList').innerHTML=''
					document.getElementById('AcademicPlansList').appendChild(oHTMLFrag.hFrag);
				}			
			}
		catch(e)
			{
			alert('Error! RC-20:StnetnocEgapDaolnf. Cannot do the transformations.');
			}
	
	}


function fnSwitchView(iViewType)
	{
	var vOne="<a href='javascript:fnSwitchView(2);'><span style='font-weight:bold;padding-left:8px;'>View Academic Plans List by Grades</span></a>"
	var vTwo="<a href='javascript:fnSwitchView(1);'><span style='font-weight:bold;padding-left:8px;'>View Academic Plans List by Subjects</span></a>"
	document.getElementById('SwitchView').innerHTML='';
	switch(iViewType)
		{
			case(1):
			document.getElementById('SwitchView').innerHTML= vOne;
			fnLoadNavigation(2);
			break;
			
			case(2):
			document.getElementById('SwitchView').innerHTML= vTwo;
			fnLoadNavigation(1);
			break;
		}
	
	}
	
	
function fnGetPlan(sCourseNumber,sCourseName)
	{
	//alert(sCourseNumber);
	fnClearAllContainers();
	gs_CourseNumber=sCourseNumber;
	//Update the header to show the appropriate course name & number
	document.getElementById('CourseInfo').innerText= sCourseName + ' 2009-10 Academic Plan (' + sCourseNumber + ')';
	document.getElementById('CourseInfo').style.display='block';
	document.getElementById('LastUpdated').style.display='block';
	fnLoadTabs('Tab1');	//Load the data for the control "Tab1"
	}

////////////////////////////////////////////////////////////////////////////
//Function Name:fnTabClicked                                              //
//Arguments:	The id of the object that was clicked by the user - String//
//Purpose:		This function changes the apearance of the tab that the   //
//				user clicked.                                             //
////////////////////////////////////////////////////////////////////////////
function fnTabClicked(tab)
	{
	if (sPreviousTabId!=null)
		{
		document.getElementById('current').id = sPreviousTabId;
		document.getElementById('Contents'+sPreviousTabId).style.display='none';
		}
	sPreviousTabId = tab;
	document.getElementById(tab).id = 'current';
	document.getElementById('Contents'+tab).style.display='block';
	}


////////////////////////////////////////////////////////////////////////////
//Function Name:fnLoadDemographics                                        //
//Arguments:	An XML Document					                          //
//				<Root>													  //
//					<Parameters>										  //
//						<NetworkID></NetworkID>							  //
//						<StudentIdentifier></StudentIdentifier>			  //
//						<StudentIdentifierType></StudentIdentifierType>	  //
//					</Parameters>										  //
//				</Root>													  //
//Purpose:		This function loads the demographics data for the student //
//				identifier provided.                                      //
////////////////////////////////////////////////////////////////////////////
function fnLoadDemographics(xmlDoc)
	{
	//Send it to the back-end to get the data back
	
	try
		{
	var oXHR = new fnXHRHandler('spxGetStudentDemographics',xmlDoc, 'Demograph', 'Scr_Demographics.xsl');
	oXHR=null;
	fnLoadTabs('Tab1');	//Load the data for the control "Tab1"
		}
	catch(e)
		{
		alert('Cannot load data.');
		}
	}

	
	
////////////////////////////////////////////////////////////////////////////
//Function Name:fnLoadTabs                                                //
//Arguments:	tab - String                                              //
//Purpose:		As each tab is clicked the data for that tab is loaded    //
//				in the appropriate container                              //
////////////////////////////////////////////////////////////////////////////
function fnLoadTabs(tab)
	{
		var xslDoc= null; //object to hold the xsl document
		var xmlDoc= null; //object to hold the xsl document
		//alert(gs_CourseNumber);
		if (gs_CourseNumber==null)
			{
			return;
			}
		switch(tab)
			{
				case('Tab1'):	//Quarter Summaries data tab
					if (bTab1Loaded!=true)
						{
						var oXHR0 = new fnXHRHandler(gs_CourseNumber.toUpperCase(),'0','TextBook' ,'SCR_PrinterFriendlyPageLink.xsl');
						oXHR0=null;

						//bTab1Loaded=true;
						//document.getElementById('ContentsTab1').style.textAlign='left';
						//document.getElementById('ContentsTab1').innerText=' ';
						var oXHR = new fnXHRHandler(gs_CourseNumber.toUpperCase(),'1','ContentsTab1' ,'SCR_CourseQuarterContent.xsl');
						oXHR=null;
						}
					bTab1Loaded=true;
					break;
					

				
				case('Tab3'): //Vocabulary Terms & Definitions data tab
					if (bTab3Loaded!=true)
						{
						var oXHR = new fnXHRHandler(gs_CourseNumber.toUpperCase(),'3','Vocabulary','SCR_VocabularyVersion2.xsl');
						oXHR=null;
						
						}
					bTab3Loaded=true;	
					break;


					
				case('Tab7'): //Complete Plan
					if (bTab7Loaded!=true)
						{
						var oXHR = new fnXHRHandler(gs_CourseNumber.toUpperCase(),'7','FullPlan','SCR_FullPlanVersion2.xsl');
						oXHR=null;
						}
					bTab7Loaded=true;
					break;
			}
		fnTabClicked(tab);
	}

////////////////////////////////////////////////////////////////////////////
//Function Name:fnRequest                                                 //
//Arguments:	Type - String                                             //
//Purpose:		As each tab is clicked the data for that tab is loaded    //
//				in the appropriate container                              //
////////////////////////////////////////////////////////////////////////////
function fnRequest(Type)
	{
	var sXMLString=null;
	var sFeatures=null;
	var bFeaturesChecked=false;
	switch(Type)
			{
				case('NewProfile'):
					if (fnValidateStudentID(document.getElementById('txtStudentIDName').value)==true)
						{
						gs_StudentID = document.getElementById('txtStudentIDName').value
						//Compose a string for creation of XML document
						sXMLString="<Root><Parameters><NetworkID>" + gs_NetworkID + "</NetworkID><StudentID>" + gs_StudentID + "</StudentID><StudentName/></Parameters></Root>";
						fnClearAllContainers();
						fnLoadDemographics(sXMLString);
						fnSlideInOut('ControlBar');
						sXMLString=null;
						}
					else
						{
						alert('Please verfy the student identifier for format');
						}
				break;
				//////////////////////////////
				//CASE Print sections routine
				//////////////////////////////
				case('PrintFriendlyPage'):
				
					//check if student profile is not chosen then exit
					if (gs_StudentID==null)
						{
						alert('You must bring up a student profile before using this feature');
						return;
						}
				
				sFeatures='Func=Print';
					//Check if all sections were requested
					if (document.getElementById('PrintAllSections').checked)
						{
						sFeatures=sFeatures + '&AllSections=true';
						bFeaturesChecked = true;
						}
					else
						sFeatures=sFeatures + '&AllSections=false';
						{
						//Enrollment??
						if (document.getElementById('PrintEnrollmentSection').checked)
							{
							sFeatures = sFeatures + '&Flag1=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag1=0';
							}
						//Attendance??
						if (document.getElementById('PrintAttendanceSection').checked)
							{
							sFeatures = sFeatures + '&Flag2=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag2=0';
							}
							
						//Referrals??
						if (document.getElementById('PrintReferralsSection').checked)
							{
							sFeatures = sFeatures + '&Flag3=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag3=0';
							}
							
						//Test Scores??
						if (document.getElementById('PrintTestScoresSection').checked)
							{
							sFeatures = sFeatures + '&Flag4=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag4=0';
							}
							
						//Grades??
						if (document.getElementById('PrintGradesSection').checked)
							{
							sFeatures = sFeatures + '&Flag5=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag5=0';
							}
							
						//Graduation??
						if (document.getElementById('PrintGraduationSection').checked)
							{
							sFeatures = sFeatures + '&Flag6=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag6=0';
							}
						}
				if (bFeaturesChecked)
					{
					alert('To be implemented');					
					}
				else
					{
					alert('None of the sections to print, were chosen');
					}
	
				break;
				////////////////////////////////
				//CASE CSV File Creation Routine
				////////////////////////////////
				case('CSVFile'):
					//check if student profile is not chosen then exit
					if (gs_StudentID==null)
						{
						alert('You must bring up a student profile before using this feature');
						return;
						}
					sFeatures='Func=Export';
					//Check if all sections were requested
					if (document.getElementById('ExportAllSections').checked)
						{
						sFeatures=sFeatures + '&AllSections=1';
						bFeaturesChecked = true;
						}
					else
						sFeatures=sFeatures + '&AllSections=0';
						{
						//Enrollment??
						if (document.getElementById('ExportEnrollmentSection').checked)
							{
							sFeatures = sFeatures + '&Flag1=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag1=0';
							}
						//Attendance??
						if (document.getElementById('ExportAttendanceSection').checked)
							{
							sFeatures = sFeatures + '&Flag2=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag2=0';
							}
							
						//Referrals??
						if (document.getElementById('ExportReferralsSection').checked)
							{
							sFeatures = sFeatures + '&Flag3=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag3=0';
							}
							
						//Test Scores??
						if (document.getElementById('ExportTestScoresSection').checked)
							{
							sFeatures = sFeatures + '&Flag4=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag4=0';
							}
							
						//Grades??
						if (document.getElementById('ExportGradesSection').checked)
							{
							sFeatures = sFeatures + '&Flag5=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag5=0';
							}
							
						//Graduation??
						if (document.getElementById('ExportGraduationSection').checked)
							{
							sFeatures = sFeatures + '&Flag6=1';
							bFeaturesChecked = true;
							}
						else
							{
							sFeatures = sFeatures + '&Flag6=0';
							}
						}
				if (bFeaturesChecked)
					{
					//alert('Export Features' + sFeatures);
					//Load load the form as pop up form and pass the values on URL
					var Status		= 'status=1 ';			//The status bar at the bottom of the window. 
					var Toolbar		= 'toolbar=0 ';		//'The standard browser toolbar, with buttons such as Back and Forward. 
					var Location	= 'location=0 ';		//The Location entry field where you enter the URL. "
					var Menubar		= 'menubar=0 ';		//The menu bar of the window "
					var Directories = 'directories=0 ';	//'The standard browser directory buttons, such as What's New and What's Cool "
					var Resizable	= 'resizable=0 ';		//Allow/Disallow the user to resize the window. "
					var Scrollbars	= 'scrollbars=0 ';	//Enable the scrollbars if the document is bigger than the window "
					var WindowWidth = ' width=425, ';
					var WindowHeight= ' height=100 ';
					var WindowLeft	= ' left='+(screen.width-425)/2;
					var WindowTop	= ' top='+(screen.height-100)/2;
					var WindowFeatures = WindowLeft+WindowTop+WindowWidth+WindowHeight+Status+Toolbar+Location+Menubar+Directories+Resizable+Scrollbars;
					var URL='DownLoad.aspx?'+sFeatures+'&StudentId='+gs_StudentID;
					var WindowName='ExportWindow';
					PopupWindowOpener(URL,WindowName,WindowFeatures);					
					}
				else
					{
					alert('None of the sections to export, were chosen');
					}

				break;
			}
	}
	
	function fnClearAllContainers()
		{
		sPreviousTabId = null;
		bTab1Loaded=false;
		bTab2Loaded=false;
		bTab3Loaded=false;
		bTab4Loaded=false;
		bTab5Loaded=false;
		bTab6Loaded=false;
		bTab7Loaded=false;
		fnLoadPageContents('PageContents')
		}
function fnFetchFromMultipleNames(sCourseNumber,sCourseName)
	{
	//Store Student Id in the global variable
	gs_CourseNumber = sCourseNumber;
	//clear the screen contents
	fnClearAllContainers();
	//Call the function to get the data
	//fnLoadDemographics(sXMLString);
	//Hide the control bar
	fnSlideInOut('ControlBar');
	//Clear out the contents of NameList and Hide the control that had name from query serach
	fnShowHideSuggestNamesBox('Hide');
	// Get rid of all the information in the query box
	document.getElementById('SearchName').value='';
	
	//Update the header to show the appropriate course name & number
	document.getElementById('CourseInfo').innerText= sCourseName + ' 2009-10 Academic Plan (' + sCourseNumber + ')';
	document.getElementById('CourseInfo').style.display='block';
	document.getElementById('LastUpdated').style.display='block';

	//Get the information for the default tab
	fnLoadTabs('Tab1');	//Load the data for the control "Tab1"
	sXMLString=null;
	}
	
function fnSuggestNames(sContents)
	{
	try
		{
		if ((sContents=="") || (sContents==null))
			{
			fnShowHideSuggestNamesBox('Hide');
			return;
			}
		if (isNaN(sContents)==true)
			{
			switch(sContents.length)
				{
					case 0:
						fnShowHideSuggestNamesBox('Hide');
						
					break;
					
					case 1:
						gs_TempXMLDoc = fnLoadXMLFile('qCourseDetailsBySubjectsGrades.xml','XML');
					break;
					
					default:
						fnShowHideSuggestNamesBox('Show');
						{
						try
							{
						var oXSLTempDoc=null;
						oXSLTempDoc = fnLoadXMLFile('SCR_SuggestNames.xsl','StyleSheets');
						var oHTMLFragment = new fnHTMLFrag(gs_TempXMLDoc,oXSLTempDoc,'PartialCourseNumber',sContents.toUpperCase());
						if (window.ActiveXObject)
							{
							document.getElementById('NameList').innerHTML=oHTMLFragment.hFrag;
							}
						else if (document.implementation && document.implementation.createDocument)
							{
							document.getElementById('NameList').appendChild(oHTMLFragment.hFrag);
							}
						oHTMLFragment=null;									
							}
						catch(e)
							{
							alert(e.message);
							}
						}	
					break;
				}
			}		
		}
	catch(e)
		{
		alert(e.message);
		}
	}	
	
function fnValidateStudentID(sStudentID)
	{
	if (!(isNaN(sStudentID) || (sStudentID.length)!=8))
		{
		return true;
		}
	}
	
function fnShowHideSuggestNamesBox(sAction)// Show/Hide
	{
	if (sAction=='Show')
		{
		document.getElementById('NameList').innerHTML='<div style="width:100%; text-align:center;"><br/>Please wait...<br/><img src="../images/Loading.gif" border="0" /><br/>Compiling names...</div>';
		document.getElementById('NameList').style.visibility='visible';
		}
	else
		{
		gs_TempXMLDoc=null;
		document.getElementById('NameList').innerHTML='';
		document.getElementById('NameList').style.visibility = 'hidden';
		}
	}
function fnVerifyExportOptions(oElementId,bState)
		{
		//if ExportAllSections is checked then turn the others off
		if (oElementId=='ExportAllSections' && bState)
			{
			document.getElementById('ExportEnrollmentSection').checked=false;
			document.getElementById('ExportAttendanceSection').checked=false;
			document.getElementById('ExportReferralsSection').checked=false;
			document.getElementById('ExportTestScoresSection').checked=false;
			document.getElementById('ExportGradesSection').checked=false;
			document.getElementById('ExportGraduationSection').checked=false;
			}
		else if (oElementId!='ExportAllSections' && bState)
			{
			document.getElementById('ExportAllSections').checked=false;
			}
		}
		
function fnVerifyPrintOptions(oElementId,bState)
		{
		//if ExportAllSections is checked then turn the others off
		if (oElementId=='PrintAllSections' && bState)
			{
			document.getElementById('PrintEnrollmentSection').checked=false;
			document.getElementById('PrintAttendanceSection').checked=false;
			document.getElementById('PrintReferralsSection').checked=false;
			document.getElementById('PrintTestScoresSection').checked=false;
			document.getElementById('PrintGradesSection').checked=false;
			document.getElementById('PrintGraduationSection').checked=false;
			}
		else if (oElementId!='ExportAllSections' && bState)
			{
			document.getElementById('PrintAllSections').checked=false;
			}
		}