﻿/***************************************************************************************
$Workfile: CheckBoxUtilities.js $
$Author: Syed Ghulam Akbar $
$Revision: 1 $
$Created Date: 06/12/06 09:20 PM $
$Description: Contains the JavaScript utilities related to the check-boxes
***************************************************************************************/

// This function is used to select all the check-boxes on the current form
// which contains the given control name
function ChangeCheckBoxesState(strControlName, blnChecked)
{
    // Loop on all the controls in the base form
    for(var iCounter=0;iCounter<document.forms[0].elements.length;iCounter++)
    {
	    var objControl = document.forms[0].elements[iCounter]; // Get the item reference
	    
	    // Check if the control is of the check box type and contains the given value in ID attribute
	    if (objControl.type == "checkbox" && objControl.id.indexOf(strControlName) >= 0 && objControl.disabled == false )
		{
		    // If it doesn, then it's the required control, so change it's state
		    objControl.checked = blnChecked;
		}
    }
}

// Return back the count of total checked controls
function GetCheckedCount(strControlName)
{
    iTotalChecked = 0;
    
    // Loop on all the controls in the base form
    for(var iCounter=0;iCounter<document.forms[0].elements.length;iCounter++)
    {
	    var objControl = document.forms[0].elements[iCounter]; // Get the item reference
	    
	    // Check if the control is of the check box type and contains the given value in ID attribute
	    if (objControl.type == "checkbox" && objControl.id.indexOf(strControlName) >= 0)
		{
		    // If it doesn, then it's the required control, so check if it's checked
		    if ( objControl.checked )
		        iTotalChecked++
		}
    }
    
    return iTotalChecked;
}

// Return back comman seperated values of the selected check-boxes
function GetSelectedValues(strControlName)
{
    strValues = "";
    
    // Loop on all the controls in the base form
    for(var iCounter=0;iCounter<document.forms[0].elements.length;iCounter++)
    {
	    var objControl = document.forms[0].elements[iCounter]; // Get the item reference
	    
	    // Check if the control is of the check box type and contains the given value in ID attribute
	    if (objControl.type == "checkbox" && objControl.id.indexOf(strControlName) >= 0)
		{
		    // If it doesn, then it's the required control, so check if it's checked
		    if ( objControl.checked )
		        strValues += objControl.value + ","
		}
    }
    
    return strValues;
}

// There is known bug in ASP.net where when radio buttons group under repeatore doesn't work
// i.e. when a radio buttons are renedered in the repeator, each redio button is assigned a new
// name and id, and thus can't be grouped. This problem solve that issue by resetting all the similar
// radio buttons
function SelectSingleRadioButton(strRadioGroupRegExp, objCurrentRadioButton)
{
    radioGroupRegExp = new RegExp(strRadioGroupRegExp);
    
    // Loop on all the controls in the base form
    for(var iCounter=0;iCounter<document.forms[0].elements.length;iCounter++)
    {
	    var objControl = document.forms[0].elements[iCounter]; // Get the item reference
	    
	    // Check if the control is of the radio button type and is also part of the current radio group
	    if (objControl.type == "radio" && radioGroupRegExp.test(objControl.name) )
		{
		    // it's the required control, so change it's state
		    objControl.checked = false;
		}
    }
    // Select the current item
    objCurrentRadioButton.checked = true;
}