function formatWeather(field, objvalue) {
    var temp = '<tr><td align="right" valign="top">' + field + ':</td><td valign="top">' + objvalue + '</td></tr>\n';
    return temp;
}
function stripPath(path) {															// Strips path information off and leaves filename
    if (path.indexOf('/') >= 0)
        path = path.substring(path.lastIndexOf('/') + 1);
    if (path.indexOf('.gif') >= 0)
        path = path.substring(0, path.indexOf('.gif'));
    return path;
}
function dhtmlLoadScript(url) {
    var e = document.createElement("script");
    e.src = url;
    e.type = "text/javascript";
    e.id = "weatherforecast"
    document.getElementsByTagName("head")[0].appendChild(e);
}
function get_weather(tempzip) {
    if (tempzip != null) {													// fix 12/14/2009 - added this line: stops weather widget from displaying if zip == null
        var refresh = 15 * (1000 * 60); //First number is number of minutes
        if (tempzip != '') {
            var url = "http://pipes.yahoo.com/pipes/pipe.run?location=" + tempzip + "&_id=d4e2a12485eaad0dad9c01e9f9e6457c&_render=json&_callback=receiveWeather"
            dhtmlLoadScript(url);
            setTimeout("get_weather('" + tempzip + "')", refresh);
        }
    } 																	// fix 12/14/2009 - added this line
}
var weatherheadermatrix = new Array();
//0 Clear Sky Header
//1 Light Wispy Clouds Header
//2 Puffy Clouds Header
//3 Extreme Weather Header
weatherheadermatrix['chance_of_rain'] = 1;
weatherheadermatrix['chance_of_snow'] = 1;
weatherheadermatrix['chance_of_storm'] = 1;
weatherheadermatrix['cloudy'] = 1;
weatherheadermatrix['dust'] = 2;
weatherheadermatrix['flurries'] = 3;
weatherheadermatrix['fog'] = 2;
weatherheadermatrix['haze'] = 2;
weatherheadermatrix['icy'] = 3;
weatherheadermatrix['mist'] = 1;
weatherheadermatrix['mostly_cloudy'] = 1;
weatherheadermatrix['mostly_sunny'] = 2;
weatherheadermatrix['partly_cloudy'] = 2;
weatherheadermatrix['rain'] = 1;
weatherheadermatrix['sandstorm'] = 2;
weatherheadermatrix['showers'] = 1;
weatherheadermatrix['sleet'] = 3;
weatherheadermatrix['smoke'] = 2;
weatherheadermatrix['snow'] = 3;
weatherheadermatrix['storm'] = 3;
weatherheadermatrix['sunny'] = 0;
weatherheadermatrix['thunderstorm'] = 3;
weatherheadermatrix['tornado'] = 3;

