function Login() { }

Login.emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
Login.userReg = /^\w+$/;
        
        
var onLogIn = new YAHOO.util.CustomEvent('Login');
var onRegister = new YAHOO.util.CustomEvent('onRegister');
var onForgotPass = new YAHOO.util.CustomEvent('onForgotPass');
var onForgotMail = new YAHOO.util.CustomEvent('onForgotMail');
var onNotesMail = new YAHOO.util.CustomEvent('onNotesMail');


Login.PadSpaces = function(val) {
    val = val.replace(/ /g, "+"); //Space
    val = val.replace(/#/g, "%23"); //Hash
    val = val.replace(/&/g, "%26"); //And
    val = val.replace(new RegExp("\\n", "g"), "Ø"); //New Line
    return val;
}

Login.SubmitLogin = function(user, pass) {
    if (user.length == 0 || pass.length == 0) {
        onLogIn.fire({ Code: "-1", Text: Strings.GetUserPassSpecify() });
        return;
    }
    if (user.length < 6 || pass.length < 6) {
        onLogIn.fire({ Code: "-1", Text: Strings.Getuserpass6() });
        return;
    }
    if (user.length > 20 || pass.length > 20) {
        onLogIn.fire({ Code: "-1", Text: Strings.Getuserpass20() });
        return;
    }
    if (!Login.userReg.test(user)) {
        onLogIn.fire({ Code: "-1", Text: Strings.GetuserLetters() });
        return;
    }
    if (pass.indexOf(' ') > -1) {
        onLogIn.fire({ Code: "-1", Text: Strings.GetpassSpaces() });
        return;
    }
    ClientServer.Login(Login.PadSpaces(user), Login.PadSpaces(pass));
    onLogIn.fire({ Code: "0", Text: Strings.GetConnecting() });
}

Login.ValidateLogin = function(type, args) {
    onLogIn.fire({ Code: args[0].Code, Text: args[0].Text });
}

Login.CreateUser = function(user, pass, confPass, email, question, answer) {
    if (user.length == 0 || pass.length == 0) {
        onRegister.fire({ Code: "-1", Text: Strings.GetUserPassSpecify() });
        return;
    }
    if (user.length < 6 || pass.length < 6) {
        onRegister.fire({ Code: "-1", Text: Strings.Getuserpass6() });
        return;
    }
    if (user.length > 20 || pass.length > 20) {
        onRegister.fire({ Code: "-1", Text: Strings.Getuserpass20() });
        return;
    }
    if (!Login.userReg.test(user)) {
        onRegister.fire({ Code: "-1", Text: Strings.GetuserLetters() });
        return;
    }
    if (pass.indexOf(' ') > -1) {
        onRegister.fire({ Code: "-1", Text: Strings.GetpassSpaces() });
        return;
    }
    if (pass != confPass) {
        onRegister.fire({ Code: "-1", Text: Strings.GetpassMatch() });
        return;
    }
    if (email == "") {
        onRegister.fire({ Code: "-1", Text: Strings.GetemailSpecify() });
        return;
    }
    if (Login.emailReg.test(email) == false) {
        onRegister.fire({ Code: "-1", Text: Strings.GetinvalidEmail() });
        return;
    }
    if (question == "") {
        onRegister.fire({ Code: "-1", Text: Strings.Getquestion50() });
        return;
    }
    if (answer == "") {
        onRegister.fire({ Code: "-1", Text: Strings.GetanswerSpecify() });
        return;
    }
    if (question.length > 50) {
        onRegister.fire({ Code: "-1", Text: Strings.Getquestion50() });
        return;
    }
    if (answer.length > 50) {
        onRegister.fire({ Code: "-1", Text: Strings.Getanswer50() });
        return;
    }
    ClientServer.Register(Login.PadSpaces(user), Login.PadSpaces(pass), Login.PadSpaces(confPass), Login.PadSpaces(email), Login.PadSpaces(question), Login.PadSpaces(answer));
    onRegister.fire({ Code: "0", Text: Strings.GetConnecting() });
}

Login.ValidateRegister = function(type, args) {
    onRegister.fire({ Code: args[0].Code, Text: args[0].Text });
}


Login.GetQuestion = function(email) {
    if (email == "") {
        onForgotPass.fire({ Code: "-1", Text: Strings.GetemailSpecify() });
        return;
    }
    if (Login.emailReg.test(email) == false) {
        onForgotPass.fire({ Code: "-1", Text: Strings.GetinvalidEmail() });
        return;
    }
    ClientServer.GetQuestion(Login.PadSpaces(email));
    onForgotPass.fire({ Code: "0", Text: Strings.GetConnecting() });
}

Login.ValidateForgotPass = function(type, args) {
    onForgotPass.fire({ Code: args[0].Code, Text: args[0].Text });
}


Login.GetPassword = function(email, answer) {
    if (email == "") {
        onForgotMail.fire({ Code: "-1", Text: Strings.GetemailSpecify() });
        return;
    }
    if (Login.emailReg.test(email) == false) {
        onForgotMail.fire({ Code: "-1", Text: Strings.GetinvalidEmail() });
        return;
    }
    if (answer.length == 0) {
        onForgotMail.fire({ Code: "-1", Text: Strings.GetanswerSpecify() });
        return;
    }
    if (answer.length > 50) {
        onForgotMail.fire({ Code: "-1", Text: Strings.Getanswer50() });
        return;
    }
    ClientServer.GetPassword(Login.PadSpaces(email), Login.PadSpaces(answer));
    onForgotMail.fire({ Code: "0", Text: Strings.GetConnecting() });
}

Login.ValidateForgotMailed = function(type, args) {
    onForgotMail.fire({ Code: args[0].Code, Text: args[0].Text });
}

Login.EmailNotes = function(email, guid, other) {
    if (other) {
        if (email == "") {
            onNotesMail.fire({ Code: "-1", Text: Strings.GetemailSpecify() });
            return;
        }
        if (Login.emailReg.test(email) == false) {
            onNotesMail.fire({ Code: "-1", Text: Strings.GetinvalidEmail() });
            return;
        }
    }
    ClientServer.EmailNotes(guid, Login.PadSpaces(email));
    onNotesMail.fire({ Code: "0", Text: Strings.GetConnecting() });
}

Login.ValidateMotesMailed = function(type, args) {
    onNotesMail.fire({ Code: args[0].Code, Text: args[0].Text });
}


onLoginReceived.subscribe(Login.ValidateLogin);
onRegisterReceived.subscribe(Login.ValidateRegister);
onForgotPassReceived.subscribe(Login.ValidateForgotPass);
onForgotPassMailed.subscribe(Login.ValidateForgotMailed);
onEmailNotes.subscribe(Login.ValidateMotesMailed);