
// Replaces all instances of the given substring.
String.prototype.replaceAll = function( 
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
    ){
    
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1){
        // Relace out the current instance.
        strText = strText.replace( strTarget, strSubString );
         

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget );
    }
 
    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
}

function SetSelectAllChecked(allCheckBoxesAndKeys, toSet)
{
    for (var i = 0; i < allCheckBoxesAndKeys.length; i++)
    {
        currentText = GetCheckBoxText(allCheckBoxesAndKeys[i].checkbox);
        if (currentText.toLowerCase() == 'select all')
        {
            allCheckBoxesAndKeys[i].checkbox.checked = toSet;
            break;
        }
    }
}

// txtBox is the txtBox being populated with the values of the drop down
// allCheckBoxStr is an array of all the checkboxes
function HandleChange(txtBox, allCheckBoxStr, hidSelectedItems)
{
    var strToSet = '';
    var hidStrToSet = '';
    var allCheckBoxesAndKeys = GetAllCheckBoxesAndKeys(allCheckBoxStr);
    var currentText = null;
    var allChecked = true;
    
    for (var i = 0; i < allCheckBoxesAndKeys.length; i++)
    {
       currentText = GetCheckBoxText(allCheckBoxesAndKeys[i].checkbox);
       
       if (currentText.toLowerCase() == 'select all')
       {
            continue;
       }
       
       if (allCheckBoxesAndKeys[i].checkbox.checked)
       {
            strToSet += currentText + ', ';
            hidStrToSet += allCheckBoxesAndKeys[i].keyvalue + '==|==';
       }
       else
       {
            SetSelectAllChecked(allCheckBoxesAndKeys, false);
            allChecked = false;
       }
       
       if (allChecked)
       {
            SetSelectAllChecked(allCheckBoxesAndKeys, true);
       }
    }
    
    if (strToSet.length > 0)
    {
        strToSet = strToSet.substr(0, strToSet.length - 2); // Remove last ,
    }
    
    if (hidStrToSet.length > 0)
    {
        hidStrToSet = hidStrToSet.substr(0, hidStrToSet.length - 5); // Remove last ,
    }
    
    txtBox.value = strToSet;
    hidSelectedItems.value = hidStrToSet;
}

// txtBox is the txtBox being populated with the values of the drop down
// allCheckBoxStr is an array of all the checkboxes
function HandleSelectAll(cbxSelectAll, txtBox, allCheckBoxStr, hidSelectedItems)
{
    var strToSet = '';
    var hidStrToSet = '';
    var allCheckBoxesAndKeys = GetAllCheckBoxesAndKeys(allCheckBoxStr);
    var currentText = null;
    
    for (var i = 0; i < allCheckBoxesAndKeys.length; i++)
    {
        if (allCheckBoxesAndKeys[i].checkbox == cbxSelectAll)
        {
            continue;
        }
    
        if (cbxSelectAll.checked == true)
        {
            allCheckBoxesAndKeys[i].checkbox.checked = true;
        }
        else
        {
            allCheckBoxesAndKeys[i].checkbox.checked = false;
        }
    }
    
    HandleChange(txtBox, allCheckBoxStr, hidSelectedItems);
}

function GetAllCheckBoxesAndKeys(allCheckBoxClientIDs)
{
    var arrayOfCheckBoxClientIDs = allCheckBoxClientIDs.split(';');
    var arrayOfHidCheckBoxValues = allCheckBoxClientIDs.replaceAll('cbx', 'hid').split(';');
    
    var arrayToRet = new Array();
    
    for (var i = 0; i < arrayOfCheckBoxClientIDs.length; i++)
    {
        arrayToRet[i] = { 'checkbox' : $get(arrayOfCheckBoxClientIDs[i]),
                          'keyvalue' : $get(arrayOfHidCheckBoxValues[i]).value };
    }
    
    return arrayToRet;
}

function AttachEventHandlerForCheckBoxes(allCheckBoxStr, txtMain, hidSelectedItems)
{
    if (allCheckBoxStr == null || allCheckBoxStr == '')
    {
        return;
    }

    var allCheckBoxesAndKeys = GetAllCheckBoxesAndKeys(allCheckBoxStr);
    var currentCheckBoxText = null;
    
    for (var i = 0; i < allCheckBoxesAndKeys.length; i++)
    {
        currentCheckBoxText = GetCheckBoxText(allCheckBoxesAndKeys[i].checkbox);
        $clearHandlers(allCheckBoxesAndKeys[i].checkbox);
        
        if (currentCheckBoxText.toLowerCase() == 'select all')
        {
            $addHandler(allCheckBoxesAndKeys[i].checkbox, 'click', function(element) { HandleSelectAll(element.target, txtMain, allCheckBoxStr, hidSelectedItems); });
        }
        else
        {
            $addHandler(allCheckBoxesAndKeys[i].checkbox, 'click', function() { HandleChange(txtMain, allCheckBoxStr, hidSelectedItems); });
        }
    }
}

function GetCheckBoxText(checkbox)
{
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        return checkbox.parentNode.parentNode.innerText;
    }

    return checkbox.parentNode.textContent;
}
