function formTextSubmit(formText, formSubmit, isListingsSearch) {
  var c1, formToSubmit;
  var msg = "";
  var searchText = formText.searchtext.value;
  var words = new Array();
  var startI = 0, endI = searchText.length - 1, done = false;
  // cut of starting and ending white space, comma, period
  while ((startI <= endI) && ! done) {
    c1 = searchText.charCodeAt(startI);
    if ((c1 <= 32) || (c1 == 44) || (c1 == 46)) {
      startI++;
    }
    else {
      done = true;
    }
  }
  done = false;
  while ((endI >= startI) && ! done) {
    c1 = searchText.charCodeAt(endI);
    if ((c1 <= 32) || (c1 == 44) || (c1 == 46)) {
      endI--;
    }
    else {
      done = true;
    }
  }
  // break user input into words
  if (startI <= endI) {
    var segStartI = startI, between = false;
    startI++;
    while (startI <= endI) {
      c1 = searchText.charCodeAt(startI);
      if ((c1 <= 32) || (c1 == 44) || (c1 == 46)) {
        if (! between) {
          words.push(searchText.substring(segStartI, startI));
          between = true;
        }
      }
      else {
        if (between) {
          segStartI = startI;
          between = false;
        }
      }
      startI++;
    }
    words.push(searchText.substring(segStartI, startI));
  }

  var wordsCount = words.length;

  if (wordsCount > 0) {
    // attempt to interpret state
    var isPostalCode = false;
    var stateName = getStateName(words[wordsCount - 1]);
    var stateWordsCount = 1;
    if (stateName.length <= 0) {
      var foundCount = 0;
      var testName = getStateNameByStart(words[wordsCount - 1]);
      if (testName.length > 0) {
        foundCount++;
        stateName = testName;
        stateWordsCount = 1;
      }
      if (wordsCount >= 2) {
        testName = getStateNameByStart(words[wordsCount - 2] + " " + words[wordsCount - 1]);
        if (testName.length > 0) {
          foundCount++;
          stateName = testName;
          stateWordsCount = 2;
        }
      }
      if (wordsCount >= 3) {
        testName = getStateNameByStart(words[wordsCount - 3] + " " + words[wordsCount - 2] + " " + words[wordsCount - 1]);
        if (testName.length > 0) {
          foundCount++;
          stateName = testName;
          stateWordsCount = 3;
        }
      }
      if (foundCount > 1) {
        stateName = "";
      }
    }
    if (stateName.length <= 0) {
      // try to interpret as a zip
      if (words.length == 1) {
        if ((words[0].length == 5) && (parseInt(words[0], 10) >= 0)) {
          isPostalCode = true;
        }
        else if ((words[0].length == 6) && (parseInt(words[0].charAt(3), 10) >= 0)) {
          isPostalCode = true;
        }
      }
      else if ((words.length == 2) && (words[0].length == 3) && (words[1].length == 3) && (parseInt(words[1].charAt(0), 10) >= 0)) {
        isPostalCode = true;
      }
    }
    if (isPostalCode) {
      // zip submits to current page
      formToSubmit = formText;
    }
    else {
      if (stateName.length <= 0) {
        msg = "Please include the name or abbreviation of the State or Province.";
      }
      else {
        formToSubmit = formSubmit;
        var citySegment = words.slice(0, wordsCount - stateWordsCount).join("-");
        var stateSegment = stateName.replace(/\s/g, "-");
        if (citySegment.length > 0) {
          // city-state submits to results
          if (isListingsSearch) {
            routeTemplate = listingsResultsRoute;
          }
          else {
            routeTemplate = agentsResultsRoute;
          }
//          formSubmit.action = formSubmit.action + stateSegment + "/" + citySegment + "/";
          formSubmit.action = routeTemplate.replace("{state}", stateSegment).replace("{city}", citySegment).replace("{page}", "1");
        }
        else {
          // state submits to city list
//          formSubmit.action = formSubmit.action + stateSegment + "/";
          if (isListingsSearch) {
            routeTemplate = listingsCityListRoute;
          }
          else {
            routeTemplate = agentsCityListRoute;
          }
          formSubmit.action = routeTemplate.replace("{state}", stateSegment);
        }
      }
    }
    if (msg.length > 0) {
      alert(msg);
    }
    else {
      formToSubmit.submit();
    }
  }
  return false;
}

function listingsSortSubmit() {
  if (! byPostalCode) {
    var formToSubmit = formSubmit;
  }
  else {
    var formToSubmit = formZip;
  }
  formToSubmit.action = listingsResultsRoute.replace("{state}", stateSegment).replace("{city}", citySegment).replace("{page}", "1");
  formToSubmit.sort.value = formSort.selSort.value;
  formToSubmit.submit();
}


