/* Signup uses jQuery to Run. */

// Set the code that runs when the page is ready
$(document).ready(function () {
                      // Add events
                      $('#next').click(addUser);
                      $('#myusername').focus();
                  });

// This function validates the form data
function addUser() {
    // Get the for data
    var username = $('#myusername').val();
    var password1 = $('#mypassword1').val();
    var password2 = $('#mypassword2').val();
    var trybeName = '';

    // Make sure we have the data we want
    if (username.length < 1) {
        showFaderMsg({msg: 'A Username is required. [GT_E0169]',
                      msgContainer: '#errMsg', msgIsErr: true, pauseDur:15000});
        $('#myusername').focus();
        return;
    }

    if ((password1.length < 1) || (password2.length < 1)) {
        showFaderMsg({msg: 'Both Password fields are required. [GT_E0170]',
                      msgContainer: '#errMsg', msgIsErr: true, pauseDur:15000});
        $('#mypassword1').focus();
        return;
    }

    // If we have no trybeID, make sure something is checked
    //if (TID.length < 1) {
    if (ALLOWCHOOSETRYBE == '1') {
        if ($(':radio[checked]').length < 1) {
            showFaderMsg({msg: 'A Trybe must be selected. [GT_E0175]',
                          msgContainer: '#errMsg', msgIsErr: true, pauseDur:15000});
            return;
        }

        trybeName = $(':radio[checked]').val();
    } else {
        trybeName = TRYBENAME;
    }

    // Add the user
    jQuery.getJSON(BASEURL+'users/addInitial',
                   {loginid: username, pword1: password1, pword2: password2, mgname: MGNAME,
                   promocode: PC, trybename: trybeName, amid: AMID, syn: SYN, ack: ACK,
                   synlist:genS(['loginid','mgname','trybename','promocode','amid']),
                   acklist:genH(username + MGNAME + trybeName + PC + AMID), rand: Math.random()},
                   function(data, textStatus) {
                       // Decode the JSON
                       results = data;

                       // Check to make sure there was no error
                       if (!results.success) {
                           // Show a message
                           showFaderMsg({msg: 'The account could not be created.&nbsp;[' + results.errorNumber + ']&nbsp;' + results.extendedText,
                                        msgContainer: '#errMsg', msgIsErr: true, pauseDur:15000});
                           return;
                       }

                       // The account should now be created so inject some
                       // stuff on the form. Doing things this way prevents
                       // us from "showing our cards" to potential snoopers
                       $('#regForm').append('<input type="hidden" name="newuserid" value="' + results.additionalData + '" />');
                       $('#regForm').submit();
                   });
}

