// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Function erase the default value text of inputs in the signup form

function eraseDefaultText(input) {
  if (input.value.indexOf(input.default_text) > -1) {
    input.value = '';
  }
}

// Listener functions for each field

function emailListener() {
  eraseDefaultText(document.getElementById('subscriber_email'));
}

function zipListener() {
  eraseDefaultText(document.getElementById('subscriber_zip_code'));
}

// Function that sets onfocus listeners to fields onload

function signupFields() {
  if (!document.getElementById) return false;
  // Email
  if (document.getElementById('subscriber_email')) {
    var email = document.getElementById('subscriber_email');
    email.default_text = email.value;
    email.addEventListener('focus', emailListener, false);
  }
  // Zipcode
  if (document.getElementById('subscriber_zip_code')) {
    var zip = document.getElementById('subscriber_zip_code');
    zip.default_text = zip.value;
    zip.addEventListener('focus', zipListener, false);
  }
}

// Add to onload
addLoadEvent(signupFields);