
// It is assumed that the location list is in sort sequence initially

var singleLocation = true;  // Allows an item to be selected once only

var sortLocations = true;  // Only effective if above flag set to true

var sortSearch = true;  // Will order the search list in sort sequence



// Initialize - invoked on load

function locationListInit() {

  var locationList = document.getElementById("location_list");

  var searchList = document.getElementById("searchlisting");

  var searchOptions = searchList.options;

//  searchOptions[0] = null;  // Remove initial entry from picklist (was only used to set default width)

//  locationList.focus();  // Set focus on the selectlist

}



// Adds a selected item into the picklist

function addIt() {

  var locationList = document.getElementById("location_list");

  var selectIndex = locationList.selectedIndex;

  var locationOptions = locationList.options;

  var searchList = document.getElementById("searchlisting");

	for(var i=0; i<searchList.options.length; i++) {
			if(searchList.options[i].text == "No Cities Selected...") {

				searchList.options[i] = null;

			}
	}

  var searchOptions = searchList.options;

  var searchLength = searchOptions.length;

  if (selectIndex > -1) {

    searchOptions[searchLength] = new Option(locationList[selectIndex].text);

    searchOptions[searchLength].value = locationList[selectIndex].value;
    
    searchOptions[searchLength].selected = true;

// If single selection, remove the item from the location list

    if (singleLocation) {

      locationOptions[selectIndex] = null;

    }

    if (sortSearch) {

      var tempText;

      var tempValue;

      // Sort the search list

     while (searchLength > 0 && searchOptions[searchLength].value < searchOptions[searchLength-1].value) {

        tempText = searchOptions[searchLength-1].text;

        tempValue = searchOptions[searchLength-1].value;

        searchOptions[searchLength-1].text = searchOptions[searchLength].text;

        searchOptions[searchLength-1].value = searchOptions[searchLength].value;

        searchOptions[searchLength].text = tempText;

        searchOptions[searchLength].value = tempValue;

        searchLength = searchLength - 1;

      }

    }

  }

}



// Deletes an item from the searchlisting

function delIt() {

  var locationList = document.getElementById("location_list");

  var locationOptions = locationList.options;

  var locationlength = locationOptions.length;

  var searchList = document.getElementById("searchlisting");

  var searchIndex = searchList.selectedIndex;

  var searchOptions = searchList.options;

  var searchLength = searchOptions.length;

  if (searchIndex > -1) {

    // If single selection, replace the item in the select list

    if (singleLocation) {

      locationOptions[locationlength] = new Option(searchList[searchIndex].text);

      locationOptions[locationlength].value = searchList[searchIndex].value;

	  searchLength = searchLength - 1;
    }

    searchOptions[searchIndex] = null;

    if (singleLocation && sortLocations) {

      var tempText;

      var tempValue;

      // Re-sort the locations list

      while (locationlength > 0 && locationOptions[locationlength].value < locationOptions[locationlength-1].value) {

        tempText = locationOptions[locationlength-1].text;

        tempValue = locationOptions[locationlength-1].value;

        locationOptions[locationlength-1].text = locationOptions[locationlength].text;

        locationOptions[locationlength-1].value = locationOptions[locationlength].value;

        locationOptions[locationlength].text = tempText;

        locationOptions[locationlength].value = tempValue;

        locationlength = locationlength - 1;

      }

     while (searchLength > 0) {

//		alert(searchLength);

        searchLength = searchLength - 1;

	    searchOptions[searchLength].selected = true;

      }

    }

  }

}
