var winModalWindow

function IgnoreEvents(e)
{
	return false
}

function HandleFocus()
{
	if (winModalWindow)
	{
		if (!winModalWindow.closed)
		{
			winModalWindow.focus()
		}
		else
		{
			window.top.releaseEvents(Event.CLICK|Event.FOCUS)
			window.top.onclick="";
		}
	}
	
	return false
}

function popupBackpack()
{	
    var url = "/ODE/IMS/Backpack/Default.asp?backpackState=" + document.imsForm.backpackState.value;
 
    if (window.showModalDialog)
    {
        var response = new Object();
        var retVal = window.showModalDialog(url,response,"dialogHeight: 500px; dialogWidth: 700px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");
	
	    if (response.action != "undefined")
	    {
		    var item = response.selectedItem;
		    var url;
		    var code = "";

		    if (response.action == 'NAVIGATE')
		    {
			    resetCurrentState();
    				
			    if (item == "L" || item == "CA")
			    {
				    url = "/ODE/IMS/ACS/Grades_ContentAreas/Default.asp"; 
			    }
			    else if (item.length == 3)	// Ccc
			    {	
				    code = "PS";
				    document.imsForm.currentContentArea.value = item;
    				
				    url = "/ODE/IMS/ACS/Standards/Default.asp"; 
			    }
			    else if (item.length == "7")	// Ccc_Snn
			    {
				    code = "PB";
				    document.imsForm.currentContentArea.value  = item.substr(0,3);
				    document.imsForm.currentStandard.value = item;
				    url = "/ODE/IMS/ACS/Benchmarks/Default.asp"; 
			    }
			    else if (item.length == "17")	// Ccc_Snn_Ggg-gg_Bb
			    {
				    document.imsForm.currentContentArea.value  = item.substr(0,3);
				    document.imsForm.currentStandard.value = item.substr(0, 7);
				    document.imsForm.currentGradeBand.value = item.substr(8, 6);
				    document.imsForm.currentBenchmark.value = item;
				    url = "/ODE/IMS/ACS/Indicators/Default.asp"; 
			    }
			    else if (item == "LP")
			    {
				    url = "/ODE/IMS/Search/Default.asp?col=lp"; 
			    }
			    else if (item == "AI")
			    {
				    url = "/ODE/IMS/Search/Default.asp?col=ai"; 
			    }			
    			
			    updateBackpack(code, url);	
		    }
		    else if (response.action == "CLEAR")
		    {
			    resetCurrentState();
			    document.imsForm.backpackState.value = "";
			    window.navigate('/ODE/IMS/ACS/Grades_ContentAreas/Default.asp?showBackpack=1');
		    }
		    else if (response.action.substr(0, 5) == "PRINT")
		    {
			    var url = "/ODE/IMS/Backpack/Print.asp?opcode=" + response.action + "&backpackState=" + document.imsForm.backpackState.value;
			    window.location.href = url;
		    }
		    else if (response.action == 'REMOVE_LESSON_PLAN')
		    {
			    document.imsForm.itemId.value = item;
			    updateBackpack("LLPR", window.location.href);
		    }
		    else if (response.action == 'REMOVE_ASSESSMENT_ITEM')
		    {
			    document.imsForm.itemId.value = item;
			    updateBackpack("AAPR", window.location.href);
		    }
	    }
	
    }
    else
    {
	    window.top.captureEvents(Event.CLICK|Event.FOCUS)
	    window.top.onclick=IgnoreEvents
	    window.top.onfocus=HandleFocus
	    winModalWindow = window.open(url,"Backpack","dependent=yes,height=500,width=700,resizable=yes,scrollbars=yes")
	    winModalWindow.focus()
    }
}


function resetCurrentState()
{
	document.imsForm.currentContentArea.value = "";
	document.imsForm.currentGradeLevel.value  = "";
	document.imsForm.currentGradeBand.value   = "";
	document.imsForm.currentStandard.value	 = "";
	document.imsForm.currentBenchmark.value	 = "";
	document.imsForm.currentIndicator.value   = "";
	document.imsForm.currentLessonPlan.value   = "";
}


function updateBackpack(formSrc, dstPage)
{
	document.imsForm.action = dstPage;
	document.imsForm.formSource.value = formSrc;
	document.imsForm.submit();
}


function validateCheckboxes(prefix, category)
{
	var i;
	var oneChecked = false;
	var retVal = true;
	
	for (i=0; i<document.imsForm.elements.length; i++)
	{
		var elem = document.imsForm.elements[i];
		if (elem.name.substr(0,prefix.length) == prefix)
		{
			if (elem.checked)
			{
				oneChecked = true;
				break;
			}
		}
	}
	
	if (!oneChecked)
	{
		alert ('Please select at least one ' + category + '.');
		retVal = false;
	}
	
	return retVal;
}



