﻿/*  This function goes through a given filterGroup in a datasource.
    It finds any checked itmes and places them into a checkedItems collection 
    attached to the dataGroup. This will then determine later on, which filters
    to use when testing a data object to see if it matches filter criteria.
*/
function updateChecked(filterGroup, datasource)
{
    datasource.filterGroups[filterGroup].checkedItems = new Array();
    for (filterKey in datasource.filterGroups[filterGroup].filterItems)
    {
        filterItem = datasource.filterGroups[filterGroup].filterItems[filterKey];
        htmlObject = $get(filterItem.htmlObject);
        if (htmlObject && htmlObject.checked)
        {
            datasource.filterGroups[filterGroup].checkedItems[datasource.filterGroups[filterGroup].checkedItems.length] = filterItem;
            if (htmlObject.parentNode.parentNode.className == "nofloat") 
            {
                htmlObject.parentNode.parentNode.style.backgroundImage = "url(/_images/phonestab/filterbkgdlong.gif)";
            }
            else if (htmlObject.parentNode.parentNode.className == "filterImage")
            {
                htmlObject.parentNode.parentNode.style.backgroundImage = "url(/_images/phonestab/filterbkgdtall.gif)";
            }
            else 
            {
                htmlObject.parentNode.parentNode.style.backgroundImage = "url(/_images/phonestab/filterbkgd.gif)";
            }
        } 
        else if (htmlObject)
        {
            htmlObject.parentNode.parentNode.style.backgroundImage = "";
        }
    }
}

/*  This function is used to return a comma seperated list 
    of checked filters for a given datasource. */
function getCheckedFilters(datasource)
{
    var dl = document.createElement("dl");
    for (groupKey in datasource.filterGroups)
    {
        var dt = document.createElement("dt");
        dt.appendChild(document.createTextNode(datasource.filterGroups[groupKey].name));
        var dd = document.createElement("dd");
       
      if((datasource.filterGroups[groupKey].checkedItems == undefined)||(datasource.filterGroups[groupKey].checkedItems == null))
      {
          updateChecked(groupKey, datasource);
      }
        
        for (itemIndex in datasource.filterGroups[groupKey].checkedItems)
        {
            if (dd.hasChildNodes())
            {
                var graySpan = document.createElement("span");
                graySpan.className = "gray";
                if (datasource.filterGroups[groupKey].isandstatement) { graySpan.appendChild(document.createTextNode(" and ")); }
                else { graySpan.appendChild(document.createTextNode(" or ")); }
                dd.appendChild(graySpan);
            }
            dd.appendChild(document.createTextNode(datasource.filterGroups[groupKey].checkedItems[itemIndex].name));
            
            var graySpan = document.createElement("span");
            graySpan.className = "gray";
            graySpan.appendChild(document.createTextNode(" ("));
            dd.appendChild(graySpan);
            
            var a = null; 
            a = document.createElement("a");
            a.href = "/removeFilter:" + datasource.filterGroups[groupKey].checkedItems[itemIndex].name;
            a.title = "removeFilter:" + groupKey + ":" + itemIndex;
            a.onclick = function() 
            {
                $get(datasource.filterGroups[(this.title.split(":"))[1]].checkedItems[(this.title.split(":"))[2]].htmlObject).click();
                return false;
            }
            
            var imgX = new Image();
            imgX.src = "/_images/phonesTab/x_button.gif";
            
            a.appendChild(imgX);
            dd.appendChild(a);
            
            var graySpan = document.createElement("span");
            graySpan.className = "gray";
            graySpan.appendChild(document.createTextNode(")"));      
            dd.appendChild(graySpan);      
        }
        
        if (dd.hasChildNodes())
        {
            dl.insertBefore(dd, dl.firstChild);
            dl.insertBefore(dt, dl.firstChild);
        }
        
    }
    return dl;
}