﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AntennaWeb");

AntennaWeb.AddressEntry = function(element) {
    AntennaWeb.AddressEntry.initializeBase(this, [element]);

    this.txtAddress = null;
    this.txtCity = null;
    this.ddlState = null;
    this.txtZipcode = null;
    this.rbObstructed = null;
    this.rbSingle = null;
    this.rbMultiple = null;
    this.rbOther = null;
    this.txtHeight = null;
    this.spHeight = null;
    this.btnSubmit = null;
    this.btnClear = null;

    this.geocoder = null;
    this.geoCallbackDelegate = null;
    this.onHeightChangeDelegate = null;
}

AntennaWeb.AddressEntry.prototype = {
    initialize: function() {
        AntennaWeb.AddressEntry.callBaseMethod(this, 'initialize');

        this.geocoder = new GClientGeocoder();
        this.geoCallbackDelegate = Function.createDelegate(this, this.geoCallback);

        this.onHeightChange();

        this.txtAddress.focus();
    },

    dispose: function() {
        //Add custom dispose actions here
        AntennaWeb.AddressEntry.callBaseMethod(this, 'dispose');
    },

    onHeightChange: function() {
        this.txtHeight.disabled = !this.rbOther.checked;
        this.spHeight.disabled = !this.rbOther.checked;
        if (this.rbOther.checked == false)
            this.txtHeight.value = '';
    },

    validateAddress: function() {

        var valid = true;
        var errorText = '';
        var reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

        if (reZip.test(this.txtZipcode.value.trim()) == false) {
            errorText = 'The ZIP Code field appears to be in an invalid format.';
            valid = false;
        }

        if (this.rbOther.checked) {
            var height = parseInt(this.txtHeight.value);
            if (height < 0 || height > 10000 || isNaN(height)) {
                errorText = 'The Height field is not in the range of 0 to 10,000 feet.';
                valid = false;
            }
        }

        if (valid == false) {
            window.alert(errorText);
        }

        return valid;
    },

    onClearClick: function(obj, args) {
        this.txtAddress.value = '';
        this.txtCity.value = '';
        this.ddlState.value = null;
        this.txtZipcode.value = '';
        this.txtHeight.value = '';

        this.rbSingle.checked = true;
        this.rbObstructed.checked = true;

        return false;
    },

    onSubmitClick: function(obj, args) {

        if (this.validateAddress() == false)
            return false;

        if (this.geocoder) {

            this.addressLine = "";

            if (this.txtAddress.value != '' && this.txtAddress.value != null)
                this.addressLine = this.addressLine + this.txtAddress.value + ', ';

            if (this.txtCity.value != '' && this.txtCity.value != null)
                this.addressLine = this.addressLine + this.txtCity.value + ', ';

            if (this.ddlState.value != '' && this.ddlState.value != null)
                this.addressLine = this.addressLine + this.ddlState.value + ', ';

            this.addressLine = this.addressLine + this.txtZipcode.value;

            this.geocoder.getLocations(this.addressLine, this.geoCallbackDelegate);
        }

        return false;
    },

    extractDetails: function(node, place) {
        var type = typeof node;

        if (type == "object") {
            for (var key in node) {

                switch (key.toString()) {
                    case "CountryNameCode":
                        place.Country = node[key];
                        break;

                    case "PostalCodeNumber":
                        place.PostalCode = node[key];
                        break;

                    case "LocalityName":
                        place.City = node[key];
                        break;

                    case "ThoroughfareName":
                        place.Address = node[key];
                        break;

                    case "AdministrativeAreaName":
                        place.State = node[key];
                        break;
                }

                this.extractDetails(node[key], place);
            }
        }
    },

    geoCallback: function(response) {

        var myPlace = new Object();

        if (response && response.Status.code == 200) {

            if (response.Placemark.length > 0) {

                var place = response.Placemark[0];

                this.extractDetails(place.AddressDetails, myPlace);

                myPlace.AddressLine = place.address;
                myPlace.Latitude = place.Point.coordinates[1];
                myPlace.Longitude = place.Point.coordinates[0];
                myPlace.Accuracy = place.AddressDetails.Accuracy;
                myPlace.Housing = this.getHousingType();
                myPlace.Obstructed = this.rbObstructed.checked;
                myPlace.Height = this.txtHeight.value;

                Sys.Debug.clearTrace();
                Sys.Debug.traceDump(myPlace);
            }
            else {
                myPlace.Error = "No locations were returned for this address.";
            }
        }
        else {
            myPlace.Error = this.getErrorCode(response.Status.code);
        }

        // wacky hack to show PR as state in map displays
        if (myPlace.Country != 'US' && !myPlace.State)
            myPlace.State = myPlace.Country;

        var jsonPlace = JSON.encode(myPlace);

        __doPostBack('__PAGE', jsonPlace);
    },

    getErrorCode: function(statusCode) {

        var errorCode = "";

        switch (statusCode) {
            case 500:
                errorCode = "Internal server error";
                break;

            case 601:
                errorCode = "Empty address";
                break;

            case 602:
                errorCode = "Unknown address";
                break;

            case 603:
                errorCode = "Unavailable address";
                break;

            case 610:
                errorCode = "Invalid key";
                break;

            case 620:
                errorCode = "Too many queries";
                break;

            default:
                errorCode = "No response from server";
                break;
        }

        return errorCode;
    },

    getHousingType: function() {

        if (this.rbSingle.checked)
            return 'S'
        else if (this.rbMultiple.checked)
            return 'M'
        else if (this.rbOther.checked)
            return 'O'
        else
            return 'S';
    },

    get_txtAddress: function() {
        return this.txtAddress;
    },

    set_txtAddress: function(value) {
        this.txtAddress = value;
    },

    get_txtCity: function() {
        return this.txtCity;
    },

    set_txtCity: function(value) {
        this.txtCity = value;
    },

    get_ddlState: function() {
        return this.ddlState;
    },

    set_ddlState: function(value) {
        this.ddlState = value;
    },

    get_txtZipcode: function() {
        return this.txtZipcode;
    },

    set_txtZipcode: function(value) {
        this.txtZipcode = value;
    },

    get_rbObstructed: function() {
        return this.rblObstructed;
    },

    set_rbObstructed: function(value) {
        this.rbObstructed = value;
    },

    get_rbSingle: function() {
        return this.rbSingle;
    },

    set_rbSingle: function(value) {
        this.rbSingle = value;
        this.rbSingle.onclick = this.getHeightChangeDelegate();
    },

    get_rbMultiple: function() {
        return this.rbMultiple;
    },

    set_rbMultiple: function(value) {
        this.rbMultiple = value;
        this.rbMultiple.onclick = this.getHeightChangeDelegate();
    },

    get_rbOther: function() {
        return this.rbOther;
    },

    set_rbOther: function(value) {
        this.rbOther = value;
        this.rbOther.onclick = this.getHeightChangeDelegate();
    },

    get_txtHeight: function() {
        return this.txtHeight;
    },

    set_spHeight: function(value) {
        this.spHeight = value;
    },

    get_spHeight: function() {
        return this.spHeight;
    },

    set_txtHeight: function(value) {
        this.txtHeight = value;
    },

    get_btnSubmit: function() {
        return this.btnSubmit;
    },

    set_btnSubmit: function(value) {
        this.btnSubmit = value;

        var onSubmitClickDelegate = Function.createDelegate(this, this.onSubmitClick);

        this.btnSubmit.onclick = onSubmitClickDelegate;
    },

    get_btnClear: function() {
        return this.btnClear;
    },

    set_btnClear: function(value) {
        this.btnClear = value;

        var onClearClickDelegate = Function.createDelegate(this, this.onClearClick);

        this.btnClear.onclick = onClearClickDelegate;
    },

    getHeightChangeDelegate: function() {

        if (this.onHeightChangeDelegate == null) {
            this.onHeightChangeDelegate = Function.createDelegate(this, this.onHeightChange);
        }

        return this.onHeightChangeDelegate;
    }
}

AntennaWeb.AddressEntry.registerClass('AntennaWeb.AddressEntry', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
