﻿//Purpose: A callback function to fill the Towns dropdown list based on a selected county

function ReceiveServerData(arg,context)
{
    
    //var lbAllTowns = document.getElementById(context);
    context.innerHTML= "";
    var rows = arg.split('||');
    AddSelectAll(context);
      var opt = document.createElement("option");
      opt.value = " ";
      opt.innerHTML = " ";
      context.appendChild(opt);
      
      //alert(opt); 
    for (var i = 0; i < rows.length - 1; ++i)
    {
         var fields = rows[i].split('|'); 
         var townText = fields[0];
         var townValue = fields[1];
         var option = document.createElement("option");
         
         option.value = townValue;
         option.innerHTML = townText;
         context.appendChild(option);
    }  
}

function AddSelectAll(objSelect)
{
    if(objSelect.size < 1)
    {
     var option = document.createElement("option");
     
     option.value = "";
     option.innerHTML = "";
     objSelect.appendChild(option);
    }
}

//Move items between AllTown and SelectedTowns listboxes
function additem(ctrlSource,ctrlTarget,ctrlHidden,all_towns)
{
    var Source = document.getElementById(ctrlSource);
    var Target = document.getElementById(ctrlTarget);
    var Hidden = document.getElementById(ctrlHidden);
    
	if(Source.selectedIndex>0)
	{
		move(Source,Target);
		removeitembyname(Target,all_towns)
		SortD(Target);
	}
	buildHidden(Hidden, Target);
	
}

function buildHidden(oHidden, oTarget)
{
	oHidden.value = "";
    for(var i=0; i<oTarget.length; i++)
    {
	    oHidden.value += oTarget.options[i].value + ",";
    }
}

function buildDropDown(oHidden, oTarget)
{ if(oHidden!=null && oTarget!=null) {  
	removeItems(oTarget);
	var oHiddenItems = oHidden.value.split(",");
    oHiddenItems.sort();
    var oHiddenLength = oHiddenItems.length;
    for(var oHiddenItemIndex = 0; oHiddenItemIndex < oHiddenLength; oHiddenItemIndex ++)
    {
        var localItem = oHiddenItems[oHiddenItemIndex];
        if(localItem != "")
            addItemToTarget(oTarget, localItem, localItem);
    }
//    if (oTarget.length == 0)
//        additembyname(oTarget, "All Towns");
  }
}

function removeItems(source)
{  if(source!=null && source.options.length!=null) {
    var allItems = source.options.length;
    for(var iSource=allItems; iSource >= 0 ; iSource--)
    {
        source.options[iSource] = null;
    }
   }
}

function removeitembyname(Source, theName)
{
    for(var i=0; i<Source.length; i++)
    {
	    if(Source.options[i].text == theName)
	    {
		    Source.remove(i);
	    }
    }
	BumpUp(Source);
}

function addItemToTarget(oTarget, sText, sValue)
{
    var no = new Option();
    no.value = sValue;
    no.text = sText;
    oTarget.options[oTarget.length] = no;
}

function additembyname(Target, theName)
{
    if(Target!=null && Target.length==0) {
    var no = new Option();
    no.value = "";
    no.text = theName;
    Target.options[Target.length] = no;}
}

function removeitem(ctrlSource, ctrlHidden, all_towns)
{
    var Source = document.getElementById(ctrlSource);
    var Hidden = document.getElementById(ctrlHidden);    
    
    for(var i=0; i<Source.length; i++)
    {
	    if(Source.options[i].selected && Source.options[i].value != "" && Source.length > 0)
	    {
	        Source.options[i] = null;
	        break;
	    }
    }
    BumpUp(Source);
    
    if (Source.length == 0)
	    additembyname (Source,all_towns);
	
	//alert(Hidden.value);
	buildHidden(Hidden, Source);

}

function move(Source,Target)
{
    if ((Source != null) && (Target != null))
    {
        if (Source.options.selectedIndex >= 0)
        {
            var newOption = new Option();
            newOption.text = Source.options[Source.options.selectedIndex].text;
            newOption.value = Source.options[Source.options.selectedIndex].value;
           
            var len = Target.length;
			var found = false;
			for(var count = 0; count < len; count++)
			{
				if (Target.options[count] != null)
				{
					if (newOption.text == Target.options[count].text)
					{
					    found = true;
					    break;
					}
				}
			}

			if (found != true)
			{
				Target.options[Target.length] = newOption; //Append the item in Target
				len++;
			}
        }
    }
}


/***********************
Name:			BumpUp
Description:	This function removes empty Option objects from the Select object array
Input:			box, a Select object
Programmer:	Michael Coca
Date:			4.30.02
************************/
function BumpUp(box)
{
	var i=0;
	while (i < box.length)
	{
		if (box.options[i].text == "")
			box.options[i] = null;
		i++;
	}
}

function SortD(box)
{
    arrTexts = new Array();
    for(i=0; i<box.length; i++)
    {
      arrTexts[i] = box.options[i].text;
    }

    arrTexts.sort();

    for(i=0; i<box.length; i++)
    {
      box.options[i].text = arrTexts[i];
      box.options[i].value = arrTexts[i];
    }
}