var http;
var isWorking;
var br = false;
isWorking = false;
psurl = '/propselect.php';

function init() {
	defaultTown();
	defaultHood();
	//defaultZip(); 
}

function resetCountyIndex() {
	var sel = "towns"; //county
	var c = document.getElementById(sel);
	c.selectedIndex = 0;
}

function resetQS() {
	//defaultTown();
        resetCountyIndex();
	defaultHood();
}

function defaultTown() {
	var sel = "towns";
	emptySelect(sel);
	addOption(sel, "City","");
}

function defaultHood() {
	var sel = "hoods";
	emptySelect(sel);
	addOption(sel,'','Neighborhoods');
}

function defaultZip() {
	var sel = "zipcodes";
	emptySelect(sel);
	addOption(sel, 'Zip Code(s)','');
}

function showZipCodes() {
	var lcc=document.getElementById('label_cc');
	lcc.className='hidden';
	var cc=document.getElementById('countycity');
        cc.style.display='none';
	var zci = document.getElementById('zipcode_input');
	zci.style.display='block';
	var lzc = document.getElementById('label_zc');
	lzc.innerHTML = "Zip Code Search";
	resetQS();
}

function showCountyCity() {
	//getCounties();
	var lzc=document.getElementById('label_zc');
	var zci=document.getElementById('zipcode_input');
	var cc=document.getElementById('countycity');
	zci.style.display='none';
	cc.style.display='block';
	lzc.className='hidden';
	lzc.innerHTML = "Search by Zip Code";
	document.getElementById('zipcodes').value='';
}

function XMLHttp() {
        var xmlhttp;
        try {
                xmlhttp = new XMLHttpRequest();
        }
        catch(error) {
                try {
                   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (error) {
                   return false;
                }
        }
        return xmlhttp;
}

function trim2length(str, elen) {
    //str: string to cut
    //elen: length to cut

    if (str.length > elen) {
	str = str.substr(0, elen);
   }
   return str;
}

function addOptGroup(select, label, prevlabel) {
    sel = document.getElementById(select);
    grp = document.createElement("OPTGROUP");
    grp.label = "-- " + label + " --"; // != prevlabel ? label : prevlabel;
    grp.className = "optiongroup"; 
    sel.appendChild(grp);
}

function strSearch(str, what) {
   if (str.search(what)) {
	return str.replace(what, '');
   }

   return str;
}

function addOption(select, name, value, label) {     
	sel = document.getElementById(select);
    opt = document.createElement("OPTION");
	opttext = strSearch(name, ' County'); 
	opt.text  = value; //opttext;
	opt.value = opttext;
        sel.options.add(opt);
}

function emptySelect(sel) {
	sel.selectedIndex=0;
        citysel = document.getElementById(sel);
        for(i=citysel.length-1; i >= 0; i--) {
           citysel.options[i] = null;
        }

}

function inarray(text, arr) {
   for (var x in arr) {
	if (x == text) {
		return true;
	}
   }
   return false;
}

function stateChange() {
        para = document.getElementById(result);
        //para.style.display = "block";
        if (http.readyState == 4) {
                if (http.status == 200) {
                   if (!br) {
                      stuff = http.responseText;
                      emptySelect(result);
                      var info = stuff.split("\n");
                      var i;
			if (info[0] == "City/County" || info[0] == "Neighborhoods") {
			   addOption(result, '', info[0]);
			}
                      for(i=1;i<info.length-1;i++) {
			   addOption(result, info[i], info[i]);
                      }
                      //para.innerHTML = stuff;
                        para.style.display = "inline";
                   }
                   isWorking = false;
                   http = false;
                }
        }
}

        function doareajump(){
           init();
	   with(document.propform){
                if(county.options[county.selectedIndex] != 0){
                   cts = county.selectedIndex;
                   pclass = county.options[county.selectedIndex].value;
                   propt = 1;
                   if (!http) http = new XMLHttp();
                   if (http) {
                      url = psurl + "?f=getCounties&proptype=" + propt + "&county=" + pclass + "&cts=" + cts;
                      if (!isWorking && http) {
                        result = "towns";
                        http.onreadystatechange = stateChange;
                        http.open("GET",url, true);
                        isWorking = true;
                        http.send(null);
                      }
                   }

                }
           }
        }

function getCounties() {
   with(document.propform) {
	propt = 1; //Residential only
        if (!http) http = new XMLHttp();
        if (http) {
        url = psurl + "?counties";
        if (!isWorking && http) {
           result = "towns";
           http.onreadystatechange = stateChange;
           http.open("GET",url, true);
           isWorking = true;
           http.send(null);
        }
	}
   }
}

function getHoods() {
   with(document.propform) {
      qs = '';
      var town = document.getElementById('towns');
      if(town.selectedIndex != 0) { //county
         /*cts = county.selectedIndex;
         city = town.selectedIndex;
         pclass = county.options[county.selectedIndex].value;*/
         cityv = town.options[town.selectedIndex].value;
	 qs = "&city=" + cityv; //"&county=" + pclass + "&cts=" + cts + "&cs=" + city + "&city=" + cityv;
      } else {
	if (zipcodes.value != '') {
	   zips = zipcodes.value;
	   qs = "&zipcodes=" + zips;
        }
      }
      if (qs != '') {
         propt = 1; //Residential only
         if (!http) http = new XMLHttp();
         if (http) {
            url = psurl + "?f=getHoods&proptype=" + propt + qs;
            if (!isWorking && http) {
               result = "hoods";
               http.onreadystatechange = stateChange;
               http.open("GET",url, true);
               isWorking = true;
               http.send(null);
            }
         }
      }

   }
}

function getHoodsForLand() {
   with(document.landform) {
      qs = '';
      county = document.getElementById('formcounty').value;
      town   = document.getElementById('formtown').value;
      if(county != '') {
         cts = county; //county.selectedIndex;
         city = town; //town.selectedIndex;
         pclass = county; //infoarray[county].options[cts].value;
	 town = document.getElementById('formtown');
	 
         cityv = town.options[town.selectedIndex].value; //infoarray[town].options[city].value;
         qs = "&county=" + pclass + "&cts=" + cts + "&cs=" + city + "&city=" + cityv;
      }/*/ else {
        if (zipcodes.value != '') {
           zips = zipcodes.value;
           qs = "&zipcodes=" + zips;
        }
      }*/
      if (qs != '') {
         propt = 4; //Residential only
         if (!http) http = new XMLHttp();
         if (http) {
            url = psurl + "?f=getHoods&proptype=" + propt + qs;
            if (!isWorking && http) {
               result = "subdivs";
               http.onreadystatechange = stateChange;
               http.open("GET",url, true);
               isWorking = true;
               http.send(null);
            }
         }
      }

   }
}


function getZipCodes() {
   with(document.propform) {
         propt = 1; //Residential only
         if (!http) http = new XMLHttp();
         if (http) {
            //url = psurl + "?proptype=" + propt + "&county=" + pclass + "&cts=" + cts + "&cs=" + city + "&city=" + cityv + "&f=getZips";
	    url = psurl + "?proptype=" + propt + "&f=getZips";
            if (!isWorking && http) {
               result = "zipcodes";
               result = "zipcodes";
               http.onreadystatechange = stateChange;
               http.open("GET",url, true);
               isWorking = true;
               http.send(null);
            }
         }

   }
}


