﻿/// <reference path="http://extjs.cachefly.net/ext-3.3.1/adapter/ext/ext-base.js" />
/// <reference path="http://extjs.cachefly.net/ext-3.3.1/ext-all.js" />
/// <reference path="locale.js" />
// groupUrl is defined in Views/Shared/Site.Master
var DataProxyURL = groupUrl + "/Account/Register";
var ProfileURL = groupUrl + "/Account/SendProfile";
var CheckURL = groupUrl + "/Account/CanRegister";

var frmRegistration;
var frmEmail;

Ext.onReady(function() {
    Ext.QuickTips.init();
    displayEmailForm('');
});

function CheckEmailAddress(email) {
    if (frmEmail) {
        frmEmail.getForm().submit({
            method: 'POST',
            waitTitle: locale.getLocaleString('regWaitTitle'),
            waitMsg: locale.getLocaleString('regWaitMsg'),
            success: displayRegistrationForm,
            failure: function(form, action) {
                if (action.failureType == 'server') {
                    Ext.Msg.show({
                        title: locale.getLocaleString('regErrMsgTitle1'),
                        msg: '<p>' + action.result.errors.reason + '</p>',
                        icon: Ext.Msg.INFO,
                        buttons: { yes: locale.getLocaleString('profileSend'), cancel: true },
                        fn: function(btn) { if(btn == 'yes') requestProfile(email); } 
                    });
                } else if (action.failureType == 'client') {
                    Ext.Msg.show({
                        title: locale.getLocaleString('regErrMsgTitle2'),
                        msg: locale.getLocaleString('regErrMsg2'),
                        icon: Ext.Msg.INFO,
                        buttons: Ext.Msg.OK
                    });
                    form.enable();
                } else { //if (action.failureType == 'connect') {
                    Ext.Msg.show({
                        title: locale.getLocaleString('regErrMsgTitle3'),
                        msg: locale.getLocaleString('regErrMsg3'),
                        icon: Ext.Msg.ERROR,
                        buttons: Ext.Msg.OK
                    });
                }
            }
        });
    }
}

