﻿Ext.namespace('MSDImages');

//Make new Vtype for passwords
Ext.apply(Ext.form.VTypes, {

    password: function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },

    passwordText: 'Passwords do not match',

    username: function(val, field) {
        var conn = new Ext.data.Connection();
        conn.request({
            url: '/account/CheckUsernameAvailability',
            params: { "username": val },
            success: function(data) {
                if (data.responseText == "false") {
                    field.markInvalid();
                }
            },
            failure: function() {
                Ext.Msg.alert('Status', 'Unable to check username availability');
            }
        });

        return true;
    },

    usernameText: 'Username is already taken',

    emailAvail: function(val, field) {
        //Do a regex check first
        var regex = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

        if (regex.test(val)) {
            var conn = new Ext.data.Connection();
            conn.request({
                url: '/account/CheckEmailAvailability',
                params: { "email": val },
                success: function(data) {
                    if (data.responseText == "false") {
                        field.markInvalid();
                        field.markedAsInvalid = true;
                    } else {
                        field.markedAsInvalid = false;
                    }
                },
                failure: function() {
                    Ext.Msg.alert('Status', 'Unable to check email address availability');
                }
            });
            
        } else {
            return false;
        }

        return true;
    },

    emailAvailText: 'Invalid email format'
});

MSDImages.RegisterApp = function() {

    //private space
    var fieldArray;
    var firstname, lastname, username, email, password, password2, affiliation, btnSubmit, country;

    function IsValid() {
        var isValid = true;
        for (var i = 0; i < fieldArray.length; i++) {
            if (!fieldArray[i].isValid()) {
                isValid = false;
            }
        }
        //console.log(email.markedAsInvalid);
        return isValid && !email.markedAsInvalid;
    }

    function initFields() {

        country = new Ext.form.ComboBox({
            typeAhead: true,
            triggerAction: 'all',
            transform: 'countryId',
            forceSelection: true,
            emptyText: 'Please select a country',
            allowBlank: false,
            msgTarget: 'countryIdError',
            width: 250//,
            //hiddenName: 'countryIdd'

        });

        firstname = new Ext.form.TextField({
            applyTo: 'firstname',
            allowBlank: false,
            regex: /^\S/,
            maxLength: 100,
            regexText: 'First character cannot be a space',
            msgTarget: 'firstnameError',
            invalidText: 'Please enter your first name',
            blankText: 'Please enter your first name',
            width: 250
        });

        lastname = new Ext.form.TextField({
            applyTo: 'lastname',
            allowBlank: false,
            regex: /^\S/,
            maxLength: 100,
            regexText: 'First character cannot be a space',
            blankText: 'Please enter your last name',
            msgTarget: 'lastnameError',
            width: 250
        });

        affiliation = new Ext.form.TextArea({
            applyTo: 'affiliation',
            maxLength: 1000,
            height: 100,
            width: 250,
            msgTarget: 'affiliationError'
        });

        username = new Ext.form.TextField({
            applyTo: 'username',
            allowBlank: false,
            vtype: 'username',
            minLength: 4,
            maxLengthText: 'The maximum length for a username is 50 characters',
            maxLength: 50,
            minLengthText: 'The minimum length for a username is 4 characters',
            regex: new RegExp('^[a-zA-Z0-9]+$'),
            regexText: 'Username must be characters A-Z and numbers 0-9',
            blankText: 'Please enter your username',
            width: 150,
            msgTarget: 'usernameError',
            invalidText: 'Username is already taken'
        });

        email = new Ext.form.TextField({
            applyTo: 'email',
            id: 'yez',
            markedAsInvalid: false,
            //vtype: "email",
            vtype: 'emailAvail',
            allowBlank: false,
            blankText: 'Please enter your email address',
            invalidText: 'There is already an account registered to this email address',
            msgTarget: 'emailError',
            width: 250
        });

        password = new Ext.form.TextField({
            applyTo: 'password',
            allowBlank: false,
            id: 'firstpass',
            maxLength: 20,
            minLength: 5,
            blankText: 'Please enter your password',
            msgTarget: 'passwordError',
            width: 150
        });

        password2 = new Ext.form.TextField({
            applyTo: 'password2',
            allowBlank: false,
            vtype: 'password',
            initialPassField: 'firstpass',
            blankText: 'Please confirm your password',
            msgTarget: 'password2Error',
            width: 150
        });

        btnSubmit = new Ext.Button({
            applyTo: 'btnSubmit',
            id: 'extBtnSubmit',
            text: 'Join',
            height: 30,
            width: 80,
            handler: function() {
                if (IsValid()) {
                    document.forms['registration'].submit();
                } else {
                    Ext.Msg.show({
                        title: 'Unable to register new account',
                        msg: 'Please check the form and try again<br /><br />Invalid entries are highlighted in red',
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR,
                        maxWidth: 100
                    });
                }
            }
        });

        fieldArray = new Array(firstname, lastname, email, password, password2, country);
    }

    //public space
    return {
        init: function() {
            initFields();
            Ext.QuickTips.init();
        },

        Validate: function() {
            if (!IsValid()) {
                Ext.Msg.show({
                    title: 'Unable to register new account',
                    msg: 'Please check the form and try again<br /><br />Invalid entries are highlighted in red',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.ERROR,
                    maxWidth: 100
                });

                return false;
            }
        }
    }

} ();

