$(document).ready(function(){

	$('#login_form input:first').focus();
    // Set up a handler to submit the login_form form
    $("#login_button").click(function(){
        var formErrorMessage = null;
    
        var missingField = null;
        if(!$("#charmgirlname").val())
            missingField = $("#charmgirlname").prev("label").text();
        if(!$("#charmgirlpass").val())
            missingField = $("#charmgirlpass").prev("label").text();
    
        if(missingField != null)
            formErrorMessage = $("#sharedServicesMessages .fieldRequired").html().replace("[FieldName]", missingField);

        if(formErrorMessage == null){
            $("#form_error").html("<img src=\"images/login/loading.gif\" alt=\"...\" />");
            $.post("loginSubmit.action", $("#login_form").serialize(), function(data, textStatus){
                if($(data).find("success").text() != ""){
                    $("#form_error").text("");
                    location.href = "profile.action";
                }
                else{
                    $("#form_error").text($("#loginMessages .loginFailed").text());
                }
            });
        }
        else{
            $("#form_error").text(formErrorMessage);
        }
        return false;
    }, "xml");
    
    // Set up a handler to submit the forgot_form form
    $("#forgot_button").click(function(){
        $("#forgot_error").html("<img src=\"images/login/loading.gif\" alt=\"...\" />");
        $.post("forgotPassword.action", $("#forgot_form").serialize(), function(data, textStatus){
            if($(data).find("error").text() != ""){
                $("#forgot_error").text(errorMessage);
            }
            else{
                $("#forgot_error").text("");
                $("#forgot_form").hide();
                $("#forgot_sent").show();
            }
        });
        return false;
    }, "xml");
    
	 // Submit form on enter keypress
	$(function() {
		$("form#login_form input").keypress(function (e) {
			if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
				$("#login_button").click();
				return false;
			} else {
				return true;
			}
		});
	});
});
