var read = require('read');
var request = require('superagent');
var Spinner = require('cli-spinner').Spinner;
var spinner = new Spinner('registering...');
spinner.setSpinnerString('/-\\');

function register(resumeJson) {
    console.log('What username would like to reserve? Your resume will be available at registry.jsonresume.org/{username}:'.cyan);
    read({
        prompt: "username: "
    }, function(er, user) {
        if (er) {
            console.log('');
            process.exit();
        }
        read({
            prompt: "email: ",
            default: resumeJson.email
        }, function(er, email) {
            if (er) {
                console.log('');
                process.exit();
            }
            read({
                prompt: "password: ",
                silent: true
            }, function(er, pass) {
                if (er) {
                    console.log('');
                    process.exit();
                }
                read({
                    prompt: "re-enter password: ",
                    silent: true
                }, function(er, pass2) {
                    verify({
                        username: user,
                        email: email,
                        password: pass,
                        verify: pass2,
                        passMatch: (pass === pass2)
                    });
                });
            });
        });
    });
}

function verify(userInfo) {
    if (!userInfo.passMatch) {
        console.log('Your passwords did not match, try again.'.red);
    } else {
        attemptPost(userInfo);
    }
}

module.exports = register;

function attemptPost(userInfo) {
    spinner.start();
    request
        .post('http://registry.jsonresume.org/user')
        .send(userInfo)
        .set('X-API-Key', 'foobar')
        .set('Accept', 'application/json')
        .end(function(error, response) {
            spinner.stop();
            console.log();

            // console.log(response.body); //if success. 
            if (error) {
                console.log(error);
                console.log('There has been an error publishing your resume'.red);
            } else if (response.body.message === 'success') {
                console.log('Success! You are now registered at jsonresume.org.'.green);
                console.log('You can now type'.cyan, 'resume publish', 'to publish to a custom domain extention.'.cyan);
            } else if (response.body.error && response.body.error.field === 'email') {
                // console.log(response.body.error.message.red) // change the response message
                console.log('Email already in use, please use a different one.'.red);
                read({
                    prompt: "enter a different email: "
                }, function(er, email) {
                    if (er) {
                        console.log();
                        process.exit();
                    }
                    userInfo.email = email;
                    attemptPost(userInfo);
                });

            } else if (response.body.error && response.body.error.field === 'username') {
                read({
                    prompt: "enter a different username: "
                }, function(er, username) {
                    if (er) {
                        console.log();
                        process.exit();
                    }
                    userInfo.username = username;
                    attemptPost(userInfo);
                });

            }
            // console.log();
        });
    return;
}
@@ -184,16 +184,104 @@
 -%5C%5C');%0A%0A
+function validEmail(email) %7B%0A    var re = /%5CS+@%5CS+%5C.%5CS+/;%0A    return re.test(email);%0A%7D%0A%0A
 function
@@ -300,24 +300,24 @@
 sumeJson) %7B%0A
-
     console.
@@ -697,32 +697,225 @@
 on(er, email) %7B%0A
+            if (!validEmail(email)) %7B%0A                console.log('Incorrect email format. Try again.'.red);%0A                process.exit();%0A                register(resumeJson);%0A            %7D%0A
             if (
@@ -1916,16 +1916,19 @@
 ister;%0A%0A
+%0A%0A%0A
 function
@@ -2237,17 +2237,16 @@
 .log();%0A
-%0A
         
@@ -3222,32 +3222,33 @@
               %7D%0A
+%0A
                 
@@ -3330,33 +3330,32 @@
             %7D);%0A
-%0A
             %7D el
@@ -3770,32 +3770,32 @@
 Post(userInfo);%0A
+
                 
@@ -3798,17 +3798,16 @@
     %7D);%0A
-%0A
         

add regex email validation