function enableCats(enable) {

  var i1 = 0, hiI = 12;
  while (i1 <= hiI) {

    elem = eval("document.frmListingsSearch.cat" + i1);

    if (elem) {
      elem.disabled = ! enable;
    }
    i1++;
  }

//  document.frmListingsSearch.catSfh.disabled = ! enable;
//  document.frmListingsSearch.catCondo.disabled = ! enable;
//  document.frmListingsSearch.catMfh.disabled = ! enable;
//  document.frmListingsSearch.catLand.disabled = ! enable;
}

function verifySubmit(formVerify, formState) {
  var msg = "";

  if (formVerify.startText.value.length < 2) {
    msg = "Please enter at least the first two letters of your agent's last name.\n";
  }


  if (msg.length > 0) {
    alert(msg);
  }
  else {
    formVerify.action = formVerify.action + formState.state.value + "/Subresults";
    formVerify.submit();
  }
  return false;
}


function listingsSearchSubmit() {
  var msg = "", cats = "";
  var catAll = true, catsFound = 0;

  if (! formSearch.catAll || ! formSearch.catAll.checked) {
    var i1 = 0, hiI = 12, prefix = "";
    while (i1 <= hiI) {
      elem = eval("document.frmListingsSearch.cat" + i1);
      if (elem) {
        if (elem.checked) {
          cats = cats + prefix + i1;
          prefix = "-";
          catsFound++;
        }
        else {
          catAll = false;
        }
      }
      i1++;
    }
  }

  if (! catAll && (catsFound <= 0)) {
    msg = "Please select at least one Category.\n";
  }



  if (msg.length > 0) {
    alert(msg);
  }
  else {
    if (! byPostalCode && catAll && (formSearch.minPrice.value == "0") && (formSearch.maxPrice.value == "max") && (formSearch.beds.value == "") && (formSearch.baths.value == "") && (formSearch.selSort.value == "")) {
      formClean.action = listingsResultsRoute.replace("{state}", stateSegment).replace("{city}", citySegment).replace("{page}", "1");
//      alert(formClean.action);
      formClean.submit();
    }
    else {
      if (! byPostalCode) {
        var formToSubmit = formSubmit;
      }
      else {
        var formToSubmit = formZip;
      }
      formToSubmit.action = listingsResultsRoute.replace("{state}", stateSegment).replace("{city}", citySegment).replace("{page}", "1");
      formToSubmit.cat.value = cats;
      formToSubmit.minPrice.value = formSearch.minPrice.value;
      formToSubmit.maxPrice.value = formSearch.maxPrice.value;
      formToSubmit.beds.value = formSearch.beds.value;
      formToSubmit.baths.value = formSearch.baths.value;
      formToSubmit.sort.value = formSearch.selSort.value;
//      alert(formToSubmit.name);
      formToSubmit.submit();
    }
  }
  return false;
}

function setPriceMin(newMin) {
  var elemMin = document.frmListingsSearch.minPrice;
  var i1 = elemMin.length - 1, done = false;
  while ((i1 > 0) && ! done) {
    if (elemMin.options[i1].value <= newMin) {
      done = true;
    }
    else {
      i1--;
    }
    elemMin.selectedIndex = i1;
  }
}

function setPriceMax(newMax) {
  var elemMax = document.frmListingsSearch.maxPrice;
  var i1 = 1, hiI = elemMax.length - 1, done = false;
  while ((i1 < hiI) && ! done) {
    if (elemMax.options[i1].value >= newMax) {
      done = true;
    }
    else {
      i1++;
    }
    elemMax.selectedIndex = i1;
  }
}

function priceChange(sender) {
  if (sender.name == "minPrice") {
    var elem = document.frmListingsSearch.maxPrice;
    var newValue = Number(sender.options[sender.selectedIndex].value);
    if (newValue > elem.options[elem.selectedIndex].value) {
      setPriceMax(newValue + 1);
    }
  }
  else if (sender.name == "maxPrice") {
    var elem = document.frmListingsSearch.minPrice;
    var newValue = Number(sender.options[sender.selectedIndex].value);
    if (newValue < elem.options[elem.selectedIndex].value) {
      setPriceMin(newValue - 1);
    }
  }
}

function frmInfoSubmit(formInfo) {
  var msg = "";
  if (formInfo.name.value.length == 0) {
    msg += 'Please enter your name.\n';
  }
  if (formInfo.email.value.length == 0) {
    msg += 'Please enter your email address.\n';
  }
  else {
    var p1 = formInfo.email.value.indexOf('@');
    if ((p1 <= 0) || (p1 >= formInfo.email.value.length - 1)) {
      msg += 'Please check the format of your email address.\n';
    }
  }
  if (msg.length > 0) {
    alert (msg);
    return false;
   }
  else {
    return true;
  }
}


