function initMap(form) {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -33.8688, lng: 151.2195},
        zoom: 13,
        mapTypeId: 'roadmap'
    });

    // Create the search box and link it to the UI element.
    var input = document.getElementById(form + ':addressSearch');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function () {
        searchBox.setBounds(map.getBounds());
    });

    var markers = [];
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function () {
        var places = searchBox.getPlaces();

        if (places.length == 0) {
            return;
        }

        // Clear out the old markers.
        markers.forEach(function (marker) {
            marker.setMap(null);
        });
        markers = [];

        // For each place, get the icon, name and location.
        var bounds = new google.maps.LatLngBounds();
        places.forEach(function (place) {
            if (!place.geometry) {
                console.log("Returned place contains no geometry");
                return;
            }
            var icon = {
                url: place.icon,
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(25, 25)
            };

            // Create a marker for each place.
            markers.push(new google.maps.Marker({
                map: map,
                icon: icon,
                title: place.name,
                position: place.geometry.location
            }));

            if (place.geometry.viewport) {
                // Only geocodes have viewport.
                bounds.union(place.geometry.viewport);
            } else {
                bounds.extend(place.geometry.location);
            }
        });
        parseAddress(places, form);
        map.fitBounds(bounds);
    });
}

function parseAddress(places, form) {
    var streetNumber;
    var streetAddress;
    var extendedAddress;
    var locality;
    var region;
    var country;
    var zipCode;

    if (1 === places.length && 1 <= places[0].address_components.length) {
        for (var l1 = 0; l1 < places[0].address_components.length; l1++) {
            for (var l2 = 0; l2 < places[0].address_components[l1].types.length; l2++) {
                var breakLoop = false;
                switch (places[0].address_components[l1].types[l2]) {
                    case 'street_number':
                        streetNumber = places[0].address_components[l1].long_name;
                        breakLoop = true;
                        break;
                    case 'route':
                        streetAddress = places[0].address_components[l1].long_name;
                        breakLoop = true;
                        break;
                    case 'sublocality':
                        extendedAddress = places[0].address_components[l1].long_name;
                        breakLoop = true;
                        break;
                    case 'locality':
                        locality = places[0].address_components[l1].long_name;
                        break;
                    case 'administrative_area_level_1':
                        region = places[0].address_components[l1].long_name;
                        breakLoop = true;
                        break;
                    case 'country':
                        country = places[0].address_components[l1].long_name;
                        breakLoop = true;
                        break;
                    case 'postal_code':
                        zipCode = places[0].address_components[l1].long_name;
                        breakLoop = true;
                        break;
                }
                if (breakLoop) {
                    break;
                }
            }
        }
        $("#"+form+"\\:street").val(streetAddress + ' ' + streetNumber);
        $("#"+form+"\\:streetHdn").val(streetAddress + ' ' + streetNumber);

        $("#"+form+"\\:colony").val(extendedAddress);
        $("#"+form+"\\:colonyHdn").val(extendedAddress);

        $("#"+form+"\\:department").val(locality);
        $("#"+form+"\\:departmentHdn").val(locality);

        $("#"+form+"\\:state").val(region);
        $("#"+form+"\\:stateHdn").val(region);

        $("#"+form+"\\:cp").val(zipCode);
        $("#"+form+"\\:cpHdn").val(zipCode);
    }


}

function clearData(form) {
    $("#"+form+"\\:street").val("");
    $("#"+form+"\\:colony").val("");
    $("#"+form+"\\:department").val("");
    $("#"+form+"\\:state").val("");
    $("#"+form+"\\:cp").val("");

    $("#"+form+"\\:streetHdn").val("");
    $("#"+form+"\\:colonyHdn").val("");
    $("#"+form+"\\:departmentHdn").val("");
    $("#"+form+"\\:stateHdn").val("");
    $("#"+form+"\\:cpHdn").val("");
}

function initAutocomplete(form) {
    initMap(form);
}
