// static
void LLPanelLogin::setFields(const LLSavedLoginEntry& entry, bool takeFocus)
{
	if (!sInstance)
	{
		llwarns << "Attempted setFields with no login view shown" << llendl;
		return;
	}

	LLCheckBoxCtrl* remember_pass_check = sInstance->getChild<LLCheckBoxCtrl>("remember_check");
	LLComboBox* login_combo = sInstance->getChild<LLComboBox>("first_name_combo");
	login_combo->setLabel(entry.getFirstName());
	login_combo->resetDirty();
	login_combo->resetTextDirty();

	LLLineEditor* last_name = sInstance->getChild<LLLineEditor>("last_name_edit");
	last_name->setText(entry.getLastName());
	last_name->resetDirty();

	if (entry.getPassword().empty())
	{
		sInstance->childSetText("password_edit", std::string(""));
		remember_pass_check->setValue(LLSD(false));
	}
	else
	{
		const std::string filler("123456789!123456");
		sInstance->childSetText("password_edit", filler);
		sInstance->mIncomingPassword = filler;
		sInstance->mMungedPassword = entry.getPassword();
		remember_pass_check->setValue(LLSD(true));
	}

	LLComboBox* server_combo = sInstance->getChild<LLComboBox>("server_combo");
	if (server_combo->getSimple() != entry.getGridName())	// Avoid loops.
	{
		server_combo->setSimple(entry.getGridName());	// Same string as used in login_show().
	}

	LLViewerLogin* vl = LLViewerLogin::getInstance();

	if (entry.getGrid() == GRID_INFO_OTHER)
	{
		vl->setGridURI(entry.getGridURI().asString());
		vl->setHelperURI(entry.getHelperURI().asString());
		vl->setLoginPageURI(entry.getLoginPageURI().asString());
	}

	EGridInfo entry_grid = entry.getGrid();

	if (entry_grid == GRID_INFO_OTHER || entry_grid != vl->getGridChoice())
	{
		vl->setGridChoice(entry_grid);

		// grid changed so show new splash screen (possibly)
		loadLoginPage();
	}

	if (takeFocus)
	{
		giveFocus();
	}
}
void LLPanelLogin::loadLoginPage()
{
	if (!sInstance) return;
	
	std::ostringstream oStr;

	LLViewerLogin* vl = LLViewerLogin::getInstance();
	std::string login_page = vl->getLoginPageURI();
	if (login_page.empty())
	{
		login_page = sInstance->getString( "real_url" );
		vl->setLoginPageURI(login_page);
	}
	oStr << login_page;
	
	// Use the right delimeter depending on how LLURI parses the URL
	LLURI login_page_uri = LLURI(login_page);
	std::string first_query_delimiter = "&";
	if (login_page_uri.queryMap().size() == 0)
	{
		first_query_delimiter = "?";
	}

	// Language
	std::string language = LLUI::getLanguage();
	oStr << first_query_delimiter<<"lang=" << language;
	
	// First Login?
	if (gSavedSettings.getBOOL("FirstLoginThisInstall"))
	{
		oStr << "&firstlogin=TRUE";
	}

	// Channel and Version
	std::string version = llformat("%d.%d.%d (%d)",
						LL_VERSION_MAJOR, LL_VERSION_MINOR, LL_VERSION_PATCH, LL_VIEWER_BUILD);

	char* curl_channel = curl_escape(LL_CHANNEL, 0);
	char* curl_version = curl_escape(version.c_str(), 0);
	char* curl_t = curl_escape(LLAppViewer::instance()->getWindowTitle().c_str(), 0);

	oStr << "&channel=" << curl_channel;
	oStr << "&version=" << curl_version;
	oStr << "&t=" << curl_t;
	if(LL_CHANNEL != EMERALD_RELEASE_CHANNEL)
		oStr << "&unsupported=1";

	curl_free(curl_channel);
	curl_free(curl_version);
	curl_free(curl_t);

	// grid=blah code was here. Due to the implementation of the Emerald login manager, sending
	// this information is a very bad idea. Don't do it.

	gViewerWindow->setMenuBackgroundColor(false, !LLViewerLogin::getInstance()->isInProductionGrid());
	gLoginMenuBarView->setBackgroundColor(gMenuBarView->getBackgroundColor());


#if USE_VIEWER_AUTH
	LLURLSimString::sInstance.parse();

	std::string location;
	std::string region;
	std::string password;
	
	if (LLURLSimString::parse())
	{
		std::ostringstream oRegionStr;
		location = "specify";
		oRegionStr << LLURLSimString::sInstance.mSimName << "/" << LLURLSimString::sInstance.mX << "/"
			 << LLURLSimString::sInstance.mY << "/"
			 << LLURLSimString::sInstance.mZ;
		region = oRegionStr.str();
	}
	else
	{
		if (gSavedSettings.getBOOL("LoginLastLocation"))
		{
			location = "last";
		}
		else
		{
			location = "home";
		}
	}
	
	std::string firstname, lastname;

    if(gSavedSettings.getLLSD("UserLoginInfo").size() == 3)
    {
        LLSD cmd_line_login = gSavedSettings.getLLSD("UserLoginInfo");
		firstname = cmd_line_login[0].asString();
		lastname = cmd_line_login[1].asString();
        password = cmd_line_login[2].asString();
    }
    	
	if (firstname.empty())
	{
		firstname = gSavedSettings.getString("FirstName");
	}
	
	if (lastname.empty())
	{
		lastname = gSavedSettings.getString("LastName");
	}
	
	char* curl_region = curl_escape(region.c_str(), 0);

	oStr <<"firstname=" << firstname <<
		"&lastname=" << lastname << "&location=" << location <<	"&region=" << curl_region;
	
	curl_free(curl_region);

	if (!password.empty())
	{
		oStr << "&password="******"&password=$1$" << password;
	}
	if (gAutoLogin)
	{
		oStr << "&auto_login=TRUE";
	}
	if (gSavedSettings.getBOOL("ShowStartLocation"))
	{
		oStr << "&show_start_location=TRUE";
	}	
	if (gSavedSettings.getBOOL("RememberPassword"))
	{
		oStr << "&remember_password=TRUE";
	}	
	BOOL show_server = sInstance ? sInstance->mShowServerCombo : FALSE;
	if (show_server || gSavedSettings.getBOOL("ForceShowGrid"))
	{
		oStr << "&show_grid=TRUE";
	}
#endif
	
	LLMediaCtrl* web_browser = sInstance->getChild<LLMediaCtrl>("login_html");
	
	// navigate to the "real" page 
	web_browser->navigateTo( oStr.str(), "text/html" );
}
// static
void LLPanelLogin::onSelectServer(LLUICtrl*, void*)
{
	// *NOTE: The parameters for this method are ignored.

	// This function is only called by one thread, so we can use a static here.
	static bool looping;
	if (looping) return;
	looping = true;

	// LLPanelLogin::onServerComboLostFocus(LLFocusableElement* fe, void*)
	// calls this method.

	// The user twiddled with the grid choice ui.
	// apply the selection to the grid setting.
	std::string grid_name;
	S32 grid_index;

	LLComboBox* combo = sInstance->getChild<LLComboBox>("server_combo");
	LLSD combo_val = combo->getValue();

	if (LLSD::TypeInteger == combo_val.type())
	{
		grid_index = combo->getValue().asInteger();
		grid_name = combo->getSimple();
	}
	else
	{
		// no valid selection, return other
		grid_index = (S32)GRID_INFO_OTHER;
		grid_name = combo_val.asString();
	}

	// This new selection will override preset uris
	// from the command line.
	LLViewerLogin* vl = LLViewerLogin::getInstance();

	if(grid_index != GRID_INFO_OTHER)
	{
		vl->setGridChoice((EGridInfo)grid_index);
	}
	else
	{
		vl->setGridChoice(grid_name);
	}

	// Find a saved login entry that uses this grid, if any.
	bool found = false;
	LLSavedLoginsList const& entries = sInstance->mLoginHistoryData.getEntries();
	for (LLSavedLoginsList::const_reverse_iterator i = entries.rbegin(); i != entries.rend(); ++i)
	{
		if (!i->asLLSD().isMap())
		{
			continue;
		}
		if (i->getGridName() == grid_name)
		{
		  	if (!vl->nameEditted())
			{
				// Change the other fields to match this grid.
				LLPanelLogin::setFields(*i, false);
			}
			else	// Probably creating a new account.
			{
				// Likely the current password is for a different grid.
				clearPassword();
			}
			found = true;
			break;
		}
	}
	if (!found)
	{
		clearPassword();

		// If the grid_name starts with 'http[s]://' then
		// we have to assume it's a new loginuri, set
		// on the commandline.
		if (grid_name.substr(0, 4) == "http")
		{
			// Use it as login uri.
			vl->setGridURI(grid_name);
			// And set the login page if it was given.
			std::string loginPage = gSavedSettings.getString("LoginPage");
			std::string helperURI = gSavedSettings.getString("CmdLineHelperURI");
			if (!loginPage.empty()) vl->setLoginPageURI(loginPage);
			if (!helperURI.empty()) vl->setHelperURI(helperURI);
		}
	}

	// grid changed so show new splash screen (possibly)
	loadLoginPage();

	looping = false;
}