/**
 * Ax_WebUI - Miscellaneous JavaScript functions for web UI.
 * ---------------------------------------------------------------------------
 *
 * ---------------------------------------------------------------------------
 * Version: 2006-04-01
 * ---------------------------------------------------------------------------
 *
 * LICENSE:
 *
 * For use only with explicit permission from Dan Kamins / AxonChisel.net.
 *
 * ---------------------------------------------------------------------------
 * @author Dan Kamins [d a x  A.T  a x o n c h i s e l  D.O.T  n e t]
 * @package libAxonChisel
 * @tab 4
 * ---------------------------------------------------------------------------
 * Copyright (c) 2006, AxonChisel.net
 * ---------------------------------------------------------------------------
 */


/**
 * Count chars in sInputId field and write into asasociated span,
 * trimming input field if over limit.
 *
 * @param string sInputId  Id of input element to count size of value.
 * @param string sCountId  Id of span-type element to write count to.
 * @param int iMaxLen      Length cap to trim input down to.
 *                         0=no trimming.
 *
 * @author Dan Kamins [d a x  A.T  a x o n c h i s e l  D.O.T  n e t]
 */
function ax_WebUI_input_countChars(sInputId, sCountId, iMaxLen)
{
	// Verify getElementById (prevents strict JS warning in Firefox):
	if (typeof document.getElementById == "undefined") { return; }

	// Count chars, enforce length limit, and update length display:
	var iLen = document.getElementById(sInputId).value.length;
	if ((iMaxLen > 0) && (iLen > iMaxLen)) {
		document.getElementById(sInputId).value =
			document.getElementById(sInputId).value.substr(0, iMaxLen);
		iLen = iMaxLen;
	}
	document.getElementById(sCountId).innerHTML = iLen;
}


/**
 * Show/Hide elements such that only one element
 * (associated with currently selected select option) is visible.
 *
 * @param string sSelectId      Id of SELECT element.
 * @param string sElementIdPfx  Prefix of elements to show/hide.
 *	                            Select values are appended to this prefix.
 *
 * @author Dan Kamins [d a x  A.T  a x o n c h i s e l  D.O.T  n e t]
 */
function ax_WebUI_select_showAndHideElements(sSelectId, sElementIdPfx)
{
	// Verify getElementById (prevents strict JS warning in Firefox):
	if (typeof document.getElementById == "undefined") { return; }

	// Iterate select options, show/hiding elements:
	var elSelect = document.getElementById(sSelectId);
	for (var i=0; i<elSelect.length; ++i)
	{
		var elShowHide = document.getElementById(sElementIdPfx + elSelect[i].value);
		if (elSelect[i].selected) {
			elShowHide.style.display = "block";
		} else {
			elShowHide.style.display = "none";
		}
	}
}


/**
 * Show/Hide elements such that only one element
 * (associated with currently selected radio button) is visible.
 *
 * @param string sRadioName     Name (form field) shared by set of radio buttons.
 * @param string sElementIdPfx  Prefix of elements to show/hide.
 *	                            Radio button values are appended to prefix.
 *
 * @author Dan Kamins [d a x  A.T  a x o n c h i s e l  D.O.T  n e t]
 */
function ax_WebUI_radio_showAndHideElements(sRadioName, sElementIdPfx)
{
	// Verify getElementById (prevents strict JS warning in Firefox):
	if (typeof document.getElementById == "undefined") { return; }

	var arelRadio = document.getElementsByName(sRadioName);
	for (var i=0; i<arelRadio.length; ++i)
	{
		var elShowHide = document.getElementById(sElementIdPfx + arelRadio[i].value);
		if (arelRadio[i].checked) {
			elShowHide.style.display = "block";
		} else {
			elShowHide.style.display = "none";
		}
	}
}


/**
 * Enable/disable items based on checkbox status.
 *
 * All elements have the sDisabledSuffix appended to their CSS classes
 * if the checkbox is unchecked.  If checked, the suffix is removed from
 * the CSS class names.
 *
 * Elements in arsInputIds are further marked as "disabled" (preventing input)
 * in the case the checkbox is unchecked.
 *
 * Usually hooked to onclick event of checkbox and called onload.
 *
 * @param string sCheckboxId           Id of checkbox element.
 * @param string sDisabledSuffix       CSS class suffix for disabled items.
 * @param array(string) arsDisplayIds  Label/text item IDs to gray out/etc.
 * @param array(string) arsInputIds    Input item IDs to enable/disable.
 * @param boolean bCheckEnables        Check enables? F=check disables.
 *
 * @author Dan Kamins [d a x  A.T  a x o n c h i s e l  D.O.T  n e t]
 */
function ax_WebUI_checkbox_enableAndDisableElements( sCheckboxId, 
                                                     sDisabledSuffix, 
                                                     arsDisplayIds,
                                                     arsInputIds,
                                                     bCheckEnables )
{
	// Verify getElementById (prevents strict JS warning in Firefox):
	if (typeof document.getElementById == "undefined") { return; }

	// Prep:
	var bEnabled = document.getElementById(sCheckboxId).checked;
	if (!bCheckEnables) { bEnabled = !bEnabled; }
	var arsAllElementIds = arsDisplayIds.concat(arsInputIds);
	
	// Enable/disable CSS class of all elements:
	for (var i=0; i<arsAllElementIds.length; ++i) {
		var elLabel = document.getElementById(arsAllElementIds[i]);
		var sClass = elLabel.className;
		var iDisabledIdx = sClass.lastIndexOf(sDisabledSuffix);
		if (bEnabled && (iDisabledIdx>=0)) {
			elLabel.className = sClass.slice(0, iDisabledIdx);
		} else if (!bEnabled && (iDisabledIdx==-1)) {
			elLabel.className = sClass + sDisabledSuffix;
		}
	}

	// Enable/disable input elements:
	for (i=0; i<arsInputIds.length; ++i) {
		document.getElementById(arsInputIds[i]).disabled = !bEnabled;
	}
}


/**
 * Check/uncheck all checkboxes named sInputName in form with id sFormId.
 *
 * @param string sFormId     Id of the form containing checkboxes.
 * @param string sInputName  Name (field name) of the checkbox array.
 *                           (Name should include [] brackets).
 * @param boolean bChecked   TRUE=check all.  FALSE=uncheck all.
 *
 * @author Dan Kamins [d a x  A.T  a x o n c h i s e l  D.O.T  n e t]
 */
function ax_WebUI_checkbox_checkAll(sFormId, sInputName, bChecked)
{
	// Verify getElementById (prevents strict JS warning in Firefox):
	if (typeof document.getElementById == "undefined") { return; }

	// Setup and iterate/set all checkboxes:
	var elForm = document.getElementById(sFormId);
	var arelInputs = elForm.getElementsByTagName("input");
	for (var i = 0; i < arelInputs.length; ++i) {
		if (arelInputs[i].getAttribute("name") == sInputName) {
			arelInputs[i].checked = bChecked;
		}
	}
}


/**
 * Start loading an image, such as for a hover/rollover replacement image.
 *
 * @param string sSrc      Image src, as for IMG tag's src attribute.
 * @return Image   Image object loading image specified. Caller may ignore.
 */
function ax_WebUI_preloadImage(sSrc)
{
	var img = new Image();
	img.src = sSrc;
	return img;
}

