
var $ = jQuery;
var zusatz1 = '';
var zusatz2 = '';
var zusatz3 = '';

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var from;
function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    return elt.options[elt.selectedIndex].text;
}
function getZip(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    var blabla = elt.options[elt.selectedIndex].text.split(" ");
    return blabla[0];
}



function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var vienna = new google.maps.LatLng(48.208174, 16.373819);
    var mapOptions = {
        zoom: 16,
        center: vienna
    }
    map = new google.maps.Map(document.getElementById('mapa'), mapOptions);
    directionsDisplay.setMap(map);

}

function calcRoute() {


    var WAY;
    if ($("form").data("way") === "zum") {
        WAY = "ZUM";
    } else {
        WAY = "VOM";
    }
    if (WAY == 'ZUM') {
        var start = from;
        var end = 'Flughafen Wien, Schwechat';
    } else {
        var start = 'Flughafen Wien, Schwechat';
        var end = from;
    }





    if (zusatz1 === 'none' || zusatz1 === '') {
        zusatzX1 = '';
    } else {
        zusatzX1 = zusatz1;
    }
    if (zusatz2 === 'none' || zusatz2 === '') {
        zusatzX2 = '';
    } else {
        zusatzX2 = zusatz2;
    }
    if (zusatz3 === 'none' || zusatz3 === '') {
        zusatzX3 = '';
    } else {
        zusatzX3 = zusatz3;
    }
    // document.getElementById('aaa1').innerHTML = zusatzX1;
    //  document.getElementById('aaa2').innerHTML = zusatzX2;
    // document.getElementById('aaa3').innerHTML = zusatzX3;

    var checkboxArray = [zusatzX1, zusatzX2, zusatzX3];



    var waypts = [];
    for (var i = 0; i < checkboxArray.length; i++) {
        var adresanesto = checkboxArray[i];
        if (adresanesto !== "") {

            waypts.push({
                location: adresanesto,
                stopover: true});
        }
    }




    var request = {
        origin: start,
        destination: end,
        waypoints: waypts,
        optimizeWaypoints: false,
        travelMode: google.maps.TravelMode.DRIVING};
    // document.getElementById('aaa4').innerHTML = waypts;

    var totaldistance = 0;
    var totalduration = 0;

    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            var route = response.routes[0];

            // For each route, display summary information.
            for (var i = 0; i < route.legs.length; i++) {
                totaldistance = totaldistance + route.legs[i].distance.value;
                totalduration = totalduration + route.legs[i].duration.value;
            }
            var seconds = totalduration;


            var date = new Date(seconds * 1000);
            var hh = date.getUTCHours();
            var mm = date.getUTCMinutes();
            var ss = date.getSeconds();
            if (hh < 10) {
                hh = "0" + hh;
            }
            if (mm < 10) {
                mm = "0" + mm;
            }
            if (ss < 10) {
                ss = "0" + ss;
            }
            var mins = Math.floor(seconds / 60);



            document.getElementById('KM').innerHTML = (totaldistance / 1000).toFixed(1);
            document.getElementById('MIN').innerHTML = mins;
            // document.getElementById('km2').innerHTML = (totaldistance / 1000).toFixed(1);
            // document.getElementById('min2').innerHTML = mins;

//			   document.querySelector('#route_distance').innerHTML = (totaldistance/1000).toFixed(1)+" Km lang ";
//	   document.querySelector('#route_time').innerHTML = "Dauer ca. "+hh+" Stunden, und "+mm+" Minuten";
            /*document.F1.km.value = (totaldistance/1000).toFixed(1);
             Die Route ist Km lang und in Dauer von ca. Stunden und Minuten
             /*document.F1.price.value = '156';*/
        }
    });
}
google.maps.event.addDomListener(window, 'load', initialize);

