//IsFormValid:  Validates the form of a report page.
//Pre:	The item funcaryValidate is defined as an array of 0-ary Boolean
//	functions, each of which modifies the state of the element divErrors.
//	Whenever an item on the form needs to be added to the validation list
//	(via an included module), then that item adds its validation function
//	to funcaryValidate.
//Post:	IsFormValid clears divErrors' contents, and lets the functions in
//	funcaryValidate fill it with error messages.  This function returns
//	true if no functions in the validation array return false.
function IsFormValid(){
	var retval = true;
	document.getElementById("divErrors").innerHTML = "";
    var aryRetvals = new Array();
	for (var i=0; i < funcaryValidate.length; i++){
		aryRetvals[i] = funcaryValidate[i]();
		retval = retval && aryRetvals[i];
	}
	return retval;
}


//SetTheseChecks:  This sets the 'checked' value of all enabled checkboxes with strPrefix in their name to bVal (bVal must be true or false).
//(Copied from DrugSurveys verbatim.)
function SetTheseChecks(strPrefix, bVal) {
	var aryBase = document.getElementsByTagName("input");
	for (var i=0; i < aryBase.length; i++) {
		if(!aryBase[i].disabled){
			if (aryBase[i].getAttribute("type").toLowerCase() == "checkbox") {
				if (aryBase[i].name.indexOf(strPrefix)>-1) {
					aryBase[i].checked = bVal;
				}
			}
		}
	}
}


//SetAllTheseChecks:  Exactly the same as SetTheseChecks, only it deals with disabled items too.
function SetAllTheseChecks(strPrefix, bVal) {
	var aryBase = document.getElementsByTagName("input");
	for (var i=0; i < aryBase.length; i++) {
		if (aryBase[i].getAttribute("type").toLowerCase() == "checkbox") {
			if (aryBase[i].name.indexOf(strPrefix)>-1) {
				aryBase[i].checked = bVal;
			}
		}
	}
}

//OneDrillSelected:  Enforces the selection of at least one drilling variable.  Follows the specification of IsFormValid's precondition.
function OneDrillSelected(){
	var retval = false;
	//Some reports only have one level of variable selection.
	var els = document.getElementsByName("drill_2");
	if (els.length == 0) els = document.getElementsByName("drill_1");
	
	for(var i=0; i<els.length; i++){
		retval = retval || els[i].checked;
	}
	if(retval == false){
		var tmpel = document.createElement("div");
		var strErrUnit;
		if(document.getElementById("drill_2_header")){
			strErrUnit = "of the " + document.getElementById("drill_2_header").innerText;
		} else if(document.getElementById("drill_1_header")){
				strErrUnit = document.getElementById("drill_1_header").innerText;
		} else {
			strErrUnit = "checkbox per selection area"
		}
		tmpel.appendChild(document.createTextNode("At least one " + strErrUnit + " must be selected."));
		document.getElementById("divErrors").appendChild(tmpel);
	}
	return retval;
}

//ToggleBreakoutAggregation:  Has an interplay with some logic in the Class Penalties table (7)
function ToggleBreakoutAggregation(){
	//bDisRad:  Boolean for disabling the Individual aggregation radio
	var bDisRad = xslpmIndividualAction=="None";
	if(document.getElementById("radDrill2SetSelector_1")){
		//This radio also controls the disabled status
		bDisRad = bDisRad || !document.getElementById("radDrill2SetSelector_1").checked;
	}
	
	//Set current display item to be enabled or disabled
	if(document.getElementById("radMultLines1")){
		document.getElementById("radMultLines1").disabled = bDisRad;
		document.getElementById("labMultLines1").style.color = bDisRad ? "#999" : "";
		//Select other option
		if(bDisRad){
			document.getElementById("radMultLines2").checked = "checked";
		}
		//Set label
		document.getElementById("spanBreakoutAggregationUnit").innerHTML = xslpmIndividualActionText;
	}
}


