﻿var ClientGeocoder = new GClientGeocoder();
function showAddress() {
    if ((document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdAddressCK").value != "") && (document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdCityCK").value != "")) {
        address = document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdAddressCK").value + ', ' + document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdCityCK").value + " WA ";
        //get rid of capitalization to level the field
        address = address.toLowerCase();
        if (address.indexOf("university village") > 0) {
            if (address.indexOf("street") > 0) {
                address = address.replace("street", "");
                ClientGeocoder.getLocations(address, evaluateGResponse);
            }
            else {
                if (address.indexOf("st.") > 0) {
                    address = address.replace("st.", "");
                    ClientGeocoder.getLocations(address, evaluateGResponse);
                }
                else {
                    if (address.indexOf("st") > 0) {
                            address = address.replace("st", "");
                            ClientGeocoder.getLocations(address, evaluateGResponse);
                    }
                    else {
                        //no street present, send to google
                        ClientGeocoder.getLocations(address, evaluateGResponse);
                    }
                }
            }
        }
        else if (address.indexOf("u village") > 0) {
            if (address.indexOf("street") > 0) {
                address = address.replace("street", "");
                ClientGeocoder.getLocations(address, evaluateGResponse);
            }
            else {
                if (address.indexOf("st.") > 0) {
                    address = address.replace("st.", "");
                    ClientGeocoder.getLocations(address, evaluateGResponse);
                }
                else {
                    if (address.indexOf("st") > 0) {
                        address = address.replace("st", "");
                        ClientGeocoder.getLocations(address, evaluateGResponse);
                    }
                    else {
                        //no street present, send to google
                        ClientGeocoder.getLocations(address, evaluateGResponse);
                    }
                }
            }
        }
        else {
            ClientGeocoder.getLocations(address, evaluateGResponse);
        }
    }
}

function evaluateGResponse(response) {
    if (!response || response.Status.code != 200) {
        //Bad address
        alert("Sorry, the Google gods cannot find that address in their database. Please make corrections and try again. If you think the Google Gods are wrong, please call 206.883.7656 to sort this out with a fellow cupcake lovin’ human");
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLat").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLng").value = "";
    }
    else {
        placeArray = response.Placemark;
        tot = placeArray.length;
        if (tot == 1) {
            //Verify address accuracy is 8 or better
            //alert(response.Placemark[0].AddressDetails.Accuracy);
            if (response.Placemark[0].AddressDetails.Accuracy >= 8) {
                //only one address returned
                //check for country area
                if (response.Placemark[0].AddressDetails.Country != null) {
                    //check for administrative area
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea != null) {
                        //check for subadministrative area
                        if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea != null) {
                            // subadmin exists set coordinates and zip
                            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLat").value = response.Placemark[0].Point.coordinates[1];
                            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLng").value = response.Placemark[0].Point.coordinates[0];
                        }
                        else {
                            // no subadmin set coordinates and zip
                            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLat").value = response.Placemark[0].Point.coordinates[1];
                            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLng").value = response.Placemark[0].Point.coordinates[0];
                        }
                    }
                    else {
                        //administrative area is not available, try to resolve anyway
                        nAddy = response.Placemark[0].address;
                        intsnZip = nAddy.indexOf('98');
                        nZip = nAddy.substr(intsnZip, 5);
                        //alert(nZip);
                        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = nZip;
                        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLat").value = response.Placemark[0].Point.coordinates[1];
                        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLng").value = response.Placemark[0].Point.coordinates[0];   
                    }
                }
                else {
                    //weird address with no country or admin areas such as u village, try to resolve
                    wAddy = response.Placemark[0].address;
                    intsZip = wAddy.indexOf('98');
                    wZip = wAddy.substr(intsZip, 5);
                    //alert(wZip);
                    document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = wZip;
                    document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLat").value = response.Placemark[0].Point.coordinates[1];
                    document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLng").value = response.Placemark[0].Point.coordinates[0];      
                }
            }
            else {
                alert("Sorry, the Google gods cannot find that address in their database. Please make corrections and try again. If you think the Google Gods are wrong, please call 206.883.7656 to sort this out with a fellow cupcake lovin’ human");
            }
        }
        else {
            //multiple results returned, user must select the appropriate result
            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = "";
            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLat").value = "";
            document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtLng").value = "";

            //Create Did You Mean Header

            //Create variable to track number of valid addresses
            var ua = 0;
            var bad = 0;

            //Loop through and add addresses
            for (var i = 0; i < placeArray.length; i++) {
                //verify accuracy is 8
                if (response.Placemark[i].AddressDetails.Accuracy >= 8) {
                    ua = ua + 1;
                    addAddressSelection(response.Placemark[i].address, ua);
                }
                else {
                    bad = bad + 1;
                }
            }

            //verify at least one good address
            if (bad == tot) {
                alert("Sorry, the Google gods cannot find that address in their database. Please make corrections and try again. If you think the Google Gods are wrong, please call 206.883.7656 to sort this out with a fellow cupcake lovin’ human");
            }
            else {
                //there was at least one good address add spacing at end of list
                var stupidLine = document.createEntityReference("nbsp");
                var mySpace = document.createElement("br");
                var endA = document.getElementById("address-area");
                endA.appendChild(mySpace);
                endA.appendChild(stupidLine);
                endA.appendChild(mySpace);
            }
        }
    }
}

function addAddressSelection(address, n) {
    if (!document.getElementById || !document.createElement)
        return false;

    var addressArea = document.getElementById("address-area");

    if (!addressArea)
        return;

    var newLine = document.createElement("br");
    //check for first address
    if (n == 1) {
        var dymHeader = document.createElement("h2");
        var dymTxt = document.createTextNode("Did you mean?");
        dymHeader.appendChild(dymTxt);
        addressArea.appendChild(dymHeader);
        addressArea.appendChild(newLine);
    }

    addressArea.appendChild(newLine);

    var newAddressLink = document.createElement("A");

    // Set up the new input for file uploads
    newAddressLink.setAttribute("href", "#");
    newAddressLink.onclick = function() { setNewAddress(address); }
    var linkText = document.createTextNode(address);
    newAddressLink.appendChild(linkText);
    newAddressLink.setAttribute("style", "line-height: 30px");


    // The new box needs a name and an ID
    if (!addressArea.lastAssignedId)
        addressArea.lastAssignedId = 100;

    newAddressLink.setAttribute("id", "dynamic" + addAddressSelection.lastAssignedId);
    newAddressLink.setAttribute("name", "dynamic:" + addAddressSelection.lastAssignedId);
    addressArea.appendChild(newAddressLink);
    addAddressSelection.lastAssignedId++;
}

function setNewAddress(address) {
    addArray = address.split(",");
    document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdAddressCK").value = addArray[0];
    //alert("1 " + addArray[0] + " 2 " + addArray[1] + " 3 " + addArray[2]);
    addSubArray = addArray[2].split(" ");
    //alert("1 " + addSubArray[0] + " 2 " + addSubArray[1] + " 3 " + addSubArray[2]);
    if (addSubArray[0].length > 4) {
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdCityCK").value = addArray[1];
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = addSubArray[2];
        showAddress(addArray[0] + "Seattle WA " + addSubArray[2]);
    }
    else {
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdCityCK").value = addArray[1];
        document.getElementById("ctl00_ContentPlaceHolder1_WZCupcakeOrder_txtdZipCK").value = addSubArray[2];
        showAddress(addArray[0] + "Seattle WA " + addArray[2]);
    }
}

function googlegodsonly() {
    alert('Sorry, on the Google can set this value.');
}