require("luamods/layout")
require("luamods/db")
require("luamods/cgi")

message=""
--************ If add new user button is pressed ************--
if cgi.post.createbutton == "Add New User" then
    username = cgi.post.new_user or ""
    newpass = cgi.post.new_pass or ""
    retypenewpass = cgi.post.new_rpass or ""

    if cgi.post.type == "0" then
        usertype = "administrator"
    elseif cgi.post.type == "2" then
        usertype = "developer"
    else
        usertype = "guest"
    end

    message = ""

    if username == "" then
        message = message.."Username cannot be blank</BR>"
    end

    if username:match("[^%w_]") ~= nil then
        message = message.."Username may only contain letters, numbers and underscores</BR>"
    end

    if newpass ~= retypenewpass then
        message = message.."Passwords do not match</BR>"
    else
        if newpass == "" then
            message = message.."Password cannot be blank</BR>"
        elseif string.len(newpass) < 5 then
            message = message.."Password needs to be at least 5 characters</BR>"
        end
    end

    if message == "" then
        check = db.createUser( username , newpass , cgi.post.type )
        if check == true then
            saved = 1
        else
            message = "Error: Username \""..username.."\" already exists"
        end
    end
end

header = layout.getHeader()
footer = layout.getFooter()
content = [[
                    <div id="info">
                        <div style="background:url(images/users.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>Users - Create New User</h1></center>
                        </div>
                    </div>
]]

if (saved == 1) then
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2>User created</h2>
                            <b>New ]]..usertype..[[ account created for username "]]..username..[["</b><br><br>
                            <a href="users_manage.lua">Back to user accounts</a>
                        </center>
                    </div>
]]
    print(header..content..footer)
    exit(0)
end


if message ~= "" then content = content..[[
                    <div id="info">
                        <center><b>]]..message..[[</b></center>
                    </div>
    ]]
end

content = content..[[
                    <script>
                        function validateForm(f)
                        {
                            if (f.getAttribute('submitted')) return false;

                            errorstring = "";
                            if (document.form1.new_user.value == "")
                            {
                                errorstring += "* Username cannot be blank\n";
                            }

                            if (document.form1.new_pass.value != document.form1.new_rpass.value)
                            {
                                errorstring += "* Passwords do not match\n";
                            }
                            else if (document.form1.new_pass.value == "")
                            {
                                errorstring += "* Password cannot be blank\n";
                            }
                            else if (document.form1.new_pass.value.length < 5)
                            {
                                errorstring += "* Password needs to be at least 5 characters\n";
                            }

                            var illegalChars = /\W/;
                            if (illegalChars.test(document.form1.new_user.value))
                            {
                                errorstring += "* Username may only contain letters, numbers and underscores\n";
                            }


                            if (errorstring != "")
                            {
                                errorstring = "The following errors were detected: \n\n" + errorstring;alert(errorstring);
                                return false;
                            }

                            f.setAttribute('submitted','true');
                        }
                    </script>

                    <div class="formLayoutfull">
                        <h3>Account types</h3>
                        Administrator accounts have full access to all configurable options.<br>
                        Guest accounts can view the current configuration but cannot change anything.<br>
                        Developer accounts are the same as administrators, but also see debugging information.<br>
                        <h3>Enter new user details</h3>
                            <form action="users_new.lua" method="post" onsubmit="return validateForm(this);" name="form1">
                                <table border=0 cellspacing=10 valign="top">
                                    <tr>
                                        <td>Username:</td>
                                        <td><input type="text" name="new_user"></input></td>
                                    </tr>
                                    <tr>
                                        <td>Account type:</td>
                                        <td>
                                            <SELECT name="type">
                                                <OPTION value="0">Administrator</OPTION>
                                                <OPTION value="1">Guest</OPTION>
                                                <OPTION value="2">Developer</OPTION>
                                            </SELECT>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Password:</td>
                                        <td><input type="password" name="new_pass"></input></td>
                                    </tr>
                                    <tr>
                                        <td>Retype Password:</td>
                                        <td><input type="password" name="new_rpass"></input></td>
                                    </tr>
                                </table>
                                <br><hr><br>
                                <input type="button" value="Back to Users" onclick="window.location.assign('users_manage.lua');"/> 
                                <input type="submit" name="createbutton" value="Add New User"></input>
                            </form>
                    </div>
                    <br>
]]

print(header..content..footer)
