function documentready() {
    $("#login").click(function(e) {
	// Stop the normal click
	e.preventDefault();
	return logincb();
    });
    $('#signup').click(function(e) {
	// Stop the normal click
	e.preventDefault();
	signup();
    });
    if (!("autofocus" in document.createElement("input"))) {
	document.getElementById("username").focus();
    }
    $(function(){
	$("#username").add("#pass").keydown(function(e){
	    if (e.keyCode == 13) {
		return logincb();
	    }
	});
    });
}

function logincb(){
    var user = $("#username").val();
    var pass = $("#pass").val();

    if (!user) {
	errmsg("Email is empty",
	    "Email should not be empty");
	return false;
    }

    if (!pass){
	errmsg("Password is empty",
	    "Password should not be empty");
	return false;
    }

    var args = user + " " + pass;
    sendrequest('login', args, logincallback);
}

function logincallback (data){
    if (!data) {
	errmsg('No data in response.');
	return false;
    }
    if (data.result == "Ok"){
	window.location = "/parties.html";
    } else {
	errmsg(data.result, data.longresult)
    }
}

function signup() {
    var user = $('#signusername').val();
    var pass = $('#signpass').val();
    var pass2 = $('#signpass2').val();
    var cname = $('#signcharacter').val();
    var race = $('#signrace').val();
    var prof = $('#signprof').val();

    if (!user) {
	errmsg("Email is empty",
	    "Email should not be empty");
	return false;
    }

    if (!cname) {
	errmsg("Character name is empty",
	    "Character name should not be empty");
	return false;
    }

    if (!pass || pass.length < 6){
	errmsg("Password too short",
	    "Password must be at least 6 characters");
	return false;
    }

    if (pass != pass2) {
	var pass = $('#signpass').focus();
	errmsg("Passwords don't match",
	    "The passwords you just typed don't match.");
	return false;
    }

    var args = user + " " + pass + " " + cname + " " + race + " " + prof;
    sendrequest('createall', args, signupcallback);
}

function signupcallback (data){
    if (!data) {
	errmsg('No data in response.');
	return false;
    }
    if (data.result == "Ok"){
	window.location = "/quest.html";
    } else {
	errmsg(data.result, data.longresult);
    }
}

/* vim: set sw=4 sts=4 syn=javascript : */


