﻿
var alertSettingsForm = getAlertSettingsForm('registration');
var alertButtons = alertSettingsForm.buttons;
var saveAlerts = alertSettingsForm.buttons[0].initialConfig.handler;

alertSettingsForm.buttons = null;
alertButtons.length = 1;
alertButtons[0].text = 'Activate & Test Email Alerts';
alertButtons[0].handler = function() {

    state.set('countryState', Ext.getCmp('registerWindowcountryCombo').value);
    state.set('regionState', Ext.getCmp('registerWindowregionCombo').value);
    state.set('cityState', Ext.getCmp('registerWindowcityCombo').value);

    var registerWindow = Ext.getCmp('registrationWindow');

    Ext.Msg.wait('Registering...', 'Please wait...');
    registerWindow.getForm().submit({
        success: function(f, a) {

            saveAlerts();
            //Hide progress bar and settings window
            Ext.Msg.alert('Information', 
                            'Settings were successfully saved.', 
                            function(){
                                AlertSettingsWindow.hide();
                            });
        },
        failure: function(f, a) {
            Ext.Msg.alert('Error', 'Email address already registered.');
        }
    });
}

var AlertSettingsWindow = new Ext.Window({
    id: 'AlertSettingsWindow',
    title: 'Alerts Settings & Activation',
    width: 385,
    height: 450,
    plain: true,
    closeAction: 'hide',
    layout: 'form',
    modal: true,
    resizable: false,
    buttonAlign: 'center',
    items: alertSettingsForm,
    buttons: alertButtons
});

var RegisterWindow = new Ext.Window({
    title: 'Registration & Geo Targeting Settings',
    width: 385,
    height: 450,
    plain: true,
    closeAction: 'hide',
    layout: 'form',
    modal: true,
    resizable: false,
    buttonAlign: 'center',
    items: new Ext.FormPanel({
        id: 'registrationWindow',
        url: 'sign_up.ashx?signup=true&requestId=',
        style: 'margin: 15px;',
        bodyStyle: 'background-color: transparent;',
        border: false,
        items: [
            {
                xtype: 'fieldset',
                id: 'registrationInformation',
                title: 'Registration Information',
                layout: 'form',
                labelWidth: 155,
                width: 335,
                height: 170,
                defaults: {
                    width: 150,
                    labelStyle: 'height: 30px; white-space: nowrap;'
                },
                items: [{
                    xtype: 'hidden',
                    name: 'competitorsDomain'
                }, {
                    xtype: 'textfield',
                    id: 'firstName',
                    fieldLabel: 'What\'s your name',
                    name: 'firstName',
                    labelSeparator: '?',
                    allowBlank: false,
                    bodyCls: 'dddd'
                }, {
                    xtype: 'textfield',
                    id: 'email',
                    fieldLabel: 'What\'s your email address',
                    name: 'email',
                    allowBlank: false,
                    labelSeparator: '?',
                    vtype: 'email'
                }, {
                    xtype: 'textfield',
                    id: 'domainName',
                    fieldLabel: 'Your domain name is ',
                    name: 'homesite',
                    value: 'http://',
                    allowBlank: false,
                    labelSeparator: '',
                    labelStyle: 'white-space: nowrap;',
                    regex: domainValidator
                }, {
                    xtype: 'label',
                    html: '<span style="font-style: italic; font-size: 12px;">We need to know your domain name in order to compare it to your PPC competitors.</span>'
                }
                ]
            },
            GeoLocationFieldset({
                window: 'registerWindow',
                height: 170,
                title: 'Geo-Target Select where to research and monitor'
            })
        ]
    }),

    buttons: [
        {
            text: 'Next',
            handler: function() {

                /*if (getCookie('registerDateUtc')) {

                    Ext.MessageBox.alert('You are Already Signed Up',
                        'You are already signed up for a FREE Trial.<br>' +
                        'Please just <a href="javascript:loginHideError(\'KeywordResearchTool.aspx\')" style="color: #c02206;">log in here</a> to access your account.');

                    return;
                }*/

                var registerWindow = Ext.getCmp('registrationWindow');

                if (!ValidateForm(registerWindow)) {
                    return;
                }

                var competitorsDomain = '';
                if (Ext.getDom('competitorsDomain')) {

                    competitorsDomain = Ext.getDom('competitorsDomain').value;

                    if (competitorsDomain.replace(/^http:\/\//i, '').replace(/^www./i, '').replace(/\/$/i, '') ==
                        registerWindow.getForm().findField('homesite').getValue().replace(/^http:\/\//i, '').replace(/^www./i, '').replace(/\/$/i, '')) {

                        Ext.Msg.alert('Error', 'Your domain and PPC competitor\'s domain should not be the same.');
                        return;
                    }

                    registerWindow.getForm().findField('competitorsDomain').setValue(competitorsDomain);
                }
                
                AlertSettingsWindow.show();
                RegisterWindow.hide();
            }
        }
    ],

    listeners: {
        show: function() {

            var countryState = state.get('countryState', 'US');
            var regionState = state.get('regionState', '');
            var cityState = state.get('cityState', '');

            var updateGeoComntrols = function() {
                Ext.getCmp('registerWindowcountryCombo').setValue(countryState);
                var regionCombo = Ext.getCmp('registerWindowregionCombo');
                var cityCombo = Ext.getCmp('registerWindowcityCombo');

                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} });
                }
            };

            setTimeout(function() {
                Ext.getCmp('firstName').focus();
                Ext.getCmp('domainName').clearInvalid();
                Ext.getCmp('firstName').clearInvalid();
                updateGeoComntrols();
            }, 1);
        }
    }
});
