﻿
function ClearDropDownList(element) {
    GetElementByID(element).options.length = 0;
}


function FillWeatherList(selIndexCity) {

    ClearDropDownList('ddlWeatherCities');

    try {

        var ddlIndex = 0;
        
        for (var i = 0; i < _ddlWtrCity.length; i++) {

            var weatherListValue = _ddlWtrCity[i].period1 + "||" + _ddlWtrCity[i].summary1 + "||" + _ddlWtrCity[i].temperature1 + "||" + _ddlWtrCity[i].icon1 + "||" + _ddlWtrCity[i].period2 + "||" + _ddlWtrCity[i].summary2 + "||" + _ddlWtrCity[i].temperature2 + "||" + _ddlWtrCity[i].icon2 ;
                                  
            GetElementByID('ddlWeatherCities').options[ddlIndex] = new Option(_ddlWtrCity[i].name, weatherListValue);
            ddlIndex++;
        }
        
        try {
            
            try {
                GetElementByID('ddlWeatherCities').selectedIndex = getCookie("ddlIndex");
            }catch(e) {
               GetElementByID('ddlWeatherCities').selectedIndex = selIndexCity;
            }
             

            if (GetElementByID('ddlWeatherCities').options.length > 0) {
                GetWeatherDataForSelectedCity();
            }
            else {
                GetElementByID('ddlWeatherCities').options[0] = new Option("No cameras present", 0);
                PicFail();
            }
        }
        catch (e) { alert("FillWeatherList " + e.message); }
    }
    catch (e) { alert("FillWeatherList " + e.message); }
}


function GetWeatherDataForSelectedCity() {

    SetTextByElement('Period1', GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[0]);

    SetTextByElement('Summary1', GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[1] + ", " +
                                          GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[2]);



    SetTextByElement('Period2', GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[4]);

    SetTextByElement('Summary2', GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[5] + ", " +
                                          GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[6]);
   
   GetElementByID('Icon1').src= GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[3];
   GetElementByID('Icon2').src = GetElementByID('ddlWeatherCities').options[GetElementByID('ddlWeatherCities').selectedIndex].value.split("||")[7];

   setCookie("ddlIndex", GetElementByID('ddlWeatherCities').selectedIndex);
   
}
