<%@ Page Language="C#" AutoEventWireup="true"  Culture="auto:en-US" meta:resourcekey="PageResource1" UICulture="auto:en-US" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" runat="server" dir="<%$ Resources: HTMLDirectionality %>" >
<head runat="server">
    <title>Currency Exchange Calculator</title>
</head>
<script runat="server">
    
    protected override void InitializeCulture()
    { 
        // override virtual method InitializeCulture() to check if profile contains a user language setting
        string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
        if ( UserCulture != "")
        { 
            // there is a user language setting in the profile: switch to it
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
        }
    }
    
    private void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack || (string)Session["CultureName"] != CultureInfo.CurrentCulture.Name)
        {
            DateTimePlaceholder.Text = DateTime.Now.ToLongDateString();

            // Populate the language dropdown with the list of available languages on the server
            string[] ResLangs = {"ar","de","es","fr","ja","en"};
            DropDownLanguage.Items.Clear();   // Clears the dropdown in case of a culture change
            foreach (string ResLanguage in ResLangs)
            {
                CultureInfo TempCultureInfo = new CultureInfo(ResLanguage);
                ListItem ResourceLanguage = new ListItem(TempCultureInfo.NativeName, TempCultureInfo.Name);
                if (TempCultureInfo.Equals(CultureInfo.CurrentUICulture))
                {
                    ResourceLanguage.Selected = true;
                }
                DropDownLanguage.Items.Add(ResourceLanguage);
        
            }
            
            // Populate the currency drop-down with currency names
            // If possible set the default selected currency to the currency of the CurrentCulture
            RegionInfo UserRegion = new RegionInfo(CultureInfo.CurrentCulture.Name);
            RegionInfo TargetRegion;
            
            XmlTextReader ExchangeRateTextReader = new XmlTextReader(Server.MapPath("~/ExchangeRates.xml"));
            DataSet ExchangeRateDataSet = new DataSet("ds");
            ExchangeRateDataSet.ReadXml(ExchangeRateTextReader);
            DataTable CurrencyTable = ExchangeRateDataSet.Tables["Currency"];
            DataTableReader CurrencyTableReader = CurrencyTable.CreateDataReader();
            CurrencyDropDown.Items.Clear();   // Clears the dropdown in case of a culture change
            while (CurrencyTableReader.Read())
            {
                TargetRegion = new RegionInfo(CurrencyTableReader.GetString(0));
                ListItem Currency = new ListItem((string)GetGlobalResourceObject("CurrencyNames", TargetRegion.Name), CurrencyTableReader.GetString(1));
                if (TargetRegion.ISOCurrencySymbol == UserRegion.ISOCurrencySymbol)
                {
                    Currency.Selected = true;
                }
                CurrencyDropDown.Items.Add(Currency);
            }
            Session["CultureName"] = CultureInfo.CurrentCulture.Name;
        }

    }

    private string CalculateExchangeValue(string Rate)
    {
        double Value;
        try
        {
            Value = double.Parse(ConvertValue.Text);
        }
        catch
        {
            Value = 0;
        }
        double ExchangeValue = (double.Parse(Rate, CultureInfo.InvariantCulture) / double.Parse(CurrencyDropDown.SelectedItem.Value, CultureInfo.InvariantCulture)) * Value;
        
        return ExchangeValue.ToString("F");
    }

    private void ConvertButton_Click(object sender, EventArgs e)
    {
        ExchangeGrid.DataBind();
    }

    protected void DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        string SelectedLanguage = DropDownLanguage.SelectedValue.ToString();
        //Save selected user language in profile 
        Profile.SetPropertyValue("PreferredCulture", SelectedLanguage);

        //Force re-initialization of the page to fire InitializeCulture()
        Response.Redirect(Request.Url.LocalPath);
    }
</script>
<body bgcolor="moccasin">
    <form id="form1" runat="server">
    <div>
        <h2>
            <asp:Label ID="LabelTitle" runat="server" Text="<%$ Resources:LabelResource1.Text %>" BackColor="<%$ Resources:BackgroundColor %>"></asp:Label>&nbsp;</h2>
        <asp:Label ID="DateTimePlaceholder" runat="server" Text="<%$ Resources:LabelResource2.Text %>" BackColor="<%$ Resources:BackgroundColor %>"></asp:Label><br />
        <asp:DropDownList ID="DropDownLanguage" runat="server" OnSelectedIndexChanged="DropDownLanguage_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList><br /><br />
        <asp:Label ID="LabelInstruction" runat="server" Text="Enter an amount, choose a currency and click the Convert button to calculate the exchange values." meta:resourcekey="LabelResource3"></asp:Label>
        <br />
        &nbsp;<asp:TextBox ID="ConvertValue" runat="server" meta:resourcekey="TextBoxResource1" Text="100"></asp:TextBox>&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="CurrencyDropDown" runat="server" meta:resourcekey="DropDownListResource1">
        </asp:DropDownList>
        <asp:Button ID="ConvertButton" runat="server" Text="Convert" OnClick="ConvertButton_Click" meta:resourcekey="ButtonResource1" />
        <asp:XmlDataSource DataFile="ExchangeRates.xml" ID="XmlExchangeRates"
            runat="server"></asp:XmlDataSource>
        &nbsp;&nbsp;
        <br />
        <asp:GridView AutoGenerateColumns="False" DataSourceID="XmlExchangeRates" ID="ExchangeGrid"
            runat="server" meta:resourcekey="GridViewResource1" >
            <Columns>
                <asp:TemplateField HeaderText="Name" meta:resourcekey="TemplateFieldResource1">
                    <ItemTemplate>
                        <asp:Label ID="ColumnCurrency" runat="server" Text='<%# (string)GetGlobalResourceObject("CurrencyNames",(string)Eval("Region")) %>' meta:resourcekey="LabelResource4"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Exchange Value" meta:resourcekey="TemplateFieldResource2">
                    <ItemTemplate>
                        <asp:Label ID="ColumnExchangeValue" runat="server" Text='<%# CalculateExchangeValue((string)Eval("Rate")) %>' meta:resourcekey="LabelResource5"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Right" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <asp:Localize runat="server" meta:resourcekey="LiteralResource1">Disclaimer: The rate calculation above is based on 
exchange rates published on Dec 9th, 2004 by the United States Federal Reserve 
Bank of New York. For up-to-date exchange rates please go to the website of this 
organization. This sample application is not intended to provide accurate or 
up-to-date currency conversions.</asp:Localize>
        </div>
    </form>
</body>
</html>