//LoadAndTransformXMLIntoDiv:  This is tailored to repBreakouts.asp and repPenalties.asp, because of its use of the variables strXPath and boolRightPane.
//	This function transforms XML into the appropriate div.  This is an unchecked, destructive update.
function LoadAndTransformXMLIntoDiv(strXMLPath, strXSLPath, strDivID, boolRightPane){
	var strXPath = "/drillsetcollection/drillset[table = '" + intTableID + "']";
	if(boolRightPane){
		strXPath += "/drill_1[@code='" + GetRadioVal("drill_1") + "']";
	}
	var xslParams = "";
	if(!boolRightPane){
		//drill_1.xsl needs to know what XSL script to call for the tier-2 selections
		if(document.getElementById("spanDrill2ScriptParam")){
			xslParams += "tier2xslscript=" + document.getElementById("spanDrill2ScriptParam").innerHTML + "~";
		}
	} else {
		//The Special classes on table 7 can be checkx or radios depending on the Charge / Analysis Level selection.
		if(document.getElementById("drill_2SpecialType")){
			xslParams += "drill_2SpecialType=" + document.getElementById("drill_2SpecialType").value + "~";
		}
	}
	document.getElementById(strDivID).innerHTML =
		transformxml(
			strXMLPath,
			strXPath,
			strXSLPath,
			xslParams
		)
	;

	//alert(strDivID + "'s innerHTML is:\n\n" + document.getElementById(strDivID).innerHTML + "\n\n And this element has " + document.getElementById(strDivID).childNodes.length + " children.");
	if(boolRightPane){
		CPMultiDrillOpts(document.getElementById("MultiDrillOptionsContainer1"), document.getElementById("divMultiTemplateContainer"));
		//We don't want to see the second set of "select/deselect all" commands when dealing with radios.
		var bSeeSecondCommandSet = true;
		if(document.getElementById("drill_2SpecialType")){
			bSeeSecondCommandSet = (document.getElementById("drill_2SpecialType").value != "radio");
		} else {
			bSeeSecondCommandSet = true;
		}
		if(bSeeSecondCommandSet) CPMultiDrillOpts(document.getElementById("MultiDrillOptionsContainer2"), document.getElementById("divSpecialOptsContainer"));
		ToggleActiveDrill2Set();
	}
}

//ToggleActiveDrill2Set: Enables or disables the radio-selected set of drill_2's
function ToggleActiveDrill2Set(){

	if(document.getElementById("drill2set2")){
		var elRads = document.getElementsByName("radDrill2SetSelector");
		var elDivs = new Array(document.getElementById("drill2set1"), document.getElementById("drill2set2"));
		for(var i=0; i<elDivs.length; i++){
			// Uncheck everything that's about to be disabled (if a 
			// radio's selected, checkboxes of the same name get 
			// unchecked; this would happen in the background, so 
			// just uncheck everything to prevent confusion) 
			// --AJN 20051103
			// Doesn't work yet.  Dunno why.  --AJN, .5 hours later.
			//SetTheseChecks("drill_2", !elRads[i].checked);
			EnableAllChildInputs(elDivs[i], elRads[i].checked);
		}
		if(elRads[0].checked) ToggleBreakoutAggregation();
	}
	DisableUnsupportedInputs();
}

//DisableUnsupportedInputs:  Some drilldown items aren't yet supported, so uncheck and disable them.
function DisableUnsupportedInputs(){
	var els = document.getElementsByName("drill_2");
	for (var i=0; i < els.length; i++){
		if(false){//No unsupported variables now.  (els[i].value == "P" || els[i].value == "N"){
			els[i].checked = false;
			els[i].disabled = true;
		}
	}
}

//DoReportOnLoad:  Some functions require the page to be completely loaded in IE; pause the function calls for IE.
//	Things fixed themselves before this function worked.  The commented-out lines didn't work for unknown reasons.  -AJN 20051122
function DoReportOnLoad(){
	if(typeof(window.event) != "undefined"){
		//document.getElementById("bodyMain").attachEvent("onload", function(){alert("HI!");});
		//document.getElementById("bodyMain").attachEvent("onload", DoReportOnLoadSansDelay);
	} else {
	}
	DoReportOnLoadSansDelay();
}

//DoReportOnLoadSansDelay:  Immediately run through all the functions that want to fire when a report page loads.
function DoReportOnLoadSansDelay(){
	for(var i=0; i<funcaryOnLoad.length; i++){
		funcaryOnLoad[i]();
	}
}


