﻿var point1;
var point2;

function setFieldFocus(){
    document.getElementById("liShowZip").style.backgroundColor = "#999999";
    document.getElementById("liShowAddress").style.backgroundColor = "#999999";
    document.getElementById("liShowState").style.backgroundColor = "#999999";
	
	if (document.getElementById("hfSearchType").value == "zip"){
		if (document.getElementById("divSearchZip").style.display != "none"){
			document.getElementById("tbZip").focus();
		}
        document.getElementById("liShowZip").style.backgroundColor = "#036";
	}else if (document.getElementById("hfSearchType").value == "address"){
		if (document.getElementById("divSearchAddress").style.display != "none"){
			document.getElementById("tbAddressLine1").focus();
		}
        document.getElementById("liShowAddress").style.backgroundColor = "#036";
	}else if (document.getElementById("hfSearchType").value == "state"){
		if (document.getElementById("divSearchState").style.display != "none"){
			document.getElementById("ddlState").focus();
		}
        document.getElementById("liShowState").style.backgroundColor = "#036";
	}
}

function GetPointFromSearch(){
	if (document.getElementById("hfSearchType").value == "zip"){
		GetPointFromZipCode();
	}else if (document.getElementById("hfSearchType").value == "address"){
		GetPointFromAddress();
	}else if (document.getElementById("hfSearchType").value == "state"){
		__doPostBack('btnSearch2','');
	}
}

function GetPointFromZipCode(){
	var zip;
	zip = document.getElementById("tbZip").value;
    if (zip == ""){
		alert("Please enter a zip code to continue.");
    }else if (isInteger(zip) == false){
		alert("Please enter a number in the zip code field to continue.");
    }else{
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(zip,function(point) {
			if (point == null){
				alert("The zip code you entered could be found. Please try again.");
			}else{
				SetHiddenLatLng(point);
 				SetHiddenAddress(zip);
			   __doPostBack('btnSearch2','');			
			}
        });   
    }
}

function GetPointFromAddress(){
	var address;
	if (document.getElementById("tbAddressCity").value != ""){
		if (document.getElementById("tbAddressLine1").value != undefined){
			address = document.getElementById("tbAddressLine1").value;
		}
		if (address != ""){
			address = address + ", ";
		}
		address = address + document.getElementById("tbAddressCity").value;
		if (address != ""){
			address = address + ", ";
		}
		address = address + document.getElementById("ddlAddressState").value;
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(point) {
			if (point == null){
				alert("The address you entered could be found. Please try again.");
			}else{
				SetHiddenLatLng(point);
				SetHiddenAddress(address);
				__doPostBack('btnSearch2','');
			}
		});
	}else{
        alert("Please complete all address fields to continue.");		
    }
}

function SetHiddenLatLng(point){
    document.getElementById("hfLatLng").value = point.toString();
}

function SetHiddenAddress(address){
    document.getElementById("hfAddress").value = address;
}

function showSearchType(type){
    document.getElementById("divSearchAddress").style.display = "none";
    document.getElementById("divSearchZip").style.display = "none";
    document.getElementById("divSearchState").style.display = "none";  

   	document.getElementById("searchOptionCount").style.display = "block";
	//document.getElementById("searchOptionType").style.display = "block";
	document.getElementById("searchButtons").style.display = "block";
  
    document.getElementById("liShowState").style.backgroundColor = "#999999";  
    document.getElementById("liShowZip").style.backgroundColor = "#999999";  
    document.getElementById("liShowAddress").style.backgroundColor = "#999999";  
    
    if (type == "state"){
        document.getElementById("divSearchState").style.display = "block";  
        document.getElementById("liShowState").style.backgroundColor = "#036";  
        document.getElementById("tbZip").value = '';  
        document.getElementById("hfLatLng").value = '';
        document.getElementById("ddlLocationCount").value = 'ALL';
        document.getElementById("ddlState").focus();
    }else if (type == "zip"){
        document.getElementById("divSearchZip").style.display = "block";
        document.getElementById("liShowZip").style.backgroundColor = "#036";  
        document.getElementById("ddlLocationCount").value = '5';            
        document.getElementById("tbZip").focus();
    }else if (type == "address"){
        document.getElementById("divSearchAddress").style.display = "block";
        document.getElementById("liShowAddress").style.backgroundColor = "#036";  
        document.getElementById("ddlLocationCount").value = '5';            
        document.getElementById("tbAddressLine1").focus();
   }
	searchType = type;
    document.getElementById("hfSearchType").value = type;
}

function switchViews(type){
	document.getElementById("results_ListView").style.display = "none";
	document.getElementById("results_ExcelView").style.display = "none";

	if (type == "excel"){
		document.getElementById("results_ExcelView").style.display = "block";
	}else if (type == "list"){
		document.getElementById("results_ListView").style.display = "block";
	}
}