﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("ThreeIreland");

ThreeIreland.AutoAddress = function(threeIrelandMap)
{
    this._threeIrelandMap = threeIrelandMap;
    this.latestSearch = null;
    InitializeCoordinatesDataStructures();
}

function InitializeCoordinatesDataStructures()
{   
    //ADD THE COUNTIES
    counties = new Array();
    counties.push(NewCounty("Cork", -9.0098, 51.9216));
    counties.push(NewCounty("Kerry", -10.4181, 52.1261));
    counties.push(NewCounty("Limerick", -8.76149, 52.5174));
    counties.push(NewCounty("Clare", -9.11716, 52.8621));
    counties.push(NewCounty("Mayo", -9.3523, 53.9106));
    counties.push(NewCounty("Galway", -9.07896, 53.341));
    counties.push(NewCounty("Leitrim", -8.00377, 54.1405));
    counties.push(NewCounty("Sligo", -8.64608, 54.1921));
    counties.push(NewCounty("Donegal", -7.87119, 54.922));
    counties.push(NewCounty("Waterford", -7.55707, 52.1508));
    counties.push(NewCounty("Wexford", -6.58343, 52.4603));
    counties.push(NewCounty("Wicklow", -6.3961, 52.9566));
    counties.push(NewCounty("Kildare", -6.81924, 53.1535));
    counties.push(NewCounty("Kilkenny", -7.29334, 52.569));
    counties.push(NewCounty("Tipperary", -7.92334, 52.6848));
    counties.push(NewCounty("Offaly", -7.81382, 53.1375));
    counties.push(NewCounty("Longford", -7.70597, 53.7327));
    counties.push(NewCounty("Monaghan", -6.94198, 54.1617));
    counties.push(NewCounty("Louth", -6.40168, 53.9065));
    counties.push(NewCounty("Roscommon", -8.35022, 53.6976));
    counties.push(NewCounty("Meath", -6.77952, 53.6484));
    counties.push(NewCounty("Westmeath", -7.46481, 53.5586));
    counties.push(NewCounty("Carlow", -6.81015, 52.6909));
    counties.push(NewCounty("Cavan", -7.40836, 54.0356));
    counties.push(NewCounty("Laois", -7.33102, 52.9973));
    counties.push(NewCounty("Dublin", -6.29321, 53.4065));

    //ADD THE POSTCODES
    postcodes = new Array();   
    postcodes.push(NewPostCode("1",-6.2622,53.3527));
    postcodes.push(NewPostCode("2",-6.25657,53.3391));
    postcodes.push(NewPostCode("3",-6.2094,53.3629));
    postcodes.push(NewPostCode("4",-6.21456,53.3228));
    postcodes.push(NewPostCode("5",-6.17142,53.3808));
    postcodes.push(NewPostCode("6",-6.26544,53.3182));
    postcodes.push(NewPostCode("6W",-6.31543,53.308));
    postcodes.push(NewPostCode("7",-6.30828,53.3605));
    postcodes.push(NewPostCode("8",-6.31775,53.3507));
    postcodes.push(NewPostCode("9",-6.25049,53.3844));
    postcodes.push(NewPostCode("10",-6.35641,53.3391));
    postcodes.push(NewPostCode("11",-6.32327,53.3947));
    postcodes.push(NewPostCode("12",-6.33706,53.3132));
    postcodes.push(NewPostCode("13",-6.13945,53.4012));
    postcodes.push(NewPostCode("14",-6.26793,53.2984));
    postcodes.push(NewPostCode("15",-6.38563,53.3887));
    postcodes.push(NewPostCode("16",-6.28062,53.2748));
    postcodes.push(NewPostCode("17",-6.2024,53.407));
    postcodes.push(NewPostCode("18",-6.21192,53.2668));
    postcodes.push(NewPostCode("20",-6.3765,53.3563));
    postcodes.push(NewPostCode("22",-6.40111,53.3228));
    postcodes.push(NewPostCode("24",-6.37934,53.2851));
}

