function QuoteLoginTriesExceeded( id , req ) {
	QuoteLoginWarn( id , aJSLangDefs.Login_tries_exceeded );
}

function QuoteLoginWarn( id , msg ) {
	var warn = $("#"+id+"_warning");
	if (!warn.length) {
		warn = $("<div></div>" );
		warn.attr( "id" , id + "_warning" );
		warn.addClass( "QuoteLoginWarning" );
		$("#"+id+"_cont").append( warn );
	}
	warn.html( msg );
}

function QuoteLoginFailed( id , req ) {
	var pwd = $("#"+id+"_pwd");
	pwd.val( "" );

	var lnk = $("<a></a>").addClass( "nrm" ).attr("target","_blank").attr( "href" , req.url_forgotten_password ).html( req.ttl_forgotten_password );
	$("#offer_contact_name_comment").html( "" );
	$("#offer_contact_name_comment").append( lnk );

	QuoteLoginWarn( id , aJSLangDefs.login_Failed );
}

function QuoteLoginClose( id ) {
	if (typeof $("#"+id).data("isopened") == "undefined") return;
	if (!$("#"+id).data("isopened")) return;
	$("#"+id).css("width", "" );
	$("#"+id+"_cont_span").html( "" );
	$("#"+id+"_warning").html( "" );

	$("#"+id).data("isopened",false);
}

function QuoteLoginOpen( id ) {
	if ($("#"+id).data("isopened")) return;

	$("#"+id).css("width", 120 );
	var cont = $("#"+id+"_cont_span");
	if (cont.length == 0) {
		cont = $("<span></span>").attr( "id" , id + "_cont_span" );
		$("#"+id+"_cont").append( cont );
	}
	cont.append( aJSLangDefs.login_Password + ":" );
	var pwd = $("<input></input>");
	pwd.attr( "type" , "password" );
	pwd.attr( "name" , id + "_pwd" );
	pwd.attr( "id" , id + "_pwd" );
	pwd.addClass( "FrontendTextbox" );
	pwd.addClass( "QuoteLoginEmailPassword" );


	pwd.keydown( function(e) { return QuoteLoginPasswordDoNotSubmit(id,e); } );
	
	var smb = $("<input></input>" );
	smb.attr( "type" , "button" );
	smb.attr( "id" , id + "_login" );
	smb.attr( "name" , id + "_login" );
	smb.addClass( "minismb" );
	smb.click( function() {
		QuoteLoginOnTheFly( id );
	} );
	smb.val( aJSLangDefs["login_Login"] );
	cont.append( " " );
	cont.append( pwd );
	cont.append( " " );
	cont.append( smb );
	pwd.focus();
	$("#"+id).data("isopened",true);
}

function QuoteLoginUser( id , req ) {
	var uname = $("#"+id).val();
	var pwd = $("#"+id+"_pwd").val();
	var cont = $("#"+id+"_cont");
	cont.html( uname );
	var ql = $("#"+id );
	ql.data( "loggedin" , true );	
	ql.data( "opened" ,false );
	
	$("#offer_contact_name_comment").html( "" );
	if (typeof RefreshFrontendMenu != "undefined") RefreshFrontendMenu();
	if (typeof RefreshLoginBox != "undefined") RefreshLoginBox();
	if (typeof RefreshQuoteName != "undefined") RefreshQuoteName( "offer_contact_name" , req.user_name );
	if (typeof RefreshQuotePhone != "undefined") RefreshQuotePhone( "offer_contact_phone" , req.contact_phone );
}

function QuoteLoginOnTheFly( id ) {
	var ql = $("#"+id);
	if (ql.data( "loggedin" )) return;	// vazze ag
	var u_name = ql.val();
	if (!u_name.length) {
		// alert( aJSLangDefs.quotepage_error_Contact_email_not_filled );
		return;
	}
	var passwd = $("#"+id+"_pwd").val();
	// tehat meg nincs belepve
	var cont = $("#"+id+"_cont");
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					inajax: 1,
					ajaxevent: "login-on-the-fly",
					uname: u_name,
					pwd: passwd
				}
			),
			success: function( r ) {
				req = eval( r );
				if (typeof req.logins_exceeded!="undefined") {
					// login tries exceeded
					QuoteLoginTriesExceeded( id , req );
				} else if (typeof req.login_failed!="undefined") {
					// login failed
					QuoteLoginFailed( id , req );
				} else if (typeof req.user_existing!="undefined") {
					// existing user, should bring up password
					if (req.user_existing) QuoteLoginOpen( id , req );
				} else if (typeof req.login_succeeded!="undefined") {
					// login succeeded
					QuoteLoginUser( id , req );
				}
			}
		}
	);
}

function QuoteLoginUserLostFocus( id ) {
	if ($("#"+id).val().length==0) return;
	// megnezzuk, hogy van-e nalunk ilyen user
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					inajax: 1,
					ajaxevent: "login-email-request",
					email: $("#"+id).val()
				}
			),
			success: function( r ) {
				req = eval( r );
			}
		}
	);
}


function QuoteLoginEmailDoNotSubmit( o , e) {
	if (e.keyCode==13) {
		o.focusNextInputField();
		return false;
	}
}

function QuoteLoginPasswordDoNotSubmit( id , e ) {
	if (e.keyCode==13) {
		QuoteLoginOnTheFly( id );
		return false;
	}
}

$.fn.focusNextInputField = function() {
    return this.each(function() {
        var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
        var index = fields.index( this );
        if ( index > -1 && ( index + 1 ) < fields.length ) {
			fields.eq( index ).blur();
            fields.eq( index + 1 ).focus();
        }
        return false;
    });
};

function InitQuoteLoginEmail( id , loggedin ) {
	var ql = $("#"+id);
	ql.data( "loggedin" , loggedin );
	if (!loggedin) ql.data( "opened" , false );
	if (ql.attr("type")=="text") {
		ql.blur( function() { QuoteLoginUserLostFocus( id ) } );
	}
	ql.keypress( function(e) { return QuoteLoginEmailDoNotSubmit(ql,e); } );
	ql.keydown( function(e) { return QuoteLoginEmailDoNotSubmit(ql,e); } );
}

