﻿var geocoder;
var map;
var map2;
var marker;
var marker2;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay;

function initialize() {
    //MAP
    directionsDisplay = new google.maps.DirectionsRenderer();
    //GEOCODER
    geocoder = new google.maps.Geocoder();

}

//put so jquery runs in an update panel
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
    if (args.get_error() == undefined) {
        alertSuggest();
    }
}

function alertSuggest() {
    $(document).ready(function () {

        initialize();

        $(function () {
            $("#MainContent_txtFrAddress").autocomplete({
                //This bit uses the geocoder to fetch address values
                source: function (request, response) {
                    //geocoder.geocode({ 'address': request.term + ', USA' }, function (results, status) {
                    geocoder.geocode({ 'address': request.term }, function (results, status) {
                        response($.map(results, function (item) {
                            if (item.types[0] == "street_address") {
                                //if (item.types[0] != "locality" && item.types[0] != "route" && item.types[0] != "intersection" && item.types[0] != "political" && item.types[0] != "country" && item.types[0] != "administrative_area_level_1" && item.types[0] != "administrative_area_level_2" && item.types[0] != "administrative_area_level_3" && item.types[0] != "colloquial_area" && item.types[0] != "sublocality" && item.types[0] != "neighborhood" && item.types[0] != "premise" && item.types[0] != "subpremise" && item.types[0] != "postal_code" && item.types[0] != "natural_feature" && item.types[0] != "airport" && item.types[0] != "park") {
                                return {
                                    label: item.formatted_address,
                                    value: item.formatted_address,
                                    latitude: item.geometry.location.lat(),
                                    longitude: item.geometry.location.lng(),
                                    valid: item.geometry.location_type,
                                    addrcomp: item.types[0]
                                }
                            }
                        }))
                    })
                },
                //This bit is executed upon selection of an address
                select: function (event, ui) {

                    //var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
                    //marker.setPosition(location);
                    //map.setCenter(location);
                    document.getElementById("MainContent_addresstype").value = ui.item.valid;
                    document.getElementById("MainContent_addrcomp").value = ui.item.addrcomp;
                }
            })
        })

        $(function () {
            $("#MainContent_txtToAddress").autocomplete({
                //This bit uses the geocoder to fetch address values
                source: function (request, response) {
                    // geocoder.geocode({ 'address': request.term + ', USA' }, function (results, status) {
                    geocoder.geocode({ 'address': request.term }, function (results, status) {
                        response($.map(results, function (item) {
                            if (item.types[0] == "street_address") {
                                // if (item.types[0] != "locality" && item.types[0] != "route" && item.types[0] != "intersection" && item.types[0] != "political" && item.types[0] != "country" && item.types[0] != "administrative_area_level_1" && item.types[0] != "administrative_area_level_2" && item.types[0] != "administrative_area_level_3" && item.types[0] != "colloquial_area" && item.types[0] != "sublocality" && item.types[0] != "neighborhood" && item.types[0] != "premise" && item.types[0] != "subpremise" && item.types[0] != "postal_code" && item.types[0] != "natural_feature" && item.types[0] != "airport" && item.types[0] != "park") {
                                return {
                                    label: item.formatted_address,
                                    value: item.formatted_address,
                                    latitude: item.geometry.location.lat(),
                                    longitude: item.geometry.location.lng(),
                                    valid: item.geometry.location_type,
                                    addrcomp: item.types[0]
                                }
                            }
                        }))
                    })
                },
                //This bit is executed upon selection of an address
                select: function (event, ui) {

                    //if not rooftop, then not valid
                    //TODO or check to see if the type is street_address
                    document.getElementById("MainContent_addresstype2").value = ui.item.valid;
                    document.getElementById("MainContent_addrcomp2").value = ui.item.addrcomp;
                }
            })
        })


        //Airports


        var headID = document.getElementsByTagName("head")[0];
        var cssNode = document.createElement('link');
        cssNode.type = 'text/css';
        cssNode.rel = 'stylesheet';
        cssNode.href = 'css/ui-lightness/jquery-ui-1.8.4.custom.css';
        cssNode.media = 'screen';
        headID.appendChild(cssNode);

    });
}

alertSuggest();




