window.addEvent('domready', function() {
    $('prefill').innerHTML = prefill();
    $('prefill').setStyle('display', 'block');
    
    $('prefill-link').addEvent('click', function(ievent) {
        ievent.stop();

        var user = '';
        var pass = '';        
        
        Sexy.prompt('<h1>Retrieve information from Seattle Study Club</h1>Please enter your SSC Username', 'username', 
        { 
            onComplete: function( returnval )
            { 
                if( returnval )
                {
                    user = returnval;
                    Sexy.prompt('<h1>Retrieve information from Seattle Study Club</h1>Please enter your SSC Password', '', 
                    { 
                        password: true,
                        onComplete: function( returnval )
                        { 
                            if( returnval )
                            {
                                pass = returnval;
                                getValues( user, pass );
                            }
                        }
                    });
                }
            }
        });
    });
});

function getValues( user, pass )
{
    var irequest = new Request.JSON({
        method: 'post',
        async: true,
        url: 'prefill.php',
        data: {
                username: user,
                password: pass
        },
        onSuccess: function(val) {
            if ( val.error.code == 0 )
            {
                $('txtName').value = val.user.name;
                $('txtClub').value = val.user.club;
                $('txtAddress').value = val.user.address;
                $('txtCity').value = val.user.city;
                $('txtState').value = val.user.state;
                $('txtZip').value = val.user.zip;
                $('txtHome').value = val.user.home;
                $('txtWork').value = val.user.work;
                $('txtCell').value = val.user.cell;
                $('txtFax').value = val.user.fax;
                $('txtEmail').value = val.user.email;
                $('txtDiet').value = val.user.diet;
                Sexy.info('<h1>Information retrieved</h1><p>Please verify all information before submitting this registration form</p>');
            }
            else
            {
                Sexy.error('<h1>Error retrieving information</h1><p>' + val.error.message + '</p>');
            }
        }
    }).send();
}

function retrieving()
{
    return 'Getting information for the Seattle Study Club server...';
}

function prefill()
{
    return '<a id="prefill-link" href="prefil.php">Click here to fill in form</a> below with information from my Seattle Study Club website account.<br /><br />';
}