﻿function uncheckedLocation(list) {
    for(x=0; x<15; x++) {
        if(getEle(list+"_"+x) != undefined) {
            getEle(list+"_"+x).disabled = false;
            getEle(list+"_"+x).checked = false;
        } else {
            break;
        }
    }
}
function LocationChange(i) {
    var id = getID();
	for(x=0; x<6; x++) {
		showElement("SearchID"+x, (x==i));
	}
    var sortBy = getEle(id + "_SortBy");
	if(i==5){ 
	    getEle(id + "_ZipAddress").focus();
	} else {
        if(sortBy != undefined) {
	        if(sortBy.options[8] != undefined) {
                sortBy.remove(8);
                sortBy.selectedIndex = 0;
            }
        }
	}
	var ddLocSearch = getEle(id + '_LocationSearch');
	ddLocSearch.selectedIndex = i;
	uncheckedLocation("WithinCity");
	uncheckedLocation("SpecificAreas");
	uncheckedLocation("Surrounding");
	uncheckedLocation("Commuting");
	uncheckedLocation("County");
}

function mapChg(sel) {
    if(sel.selectedIndex == 1) window.location="/search.aspx";
}

function showElement(i, view) { 
	var e = getEle(i); 
	if(view) e.className = show('row'); 
	else e.className = "hide";
	if(view) {
	    var mapImg = getEle("mapImg");
	    var mapDiv = getEle("mapDiv");
	    if(mapImg != undefined) {
	        if(i == "SearchID4") {
	            mapImg.src = "map/57302/" + i + ".gif"; 
	            mapImg.style.display = "block";
	            mapDiv.style.display = "none";
	        } else {
	            mapImg.style.display = "none";
	            mapDiv.style.display = "block";
	            chgMapLoc(i);
	        }
	    }
	}
} 

function chgMapLoc(i){
    if(map != null) {
        map.clearOverlays();
        var e = eval(i + "_Ctr").split(",");
        map.setCenter(new GLatLng(parseFloat(e[0]), parseFloat(e[1])), parseInt(e[2]));
    }
}

function chkLoc(list) {
    var valid = 0;
    var chks = new Array();
    for(x=0; x<getEle(list+"_Count").value; x++) {
        if(getEle(list+"_"+x).checked) { 
            valid = valid + 1;
            chks.push(eval("sc" + getEle(list +"_" +x).value));
        }
    }
    poly(chks);
    if(valid >= 4) {
        for(x=0; x<getEle(list+"_Count").value; x++) {
            getEle(list+"_"+x).disabled = !getEle(list+"_"+x).checked;
        }    
    } else {
        for(x=0; x<getEle(list+"_Count").value; x++) {
            getEle(list+"_"+x).disabled = false;
        } 
    }   
}

function poly(chks){
    map.clearOverlays();
    if(chks.length>0){
        var tY = 0;
        var bY = 200;
        var lX = 0;
        var rX = -200;
        for(x=0; x<chks.length; x++){
            var lns = new Array();
            var e = chks[x].split(",");
            if(parseFloat(e[0]) > tY) tY = parseFloat(e[0]);
            if(parseFloat(e[2]) < bY) bY = parseFloat(e[2]);
            if(parseFloat(e[3]) < lX) lX = parseFloat(e[3]);
            if(parseFloat(e[1]) > rX) rX = parseFloat(e[1]);
            
            lns.push(new GLatLng(parseFloat(e[0]),parseFloat(e[1])));
            lns.push(new GLatLng(parseFloat(e[2]),parseFloat(e[1])));
            lns.push(new GLatLng(parseFloat(e[2]),parseFloat(e[3])));
            lns.push(new GLatLng(parseFloat(e[0]),parseFloat(e[3])));
            lns.push(new GLatLng(parseFloat(e[0]),parseFloat(e[1])));
            if(lns.length>0) map.addOverlay(new GPolyline(lns, "#FF0000", 4));
        }
        map.panTo(new GLatLng( (tY+bY)/2 , (lX+rX)/2 ));
    }
}

function showAdditonal(view, type) {
    if(view) {
        getEle(type+"Table").className = show('table'); 
        getEle(type+"Show").className = "hide"; 
        getEle(type+"Hide").className = show('row'); 
    } else {
        getEle(type+"Table").className = "hide"; 
        getEle(type+"Show").className = show('row'); 
        getEle(type+"Hide").className = "hide";
    }
}

function getQueryString(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
}

function refineSrch() {
    var id = getID();
    var sVar = getQueryString("s");
    if(sVar==undefined && getQueryString("ars")!=undefined) sVar=0;
    LocationChange(sVar);
    
    if(parseInt(sVar) < 5) {
        var locationSearch = getEle(id + "_LocationSearch").value;
        var count = 0;
        if(getEle(locationSearch + "_Count") != undefined) count = parseInt(getEle(locationSearch + "_Count").value);
        
        if(getQueryString("cnt") != undefined) {
            var ars = getQueryString("cnt").split("-");
        } else {
            var ars = getQueryString("ars").split("-");
        }
        for(x=0; x<15; x++) {
            var checkbox = getEle(locationSearch + "_" + x);
            if(checkbox != undefined) {
                for(y=0; y<ars.length; y++) {
                    if(ars[y] == checkbox.value) {
                        checkbox.checked = true;
                    }
                }
            } else {
                break;
            }
        }
        chkLoc(locationSearch);
    } else if(sVar == '5') {
        if(getQueryString("zip") > "") {
            getEle(id + "_ZipAddress").value = getQueryString("zip");
        } 
        if(getQueryString("cty") > "") {
            getEle(id + "_CityAddress").value = getQueryString("cty");
        }
        if(getQueryString("st") > "") {
            var sel = getEle(id + "_StateAddress");
            for(x=0; x<sel.length; x++){
                sel.selectedIndex = x;
                if(sel.value == getQueryString("st")){
                    break;
                }
            }
        }
        var ds = getEle(id + "_Distance");
        for(z=0; z<ds.length; z++){
            ds.selectedIndex = z;
            if(ds.value == getQueryString("ds")){
                break;
            }
        }
    } else {
        LocationChange(5,id);
    }
}
function getID(){
    return getEle("jsID").value;
}
function mapSrch(bl,cx,cy,dx,dy) {
    getEle("ctl00_cp_mi").value = bl + "-" + cx + "-" + cy + "-" + dx + "-" + dy;
}
var map = null;
function showGoogleMap(e) {
  LocationChange(e);
  if (GBrowserIsCompatible()) {
        map = new GMap2(getEle("mapDiv"));
        map.addControl(new GSmallMapControl());
        var geocoder = new GClientGeocoder();
    }
  if (geocoder) chgMapLoc("SearchID" + e);
}
function loadSearch(){
    showGoogleMap(5);
    if(window.location.search.substring(1)=='map'){
        getEle("map").className = show('row');
        getEle("srch").className = "hide";
    } else {
        refineSrch();
    }
}