/*
 * clearingInput: a jQuery plugin
 *
 * clearingInput is a simple jQuery plugin that provides example/label text
 * inside text inputs that automatically clears when the input is focused.
 * Common uses are for a hint/example, or as a label when space is limited.
 *
 * For usage and examples, visit:
 * http://github.com/alexrabarts/jquery-clearinginput
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008 Stateless Systems (http://statelesssystems.com)
 *
 * @author   Alex Rabarts (alexrabarts -at- gmail -dawt- com)
 * @requires jQuery v1.2 or later
 * @version  0.1.1
 */

(function ($) {
  $.extend($.fn, {
    clearingInput: function (options) {
      var defaults = {blurClass: 'blur'};

      options = $.extend(defaults, options);

      return this.each(function () {
        var input = $(this).addClass(options.blurClass);
        var form  = input.parents('form:first');
        var label, text;

        text = options.text || textFromLabel() || input.val();

        if (text) {
          input.val(text);

          input.blur(function () {
            if (input.val() === '') {
              input.val(text).addClass(options.blurClass);
            }
          }).focus(function () {
            if (input.val() === text) {
              input.val('');
            }
            input.removeClass(options.blurClass);
          });

          form.submit(function() {
            if (input.hasClass(options.blurClass)) {
              input.val('');
            }
          });

          input.blur();
        }

        function textFromLabel() {
          label = form.find('label[for=' + input.attr('id') + ']');
          // Position label off screen and use it for the input text
          return label ? label.css({position: 'absolute', left: '-9999px'}).text() : '';
        }
      });
    }
  });
})(jQuery);

/* ODP/SFG Additions */

function cleartext(field){

    if (field.defaultValue == field.value) 
        field.value = '';
    else 
        if (field.value == '') 
            field.value = field.defaultValue;
    
}

function testZip(zipCode){
    reZip = new RegExp(/(^\d{5}$)/);
    firstChar = zipCode.substring(0, 1);
    secondChar = zipCode.substring(1, 2);
    if (firstChar != 4) {
        zipError();
        return false;
    };
    if (secondChar != 3 && secondChar != 4 && secondChar != 5) {
        zipError();
        return false;
    };
    if (!reZip.test(zipCode)) {
        zipError();
        return false;
    }
    return true;
    
}

function zipError(){
	alert("Please enter a valid five-digit Ohio zip code.");
}

function submitZip(page) {

  var zipCode = "";

  var zipCode = jQuery("#zipField").val();
    if (testZip(zipCode)) {
      
    jQuery('#eventsContainer').html('');

    var max = 3;

    if (page == 'home')
    {
    max = 20;
    }

      var string1 = "http://my.barackobama.com/page/event/search_results?orderby=day&zip_radius[0]=";
      var string2 = "&zip_radius[1]=25&zip_radius[2]=US&country=*&event_type[]=286&event_type[]=287&event_type[]=281&limit=";
      var string3 = "&radius_unit=miles&format=commons_rss&wrap=no";

      var url = string1 + zipCode + string2 + max + string3;
    
     if (page == 'home')
     {
        jQuery('#eventsContainer').feedfetcher_nodesc({
		feed_source: url,
		max_entries: 20
         });
     }
    else {
            jQuery('#eventsContainer').feedfetcher_nodesc({
		feed_source: url,
		max_entries: 3
		});
        }
  }
}