var geocoder;

function initialize() {	
	 $.ajax({
        type: "get",
        url: "./StoreLocator/Home/GetStates",
        data: { },
        success: function (data, textStatus) {
            var storeStateNames = data.split(",");
			for (var i = 0; i < storeStateNames.length-1; i++) {
				$("#dropdown").append("<option value='" + storeStateNames[i] + "'>" + storeStateNames[i] + "</option>");
			}
        }
    });	
}
	
 function ClearText(obj, text) {
    if (obj.value != null)
        obj.value = (obj.value == '') ? text : obj.value.replace(text, "");
}

function CheckEnterZip(code, textboxvalue) {
    if (code == 13) {
        codeAddress();
        return false;
    }
    return true;
}

function codeAddress(){ 	
	if (document.getElementById("search").value.length > 0 && document.getElementById("search").value != "Enter your zip code"){
		var state = "";
		var address = document.getElementById("search").value;
		geocoder = new google.maps.Geocoder();
		geocoder.geocode({ 'address': address }, function (results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				if (results[0].address_components.length > 2) {
				    state = results[0].address_components[results[0].address_components.length - 2].short_name + "&latlng=" + results[0].geometry.location.lat() + "," + results[0].geometry.location.lng();							
					redirectSL(state);
				} 
			}		
		});
	}
}

function redirectState(){
	redirectSL($("#dropdown").get(0).value);
}

function redirectSL(state){
	window.location = "./StoreLocator/Home/Index?stateCode=" + state;
}
