﻿/// <reference path="ext-base.js" />
/// <reference path="ext-all.js" />

var GetLcationWindow = new Ext.Window({
    title: 'Geo Location',
    width: 385,
    height: 305,
    plain: true,
    layout: 'form',
    modal: true,
    resizable: false,
    items: new Ext.FormPanel({
        style: 'margin: 15px;',
        id: 'geoLocationPanel',
        bodyStyle: 'background-color: transparent;',
        border: false,
        items: [
            GeoLocationFieldset({
                window: 'geoWindow',
                height: 170
            }),
            new Ext.form.Checkbox({
                id: 'geoWindowDontAsk',
                boxLabel: 'Do not ask me again',
                labelSeparator: ''
            })
        ]
    }),

    buttonAlign: 'center',

    buttons: [
        {
            text: 'OK',
            handler: function() {

                if (Ext.getCmp('geoWindowDontAsk').checked) {
                    state.set('dontShowGeoWindow', true);
                }

                if (!GetLcationWindow.monitoring) {
                    state.set('countryState', Ext.getCmp('geoWindowcountryCombo').value);
                    state.set('regionState', Ext.getCmp('geoWindowregionCombo').value);
                    state.set('cityState', Ext.getCmp('geoWindowcityCombo').value);
                    
                    Ext.getDom('changedCountry').value = Ext.getCmp('geoWindowcountryCombo').value;
                    if (Ext.getCmp('geoWindowregionCombo').disabled == false)
                    {
                        Ext.getDom('changedRegion').value = Ext.getCmp('geoWindowregionCombo').value;
                    }
                    else
                    {
                        Ext.getDom('changedRegion').value = '';
                    }
                    
                    if (Ext.getCmp('geoWindowcityCombo').disabled == false)
                    {
                        Ext.getDom('changedCity').value = Ext.getCmp('geoWindowcityCombo').value;
                    }
                    else
                    {
                        Ext.getDom('changedCity').value = '';
                    }
                                        
                    document.getElementsByTagName('FORM')[0].submit();
                } else {
                    GetLcationWindow.monitoringSettings.type = 'saveMonitoringInfo';
                    GetLcationWindow.monitoringSettings.gmtOffset = new Date().getGMTOffset(true);
                    GetLcationWindow.monitoringSettings.country = Ext.getCmp('geoWindowcountryCombo').value;
                    GetLcationWindow.monitoringSettings.region = Ext.getCmp('geoWindowregionCombo').value;
                    GetLcationWindow.monitoringSettings.city = Ext.getCmp('geoWindowcityCombo').value;
                
                    Ext.Ajax.request({
                        url: 'accountDataHandler.ashx',
                        params: GetLcationWindow.monitoringSettings,
                        success: function(result) {
                            if (Ext.decode(result.responseText).success) {
                                GetLcationWindow.hide();
                            } else {
                                Ext.Msg.alert('Error', 'Cannot save monitoring settings.');
                            }
                        },
                        failure: function() { Ext.Msg.alert('Error', 'Cannot save monitoring setttings.'); }
                    });
                }
            }
        }
    ],

    listeners: {
        show: function() {

            var countryState = state.get('countryState', 'US');
            var regionState = state.get('regionState', '');
            var cityState = state.get('cityState', '');

            var updateGeoComntrols = function() {
                Ext.getCmp('geoWindowcountryCombo').setValue(countryState);
                var regionCombo = Ext.getCmp('geoWindowregionCombo');
                var cityCombo = Ext.getCmp('geoWindowcityCombo');

                if (countryState == '') {
                    settingsPanel.updateState = true;
                    settingsPanel.updateTitle();
                }
                else {
                    regionCombo.emptyText = 'Loading...';
                    regionCombo.reset();

                    //regionCombo.getEl().up('.x-form-item').down('label').dom.innerHTML = record.data.region + ':';
                    regionCombo.store.on('load', function() {

                        regionCombo.emptyText = 'All regions within this country';
                        if (regionCombo.store.getCount() == 0)
                            regionCombo.setDisabled(true);
                        if (regionState == '') {
                            //regionCombo.setDisabled(true);
                            regionCombo.reset();
                        }
                        else
                            regionCombo.setValue(regionState);

                    }, regionCombo, { single: true });

                    regionCombo.store.load({ params: { country: countryState} });
                }

                if (regionState != '') {
                    cityCombo.emptyText = 'Loading...';
                    cityCombo.reset();

                    //regionCombo.getEl().up('.x-form-item').down('label').dom.innerHTML = record.data.region + ':';
                    cityCombo.store.on('load', function() {
                        cityCombo.emptyText = 'All cities within this region';

                        if (cityCombo.store.getCount() == 0)
                            cityCombo.setDisabled(true);

                        if (cityCombo == '') {
                            cityCombo.reset();
                        }
                        else
                            cityCombo.setValue(cityState);

                    }, cityCombo, { single: true });

                    cityCombo.store.load({ params: { region: regionState} });
                }
            };

            if (this.monitoring) {
                Ext.Ajax.request({
                    url: 'accountDataHandler.ashx',
                    params: { type: 'monitoringSettings', gmtOffset: new Date().getGMTOffset(true) },
                    failure: function() {
                        Ext.Msg.alert('Error', 'Cannot load the settings.');
                    },
                    success: function(result) {
                        result = Ext.decode(result.responseText);

                        Ext.Msg.hide();

                        if (result.success) {
                            var monitoringSettings = result.extraData;
                            countryState = monitoringSettings.country;
                            regionState = monitoringSettings.region;
                            cityState = monitoringSettings.city;

                            GetLcationWindow.monitoringSettings = monitoringSettings;
                            updateGeoComntrols();
                        }
                    }
                });
            } else {
                updateGeoComntrols();
            }
        }
    }
});

var showFunction = GetLcationWindow.show;

GetLcationWindow.show = function(type) {
    if (type == 'monitoring') {
        GetLcationWindow.monitoring = true;
    }
    showFunction.createDelegate(GetLcationWindow)();
}