function validateRequest() {
    var addresstype = document.getElementById("MainContent_addresstype").value;
    var addresstype2 = document.getElementById("MainContent_addresstype2").value;

    var addresscomp = document.getElementById("MainContent_addrcomp").value;
    var addresscomp2 = document.getElementById("MainContent_addrcomp2").value;

    var bZipSet = false;
    var addrcomp = document.getElementById("MainContent_addrcomp").value;
    var addrcomp2 = document.getElementById("MainContent_addrcomp2").value;
    var validateFrAdd = true;
    var validateToAdd = true;
    var list = document.getElementById("MainContent_rblCity"); //Client ID of the radiolist 
    var inputs = list.getElementsByTagName("input");
    var selected;
    for (var K = 0; K < inputs.length; K++) {
        if (inputs[K].checked) {
            selected = inputs[K];
            break;
        }
    }

    //NOT ORLANDO
    if (selected.value != 1) {  //FROM  LOCATION
        var list2 = document.getElementById("MainContent_rblFromLoc"); //Client ID of the radiolist 
        var inputs2 = list2.getElementsByTagName("input");
        var selected2;
        for (var j = 0; j < inputs2.length; j++) {
            if (inputs2[j].checked) {
                selected2 = inputs2[j];
                break;
            }
        }

        if (selected2.value == "Address" || selected2.value == "Favorites") {
            validateFrAdd = true;
        }
        else {
            validateFrAdd = false;
            addresstype = "VALID";
        }
    }

    //IF NOT ORLANDO AND FROM AIRPORT IS SELECTED, DON'T VALIDATE
    //VALIDATE FROM ADDRESS
    if (validateFrAdd == true) {
        var address = document.getElementById("MainContent_txtFrAddress").value;
        if (addresstype == "ROOFTOP" || addresstype == "RANGE_INTERPOLATED" || addresscomp == "street_address") {
            //google suggest entry valid
            var arrAdd1 = address.split(",");
            addresstype = "VALID";
        }
        else if (addresscomp != "locality" && addresscomp != "route" && addresscomp != "intersection" && addresscomp != "political" && addresscomp != "country" && addresscomp != "administrative_area_level_1" && addresscomp != "administrative_area_level_2" && addresscomp != "administrative_area_level_3" && addresscomp != "colloquial_area" && addresscomp != "sublocality" && addresscomp != "neighborhood" && addresscomp != "premise" && addresscomp != "subpremise" && addresscomp != "postal_code" && addresscomp != "natural_feature" && addresscomp != "airport" && addresscomp != "park") {
            var arrAdd1 = address.split(",");
            addresstype = "VALID";
        }
        else if (addresscomp == "airport") {
        }
        else if (addresstype.length > 2 == true) {
            //google suggest entry invalid

        }
        else {
            //not a google entry, need to validate
            var arrAdd1 = address.split(",");
            //validate there are at least 2 commas

            if (arrAdd1.length > 2) {

                addresstype = "VALID";
            }
        }
    }
    //TO LOCATION
    if (selected.value != 1) {
        list2 = document.getElementById("MainContent_rblToLoc"); //Client ID of the radiolist 
        inputs2 = list2.getElementsByTagName("input");

        for (var j = 0; j < inputs2.length; j++) {
            if (inputs2[j].checked) {
                selected2 = inputs2[j];
                break;
            }
        }

        if (selected2.value == "Address" || selected2.value == "Favorites") {
            validateToAdd = true;
        }
        else {
            validateToAdd = false;
            addresstype2 = "VALID";
        }
    }

    //IF NOT ORLANDO AND TO AIRPORT IS SELECTED, DON'T VALIDATE
    //VALIDATE TO ADDRESS
    if (validateToAdd == true) {
        var address2 = document.getElementById("MainContent_txtToAddress").value;
        if (addresstype2 == "ROOFTOP" || addresstype2 == "RANGE_INTERPOLATED" || addresscomp2 == "street_address") {
            //google suggest entry valid
            var arrAdd2 = address2.split(",");
            addresstype2 = "VALID";
        }
        else if (addresscomp2 != "locality" && addresscomp2 != "route" && addresscomp2 != "intersection" && addresscomp2 != "political" && addresscomp2 != "country" && addresscomp2 != "administrative_area_level_1" && addresscomp2 != "administrative_area_level_2" && addresscomp2 != "administrative_area_level_3" && addresscomp2 != "colloquial_area" && addresscomp2 != "sublocality" && addresscomp2 != "neighborhood" && addresscomp2 != "premise" && addresscomp2 != "subpremise" && addresscomp2 != "postal_code" && addresscomp2 != "natural_feature" && addresscomp2 != "airport" && addresscomp2 != "park") {
            var arrAdd2 = address2.split(",");
            addresstype2 = "VALID";
        }
        else if (addresscomp2 == "airport") {
        }
        else if (addresstype2.length > 2 == true) {
            //google suggest entry invalid
        }
        else {
            //not a google entry, need to validate
            var arrAdd2 = address2.split(",");
            //validate there are at least 2 commas

            if (arrAdd2.length > 2) {
                addresstype2 = "VALID";
            }
        }

    }

    //IF VALID CALC MILES
    if (addresstype == "VALID" && addresstype2 == "VALID") {

        if (validateFrAdd == true) {
            //FROM ADDRESS FIELDS
            var startingIndex = 0;
            if (arrAdd1.length > 4) {  //could contain business name see if first value is numeric
                if (hasNumbers(arrAdd1[startingIndex]) != true) { //possibly business name
                    startingIndex = 1;
                }
            }

            for (var i = 0; i < arrAdd1.length; i++) {
                if (i == 0) {
                    var arrStreet1 = arrAdd1[startingIndex].split(" ");
                    for (var j = 0; j < arrStreet1.length; j++) {
                        if (j == 0) {
                            document.getElementById("MainContent_streetnumber").value = trim(arrStreet1[startingIndex]);
                        }
                    }
                    document.getElementById("MainContent_streetaddress").value = trim(arrAdd1[startingIndex].replace(arrStreet1[startingIndex], ""));
                }
                else if (i == 1) {
                    document.getElementById("MainContent_city").value = trim(arrAdd1[startingIndex + 1]);
                }
                else if (i == 2) {
                    if (hasNumbers(trim(arrAdd1[startingIndex + 2])) == true) {
                        var arrState1 = arrAdd1[startingIndex + 2].split(" ");
                        var zip1;
                        for (var k = 0; k < arrState1.length; k++) {
                            if (k == arrState1.length - 1) {
                                zip1 = k;
                                document.getElementById("MainContent_zip").value = trim(arrState1[k]);
                                bZipSet = true;
                            }
                        }
                        document.getElementById("MainContent_state").value = trim(arrAdd1[startingIndex + 2].replace(arrState1[zip1], ""));
                    }
                    else {
                        document.getElementById("MainContent_state").value = trim(arrAdd1[startingIndex + 2]);
                    }
                }
                else if (i == 3 && bZipSet == false) {
                    if (hasNumbers(arrAdd1[startingIndex + 3]) == true)
                        document.getElementById("MainContent_zip").value = trim(arrAdd1[startingIndex + 3]);
                }
            }

            bZipSet = false
        }

        if (validateToAdd == true) {
            //TO ADDRESS FIELDS
            var startingIndex = 0;
            if (arrAdd2.length > 4) {  //could contain business name see if first value is numeric
                if (hasNumbers(arrAdd2[startingIndex]) != true) { //possibly business name
                    startingIndex = 1;
                }
            }

            for (var i = 0; i < arrAdd2.length; i++) {
                if (i == 0) {
                    var arrStreet2 = arrAdd2[startingIndex].split(" ");
                    for (var j = 0; j < arrStreet2.length; j++) {
                        if (j == 0) {
                            document.getElementById("MainContent_streetnumber2").value = trim(arrStreet2[startingIndex]);
                        }
                    }
                    document.getElementById("MainContent_streetaddress2").value = trim(arrAdd2[startingIndex].replace(arrStreet2[startingIndex], ""));

                }
                else if (i == 1) {
                    document.getElementById("MainContent_city2").value = trim(arrAdd2[startingIndex + 1]);
                }
                else if (i == 2) {
                    if (hasNumbers(trim(arrAdd2[startingIndex + 2])) == true) {

                        var arrState2 = arrAdd2[startingIndex + 2].split(" ");
                        var zip2;
                        for (var k = 0; k < arrState2.length; k++) {
                            if (k == arrState2.length - 1) {
                                zip2 = k;
                                document.getElementById("MainContent_zip2").value = trim(arrState2[k]);
                                bZipSet = true;
                            }
                        }
                        document.getElementById("MainContent_state2").value = trim(arrAdd2[startingIndex + 2].replace(arrState2[zip2], ""));
                    }
                    else {
                        document.getElementById("MainContent_state2").value = trim(arrAdd2[startingIndex + 2]);
                    }
                }
                else if (i == 3 && bZipSet == false) {
                    if (hasNumbers(arrAdd2[startingIndex + 3]) == true)
                        document.getElementById("MainContent_zip2").value = trim(arrAdd2[startingIndex + 3]);
                }
            }

        }

        document.getElementById("MainContent_herror").value = "";
    }
    else if (addresstype != "VALID" && addresscomp == "airport") {
        document.getElementById("MainContent_herror").value = "From Address is an Airport.  Please select Airport.";

    }
    else if (addresstype2 != "VALID" && addresscomp2 == "airport") {
        document.getElementById("MainContent_herror").value = "To Address is an Airport.  Please select Airport.";

    }
    else if (addresstype != "VALID" && addresstype2 == "VALID") {
        document.getElementById("MainContent_herror").value = "From Address must be in the format: street address, city, state, zip";

    }
    else if (addresstype == "VALID" && addresstype2 != "VALID") {
        document.getElementById("MainContent_herror").value = "To Address must be in the format: street address, city, state, zip";

    }
    else {
        document.getElementById("MainContent_herror").value = "From & To Addresses must be in the format: street address, city, state, zip";

    }
}

function hasNumbers(t) {
    var regex = /\d/g;
    return regex.test(t);
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}