function receiveWeather(data) {
    //alert('done');
    try {
        var weatherdata = new Array();
        weatherdata['tempf'] = data.value.items[0].weather.current_conditions.temp_f.data;
        weatherdata['condition'] = data.value.items[0].weather.current_conditions.condition.data;
        weatherdata['windcondition'] = data.value.items[0].weather.current_conditions.wind_condition.data;
        weatherdata['humidity'] = data.value.items[0].weather.current_conditions.humidity.data;
        weatherdata['icon'] = stripPath(data.value.items[0].weather.current_conditions.icon.data);
        weatherdata['location'] = data.value.items[0].weather.forecast_information.city.data;
        weatherdata['date'] = data.value.items[0].weather.forecast_information.current_date_time.data;

        //Forecast Today
        if (data.value.items[0].weather.forecast_conditions[0]) {
            weatherdata['high0'] = data.value.items[0].weather.forecast_conditions[0].high.data;
            weatherdata['low0'] = data.value.items[0].weather.forecast_conditions[0].low.data;
            weatherdata['day0'] = data.value.items[0].weather.forecast_conditions[0].day_of_week.data;
        }
        //Forecast One Day
        if (data.value.items[0].weather.forecast_conditions[3]) {
            weatherdata['high1'] = data.value.items[0].weather.forecast_conditions[1].high.data;
            weatherdata['low1'] = data.value.items[0].weather.forecast_conditions[1].low.data;
            weatherdata['day1'] = data.value.items[0].weather.forecast_conditions[1].day_of_week.data;
            weatherdata['condition1'] = data.value.items[0].weather.forecast_conditions[1].condition.data;
            weatherdata['icon1'] = stripPath(data.value.items[0].weather.forecast_conditions[1].icon.data);
        }
        //Forecast Two Day
        if (data.value.items[0].weather.forecast_conditions[2]) {
            weatherdata['high2'] = data.value.items[0].weather.forecast_conditions[2].high.data;
            weatherdata['low2'] = data.value.items[0].weather.forecast_conditions[2].low.data;
            weatherdata['day2'] = data.value.items[0].weather.forecast_conditions[2].day_of_week.data;
            weatherdata['condition2'] = data.value.items[0].weather.forecast_conditions[2].condition.data;
            weatherdata['icon2'] = stripPath(data.value.items[0].weather.forecast_conditions[2].icon.data);
        }
        //Forecast Three Day
        if (data.value.items[0].weather.forecast_conditions[3]) {
            weatherdata['high3'] = data.value.items[0].weather.forecast_conditions[3].high.data;
            weatherdata['low3'] = data.value.items[0].weather.forecast_conditions[3].low.data;
            weatherdata['day3'] = data.value.items[0].weather.forecast_conditions[3].day_of_week.data;
            weatherdata['condition3'] = data.value.items[0].weather.forecast_conditions[3].condition.data;
            weatherdata['icon3'] = stripPath(data.value.items[0].weather.forecast_conditions[3].icon.data);
        }
        //Weather Header

        document.getElementById('weatherheader').style.backgroundImage = 'url(/Content/images/weather/header/header_' + weatherheadermatrix[weatherdata['icon']] + '.jpg)';
        if (ie6) {
            //document.getElementById('weatherbox').style.background = 'none';
            document.getElementById('weatherbox').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/Content/images/weather/icons_large/' + weatherdata['icon'] + '.png", sizingMethod="crop")';
            //document.getElementById('weatherbox').style.backgroundRepeat = 'no-repeat';
        } else {
            document.getElementById('weatherbox').style.backgroundImage = 'url(/Content/images/weather/icons_large/' + weatherdata['icon'] + '.png)';
        }
        document.getElementById('weatherdegrees').innerHTML = weatherdata['tempf'] + '&deg;';
        document.getElementById('weatherdetails').innerHTML = 'L' + weatherdata['low0'] + ' - H' + weatherdata['high0'] + '<br/>' + weatherdata['location'] + '<br/><span class="orangetext">Extended Forecast</span>';
        //Forecast Modal Window
        //Day 1
        document.getElementById('weather_day1').innerHTML = weatherdata['day1'];
        document.getElementById('weather_icon1').src = '/Content/images/weather/icons_small/' + weatherdata['icon1'] + '.gif';
        document.getElementById('weather_icon1').title = weatherdata['condition1'];
        document.getElementById('weather_low1').innerHTML = weatherdata['low1'] + '&deg;';
        document.getElementById('weather_high1').innerHTML = weatherdata['high1'] + '&deg;';
        //Day 2
        document.getElementById('weather_day2').innerHTML = weatherdata['day2'];
        document.getElementById('weather_icon2').src = '/Content/images/weather/icons_small/' + weatherdata['icon2'] + '.gif';
        document.getElementById('weather_icon2').title = weatherdata['condition2'];
        document.getElementById('weather_low2').innerHTML = weatherdata['low2'] + '&deg;';
        document.getElementById('weather_high2').innerHTML = weatherdata['high2'] + '&deg;';
        //Day 3
        document.getElementById('weather_day3').innerHTML = weatherdata['day3'];
        document.getElementById('weather_icon3').src = '/Content/images/weather/icons_small/' + weatherdata['icon3'] + '.gif';
        document.getElementById('weather_icon3').title = weatherdata['condition3'];
        document.getElementById('weather_low3').innerHTML = weatherdata['low3'] + '&deg;';
        document.getElementById('weather_high3').innerHTML = weatherdata['high3'] + '&deg;';
        //Extra Information
        document.getElementById('weather_condition').innerHTML = 'Weather: ' + weatherdata['condition'];
        document.getElementById('weather_windcondition').innerHTML = weatherdata['windcondition'];
        document.getElementById('weather_humidity').innerHTML = weatherdata['humidity'];
        document.getElementById('weather_date').innerHTML = weatherdata['date'];
    } catch (e) {
        //alert(e);
        document.getElementById('weatherbox').style.backgroundImage = 'url(/Content/images/spacer.gif)';
        document.getElementById('weatherdegrees').innerHTML = '&nbsp;&nbsp;';
        document.getElementById('weatherdetails').innerHTML = '&nbsp;&nbsp;<br/>&nbsp;';
    }
}

//shows weather on weatherbar based on current zip code
function show_weather_form() {
    $('weatherpanel1').setStyle('display', 'none');
    $('weatherpanel2').setStyle('display', 'block');
}

//processes zip code change
function change_zip() {
    var z = $("newlocation");
    if (z.value.length != 5 || !isFinite(z.value)) {
        return (alert("Please enter a valid zip code."));
    }
    $("locationform").action = '/home/changezip';
    $("locationform").submit();
}