require "scripts/style"
require "scripts/widgets/shade"

MyFont = {
  standardFont,
  30,
  BlackColor
};

NameFont = {
  standardFont,
  15,
  BlackColor
};

local playername = gEnterNameValue;
local deleteselected = false;

if not playername or playername=="" then
	deleteselected = true;
	playername = GetString("enternameprompt") ;
end

BlockKeyboard("1");
PlaySound("POPUP-IN");
MakeDialog
{
	MakeShade(),
	PopBitmap
	{
		image="backgrounds/dialog",
		x=kCenter,
		y=kCenter,

    SetStyle(DialogTitleText),
			Text
			{
				name = "entername",
				label = "entername"
			};
	
	  SetStyle(DialogBodyText),
		Bitmap
		{
			image="backgrounds/textfield.png",
			x=150,
			y=170,
			scale = 1.1,

			TextEdit
			{
				font = NameFont,
				name = "nameedit",
				label = playername,
				flags = kVAlignCenter+kHAlignCenter,
				enablewindow = "enterok",
				utf8=true,
				clearinitial = deleteselected,
				x=4,y=0,w=kMax-4,h=kMax,
				length=9,
				ignore = kIllegalNameChars

			};
		};

		SetStyle( ButtonStyle ),

		Button
		{
			x=kCenter+80, y=kMax-50,

			name="enterok",
			label="ok",
			default=true,	-- Make this the default button
			command =
				function()
					local newname = GetLabel("nameedit");
					-- crop leading and trailing spaces
					newname = string.gsub( newname, "^%s+", "" );
					newname = string.gsub( newname, "%s+$", "" );
					if (string.len( newname )==0 ) then
						DisplayDialog {
							"scripts/ok.lua",
							body ="pleaseenteraname",
							title = ""
						};
						SetFocus( "nameedit" )
					elseif (newname ~= playername and IsNameInUse(newname)) then
						DisplayDialog {
							"scripts/ok.lua",
							body ="nameinuse",
							title = ""
						};
						SetFocus( "nameedit" )
					else
            BlockKeyboard("0");
						CloseWindow(newname);
					end
				end
		},

		Button
		{
			x=kCenter-80, y=kMax-50,

			name="entercancel",
			label="cancel",
			--cancel=true,
			command = function() 
        BlockKeyboard("0");
        CloseWindow(nil); 
      end;

		};
	},

}

SetFocus( "nameedit" );

if (deleteselected) then
	SetWindowVisible("enterok", false);
end


