/**
 * Populates all contents of fields from an array variable according to current 
 * selection. The first row in the array contains input element names
 */
function PopulateAlerters(selector, srcvar, subcatvar, 
  areascitiesvar, alsubcatvar, alcitiesvar, alareasvar)
{
    PopulateArrayData(selector, srcvar);
    //FillSelectFrom(sourceid, selectid, subcatvar); //update subcategories before selecting
    FillSelectFrom(subcatvar); //update subcategories before selecting
    PopulateArrayMultiSelector(alsubcatvar);
    PopulateArrayMultiSelector(alareasvar);
    FillSelectFrom(areascitiesvar); //update subcategories before selecting
    PopulateArrayMultiSelector(alcitiesvar);
    return;
}

function FillSelectFrom(srcvar, filteridx, validx, textidx, current)
{
    var nuva = NullValue();
    var filteridx =  (filteridx? filteridx: 0);
    var validx = validx || 1;
    var textidx = textidx || 2;
    var current = current || -1;
    
    //First, clear selector
    var source = document.getElementById(srcvar[0][filteridx]);
    var filter = source.value;
    var select = document.getElementById(srcvar[0][validx]);
    //var filter = select.value;
    var n= select.options.length;
    for(var i=0; i<n ; i++)
	select.remove(0);

    for(var i=1 ; i < srcvar.length ; i++)
    {
    if ((filteridx < 0) || (srcvar[i][filteridx] == filter))
    {
	var anOption = document.createElement("OPTION");
	select.add( anOption, nuva );			//back of list
	if(current == srcvar[i][validx])
	    anOption.selected=true;
	anOption.value = srcvar[i][validx];		
	anOption.text = srcvar[i][textidx];		
	}
    }
    return;
}

/**
 * Populates all contents of fields from an array variable according to current 
 * selection. The first row in the array contains input element names
 */
function PopulateArrayMultiSelector(srcvar)
{
    var nuva = NullValue();

    var temp=document.getElementById(srcvar[0][0]);
    var filter = temp.value;
    var target=document.getElementById(srcvar[0][1]);
    //Add new options
    for(var i=1 ; i<srcvar.length ; i++)
    {
	 //Update data when id match
	 if(srcvar[i][0] == filter)
	 {
		  for(var k=0 ; k<target.options.length ; k++)
		  {
            target.options[k].selected = false;
		  }
	     for (var j = 1; j < srcvar[i].length; j++)
	     {
		      for(var k=0 ; k<target.options.length ; k++)
		      {
		          if (target.options[k].value == srcvar[i][j])
                    target.options[k].selected = true;
		      }
	     }	
	 }
    }
    return;
}


