﻿var MasterPage = function() {
    
    var usernameText;
    var passwordText;
    var submitButton;
    var agreementWindow;
    
    return {
        
        init : function() {
            
            Ext.get('notify-area').insertHtml("afterEnd", "<div id='agreement-window' class='x-hidden'></div>");
            
            usernameText = new Ext.form.TextField({
                renderTo: 'username-text',
                height: 30
            });
            
            passwordText = new Ext.form.TextField({
                renderTo: 'password-text',
                inputType: 'password',
                height: 30
            });
            
            Ext.get('signInButton').on('click', MasterPage.loginSubmit);
            
            /* Login submit button Enter event */
            var nav = new Ext.KeyNav("login_form", {
                "enter": function(e) {
                    MasterPage.loginSubmit();
                },
                scope: this
            });
                        
        },
        
        loginSubmit : function() {
			Ext.Ajax.request({
            url: 'MasterPageData.ashx',
            params: {
                method: 'Login',
                username: usernameText.getValue(),
                password: passwordText.getValue()
            },
            success: function(result) {
                var data = Ext.decode(result.responseText);
                if (data.success) {
                    if (data.agreement) {
                        if(getQueryVariable("ReturnUrl")){
			                window.location = unescape(getQueryVariable("ReturnUrl"));
                        }
                        else {
			                window.location = "/member/Default.aspx";
                        }    
                    }
                    else {
                        if (!agreementWindow) {
                            agreementWindow = new Ext.Window({
	                            applyTo: 'agreement-window',
	                            title: 'Terms and Conditions',
	                            modal: true,
	                            autoDestroy: false,
	                            height: 500,
	                            width: 500,
	                            closeAction: 'hide',
	                            footer: false,
	                            autoScroll: true,
	                            html: data.text,
                                buttons: [{
                                    text:'I Agree',
                                    handler: function() {
					                    Ext.Ajax.request({
                                            url: 'MasterPageData.ashx',
                                            params: {
                                                method: 'TermsAgreement',
                                                username: usernameText.getValue(),
                                                password: passwordText.getValue()
                                            },
                                            success: function(result) {
                                                if (data.success) {
                                                    if(getQueryVariable("ReturnUrl")){
			                                            window.location = unescape(getQueryVariable("ReturnUrl"));
                                                    }
                                                    else {
			                                            window.location = "/member/Default.aspx";
                                                    }
                                                }
                                            }
                                        });
                                    }
                                },{
                                    text: "I Don't Agree",
                                    handler: function() {
                                        agreementWindow.hide();
                                    }
                                }]
                            });
                        }
                        
                        agreementWindow.show();
                    }
                }
                else {
                    alert("Username or password incorrect");
                }
            }
            
        });
        }
        
    }

}();
Ext.onReady(MasterPage.init);