
/**
 * Login form submission
 */
function login(url){

    $("#loginMessage").empty();
    if(!($("#username").val()) || !($("#password").val())){
	$(js_t.get("<strong>Username and/or Password missing</strong>")).appendTo("#loginMessage");
        $('#loginMessage').slideDown();
	return false;
    }


    $("#loadingOverlay").expose({closeOnEsc : false, closeOnClick : false,  api: true, onLoad : function() { this.getExposed().css({'display' : 'block'}); }, onClose : function() { this.getExposed().css({'display' : 'none'}); }}).load();

    $.post( url, { "username" : $("#username").val(), "password" : $("#password").val() },
	    function(data){
		if(data.url){
		    location.href = data.url;
		    return false;
		}
		else{
		    $("#loadingOverlay").expose().close();
		    $("<strong>" + data.msg + "</strong>").appendTo("#loginMessage");
                    $('#loginMessage').show();
		    return false;
		}
	    },"json");

    return false;
}

/**
 * Popup "forgot password" dialog
 */

function popForgotPassword(url){
    $('<div id="retrievePassword" style="display:none"><form id="retrievePasswordForm" onSubmit="retrievePassword(\'' + url +'\');"><table width="350" cellspacing="5"><tr><td width="60">' + js_t.get("Username:") + '</td><td><input type="text" name="curUsername" id="curUsername" value="" /></td></tr><tr><td>' + js_t.get("Email:") + '</td><td><input type="text" name="email" id="email" value=""/></td></tr><tr><td></td><td>' + js_t.get("Your new password will be sent to your account's email address") + '</td></tr></table></form></div>').dialog({title : "Password Recovery", "resizable" : false, "width" : "500px", position : ['center',200], buttons: { "cancel": function() { $(this).dialog("close"); }, "submit" : function(){retrievePassword(url)}}});

    //Replaces button text with translated strings.
    $("button[type='button']").each(function(){
                                        $(this).html(js_t.get($(this).html()));
                                    });

}

/**
 * Post "forgot password" dialog
 */

function retrievePassword(url){
    $.ajax({"url" : url,
	    type : "post",
	    data : $("#retrievePasswordForm").serialize(),
	    success : function(data){
		if(data.status == "success"){
                    $("#retrievePassword").dialog("close");
		    $("<p>" + data.msg + "</p>").dialog({title : js_t.get("Password Sent")  , position : ['center',200] , buttons: { "OK": function() { $(this).dialog("close"); }}});
		} else
		{
		    $("<ul>" + data.msg + "</ul>").dialog({title : js_t.get("Problem retrieving account")  , position : ['center',200] , buttons: { "OK": function() { $(this).dialog("close"); }}});
		}
	    },
	    error :
	    function(){
		$("#retrievePassword").dialog("close");
		$(js_t.get("<p>There seems to have been an internal error, please try again. If the error persist please contact <a href=\"mailto:support@affeurope.com\">support@affeurope.com</a></p>")).dialog({title : js_t.get("Problem retrieving account")  , position : ['center',200] , buttons: { "OK": function() { $(this).dialog("close"); }}})
	    },
	    "dataType" : "json"});

    return false;
}