var ajax_pickers_r = make_new_ajax_request();
var last_picker_preset = null;
var all_grouped_picker_rows = new Array();

var group_picker_ctls = new Array();
var g_test = false;
//tmp = Picker();
//group_picker_ctls['Genre'] = tmp;
/*
tmp.addSelection(2);
tmp.addSelection(3);
alert(tmp.getSelection());
tmp.setSelection(8);
alert(tmp.getSelection());
*/

function picker_click(attrib, row)
{
   alert("a: " + attrib + "r: " + row);
}

function func() {alert('verks');}
function request_picker_callback()
{
   if (ajax_pickers_r.readyState == 4) 
   {  if (ajax_pickers_r.status == 200) 
      {  var d = ajax_pickers_r.responseXML;
         if (d == null)
         {  showAlert("Error getting picker data");
            return;
         }
      }
      nodes = d.getElementsByTagName('Attribute');

      // erase out all our picker rows
      all_grouped_picker_rows = new Array();

      // why does this break if I use 'i' as the loop var?!?
      for (var ii = 0; ii < nodes.length; ii++)
      {  
         last_picker_preset = user.preset;
         attrib_name_in = XML_val(nodes[ii], 'Name');

         if (group_picker_ctls[attrib_name_in])
         {  
            ctl = group_picker_ctls[attrib_name_in];
            ctl.clear();
            parent_nodes = nodes[ii].getElementsByTagName('Parent');
            for (k = 0; k < parent_nodes.length; k++)
            {  
               parent_txt = XML_val(parent_nodes[k], 'Txt');
               parent_id  = XML_val(parent_nodes[k], 'Id');
               children = parent_nodes[k].
                  getElementsByTagName('V');   

               if (children.length > 0)
               {
                  child_txt = new Array();
                  child_val = new Array();
                  for (l = 0; l < children.length; l++)
                  {  
                     txt = XML_val(children[l], 'Txt');
                     child_txt.push(txt);
                     id = XML_val(children[l], 'Id');
                     child_val.push(id);
                  }
                  ctl.addData(parent_txt, parent_id,
                     child_txt, child_val);
               }
               else
               {
                  ctl.addData(parent_txt, parent_id,
                     null, null);
               }
            }
         }
      } 
      for (i = 0; i < nodes.length; i++)
      {
         last_picker_preset = user.preset;
         attrib_name_in = XML_val(nodes[i], 'Name');

         for(j =0 ; j < pickers.length; j++)
         {
            attrib_name = pickers[j];
            // strip off the prefix I give to attributes
            attrib_name = attrib_name.replace(attr_prefix, "");
            if (attrib_name != attrib_name_in)
            {  continue;
            }
            
            el = document.getElementById(pickers[j]);

            // clear out the old picker list
            for (k = el.options.length - 1; k >= 0; k--)
            {  el.options[k] = null;
            }

            val_nodes = nodes[i].getElementsByTagName('V');

            for (k = 0; k < val_nodes.length; k++)
            {  txt = val_nodes[k].childNodes[0].nodeValue;
               opt = new Option;
               opt.text = txt;
               opt.value = txt;
               el.options[k] = opt;
            }
         }
      }
      g_test = true; 
   }
}


function request_picker_data()
{
   if (last_picker_preset != null && last_picker_preset == user.preset)
   {  return;
   }

   for(j = 0 ; j < pickers.length; j++)
   {
      el = document.getElementById(pickers[j]);

      // clear out the old picker list
      for (k = el.options.length - 1; k >= 0; k--)
      {  el.options[k] = null;
      }
   }


   xml = "<\?xml version=\"1.0\"\?><PickerRequest>";
   xml += "</PickerRequest>"
   xml = "xml=" + escape(xml);
   ajax_pickers_r.open('POST', '/picker_backend.php');
   ajax_pickers_r.onreadystatechange = request_picker_callback;
   ajax_pickers_r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax_pickers_r.send((xml));
}



