function repaintDest(depart_name, return_name) {
  departEl=document.forms[1].elements[depart_name];
  returnEl=document.forms[1].elements[return_name];
  sel_ndx = departEl.selectedIndex;
  if(document.getElementById("hidDepartCity")!=null){
    ret_sel_val= document.forms[1].hidDepartCity.value;
  }
  else{
    ret_sel_val = returnEl.options[returnEl.selectedIndex].value;
  }

  if (sel_ndx > 0 && sel_ndx <= dest_array.length) {
    valid_destinations = getValidDest(departEl.options[sel_ndx].value);

    if (valid_destinations == null || valid_destinations == "") {
      allDestCities(depart_name, return_name);
    }
    else {
      returnEl.options.length=departEl.options.length;
      for (j=1,entries=0; j < departEl.options.length; j++) {
        if (isValid(departEl.options[j].value, valid_destinations)) {
          returnEl.options[entries+1].value=departEl.options[j].value;
          returnEl.options[entries+1].text=departEl.options[j].text;
          entries++;
        }
      }
      for (j=entries+1; j < departEl.options.length; j++) {
        returnEl.options[j].value="";
        returnEl.options[j].text="";
      }
      returnEl.options.length=entries+1;

      if(ret_sel_val != null && ret_sel_val != "") {
      	setReturnSelectedIndex(ret_sel_val, return_name);
      }
    }
  }
  else {
    allDestCities(depart_name, return_name);
  }
}

function isValid(city_code, valid_destinations) {
  valid_array = valid_destinations.split(',');
  for (i=0; i < valid_array.length; i++) {
    if (city_code == valid_array[i])
      return true;
  }
  return false;
}

function setReturnSelectedIndex(ret_sel_val, return_name) {
	returnEl=document.forms[1].elements[return_name];
	for (i=1; i < returnEl.options.length; i++) {
		if( returnEl.options[i].value == ret_sel_val) {
			returnEl.options[i].selected = "selected";
		}
	}
}

function allDestCities(depart_name, return_name) {
  departEl=document.forms[1].elements[depart_name];
  returnEl=document.forms[1].elements[return_name];
  returnEl.options.length=departEl.options.length;
  for (j=0; j < departEl.options.length; j++) {
    returnEl.options[j].value=departEl.options[j].value;
    returnEl.options[j].text=departEl.options[j].text;
  }
}

function getValidDest(city_code) {
  for (j=0; j < city_array.length; j++) {
    if (city_code == city_array[j])
      return dest_array[j];
  }
  return "";
}

