﻿// JScript File

//Checks/Unchecks all of the items in the list.
function SetAllCheckBoxStates(strBaseItemName, intNumberOfItems, blnChecked)
{
    //When using a ASP CheckBoxList it renders all of its internal check boxes named with
    //the name of the control's client ID and "_#" where # is the index of the chech box.
    //So for example the first check box might be somthing like "myControl_0".
    //Inorder to change the state of these check boxes we will take the 
    //client id of the base control (strBaseItemName) and append an underscor to it 
    //and then loop through the the number of items (intNumberOfItems) and append
    //an index to the base name and then get that element and set it's checked value.

    //Append the underscore to the base name.
    strBaseItemName = strBaseItemName + "_";

    //Loop through each item in the list and set it's checked state.
    for(lp1 = 0; lp1 < intNumberOfItems; lp1++)
    {
        //Get the check box element.
        chkCurrentCheckBox = document.getElementById(strBaseItemName + lp1);
        
        //If the current item isn't set to the state we want then
        //"click" the check box to flip it's state. 
        if(chkCurrentCheckBox.checked != blnChecked)
        {
            //Click the checkbox so that it fires off click event
            //which will cause the SetSelectedItems function to run.
            chkCurrentCheckBox.click();
        }
    }
}

//Closes the Popup list.
function ClosePopup(strBehaviorID)
{
    //Get the popup control.
    popupControl = $find(strBehaviorID);
    
    //Close the current visible popup.
    if(popupControl._popupVisible == true)
    {
        //The popup is visble so we will hide it.
        popupControl.hidePopup(); 
    }    
}

//Toggles the popup.
function TogglePopup(e, strBehaviorID)
{
    //If we have an event passed to us as "e" use that, 
    //other wise try to get the "window.event".
    e = (e) ? e : ((window.event) ? window.event : "")

    //Get the popup control.
    popupControl = $find(strBehaviorID);

    if(popupControl._popupVisible == true)
    {
        //The popup is visble so we will hide it.
        popupControl.hidePopup(); 
    }
    else
    {
        //The popup is hiden so we will show it by triggering the onFocus event delegate.
        popupControl._onFocus(new Sys.UI.DomEvent(e)); 
    }
}


//Add or removes the current check box's value from the selected items.
function SetSelectedItem(strSelectedTextID, strSelectedValuesID, chkCurrentCheckBox, 
                        strSelectedSeperator, strDefaultText, strCheckBoxText, strCheckBoxValue)
{
    //Get the selected text and values elements.
    divSelectedText = document.getElementById(strSelectedTextID);
    hdnSelectedValue = document.getElementById(strSelectedValuesID);

    //See if the current check box item is checked.
    if (chkCurrentCheckBox.checked == true)
    {
        //If the selected text equals the default text then clear out the selected text value.
        if(divSelectedText.innerHTML == strDefaultText)
        {
            divSelectedText.innerHTML = "";
        }
    
        //The item is checked so add its text to the selected text.
        divSelectedText.innerHTML += strCheckBoxText + strSelectedSeperator + " ";
        hdnSelectedValue.value += strCheckBoxValue + strSelectedSeperator;
    }
    else
    {
        //The item is not checked so we need to remove it's value from the selected items.
        divSelectedText.innerHTML = divSelectedText.innerHTML.replace(strCheckBoxText + strSelectedSeperator + " ", "");
        hdnSelectedValue.value = hdnSelectedValue.value.replace(strCheckBoxValue + strSelectedSeperator, "");
    }
    
    //If the no items are selected then set the default text.
    if(divSelectedText.innerHTML == "")
    {
        divSelectedText.innerHTML = strDefaultText;
    }
    
    
    //Set the title (tool tip) of the selected items equal to it's value.
    divSelectedText.title = divSelectedText.innerHTML;
    
}