﻿if ( typeof Avencia == 'undefined' ) {
	Ext.ns('Avencia.Controls');
}

Avencia.Controls.UserRegistration = function(config) {
    this.formPanel = {};
    this.window = {};

    var successFunction = function(form, action) {
        var responseObject = eval('(' + action.response.responseText + ')');

        if (responseObject.redirect) {
            document.location = responseObject.redirect;
        }
        if (responseObject.data == "tandcrequired") {
            document.location = "UserTermsAndConditions.aspx";
        }
    };

    var failureFunction = function(form, action) {
        if (action.response) {
            var responseObject = eval('(' + action.response.responseText + ')');
            if (responseObject.data.indexOf('email address') > -1) {
                formPanel.findById('email').markInvalid(responseObject.data);
            } else if (responseObject.data.indexOf('ID already exists') > -1) {
                formPanel.findById('userId').markInvalid(responseObject.data);
            }
        } else {
            // invalid form
            return;
        }
    };


    Ext.apply(Ext.form.VTypes, {
        emailMask: /[a-z0-9_\.\+\-@]/i,
        email: function(email) {
            var mailRegex = /[a-z0-9_\.\+\-@]+@[a-z0-9_\.\+\-@]+\.[a-z0-9_\.\+\-@]+/i;
            return mailRegex.test(email)
        }
    });

    var formPanel = new Ext.form.FormPanel({
        formId: 'frmRegistration',
        autoScroll: true,
        frame: false,
        border: false,
        labelWidth: 150,
        labelPad: 10,
        renderTo: Ext.get('CenterPanel'),
        defaultType: 'textfield',
        bodyStyle: 'padding:5px;',
        defaults: {
            width: 200,
            msgTarget: 'under',
            invalidText: 'Required Field'
        },

        items: [{
            fieldLabel: 'User Id *',
            id: 'userId',
            allowBlank: false
        }, {
            fieldLabel: 'Password *',
            id: 'password',
            inputType: 'password',
            allowBlank: false
        }, {
            fieldLabel: 'Re-Enter Password *',
            id: 'password2',
            inputType: 'password',
            allowBlank: false,
            validator: function(myValue) {
                var otherPass = Ext.get('password').getValue();
                if (myValue == otherPass) {
                    return true;
                }
                return false;
            },
            invalidText: 'Passwords do not match'
        }, {
            fieldLabel: 'First Name *',
            id: 'firstName',
            allowBlank: false
        }, {
            fieldLabel: 'Last Name *',
            id: 'lastName',
            allowBlank: false
        }, {
            fieldLabel: 'Title',
            id: 'title'
        }, {
            fieldLabel: 'URL',
            id: 'userUrl'
        }, {
            fieldLabel: 'Email *',
            allowBlank: false,
            validationEvent: 'onblur',
            id: 'email',
            vtype: 'email'
        }, {
            fieldLabel: 'Organization *',
            id: 'organization',
            allowBlank: false
        },

                new Ext.form.ComboBox({
                    id: 'cmbOrganizationType',
                    hiddenName: 'organizationType',
                    fieldLabel: 'Organization Type *',
                    allowBlank: false,
                    typeAhead: true,
                    triggerAction: 'all',
                    mode: 'local',
                    forceSelection: true,
                    store: orgTypeStore, //declared in codebehind
                    displayField: 'display',
                    valueField: 'value'
                }),

            {
                fieldLabel: 'Organization Mission',
                id: 'organizationMission'
            }, {
                fieldLabel: 'Address',
                id: 'address1'
            }, {
                fieldLabel: 'Address (contd.)',
                id: 'address2'
            }, {
                fieldLabel: 'City',
                id: 'city'
            },
                new Ext.form.ComboBox({
                    id: 'cmbState',
                    hiddenName: 'state',
                    fieldLabel: 'State',
                    typeAhead: true,
                    triggerAction: 'all',
                    lazyRender: true,
                    forceSelection: true,
                    mode: 'local',
                    store: new Ext.data.SimpleStore({
                        id: 0,
                        fields: [
                            'abbreviation',
                            'name'
                        ],
                        data: [
                           ['AL', 'ALABAMA'],
                           ['AK', 'ALASKA'],
                           ['AS', 'AMERICAN SAMOA'],
                           ['AZ', 'ARIZONA'],
                           ['AR', 'ARKANSAS'],
                           ['CA', 'CALIFORNIA'],
                           ['CO', 'COLORADO'],
                           ['CT', 'CONNECTICUT'],
                           ['DE', 'DELAWARE'],
                           ['DC', 'DISTRICT OF COLUMBIA'],
                           ['FM', 'FEDERATED STATES OF MICRONESIA'],
                           ['FL', 'FLORIDA'],
                           ['GA', 'GEORGIA'],
                           ['GU', 'GUAM'],
                           ['HI', 'HAWAII'],
                           ['ID', 'IDAHO'],
                           ['IL', 'ILLINOIS'],
                           ['IN', 'INDIANA'],
                           ['IA', 'IOWA'],
                           ['KS', 'KANSAS'],
                           ['KY', 'KENTUCKY'],
                           ['LA', 'LOUISIANA'],
                           ['ME', 'MAINE'],
                           ['MH', 'MARSHALL ISLANDS'],
                           ['MD', 'MARYLAND'],
                           ['MA', 'MASSACHUSETTS'],
                           ['MI', 'MICHIGAN'],
                           ['MN', 'MINNESOTA'],
                           ['MS', 'MISSISSIPPI'],
                           ['MO', 'MISSOURI'],
                           ['MT', 'MONTANA'],
                           ['NE', 'NEBRASKA'],
                           ['NV', 'NEVADA'],
                           ['NH', 'NEW HAMPSHIRE'],
                           ['NJ', 'NEW JERSEY'],
                           ['NM', 'NEW MEXICO'],
                           ['NY', 'NEW YORK'],
                           ['NC', 'NORTH CAROLINA'],
                           ['ND', 'NORTH DAKOTA'],
                           ['MP', 'NORTHERN MARIANA ISLANDS'],
                           ['OH', 'OHIO'],
                           ['OK', 'OKLAHOMA'],
                           ['OR', 'OREGON'],
                           ['PW', 'PALAU'],
                           ['PA', 'PENNSYLVANIA'],
                           ['PR', 'PUERTO RICO'],
                           ['RI', 'RHODE ISLAND'],
                           ['SC', 'SOUTH CAROLINA'],
                           ['SD', 'SOUTH DAKOTA'],
                           ['TN', 'TENNESSEE'],
                           ['TX', 'TEXAS'],
                           ['UT', 'UTAH'],
                           ['VT', 'VERMONT'],
                           ['VI', 'VIRGIN ISLANDS'],
                           ['VA', 'VIRGINIA'],
                           ['WA', 'WASHINGTON'],
                           ['WV', 'WEST VIRGINIA'],
                           ['WI', 'WISCONSIN'],
                           ['WY', 'WYOMING']
                        ]
                    }),
                    displayField: 'name',
                    valueField: 'abbreviation'
                }),
            {
                fieldLabel: 'Zip Code',
                id: 'zip'
            }, {
                fieldLabel: 'Country',
                id: 'country',
                value: 'United States'
            }, {
                fieldLabel: 'Phone Number (example: 555-555-1234)',
                id: 'phone'
            }, {
                fieldLabel: 'Fax Number (example: 555-555-1234)',
                id: 'fax'
            }, {
                fieldLabel: 'How did you hear about us?',
                id: 'hearAboutUs'
            }
        ],

        url: 'UserRegistrationHandler.ashx',

        buttons:
            [{
                text: 'Submit',
                handler: function() {
                    formPanel.getForm().submit({
                        success: successFunction,
                        failure: failureFunction
                    });
                },
                style: 'margin-bottom:15px' //IE7
}]
    }); // end panel
    this.formPanel = formPanel;

    formPanel.region = 'center';

    var height60 = Math.min(.60 * Ext.getBody().getViewSize().height, 650)


    var regWindow = new Ext.Window({
        layout: 'border',
        title: 'Register',
        cls: 'UserRegistration',
        height: height60,
        width: 500,
        closeAction: 'hide',
        hidden: true,
        items: [{
            id: 'NorthPanel',
            region: 'north',
            xtype: 'panel',
            height: 40,
            border: false,
            items: [{
                id: 'registrationInstructions',
                html: 'Please fill out the form below to register for a new account.  Items with an asterisk are required.',
                border: false
}]
            },
            formPanel]
        });

        this.window = regWindow;

        this.show = function() {
            this.window.show();

        };

        Ext.apply(this, config);

    };   // end userRegistration
