var weather = {
  init:function(){},
  isEmpty:function(data){
      return data.replace(/^\s+|\s+$/g,"").length == 0;
  },
  isValidZip:function(data){
     var filter = /^[0-9]{5,5}$/;
      return filter.test(data);
  },
  submitChangeWeatherForm:function(weatherForm){
     href = weatherForm.action;
     error = false;
     field = $("#weather_zip #zip");
     if(weather.isEmpty(field.val()) || !weather.isValidZip(field.val())){
        error = true;
        field.attr('style','color:#ff0000');
     } else {
       field.removeAttr( 'style' );
     }
     if(error) return false;
     $.cookie('weatherZip',field.val(),{expires:365});
     weather.getWeatherByZip(href);
     return false;
  },
  bindWeatherForm:function(){
       $('#weather-submit-button').bind("click",function(){
          weather.submitChangeWeatherForm($("#weather-form")[0]);
          return false;
       });
       $('#weather-form').submit(function(){
          weather.submitChangeWeatherForm($("#weather-form")[0]);
          return false;
       });
       $('#zip').bind("focus",function(){
          field = $(this);
          field.addClass("rolledover");
          field.removeAttr( 'style' );
          if(!weather.isValidZip(field.val())){
             field.value = '';
          }
       });
  },
  getWeatherByZip:function(href){
    $('#weatherModule').load(href,weather.bindWeatherForm);
    return false;
  }
};

$(document).ready(function(){
  weather.bindWeatherForm();
});