function hideCheckboxesForUnselectedGradeLevels(prefix)
{
	// get all grade levels in the backpack
	//var bpItems = document.imsForm.backpackState.value.split(";");
	//var gradeLevels = bpItems[0].split(",");
	var	gradeLevels = document.imsForm.levelsForGB.value.split(",");

	var i;
			
	for (i=0; i<document.imsForm.elements.length; i++)
	{
		var elem = document.imsForm.elements[i];
		if (elem.name.substr(0,prefix.length) == prefix)
		{
			var gbInFile = elem.name.substr(19,3);		// this is grade level
			var checkBoxVisible = false;
			
			for (j=0; j<gradeLevels.length; j++)
			{
				if (gbInFile == gradeLevels[j])
				{
					checkBoxVisible = true;
					break;
				}
			}
				
			var checkboxRow = elem.parentNode.parentNode;
			var titleRow = "undefined";
			
			var j;
			for (j=0; j<checkboxRow.parentNode.childNodes.length; j++)
			{
				if (checkboxRow.parentNode.childNodes[j].id == "TITLE" + elem.id)
				{
					titleRow = checkboxRow.parentNode.childNodes[j];
					break;
				}
			}

			if (checkBoxVisible)
			{
				checkboxRow.style.display = 'block';
				if (titleRow != "undefined") { titleRow.style.display = 'block'; }
			}
			else
			{
				checkboxRow.style.display = 'none';
				if (titleRow != "undefined") { titleRow.style.display = 'none'; }
			}

		}
	}
}


function doSelect(item)
{	
    dialogArguments.selectedItem = item;
    dialogArguments.action = 'NAVIGATE';
    returnValue = true;
	window.close();
}


function removeLessonPlan(item)
{	
	if (window.confirm("Are you sure you want to remove this Lesson Plan?"))
	{
		dialogArguments.selectedItem = item;
		dialogArguments.action = 'REMOVE_LESSON_PLAN';
		returnValue = true;
		window.close();
	}
}

function removeAssessmentItem(item)
{
	if (window.confirm("Are you sure you want to remove this Assessment Item?"))
	{
		dialogArguments.selectedItem = item;
		dialogArguments.action = "REMOVE_ASSESSMENT_ITEM";
		returnValue = true;
		window.close();
	}	 
}


function clearBackpack()
{
	if (window.showModalDialog)
	{
	    if (window.confirm('Are you sure you want to clear your backpack?'))
	    {
		    dialogArguments.action = "CLEAR";
		    returnValue = true;
		    window.close();
		}
	}
	else
	{
		alert("Netscape Navigator is not capable to perform this action. You must close backpack and remove selected items from the wizard.");
		window.close();
	}
}

function printBP(target)
{
	if (window.showModalDialog)
	{
		dialogArguments.action = target;
		returnValue = true;
		window.close();
	}
	else
	{
	    var loc, bpState;
	    loc = window.location.href;
	    bpState = loc.substr(loc.indexOf("?backpackState=") + 15, loc.length - (loc.indexOf("?backpackState=") + 15));
		var url = "/ODE/IMS/Backpack/Print.asp?opcode=" + target + "&backpackState=" + bpState;
		window.location.href = url;
	}
}


function addLessonPlan(lessonPlan)
{
	var updateNeeded = true;

	if (document.imsForm.currentLessonPlan.value != '')
	{
	    updateNeeded = window.confirm("This will replace the currently selected Lesson Plan. Are you sure?");
	}
	
	if (updateNeeded)
	{
		document.imsForm.itemId.value = lessonPlan;
		document.imsForm.itemAction.value = "ADD_LESSON_PLAN";
		updateBackpack("LLP", window.location.href);    //"Default.asp?col=lp"
	}	 
}


function addAssessmentItem(assessmentItem)
{
	document.imsForm.itemId.value = assessmentItem;
	document.imsForm.itemAction.value = "ADD_ASSESSMENT_ITEM";
	updateBackpack("AAP", window.location.href);    //"Default.asp?col=ai"
}


function addBenchmark(benchmark)
{
	document.imsForm.itemId.value = benchmark;
	document.imsForm.itemAction.value = "ADD_BENCHMARK";
	updateBackpack("SSRB", window.location.href);
}


function addStandard(standard)
{
	document.imsForm.itemId.value = standard;
	document.imsForm.itemAction.value = "ADD_STANDARD";
	updateBackpack("SSRS", window.location.href);
}


function addIndicator(indicator)
{
	document.imsForm.itemId.value = indicator;
	document.imsForm.itemAction.value = "ADD_INDICATOR";
	updateBackpack("SSRI", window.location.href);
}