function requestProfile(email) {
    Ext.Ajax.request({
        url: ProfileURL,       // defined at top of script
        success: function() {
            Ext.Msg.show({
                title: locale.getLocaleString('profileTitleSuccess'),
                msg: locale.getLocaleString('profileTxtSuccess'),
                icon: Ext.Msg.INFO,
                buttons: Ext.Msg.OK,
                fn: function() {location = location;}
            });
        },
        failure: function(result) {
                Ext.Msg.show({
                    title: locale.getLocaleString('profileTitleFail'),
                    msg: '<p>' + locale.getLocaleString('profileTxtFail1') + '</p><p>' +
                                 locale.getLocaleString('profileTxtFail2') + '</p><p><h6>' +
                                 locale.getLocaleString('profileTxtFail3') + '</h6><br />' + 
                                 result.errors.reason + '</p>',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
        },
        params: { email: email}  // defined in view page script block
    });
}


function displayEmailForm(email) {

    if (frmRegistration)
        frmRegistration.hide(true);

    if (frmEmail) {
        frmEmail.show(true);
    } else {
        frmEmail = new Ext.FormPanel({
            id: 'frmEmail',
            labelWidth: 100, // label settings here cascade unless overridden
            url: CheckURL,
            frame: true,
            title: locale.getLocaleString('regFormTitle'),
            bodyStyle: 'padding:5px 5px 0',
            width: 400,
            defaults: { width: 230 },
            defaultType: 'textfield',

            items: [{
                fieldLabel: locale.getLocaleString('regLabelEmail'),
                name: 'email',
                vtype: 'email',
                value: email,
                allowBlank: false
}],

                buttons: [{
                    text: locale.getLocaleString('txtClear'),
                    handler: function() { if (frmEmail) frmEmail.getForm().reset(); }
                },{
                    text: locale.getLocaleString('txtNext') + ' >>',
                    handler: function() { CheckEmailAddress((frmEmail) ? frmEmail.getForm().findField('email').getValue() : ''); }
                }]
                });
                frmEmail.render("emailForm");
            }

            var frm = frmEmail.getForm();
            frm.findField('email').setValue(email);
            frm.findField('email').focus();
        }

 function displayRegistrationForm() {

    var email = '';
    if (frmEmail) {
        email = frmEmail.getForm().findField('email').getValue();
        frmEmail.hide(true);
    } else
        return;
        
    if (frmRegistration) {
        frmRegistration.show(true);
    } else {

    frmRegistration = new Ext.FormPanel({
        id: 'frmRegistration',
        labelWidth: 100, // label settings here cascade unless overridden
        url: DataProxyURL,
        frame: true,
        title: locale.getLocaleString('regFormTitle'),
        bodyStyle: 'padding:5px 5px 0',
        width: 400,
        defaults: { width: 230 },
        defaultType: 'textfield',

        items: [{
            fieldLabel: '* ' + locale.getLocaleString('regLabelForename'),
            name: 'forename',
            value: '',
            maxLength: 50,
            allowBlank: false
        }, {
            fieldLabel: '* ' + locale.getLocaleString('regLabelSurname'),
            name: 'surname',
            value: '',
            maxLength: 50,
            allowBlank: false
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelTitle'),
            name: 'title',
            value: '',
            maxLength: 50,
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelOrganization'),
            name: 'organization',
            maxLength: 50,
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelAddress1'),
            name: 'addr1',
            maxLength: 50,
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelAddress2'),
            name: 'addr2',
            maxLength: 50,
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelCity'),
            name: 'city',
            maxLength: 50,
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelState'),
            name: 'state',
            maxLength: 50,
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelZip'),
            maxLength: 50,
            name: 'postcode',
            allowBlank: true
        }, CountryField, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelWorkPhone'),
            maxLength: 50,
            name: 'phone',
            allowBlank: true
        }, {
            fieldLabel: '&nbsp;&nbsp;&nbsp;' + locale.getLocaleString('regLabelFax'),
            maxLength: 50,
            name: 'fax',
            allowBlank: true
        }, {
            fieldLabel: '* ' + locale.getLocaleString('regLabelEmail'),
            name: 'email',
            vtype: 'email',
            value: email,
            readOnly: (email && email.length > 0),
            maxLength: 128,
            allowBlank: false
        }, {
            fieldLabel: '* ' + locale.getLocaleString('regLabelProjectName'),
            name: 'project',
            value: '',
            maxLength: 50,
            allowBlank: false
        }, {
            xtype: 'panel',
            layout: 'form',
            anchor: '-40',
            items: [{
                xtype: 'checkbox',
                labelSeparator: '',
                name: 'optedIn',
                boxLabel: '<small>' + locale.getLocaleString('regLabelConsent') + '</small>',
                checked: true
}]
}],

        buttons: [{
            text: '<< ' + locale.getLocaleString('txtBack'),
            handler: function() { displayEmailForm((frmRegistration) ? frmRegistration.getForm().findField('email').getValue() : ''); }
        }, {
            text: locale.getLocaleString('txtRegister'),
            handler: function() {
                if (frmRegistration) {
                    if (frmRegistration.getForm().isValid()) {
                        frmRegistration.getForm().submit({
                            method: 'POST',
                            waitTitle: locale.getLocaleString('txtRegistering'),
                            waitMsg: locale.getLocaleString('txtTransmitting'),
                            success: function(form, action) {
                                frmRegistration.disable();
                                Ext.Msg.show({
                                    title: locale.getLocaleString('regTitleSuccess'),
                                    msg: '<p>' + locale.getLocaleString('regMsgSuccess') + '</p>',
                                    icon: Ext.Msg.INFO,
                                    buttons: Ext.Msg.OK
                                });
                            },
                            failure: function(form, action) {
                                if (action.failureType == 'server') {
                                    //var obj = Ext.util.JSON.decode(action.response.responseText);
                                    Ext.Msg.show({
                                        title: locale.getLocaleString('regTitleFail'),
                                        msg:  '<p>' + locale.getLocaleString('profileTxtFail1') + '</p><p>' +
                                             locale.getLocaleString('profileTxtFail2') + '</p><p><h6>' +
                                             locale.getLocaleString('profileTxtFail3') + '</h6><br />' + 
                                             result.errors.reason + '</p>',
                                        icon: Ext.Msg.WARNING,
                                        buttons: Ext.Msg.OK
                                    });
                                } else { //if (action.failureType == 'connect') {
                                    Ext.Msg.show({
                                        title: locale.getLocaleString('regErrMsgTitle3'),
                                        msg: locale.getLocaleString('regErrMsg3'),
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK
                                    });
                                }
                            }
                        });
                    } else {
                        Ext.Msg.show({
                            title: locale.getLocaleString('regErrMsgTitle2'),
                            msg: locale.getLocaleString('regErrMsgMissing'),
                            icon: Ext.Msg.INFO,
                            buttons: Ext.Msg.OK
                        });
                    }
                }
            }
        }, {
            text: 'Clear',
            handler: function() { if (frmRegistration) frmRegistration.getForm().reset(); }
}]
        });
        frmRegistration.render("regForm");
    }

    var frm = frmRegistration.getForm();
    frm.findField('email').setValue(email);
    frm.findField('forename').focus();
    
}