$(document).ready(function () {
    $('#CITY').change(function () {

        from = getSelectedText('STREET') + ' ' + document.getElementById('HOUSENUMBER').value + ', ' + getZip('ZIP') + ' ' + getSelectedText('CITY');
        calcRoute();
    });
    $('#ZIP').change(function () {
        from = getSelectedText('STREET') + ' ' + document.getElementById('HOUSENUMBER').value + ', ' + getZip('ZIP') + ' ' + getSelectedText('CITY');

        calcRoute();
    });
    $('#STREET').change(function () {

        from = getSelectedText('STREET') + ' ' + document.getElementById('HOUSENUMBER').value + ', ' + getZip('ZIP') + ' ' + getSelectedText('CITY');
        calcRoute();
    });


    $('#HOUSENUMBER').keyup(function () {
        from = getSelectedText('STREET') + ' ' + document.getElementById('HOUSENUMBER').value + ', ' + getZip('ZIP') + ' ' + getSelectedText('CITY');
        calcRoute();
    });

    $('#ADRESSE').change(function () {
        setTimeout(bla, 1000);

    });

    function bla() {

        from = getSelectedText('STREET') + ' ' + document.getElementById('HOUSENUMBER').value + ', ' + getZip('ZIP') + ' ' + getSelectedText('CITY');
        calcRoute();
    }


    $('#ADD1ZIP').change(function () {
        zz1_zip = $(this).val();
        if (zz1_zip === 'none') {
            zusatz1 = '';
        } else {
            zusatz1 = getZip('ADD1ZIP').substring(0, 4) + ' Wien';
        }
        calcRoute();
    });

    $('#ADD1STREET').change(function () {
        zz1_street = $(this).val();
        if (zz1_street === 'none') {
            zusatz1 = getZip('ADD1ZIP').substring(0, 4) + ' Wien';
        } else {
            zusatz1 = getSelectedText('ADD1STREET') + ' ' + document.getElementById('ADD1HOUSENUMBER').value + ', ' + getZip('ADD1ZIP').substring(0, 4) + ' Wien';
        }
        calcRoute();
    });


    $('#ADD1HOUSENUMBER').keyup(function () {
        aa1_zip = getZip('ADD1ZIP').substring(1, 4);
        if (aa1_zip !== '') {

            zusatz1 = getSelectedText('ADD1STREET') + ' ' + document.getElementById('ADD1HOUSENUMBER').value + ', ' + getZip('ADD1ZIP').substring(0, 4) + ' Wien';
            calcRoute();
        }

    });


    $('#ADD2ZIP').change(function () {
        zz2_zip = $(this).val();
        if (zz2_zip === 'none') {
            zusatz2 = '';
        } else {
            zusatz2 = getZip('ADD2ZIP').substring(0, 4) + ' Wien';
        }
        calcRoute();
    });

    $('#ADD2STREET').change(function () {
        zz2_street = $(this).val();
        if (zz2_street === 'none') {
            zusatz2 = getZip('ADD2ZIP').substring(0, 4) + ' Wien';
        } else {
            zusatz2 = getSelectedText('ADD2STREET') + ' ' + document.getElementById('ADD2HOUSENUMBER').value + ', ' + getZip('ADD2ZIP').substring(0, 4) + ' Wien';
        }
        calcRoute();
    });


    $('#ADD2HOUSENUMBER').keyup(function () {
        aa2_zip = getZip('ADD2ZIP').substring(1, 4);
        if (aa1_zip !== '') {

            zusatz2 = getSelectedText('ADD2STREET') + ' ' + document.getElementById('ADD2HOUSENUMBER').value + ', ' + getZip('ADD2ZIP').substring(0, 4) + ' Wien';
            calcRoute();
        }

    });


    $('#ADD3ZIP').change(function () {
        zz3_zip = $(this).val();
        if (zz3_zip === 'none') {
            zusatz3 = '';
        } else {
            zusatz3 = getZip('ADD3ZIP').substring(0, 4) + ' Wien';
        }
        calcRoute();
    });

    $('#ADD3STREET').change(function () {
        zz3_street = $(this).val();
        if (zz3_street === 'none') {
            zusatz3 = getZip('ADD3ZIP').substring(0, 4) + ' Wien';
        } else {
            zusatz3 = getSelectedText('ADD3STREET') + ' ' + document.getElementById('ADD3HOUSENUMBER').value + ', ' + getZip('ADD3ZIP').substring(0, 4) + ' Wien';
        }
        calcRoute();
    });


    $('#ADD3HOUSENUMBER').keyup(function () {
        aa3_zip = getZip('ADD3ZIP').substring(1, 4);
        if (aa3_zip !== '') {
            zusatz3 = getSelectedText('ADD3STREET') + ' ' + document.getElementById('ADD3HOUSENUMBER').value + ', ' + getZip('ADD3ZIP').substring(0, 4) + ' Wien';
            calcRoute();
        }
    });

    $('#ADDADDRESS').change(function () {
        aaddress = $(this).val();
        if (aaddress === '2') {
            zusatz3 = '';
            calcRoute();

        } else if (aaddress === '1') {
            zusatz2 = '';
            zusatz3 = '';
            calcRoute();

        } else if (aaddress === '0') {
            zusatz1 = '';
            zusatz2 = '';
            zusatz3 = '';
            calcRoute();

        }

    });


});






