﻿///////////////////////////////////////////////////////////////////////////
// Georgetown.County.EditInstitution namespace
///////////////////////////////////////////////////////////////////////////
Georgetown.County.EditInstitution = {};
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// INSTANCE VARIABLES
///////////////////////////////////////////////////////////////////////////
Georgetown.County.EditInstitution.Location = 
{ 
    Address : null, 
    City : null, 
    State : null, 
    Zip : null, 
    Latitude : null, 
    Longitude :  null,
    AddressList : null,
    Row : null,
    FullAddress : null
};
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////

/// <summary>
/// Google Maps Api geocoder callback function
/// </summary>
Georgetown.County.EditInstitution.GeocoderCallback = function( response )
{
    var ns = Georgetown.County.EditInstitution;
    var location = ns.Location;
    var place;
    var message;

    try
    {
        if ( parseInt( response.Status.code ) == 200 )
        {
            if ( response.Placemark.length > 1 )
            {
                location.AddressList.options.length = 0;
                location.Row.style.display = "";
                for ( var i = 0; i < response.Placemark.length; i++ )
                {
				    var options = location.AddressList.options;
                    var country;
                    var admin;
				    var value;
				    
                    place = response.Placemark[ i ];
                    country = place.AddressDetails.Country;
                    admin = country.AdministrativeArea;
                    
                    value = "{ " 
                        + "Address : \"" + admin.Locality.Thoroughfare.ThoroughfareName + "\", "
                        + "City : \"" + admin.Locality.LocalityName + "\", "
                        + "State : \"" + admin.AdministrativeAreaName + "\", "                       
                        + "Zip : ";
                    
                    if ( admin.Locality.PostalCode )
                    {
                        value += admin.Locality.PostalCode.PostalCodeNumber;
                    }
                    else value += "null";
                        
                    value += ", " 
                        + "Latitude: \"" + place.Point.coordinates[ 1 ] + "\", "
                        + "Longitude: \"" + place.Point.coordinates[ 0 ] + "\""
                        + " }";
				    options[ options.length ] = new Option( place.address, value );
                }
            }
            else
            {
                // retrieve location object
                place = response.Placemark[ 0 ];

                message = "The following address was found based on the data provided.\n\n";
                message += place.address + "\n\n";
                message += "Click \"OK\" if this is the correct address, otherwise click \"Cancel\"";

                if ( confirm( message ) ) ns.PopulateForm( place );
            }
        }
        else 
        {
            var exception = { message: "Unable to successfully retrieve the geocode data." };
            
            throw exception;
        }
    }
    catch ( e )
    {
        alert( "The following error(s) occurred:\n\n" + e.message );
    }
}

/// <summary>
/// Retrieve the latitude and longitude coordinates using the 
/// Google Maps Api.
/// </summary>
/// <param name="address">A string containing the id of the address form field.</param>
/// <param name="city">A string containing the id of the city form field.</param>
/// <param name="state">A string containing the id of the state form field.</param>
/// <param name="zip">A string containing the id of the zip form field.</param>
/// <param name="latitude">A string containing the id of the latitude form field.</param>
/// <param name="longitude">A string containing the id of the longitude form field.</param>
/// <param name="addressList">A string containing the id of the address list form field.</param>
/// <param name="htrAddressList">A string containing the id of the table row containing address list form field.</param>
/// <returns>A boolean value of false</returns>
Georgetown.County.EditInstitution.GetCoordinates = function()
{
    var ns = Georgetown.County.EditInstitution;
    var location = ns.Location;
    var geoCoder = new GClientGeocoder();
    
    location.Address = document.getElementById( arguments[ 0 ] );
    location.City = document.getElementById( arguments[ 1 ] );
    location.State = document.getElementById( arguments[ 2 ] );
    location.Zip = document.getElementById( arguments[ 3 ] );
    location.Latitude = document.getElementById( arguments[ 4 ] );
    location.Longitude = document.getElementById( arguments[ 5 ] );
    location.AddressList = document.getElementById( arguments[ 6 ] );
    location.Row = document.getElementById( arguments[ 7 ] );
    
    // build address
    location.FullAddress = location.Address.value + ", " + location.City.value + 
        ", " + location.State.value + " " + location.Zip.value;
    // location geocode information    
    geoCoder.getLocations( location.FullAddress, ns.GeocoderCallback );
    
    return false;
}

Georgetown.County.EditInstitution.PopulateForm = function( place )
{
    var ns = Georgetown.County.EditInstitution;
    var location = ns.Location;
    var point;
    
    if ( arguments.length == 2 )
    {
        var o = arguments[ 1 ];
        
        if ( location.Address ) location.Address.value = o.Address;
        if ( location.City ) location.City.value = o.City;
        if ( location.State ) location.State.value = o.State;
        if ( location.Zip ) location.Zip.value = o.Zip;
        if ( location.Latitude ) location.Latitude.value = o.Latitude;
        if ( location.Longitude ) location.Longitude.value = o.Longitude;
    }
    else
    {
        var country = place.AddressDetails.Country;
        var admin = country.AdministrativeArea;

        // retrieve the latitude and longitude
        point = new GLatLng( place.Point.coordinates[ 1 ], place.Point.coordinates[ 0 ] );
        
        if ( location.Address ) location.Address.value = admin.Locality.Thoroughfare.ThoroughfareName;
        if ( location.City ) location.City.value = admin.Locality.LocalityName;
        if ( location.State ) location.State.value = admin.AdministrativeAreaName;
        if ( location.Zip ) location.Zip.value = admin.Locality.PostalCode.PostalCodeNumber;
        if ( location.Latitude ) location.Latitude.value = point.lat();
        if ( location.Longitude ) location.Longitude.value = point.lng();
    }
}

Georgetown.County.EditInstitution.SelectLocation = function( place )
{
    var ns = Georgetown.County.EditInstitution;
    
    if ( place.length == 0 ) alert( "Please select an address first." );
    else 
    {
        ns.PopulateForm( null, eval( "(" + place + ")" ) );
        ns.Location.Row.style.display = "none";
    }
}

Georgetown.County.EditInstitution.ValidateCategories = function( sender, e )
{
	var isValid;
	
	try
	{
		var controlId = sender.getAttribute( "ControlID" );
		var treeView = $find( controlId );
		var checkedNodes = treeView.get_checkedNodes();
		
		isValid = ( checkedNodes.length >= 1 );
	}
	catch ( e )
	{
		isValid = false;
	}
	finally
	{
		e.IsValid = isValid;
	}
	
	return;
}
///////////////////////////////////////////////////////////////////////////
