var supported = document.getElementById && document.createElement;

// Error messages

var errorIncomplete = "Please fill in all required fields.";
var errorUsernameTaken = "Sorry, but an account with that name already exists!";
var errorPasswordMismatch = "Please enter matching passwords.";
var errorEmailMismatch = "Please enter matching email addresses.";

function redirect(url)
{
    document.location = url;
}

// Class definition for Field object
function Field(id, name)
{
    this.id = id;
    this.name = name;
}

function focusForm(id)
{
    if (supported) {
        document.getElementById(id).focus();
    }
}

function showError(msg)
{
    if (supported) {
        var msgElement = document.createElement('p');
        msgElement.setAttribute('class', 'error');
        var msgText = document.createTextNode(msg);
        msgElement.appendChild(msgText);
        var msgContainer = document.getElementById('message');
        if (msgContainer.childNodes.length > 0) {
            msgContainer.replaceChild(msgElement, msgContainer.firstChild);
        } else {
            msgContainer.appendChild(msgElement);
        }
    }
}

function highlightField(id)
{
    if (supported) {
        document.getElementById(id).style.backgroundColor = "#FAEBE6";
    }
}

function resetFields(fields)
{
    if (supported) {
        for (var f in fields) {
            document.getElementById(fields[f].id).style.backgroundColor = "#FFF";
        }
    }
}

function checkCreationForm()
{
    var okay = true;

    var username = document.getElementById('user').value;

    var pwd1 = document.getElementById('password').value;
    var pwd2 = document.getElementById('password2').value;
    if (pwd1 != pwd2) {
        okay = false;
        showError(errorPasswordMismatch);
    }

    var email1 = document.getElementById('email').value;
    var email2 = document.getElementById('email2').value;
    if (email1 != email2) {
        okay = false;
        showError(errorEmailMismatch);
    }

    if ((username == '') || (pwd1 =='') || (pwd2 == '') || 
        (email1 == '') || (email2 == '')) {
        okay = false;
        showError(errorIncomplete);
    }
    
    return okay;
}

