﻿
var Login = new function()
{
    this.Window = null;
    this.FormPanel = null;
    this.Email = "";
    
    this.show = function()
    {
        var panel = Login.FormPanel = new Ext.form.FormPanel({
            width: "100%",
            height: "100%",           
            frame:false,
            border:false,
            bodyStyle: 'padding: 10px 10px 0',
            items: [
                 new Ext.form.TextField({
                    id:"_SGEmail",
                    fieldLabel:"Email Address",
                    width:250,
                    autoHeight: true,
                    //allowBlank:false,
                    blankText:"Please enter a valid registered email address",
                    enableKeyEvents: true,
                    listeners: {
				        keyup: {
					        scope: this,
					        fn: function(tf, e) 
					        {  
					            if(e && e.keyCode == 13)
					            {
					                this.doLogin();
					                return;
					            }
					            
					            var btn = Login.FormPanel.buttons[1];
					            if(btn != null)
					            {					               
					                var val = tf.getValue();
					                if(val.length > 0)
					                    btn.enable();
					                else btn.disable();
					            }    
					        }
	        			}		
		        	}
                 })                 
            ],
            buttons: [
             {text:"Cancel", handler: function() { Login.cancel(); } },
             {text:"Enter", id: '_SGLoginButton', disabled: true, scope: this, handler: this.doLogin
             }
            ]
        });
        
        win = this.Window = new Ext.Window({
                id: 'SGLogin',
                title: "Investor Login",
                autoWidth:true,
                autoHeight:true,
                resizable:false,
                iconCls: 'lock',
                shim:false,
                autoScroll:false,
                animCollapse:true,
                constrainHeader:true,
                modal:true,
                items: [panel]            
            });
            
        win.DisplayPanel = panel;
        win.show();          
        
        try { Ext.getCmp("_SGEmail").focus(); } catch(e) { }
        try { Ext.getDom("_SGEmail").focus(); } catch(e) { }
        try { document.getElementById("_SGEmail").focus(); } catch(e) { }        
    }
    
    this.doLogin = function()
    {
        var sEmail = Ext.getDom("_SGEmail").value;
        
        // submit the form and get a yes/no or error                    
        SGWebApplication.SGInvestors.ValidateEmail(sEmail, this.successLogin, this.failLogin, '');        
    } 
    
    this.successLogin = function(result, eventArgs) 
    { 
        if(result == true)
        {
            Login.Email = Ext.getDom("_SGEmail").value;
            Login.close();
            return;
        }
        else
        {
            Ext.MessageBox.alert("Login Error", "Your email address is not registered for investor access.  Please send an email " + 
                "requesting access to our Investors Area to invest@scattergrasp.com");
            return;
        }                
    }
    
    this.failLogin = function() 
    { 
        Ext.MessageBox.alert("Login Error", "Failed to validate your email address.  Please try again.");
        return;
    }
    
    this.cancel = function()
    {
        if(Login.Window != null)
        {
            window.location = "http://www.scattergrasp.com";
        }
    }
    
    this.close = function()
    {
        if(Login.Window != null)
            Login.Window.close();
    }    
};

function signIn()
{
    window.location = "http://desktop.scattergrasp.com";
}

var Window = null;
function Show(sURL, sTitle)
{
    if(Window != null)
    {
        try
        {
            Window.show();            
            Window.toFront();

            var mask = new Ext.LoadMask(Window.getEl(), { msg: "Please wait..." });
            Window.Mask = mask;
            mask.show();

            Window.load({ url: sURL, text: "", scripts: true, callback: FinishedNavigate });
            Window.setTitle(sTitle);
            
            return;
        }
        catch(e) { alert("Exception opening Window: " + e); }
    }

    try
    {
        Window = new Ext.Window({
            id: '_SGDisplay',
            title: "Scattergrasp - Social Cloudware",
            width:'80%',
            height: 640,
            html : '<p><br/><br/></p>', 
            iconCls: 'welcome',
            bodyStyle: 'background-color:#ffffff;',
            shim:false,
            y: 50,
            autoScroll:true,
            shadow:false,
            closable: true,
            animCollapse:true,
            minimizable:false,
            maximizable:false,
            resizable:true,            
            closeAction: 'hide',
            constrainHeader:true
        });
        
        Window.show();         
        Window.center();
        Window.load({ url: sURL, scripts: true, text: "" });
        Window.setTitle(sTitle);
    }
    catch(e) { alert("Exception opening Window: " + e); }
}

function FinishedNavigate()
{
    try { Window.Mask.hide(); } catch(e) { }
}

var SendMessageWindow = null;
function sendMessage()
{
    var formpanel = new Ext.form.FormPanel({
        id: "_SGMessageForm",
        autoWidth: true,
        autoHeight: true,        
        bodyStyle: 'padding: 10px 10px 0',
        frame:false,
        items: [
            new Ext.form.TextField({
                id:"_SGEmail",
                fieldLabel:"Your Email",
                width:275,
                allowBlank:false,
                blankText:"Please enter your email address"
             }),
             new Ext.form.TextField({
                id:"_SGSubject",
                fieldLabel:"Subject",
                width:275,
                allowBlank:false,
                blankText:"Please enter a subject for this message"
             }),
             new Ext.form.TextArea({
                id:"_SGMessage",
                fieldLabel:"Message",
                width:275,
                height:200,
                allowBlank:false,
                blankText:"Please enter a message"
             })
        ],
        buttons: [
            { text: "Cancel", iconCls: 'xbutton', handler: cancelSendMessage },
            { text: "Send Message", iconCls: 'inboxsmall', handler: doSendMessage }
        ]
    });
     
    var win = SendMessageWindow = new Ext.Window({
        id: '_SGMessageWindow',
        title: "Send a Message to the Scattergrasp Team",
        autoWidth:true,
        autoHeight:true,
        html : '<p><br/><br/></p>', 
        iconCls: 'inboxsmall',
        shim:false,
        shadow:false,
        closable: true,
        animCollapse:true,
        minimizable:false,
        maximizable:false,
        modal:true,
        resizable:true,
        closeAction: 'close',
        constrainHeader:true,
        items: [formpanel]
    });
    
    win.show();    
    win.form = formpanel;
    
    try { Ext.getDom("_SGEmail").focus(); } catch(e) { }
    try { Ext.getCmp("_SGEmail").focus(); } catch(e) { }
}

function cancelSendMessage() { SendMessageWindow.close(); }
function doSendMessage() 
{
    // send it
    SendMessageWindow.form.getForm().submit({
        clientValidation: true,
        url: 'SendInvestorMessage.aspx',
        params: {
            Email: Ext.getDom("_SGEmail").value,
            Subject: Ext.getDom("_SGSubject").value,
            Message: Ext.getDom("_SGMessage").value
        },
        success: function(form, action) {
            Ext.Msg.alert("Success", action.result.msg);
            SendMessageWindow.close();            
        },
        failure: function(form, action) {
            switch(action.failureType)
            {
                case Ext.form.Action.CLIENT_INVALID:
                    Ext.Msg.alert("Failure", "Form fields may not be submitted with invalid values");
                    break;
                case Ext.form.Action.CONNECT_FAILURE:
                    Ext.Msg.alert("Failure", "Ajax communication failed");
                    break;
                case Ext.form.Action.SERVER_INVALID:
                   Ext.Msg.alert("Failure", action.result.msg);
                   break;
            }
        }
    });
}