//NEW COUNTY OBJECT
function NewCounty(_name, _Centroid_Long, _Centroid_Lat)
{
    var county= new Object();
    county.Name = _name;
    county.Lon = _Centroid_Long;
    county.Lat = _Centroid_Lat;
    return county;
}

//NEW POSTCODE OBJECT    //NEW COUNTY

function NewPostCode(_code, _Centroid_Long, _Centroid_Lat)
{
    var postcode = new Object();
    postcode.Code = _code;
    postcode.Lon = _Centroid_Long;
    postcode.Lat = _Centroid_Lat;
    return postcode;
}

ThreeIreland.AutoAddress.prototype =
{
    //
    // AutoAddressControl return function
    //
    onResult: function(result)
    {
        //Save the search in the object latestSearch in order to access to it when clicking ADD SEARCH to map
        this.latestSearch = result;
        //Help labels = ""
        document.getElementById('tdSearchHelp').innerText = " ";
        document.getElementById('tdOrganisationHelp').innerText = " ";
        //Enable add pin button
        if (document.getElementById('buttonRemoveSearchPin').disabled)
            document.getElementById('buttonAddSearchPin').disabled = false;
        //Get the address of the result search
        var address = result.Rows[0].Value;
        //Detect the browser
        var browser = Utility.DetectBrowser();
        if (result.GeoDirectoryIDTypeString.toUpperCase() != "ORGANISATION")
        {
            //If it is a county or district in Dublin, we retieve coordinates from the corresponding array
            if (result.GeoDirectoryIDTypeString.toUpperCase() == "COUNTY")
            {
                var selectedCountyName = result.Rows[0].Value.substring(3, result.Rows[0].Value.length);
                for (var i = 0; i < counties.length; i++)
                {
                    if (counties[i].Name == selectedCountyName)
                    {
                        this.latestSearch.Rows[6].Value = counties[i].Lat; //Lat
                        this.latestSearch.Rows[7].Value = counties[i].Lon; //Lon
                    }
                }
            }
            else if (result.GeoDirectoryIDTypeString.toUpperCase() == "POSTTOWN")
            {
                var selectedPostCode = result.Rows[0].Value.substring(7, result.Rows[0].Value.length).toUpperCase();
                for (var i = 0; i < postcodes.length; i++)
                {
                    if (postcodes[i].Code == selectedPostCode)
                    {
                        this.latestSearch.Rows[6].Value = postcodes[i].Lat; //Lat
                        this.latestSearch.Rows[7].Value = postcodes[i].Lon; //Lon
                    }
                }
            }

            //IF GeoDirectoryIDTypeString == "STREET"
            if (browser == "moz")
                document.getElementById('textSearch').value = address;
            else
                document.getElementById('textSearch').innerText = address;
            document.getElementById('tdSearchHelp').innerText = " ";
            document.getElementById('textSearchCommercial').disabled = false;
            document.getElementById('tdCurrentAddressOrganisationsCount').innerText = "(" + result.OrganisationsResultCount + " commercial properties in area)";
            //help text organisation
            if ((result.OrganisationsResultCount < 150) && (result.OrganisationsResultCount > 0))
            {
                //press arrow
                document.getElementById('tdOrganisationHelp').innerText = "Press down arrow key.";
                if (browser == "moz")
                    document.getElementById('textSearchCommercial').value = " ";
                else
                    document.getElementById('textSearchCommercial').innerText = " ";
                document.getElementById('txtFilterResultCount').innerText = " ";
                document.getElementById('textSearchCommercial').focus();
            }
            else
            {
                document.getElementById('textSearchCommercial').focus();
            }
            if (result.OrganisationsResultCount == 0)
            {
                //press arrow
                document.getElementById('textSearchCommercial').disabled = true;
            }
            //Clean the text in comercial search (because in case there has been a previous search it can the user can be confused if it continues being shown)
            if (browser == "moz")
                document.getElementById('textSearchCommercial').value = " ";
            else
                document.getElementById('textSearchCommercial').innerText = " ";
            var list = document.getElementById('lstOrganisationsResults');
            list.innerHTML = "<OPTION></OPTION>";
            list.style.visibility = "hidden";
            this.AddPinToMap();
        }
        else
        {
            //IF GeoDirectoryIDTypeString == "ORGANISATION"
            var browser = Utility.DetectBrowser();
            if (browser == "moz")
                document.getElementById('textSearchCommercial').value = address;
            else
                document.getElementById('textSearchCommercial').innerText = address;
            document.getElementById('textSearchCommercial').disabled = false;
            document.getElementById('tdCurrentAddressOrganisationsCount').innerText = " ";
            this.AddPinToMap();
        }
    },

    //
    // OrganisationResult event
    //
    onOrganisationResult: function(result)
    {
        // It is never called, organisation result is received the function
        // called is onResult(result) --> See it above.

    },

    //
    // LookupResult event
    //
    onLookupResult: function(result)
    {
        document.getElementById('tdOrganisationHelp').innerText = " ";

        if (result.ResultCount == 0)
        {
            //no result
            document.getElementById('tdSearchHelp').innerText = " ";
        }
        else
        {
            if (result.ResultCount > 150)
            {
                //too many results 
                document.getElementById('tdSearchHelp').innerText = " ";
            }
            else
            {
                if (result.ResultCount > 10)
                {
                    //press arrow
                    document.getElementById('tdSearchHelp').innerText = "Press down arrow key to see all matches.";
                }
            }
        }

        return;
    },

    //
    // OrganisationLookupResult event
    //
    onOrganisationLookupResult: function(result)
    {
        if (result.ResultCount == 0)
        {
            //no result
            document.getElementById('tdOrganisationHelp').innerText = " ";
        }
        else
        {
            if (result.ResultCount > 150)
            {
                //too many results 
                document.getElementById('tdOrganisationHelp').innerText = " ";
            }
            else
            {
                if (result.ResultCount > 10)
                {
                    //press arrow
                    document.getElementById('tdOrganisationHelp').innerText = "Press down arrow key.";
                }
                var list = document.getElementById('lstOrganisationsResults');
                list.style.visibility = "visible";
            }
        }
        return;
    },

    //**************************************************************************************************************************************

    //
    // Automatically add pin when address (residential or commercial) is selected. 
    // Delete previous pin on map if it exists. 
    //
    AddPinToMap: function()
    {
        //Determine the address (description of the pin --> txt to show in the info box)
        var address = "";
        var lat = this.latestSearch.Rows[6].Value;
        var lon = this.latestSearch.Rows[7].Value;
        var latS = new Number(lat);
        var lonS = new Number(lon)
        for (x = 0; x <= 5; x++)
        {
            if (this.latestSearch.Rows[x].Value != "")
                address += this.latestSearch.Rows[x].Value + "<br/>";
        }
        address += "<br/><DIV>Latitude: " + latS.toFixed(6) +
                    "</DIV><DIV>Longitude: " + lonS.toFixed(6) + "</DIV>&#160;";
        //Add new pin to the map
        this._threeIrelandMap.AddSearchPin(lat, //LATITUDE
                                           lon,  //LONGITUDE
                                          address + "&#160;",                          //ADDRESS
                                          true);                            //ZOOM TO PIN
    },

    //
    // "Add Search" button_click EVENT
    //
    AddSearchPin: function()
    {
        //Enable remove btn + Disable add button
        document.getElementById('buttonRemoveSearchPin').disabled = false;
        document.getElementById('buttonAddSearchPin').disabled = true;
        //Determine the address (description of the pin --> txt to show in the info box)
        var address = "";
        for (x = 0; x <= 5; x++)
        {
            if (this.latestSearch.Rows[x].Value != "")
                address += this.latestSearch.Rows[x].Value + "<br/>";
        }
        //Add new pin to the map
        this._threeIrelandMap.AddSearchPin(this.latestSearch.Rows[6].Value, //LATITUDE
                                          this.latestSearch.Rows[7].Value,  //LONGITUDE
                                          address);                         //ADDRESS
    }
}

ThreeIreland.AutoAddress.registerClass('ThreeIreland.AutoAddress', null, Sys.IDisposable);

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

