//* javascript functions for categories and locations
function changesubcategories(submenuId,wv,superior,firstitem){
  var submenu = document.getElementById(submenuId);
  var topmenu = document.getElementById(superior);

  if(submenu){
    emptyList(submenu);
    //alert('changesubcategories: topmenuval: '+topmenu[topmenu.selectedIndex].value)
    if(typeof my_categories[topmenu[topmenu.selectedIndex].value] != "undefined" && my_divisions[topmenu[topmenu.selectedIndex].value] && my_categories[topmenu[topmenu.selectedIndex].value].length > 0){
      if(firstitem){
        option = new Option(firstitem,0);
        submenu.options[submenu.length] = option;
      }
      //submenu.options[submenu.options.length] = new Option('All Categories','');
      fillList_groupings(submenu,my_categories[topmenu[topmenu.selectedIndex].value],wv, '  -  ');
      submenu.disabled = false;
      //submenu.style.dis play = 'inline';
    }else{
      //submenu.style.display = 'none';
    }
    if (submenu.options.length == 0){
      submenu.options[submenu.options.length] = new Option(firstitem,'');
      submenu.disabled = 'disabled';
    }
  }
}
function fillList( box, arr, defaultValue) {
  //alert('fill List'+ arr);
  var DselectedIndex = 0;
  for (key in arr) {
    option = new Option( arr[key],key);
    if(key == defaultValue){
      DselectedIndex = box.length;
    }
    box.options[box.length] = option;
  }
  box.selectedIndex=DselectedIndex;
}
function fillList_groupings( box, arr, defaultValue, grouping_separator) {
  var DselectedIndex = 0;
  var grouping = '';
  var current_optgroup = null;
  for (key in arr) {

    var item_arr = arr[key].split(grouping_separator);
    var item_key = item_arr[0]; // ? arr[key].substring(0, separator_index) : '';
    var item_grouping = item_arr[1] ? item_arr[1] : '';
    var item_value = item_arr[2] ? item_arr[2] : '';
    //var separator_index = arr[key].indexOf(grouping_separator);
    //var item_key = (separator_index>0) ? arr[key].substring(0, separator_index) : '';
    //var item_grouping = (separator_index>0) ? arr[key].substring(0, separator_index) : '';
    //var item_value    = (separator_index>0) ? arr[key].substring(separator_index+grouping_separator.length) : arr[key];
    if(item_grouping != grouping){
      current_optgroup = document.createElement("optgroup");
      current_optgroup.label = item_grouping;
      box.appendChild(current_optgroup);
      grouping =  item_grouping;

      var objOption=document.createElement("option")
      objOption.innerHTML = item_grouping+' - all';
      objOption.value = item_grouping;
      if(item_grouping == defaultValue)
        DselectedIndex = box.length;
      current_optgroup.appendChild(objOption);
    }

    var objOption=document.createElement("option")
    objOption.innerHTML = item_value;
    objOption.value = item_key;

    //var option = new Option(item_value,key);
    if(item_key == defaultValue){
      DselectedIndex = box.length;
    }

    if(item_grouping)
      current_optgroup.appendChild(objOption);
    else
      box.appendChild(objOption);

  }
  box.selectedIndex=DselectedIndex;
}


function changesubcategories_multiple(submenuId,wv,superior,firstitem,source_array){
  var submenu = document.getElementById(submenuId);
  var topmenu = document.getElementById(superior);
  if(submenu && topmenu){
    emptyList(submenu);
    if(!source_array)
      source_array = my_categories;
    var selected_value = topmenu[topmenu.selectedIndex].value;

    if(source_array[selected_value] && source_array[selected_value].length > 0){
      if(firstitem){
        option = new Option(firstitem,0);
        submenu.options[submenu.length] = option;
      }
      fillList_multiple(submenu,source_array[topmenu[topmenu.selectedIndex].value],wv);
    }else{
    }
  }
}

function js_in_array(arr,val) {
	var len = arr.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( arr[x] == val ) return true;
	}
	return false;
}

function fillList_multiple( box, arr, defaultValues) {
  var myvals = new Array();
  myvals = defaultValues.split('|');

  DselectedIndex = 0;
  for (key in arr) {
    option = new Option( arr[key],key);
    if(js_in_array(myvals,key))
      option.selected = true;
    box.options[box.length] = option;
  }
}

function emptyList( box ) {
  if(box!=null){
    while( box.hasChildNodes() ) { box.removeChild( box.lastChild ); } // remove optgroups as well
    //while ( box.options.length ) box.options[0] = null;
  }
}


//* similar
//* it is used for locations
//* difference: name is used as key instead of number

function setSelectInput(nm,wv,superior){
  if(superior == undefined)
    superior = "g"+nm;

  sps = document.getElementById(superior);

  for(i=0;i<sps.options.length;i++){
      sps.selectedIndex = i;
      if(updateSelectWatch(nm,wv,superior)){return;}
  }
  sps.selectedIndex = 0;
  updateSelect(nm,superior);
}
function setSelect(nm,superior){
  if(superior == undefined)
    superior = "g"+nm;

  ss = document.getElementById(superior);
  ss.options.length = 0;
  t = new Array();
  if(nm.indexOf("location_id")!=-1){
      t2 = t.concat(locationsGroupings);
  }else{
      t2 = t.concat(categoriesGroupings);
  }
  counter = 0;
  while(t2.length>0){
      oo = t2.shift();
      ss.options[counter++] = new Option(oo,oo);
  }
  updateSelect(nm,superior);
}
function updateSelect(nm,superior){
  if(superior == undefined)
    superior = "g"+nm;

  obj = document.getElementById(superior);
  idx = obj.options[obj.selectedIndex].value;
  ss = document.getElementById(nm);
  ss.options.length = 0;
  t = new Array();
  if(nm.indexOf("location_id")!=-1){
      t2 = t.concat(locationsArray[idx]);
  }else{
      t2 = t.concat(categoriesArray[idx]);
  }
  counter = 0;
  while(t2.length>0){
      oo = t2.shift();
      ss.options[counter++] = new Option(oo.text,oo.value) //new Option(oo,oo);
  }
}
function updateSelectWatch(nm,wv,superior){
returnFLAG = false;
  if(superior == undefined)
    superior = "g"+nm;


  obj = document.getElementById(superior);
  idx = obj.options[obj.selectedIndex].value;
  ss = document.getElementById(nm);
  ss.options.length = 0;
  t = new Array();
  if(nm.indexOf("location_id")!=-1){
      t2 = t.concat(locationsArray[idx]);
  }else{
      t2 = t.concat(categoriesArray[idx]);
  }
  counter = 0;
  while(t2.length>0){
      oo = t2.shift();
      ss.options[counter] = new Option(oo.text,oo.value) //new Option(oo,oo);
      if(oo.value == wv){
          ss.selectedIndex=counter;
          returnFLAG = true;
      }
      counter++;
  }
  return returnFLAG;
}
