var AjaxCombo = new Object();

//===================================================================//
AjaxCombo.filterCombo = function (paramsObj) {
   //*** all possible params
   var sourceObj       = paramsObj.sourceObj;
   var destObj         = paramsObj.destObj;
   var filterType      = paramsObj.filterType;
   var comboGroupArr   = paramsObj.comboGroupArr;
   var filterGroupName = paramsObj.filterGroupName == undefined ? "" : paramsObj.filterGroupName;
   var subCatLabel     = typeof(paramsObj.subCatLabel) == "undefined"   ? "" : paramsObj.subCatLabel;

   //*** comboGroupArr Array is used to clear all the combos when the top level combo is unselected
   //*** if the section_id is cleared the we should clear category_id and sub_Category_id combos
   UtilCombo.validateComboSelection(sourceObj);
   var recordId = sourceObj[sourceObj.selectedIndex].value;
   if (recordId == "") {
      UtilCombo.clearCombos(comboGroupArr, sourceObj.form);
      return;
   }
   var url = "index.php?_spAction=filterCombo&record_id=" + recordId + 
             "&filterType=" + filterType + "&filterGroupName=" + filterGroupName + "&showHTML=0" ;
   var formContent = 'xyz=1';

   XMLHTTP.xmlHttpObj = XMLHTTP.getXMLHTTPObject(function () {AjaxCombo.filterComboHandler(destObj, filterGroupName, subCatLabel);});
   XMLHTTP.xmlHttpObj.open("POST", url, true);
   XMLHTTP.xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
   XMLHTTP.xmlHttpObj.send(formContent);
}

//===================================================================//
AjaxCombo.filterComboHandler = function (destObj, filterGroupName, subCatLabel) {
   // alert(XMLHTTP.xmlHttpObj.responseText);
   if (XMLHTTP.xmlHttpObj.readyState==4 || XMLHTTP.xmlHttpObj.readyState=="complete") { 
      destObj.innerHTML = ""
      destObj.options.length = 0; //*** clear the destination combo
      var responseText = UtilString.trimAll(XMLHTTP.xmlHttpObj.responseText, 1);
      var xmlDoc = XMLHTTP.loadXML(responseText);
      var root = xmlDoc.documentElement;
      // read the all the <row> tags
      var rowsTag = xmlDoc.getElementsByTagName("row");
      
      if (subCatLabel == ""){
         subCatLabel = "Please Select";
      }
      if (filterGroupName == ""){ 
         destObj.options[destObj.options.length] = new Option(subCatLabel, ""); 
         for (i = 0; i < rowsTag.length; i++) {
            var rowTag = rowsTag[i];
            var recordId = rowTag.childNodes[0].firstChild.nodeValue;
            var title    = rowTag.childNodes[1].firstChild.nodeValue;
            destObj.options[destObj.options.length] = new Option(title, recordId);
         }

      } else {

         var filterGroupTemp = "";
         destObj.options[destObj.options.length] = new Option(subCatLabel, ""); 
         for (i = 0; i < rowsTag.length; i++) {
            var rowTag = rowsTag[i];
            var recordId = rowTag.childNodes[0].firstChild.nodeValue;
            var title    = rowTag.childNodes[1].firstChild.nodeValue;
            var filterGroupName = rowTag.childNodes[2].firstChild.nodeValue;

            if (filterGroupTemp != filterGroupName) {
               destObj.options[destObj.options.length] = new Option(filterGroupName, "---"); 
            }

            var filterGroupTemp = filterGroupName;
            destObj.options[destObj.options.length] = new Option(title, recordId); // adds new combo values filtered by the source combo
         }
         // UtilCombo.setSelectedIndex(destObj, selectedValue);
      }
   }
}

//===================================================================//
