/* 	House Research Department 
 *	hrdlibrary.js - Library of Website javascript functions
 *
 * Author    	Matt Burress
 * Copyright 	(c) 2008, Minnesota House of Representatives
 * Version   	2.1
 * Last Update 	Oct 2008	
 */


function ToggleByTab(sNewTab, aryTab) {
/* Purpose: Handles switching between showing or hiding a specialized set of corresponding folder tab & content elements
 * Parameters:
 *		sNewTab			- stub ID of new tab element to display
 *		aryTab			- array of stub IDs of tab elements to hide (except for the sNewTab entry)
 * Requirements:
 *		- each stub ID in aryTab must have element IDs to append the stub ID to
 *		- stub ID is added to the literal strings:
 *			"_Tab"			- main tab element
 *			"_TabTextFront"		- text within the tab element when this tab is at the forefront
 *			"_TabTextBack"		- text within the tab element when this tab is inactive/in the background
 *			"_TabContent"		- element for corresponding content
 *			(e.g., with aryTab[0] = "stuff" --> html element IDs must exist for "stuff_Tab", "stuff_TabTextFront", "stuff_TabTextBack", and "stuff_TabContent")
 */	
 
	var iIdx;

		//toggle display or hiding for each of the elements
	for (iIdx=0; iIdx < aryTab.length; iIdx++) {
		if (aryTab[iIdx] == sNewTab) {
			document.getElementById(aryTab[iIdx] + "_Tab").className = "HRDTabFront";
			document.getElementById(aryTab[iIdx] + "_TabTextBack").style.display = "none";
			document.getElementById(aryTab[iIdx] + "_TabTextFront").style.display = "block";
			document.getElementById(aryTab[iIdx] + "_TabContent").style.display = "block";
		}
		else {
			document.getElementById(aryTab[iIdx] + "_Tab").className = "HRDTabBack";
			document.getElementById(aryTab[iIdx] + "_TabTextBack").style.display = "block";
			document.getElementById(aryTab[iIdx] + "_TabTextFront").style.display = "none";
			document.getElementById(aryTab[iIdx] + "_TabContent").style.display = "none";
			
		}
	}
}


function ToggleByStub(sStub, iMax) {
/* Purpose: Handle switching between showing or hiding a specialized numeric list of elements
 * Parameters:
 *		sStub		- base portion of each element's ID
 *		iMax		- highest numeric of the IDs
 * Requirements:
 *		- each element ID must equal the stub id with a numeric appended, starting with 1
 *		  (e.g., with sStub = "XX" and iMax = 3 --> html elements must exist for the IDs "XX1", "XX2", "XX3")
 */
	var iIdx;
	
	for (iIdx = 1; iIdx <= iMax; iIdx++) {
		var oElement = document.getElementById(sStub + iIdx);
		if (oElement.style.display == "none") {
			oElement.style.display = "block";
		}
		else {
			oElement.style.display = "none";
		}
	}
}


function ToggleYearChk(iYrStart, iYrEnd, iMaxCount) {
/*	Purpose: Enables/disables checkboxes depending on how many are selected
 *	Parameters:
 *		iYrStart	- the first year, in 1-2 digit format (e.g., 2001 --> '1'; 2010 --> '10')
 *		iYrEnd		- the last year, in 1-2 digit format
 *		iMaxCount	- the max number of year check boxes that can be selected
 *	Requires:
 *		- checkboxes must have standard IDs: "yr" + two-digit numeric for year (e.g., 'yr02'; 'yr09')
 *		- year checkboxes must be continuous
 */
	var iStart
	var iEnd
	var iIdx
	var iCount
	var sYear
	var objChk
		
		//find out how many checkboxes are currently selected
	iCount = 0;
	for (iIdx = iYrStart; iIdx <= iYrEnd; iIdx++) {
		objChk = document.getElementById("yr" + sGetYear(iIdx));
		if (objChk.checked == true) {
			iCount = iCount + 1;
		}
	}
	
		//do enable/disable depending on how many checkboxes are selected
	if (iCount < iMaxCount) {							//don't have too many, allow any to be selected
		for (iIdx = iYrStart; iIdx <= iYrEnd; iIdx++) {
			objChk = document.getElementById("yr" + sGetYear(iIdx));
			objChk.disabled = false;
		}
	}
	else {										//at limit, don't allow more to be selected
		for (iIdx = iYrStart; iIdx <= iYrEnd; iIdx++) {
			objChk = document.getElementById("yr" + sGetYear(iIdx));
			if (objChk.checked == false) {
				objChk.disabled = true;
			}
		}
	}
}


function sGetYear(iNum) {
/* Purpose: Returns a padded two-digit year (with preceeding zero if needed)
 * Parameters:
 *		iNum		- number for the year
 */
	if (iNum <= 9) {
		return "0" + String(iNum);
	}
	else {
		return String(iNum);
	}
}


function sGetQueryVal(sQuery, sEntry) {
/* Purpose: Obtain the value from a querystring for a given entry name 
 * 	- for "?name1=value1&name2=value2" querystrings
 * Parameters:
 *		sQuery		- the full querystring
 *		sEntry		- the name of the entry
 * Returns: the value text for that querystring entry
 */


	var aryQuery;
	var aryVal;
	var iIdx;

	aryQuery = sQuery.split("&");

	if ((aryQuery == "") || (aryQuery == null)) {
		return "";
	}
	
	for (iIdx=0; iIdx < aryQuery.length; iIdx++) {
		aryVal = aryQuery[iIdx].split("=");
		if (aryVal[0] == sEntry) {
			return aryVal[1];
		}
	}

	return "";
}


function formHandler( boxname ) {
	var URL = boxname.options[boxname.selectedIndex].value;
	window.location.href = URL; 
}


function ClearOptions(sID, sSkip) {
/* Purpose: Clears user choices out of an select element (e.g., dropdown list)
 * Parameters:
 *		sID		- select element's id attribute
 *		sSkip		- value to skip removal of (e.g., if "all" is chosen)
 */
	var objSel
	var iIdx
	
	objSel = document.getElementById(sID);
	for (iIdx = 0; iIdx < objSel.options.length; iIdx++) {
		if (objSel.options[iIdx].value != sSkip) {
			objSel.options[iIdx].selected = false;
		}
	}
}


function DoPopup(objLink, sWindowName) {
/* Purpose: Creates a popup window
 * Parameters:
 *		objLink			- the calling webpage
 *		sWindowName		- a unique name for the window
 */
	var href;
	
	if (! window.focus) return true;
	
	if (typeof(objLink) == 'string')
	   href = objLink;
	else
	   href = objLink.href;
	
	window.open(href, sWindowName, 'width=550,height=350,scrollbars=yes');
	return false;
}


function IsValidNumber(sInput, sField, iMin, iMax) {
/* Purpose: Does form validation to check if a field contains a proper number
 * Parameters:
 *		sInput			- input text from user
 *		sField			- external name of the field being validated
 *		iMin			- minimum amount
 *		iMax			- maximum amount
 * Returns: an error message if an error occured
 */

    var sChar
    var iIdx

    for (iIdx = 0; iIdx < sInput.length; iIdx++) {
        sChar = sInput.substring(iIdx, iIdx + 1)
        if (sChar < "0" || sChar > "9") {
            return "The entry '" + sInput + "' for the '" + sField + "' is not a number.";
        }
    }
    
    if ((parseInt(sInput) < iMin) || (parseInt(sInput) > iMax)) {
        return "The entry '" + sInput + "' for the '" + sField + "' is outside the allowed range (" + iMin + " - " + iMax + ").";
	}

    return ""
}


