﻿
function getAlertSettingsForm(type) {

    var settingsForm = new Ext.form.FormPanel({
        url: 'alertsSettings.ashx?save=true',
        region: 'center',
        width: 380,
        border: false,
        margins: '3 0 3 3',
        cmargins: '3 3 3 3',
        style: 'padding-top: 15px; padding-left: 15px;',
        bodyStyle: 'background-color: transparent;',
        defaultType: 'checkbox',
        defaults: {
            ctCls: type == 'registration' ? 'alert-items-margins' : ''
        },
        labelWidth: 300,
        items: [{
            xtype: 'label',
            html: type == 'registration' ?
                'Please select when you want to be alerted<br><br>' :
                'Please select which reports you wish to have<br>emailed to you:<br><br>',
            style: 'font-size:12px; font-weight: bold;'
        }, {
            hideLabel: true,
            boxLabel: 'Keywords where your PPC competitors have higher page rank',
            name: 'beating'
        }, {
            hideLabel: true,
            height: 40,
            boxLabel: 'Keywords your PPC competitors stopped targeting<br>&nbsp;&nbsp;&nbsp;&nbsp;and Ads they stopped using',
            name: 'notFound'
        }, {
            hideLabel: true,
            boxLabel: 'Newly added keywords',
            name: 'newKeywords'
        }, {
            hideLabel: true,
            boxLabel: 'Ad changes',
            name: 'foundAndUsed'
        }, {
            hideLabel: true,
            boxLabel: 'Average position changes among PPC competitors',
            name: 'competitor'
        }, {
            hideLabel: true,
            boxLabel: 'New PPC competitors',
            name: 'newCompetitors',
            listeners: {
                check: function(checkbox, checked) {
                    if (checked) {
                        settingsForm.getForm().findField('newCompetitorsOverlap').enable();
                    } else {
                        settingsForm.getForm().findField('newCompetitorsOverlap').disable();
                    }
                }
            }
        }, {
            name: 'newCompetitorsOverlap',
            disabled: true,
            xtype: 'numberfield',
            labelSeparator: '',
            width: 30,
            allowDecimals: false,
            allowNegative: false,
            allowBlank: false,
            value: '',
            minValue: 0,
            maxValue: 100,
            fieldLabel: '&nbsp;&nbsp;&nbsp;&nbsp;Send new PPC competitors with overlap more than'
        }
        ],
        listeners: {
            beforerender: function() {
                Ext.Ajax.request({
                    url: 'alertsSettings.ashx',
                    params: { getSettings: true },
                    success: function(result) {
                        //Ext.Msg.hide();
                        var alertSettings = Ext.decode(result.responseText);
                        settingsForm.getForm().findField('beating').setValue(alertSettings.SendBeating);
                        settingsForm.getForm().findField('notFound').setValue(alertSettings.SendDeletedKwrdsAds);
                        settingsForm.getForm().findField('newKeywords').setValue(alertSettings.SendNewKeywords);
                        settingsForm.getForm().findField('foundAndUsed').setValue(alertSettings.SendFoundOrChangedAds);
                        settingsForm.getForm().findField('competitor').setValue(alertSettings.SendCompetitorChangedPos);
                        settingsForm.getForm().findField('newCompetitors').setValue(alertSettings.SendNewCompetitors);
                        settingsForm.getForm().findField('newCompetitorsOverlap').setValue(alertSettings.NewCompetitorsOverlap);

                        var label = document.createElement('span');
                        label.innerHTML = '&nbsp;%';
                        settingsForm.getForm().findField('newCompetitorsOverlap').container.dom.appendChild(label);
                    },
                    failure: function() { Ext.Msg.alert('Error', 'Cannot load alerts settings.'); }
                });
            }
        },
        monitorValid: true,
        monitorPoll: 250,
        buttons: [{
            text: 'Save',
            //formBind: true,
            handler: function() {

                if (!ValidateForm(settingsForm)) {
                    return;
                }
                Ext.Msg.wait('Saving...', 'Please wait')
                settingsForm.getForm().submit({
                    success: function(f, a) {
                        if (type == 'registration') {
                            var countryState = state.get('countryState', 'US');
                            var regionState = state.get('regionState', '');
                            var cityState = state.get('cityState', '');

                            // research page
                            if (Ext.getDom('searchText')) {

                                var domain = document.getElementById('searchText').value;

                                searchHref = 'default.aspx?searchText=' + domain + '&searchPostback=true&google=' +
                                state.get('googleState', false) + '&yahoo=' + state.get('yahooState', false) +
                                '&reportType=' + state.get('reportTypeFullState', false) + '&country=' +
                                countryState + '&region=' + regionState + '&city=' + cityState + '&userId=' + getCookie('userId');

                                window.location.href = searchHref;
                            } else { // sign up page
                                if (RegisterWindow.onDone) {
                                    RegisterWindow.onDone();
                                }
                            }
                        } else {
                            Ext.getCmp('settingsWindow').hide();
                            Ext.Msg.alert('Information', 'Settings were successfully saved.');
                        }
                    },
                    failure: function(f, a) {
                        Ext.Msg.alert('Error', 'Can not save the settings.');
                    }
                });
            }
        }, {
            text: 'Cancel',
            handler: function() {
                Ext.getCmp('settingsWindow').hide();
            }
}]
        });
        
    return settingsForm;
}

function showAlertsSettings() {

    var settingsForm = getAlertSettingsForm('monitoring');

    var settingsWindow = new Ext.Window({
    title: 'Email Alert Preferences',
        id: 'settingsWindow',
        closable: true,
        width: 405,
        height: 340,
        plain: true,
        layout: 'border',
        modal: true,
        resizable: false,
        items: [settingsForm]
    });

    settingsWindow.show();       
}