window.addEvent('domready', function () {
    // ############################################################################################################################
    // EMAIL MODAL 
    // ############################################################################################################################
    // displaying modal for agentlist email functionality.
    var email = $$('a[id^=hypEmailList]');
    email.each(function (element) {
        $(element).addEvent('click', function (e) {
            new Event(e).stop();
            $('hidListID').setProperty('value', $(element).getProperty('name'));
            $('modal-email-message').innerHTML = '<h3 class="alertwarn">Links to the properties in your list will be automatically included in the e-mail</h3>';
            $('txtEmail').setProperty('value', '');
            $('txtSubject').setProperty('value', 'PropertyDrive > ' + $('hidEmailListName' + $(element).getProperty('name')).getProperty('value'));
            $('txtBody').setProperty('value', '');
            var emailfade = new Fx.Tween('modal-email', { duration: 1000 }).start('opacity', 0, 1);

        });
    });

    // displaying modal for propertydetail functionality.
    if ($('emailproperty')) {
        $('emailproperty').addEvent('click', function (e) {
            new Event(e).stop();
            $('modal-email-message').innerHTML = '<h3 class="alertwarn">A link to the property will be automatically included in the e-mail</h3>';
            $('txtEmail').setProperty('value', '');
            $('txtSubject').setProperty('value', 'PropertyDrive > ' + $('hidEmailProperty').getProperty('value'));
            $('txtBody').setProperty('value', '');
            var emailfade = new Fx.Tween('modal-email', { duration: 1000 }).start('opacity', 0, 1);
        });
    }

    // hiding the email modal.
    if ($('modal-email')) {
        $('modal-email').setStyle('visibility', 'hidden');
    }

    // submitting the form and analyzing results. if nothing returned, email was sent. if data returned, display the error.
    if ($('frmEmail')) {
        $('frmEmail').addEvent('submit', function (e) {
            new Event(e).stop();
            this.set('send', {
                onComplete: function EmailResponse(data) {
                    if (data != '') {
                        $('modal-email-message').innerHTML = '<h3 class="alert">' + data + '</h3>';
                    }
                    else {
                        var emailfade = new Fx.Tween('modal-email', { duration: 1000 }).start('opacity', 1, 0);
                    }
                }
            }).send();
        });
    }

    // closing the email modal.
    if ($('modal-email-close')) {
        $('modal-email-close').addEvent('click', function (e) {
            var loginfade = new Fx.Tween('modal-email', { duration: 1000 }).start('opacity', 1, 0);
        });
    }

    // ############################################################################################################################
    // AGENT LIST MODAL
    // ############################################################################################################################
    if ($('modal-agentlist') && !($('google-map-canvas-results'))) {
        listmodal();
        listbox();
    }
});

function listmodal() {
	if ($('ddlListName')) {
        $('ddlListName').addEvent('click', function (e) {
            $('btnRemoteAdd').setProperty('disabled', false);
        });
    }

    if ($('rblListTypeNew')) {
        $('rblListTypeNew').addEvent('click', function (e) {
            if ($('rblListTypeNew').getProperty('checked') == true) {
                $('newlist').setStyle('display', 'block');
            }
        });
    }

    if ($('rblListTypeExisting')) {
        $('rblListTypeExisting').addEvent('click', function (e) {
            if ($('rblListTypeExisting').getProperty('checked') == true) {
                $('newlist').setStyle('display', 'none');
            }
        });
    }

    if ($('frmList')) {
        $('frmList').addEvent('submit', function (e) {
            new Event(e).stop();

            // disabling button and displaying loading icon.
            $('btnRemoteAdd').setProperty('disabled', true);
            $('modal-agentlists-loading').setStyle('display', 'inline');

            this.set('send', {
                onComplete: function addToListResult(data) {
                    if (data != '') {
                        $('modal-agentlists-message').innerHTML = '<h3 class="alert">' + data + '</h3>';
                        $('modal-agentlists-loading').setStyle('display', 'none');
                        $('btnRemoteAdd').setProperty('disabled', false);
                    } else {
                        var agentlistfade = new Fx.Tween('modal-agentlist', { duration: 0 }).start('opacity', 1, 0);
                        $('modal-agentlists-loading').setStyle('display', 'none');
                        if ($('rblListTypeNew').getProperty('checked') == true) {
                            var newAjax = new Request.HTML({
                                url: '/modal/modals.asp?modal=agentlists',
                                method: 'get',
                                update: $('modal-agentlist'),
                                onComplete: listmodal
                            });
                            newAjax.send();
                        }
                    }
                }
            }).send();
        });
    }

    // closes the agentlist modal.
    if ($('modal-agentlist-close')) {
        $('modal-agentlist-close').addEvent('click', function (e) {
            var loginfade = new Fx.Tween('modal-agentlist', { duration: 0 }).start('opacity', 1, 0);
        });
    }
}

function listbox() {
	// adds selected property to list.
    $$('a[id^=hypAddToList]').each(function (element) {
        element.addEvent('click', function (e) {
            e.stop();
            $('modal-agentlists-message').innerHTML = '<h3 class="alertwarn">' + $('hidAddToList' + $(element).getProperty('name')).getProperty('value') + '</h3>';
            $('modal-agentlists-loading').setStyle('display', 'none');
            $('hidPropertyID').setProperty('value', $(element).getProperty('name'));

            if ($('rblListTypeExisting')) {
                $('rblListTypeExisting').set('checked', true);
                $('newlist').setStyle('display', 'none');
            } else {
                $('rblListTypeNew').set('checked', true);
                $('newlist').setStyle('display', 'block');
            }

            var agentlistfade = new Fx.Tween('modal-agentlist', { duration: 0 }).start('opacity', 0, 1);
            $('btnRemoteAdd').setProperty('disabled', false);
        });
    });
    
}
