﻿var settingsScreen;
var oldCountry;
var newCountry="";
var countryHasFocus;
var keyArr=new Array("abcdefghijklmnopqrstuvwxyz");
var capsArr=new Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
var numArray = new Array("0123456789");


function OnSettingsScreenLoaded(sender)
{
    settingsScreen = sender.findName("SettingsScreen");
    if (units == unitType.english)
        OnSetFahrenheit(sender);
    else
        OnSetCelsius(sender);
}

function OnSetFahrenheit(sender, args)
{
    //alert("OnSetFahrenheit");
    // Toggle the radio buttons
    settingsScreen.findName("SettingsCelsiusSelected").opacity = 0;
    settingsScreen.findName("SettingsFahrenheitSelected").opacity = 1;
    
}

function OnSetCelsius(sender, args)
{
    //alert("OnSetCelsius");
    // Toggle the radio buttons
    settingsScreen.findName("SettingsCelsiusSelected").opacity = 1;
    settingsScreen.findName("SettingsFahrenheitSelected").opacity = 0;
}

function OnCountryClicked(sender, args)
{
    //alert("focus");    
    countryHasFocus = true;
    var countryText = settingsScreen.findName("CountryText");
    
    // Cache the old country text in case the user cancels
    oldCountry = countryText.text;
    newCountry = "";
    countryText.text = "|";
}

function OnSettingsKeyDown(e)
{
    // NOTES:
    // 9 = SPACE
    // 1 = BACKSPACE
    // 3 = ENTER
    // 8 = ESCAPE
    
    //alert("OnSettingsKeyDown");
    if(countryHasFocus)
    {
        var countryText = settingsScreen.findName("CountryText");
        writeLine("e.platformKeyCode:" + e.platformKeyCode);
        writeLine("e.key:" + e.key);
	    //alert(e.key)
	    if(e.key>=30 && e.key<=55)
	    {
		    var tf_val = countryText.text;
		    var char_val=e.key-30;
    	    var letter = "";
	        if(e.shift)
	        {
    		    letter=capsArr[0].substr(char_val,1);
    		}
    		else
    		{
    		    letter=keyArr[0].substr(char_val,1);
    		}
    		newCountry = newCountry + letter;
        }
        if(e.key >= 20 && e.key <= 29)
	    {
		    var tf_val = countryText.text;
		    var char_val = e.key - 20;
    	    var letter = numArray[0].substr(char_val,1);
    		newCountry = newCountry + letter;
        }
        if(e.key >= 68 && e.key <= 77)
	    {
		    var tf_val = countryText.text;
		    var char_val = e.key - 68;
    	    var letter = numArray[0].substr(char_val,1);
    		newCountry = newCountry + letter;
        }
        if(e.platformKeyCode == 188)
	    {
		    newCountry = newCountry + ",";
        }
        else if(e.key == 9)
        {
    		newCountry = newCountry + ' ';
        }
        else if(e.key == 3)
        {
            countryHasFocus = false;
            countryText.text = newCountry;
            getLocationData(newCountry);
            return;
        }
        else if(e.key == 8)
        {
            countryHasFocus = false;
            countryText.text = oldCountry;
            return;
        }
        else if(e.key == 1)
        {
            newCountry = newCountry.slice(0, newCountry.length - 1);
        }

	    countryText.text = newCountry + '|';
    }
}

var locationUrl = WebRoot + "/DataService/GetLocation.ashx?ss=";
function getLocationData(search)
{
    var url = locationUrl + escape(search);
    var dataReq = new httpReq(url, parseLocation);
    writeLine("loading url: " + url);
    dataReq.load();
}

function parseLocation()
{
    var locationData = this.getJsonObj();

    if (null != locationData)
    {
        try
        {
            ShowCityPopup(null, null);
            populateCityRecords(locationData);        
        }
        catch(error)
        {
            writeLine(error.name + ": " + error.description);
        }
    }
}