$(document).ready(function() {
  if(!Modernizr.input.placeholder) {
    var ids = ['first-name', 'last-name', 'address', 'suite-apt', 'city', 'zip-code', 'phone1', 'phone2', 'phone3', 'email', 'confirm-email'];
    for(var i = 0; i < ids.length; i++) {
      setInputHint($('#'+ids[i]));
    }
  }
  
  function friendly (str) {
    return str.replace(/\-/, " ");
  }
  
  $('.formDisclaimer').click(function() {
    $('#disclaimer').show();
  });  
  
  $('#disclaimer a').click(function() {
    $('#disclaimer').hide();
    return false;
  });
  
  $('.required').focus(function() {
    if(_gaq != null) {
      var referralCookie = getCookie('referringUrl');
      var keyword = "";
      if(referralCookie != null) {
        keyword = referralCookie[3];
      }
      _gaq.push(['_setAccount', 'UA-21020305-2']);
      _gaq.push(['_setMaxCustomVariables', 10]);
      _gaq.push(['_setCampSourceKey', 'SR']);
      _gaq.push(['_trackEvent','Leads', 'Initiated']);
      
      _gaq.push(['b._setAccount', 'UA-21020305-1']);
      _gaq.push(['b._setMaxCustomVariables', 10]);
      _gaq.push(['b._setCampSourceKey', 'SR']);
      _gaq.push(['b._trackEvent','Leads', 'Initiated']);
      
      _gaq.push(['c._setAccount', 'UA-21020305-12']);
      _gaq.push(['c._setMaxCustomVariables', 10]);
      _gaq.push(['c._setCampSourceKey', 'SR']);
      _gaq.push(['c._trackEvent','Leads', 'Initiated']);
    }
    $('.required').unbind('focus');
  });
  
  $('#phone1').keyup(function() {
    if($(this).val().length >= 3) {
      if($('#phone2').val().indexOf('#') != -1) {
        $('#phone2').val('');
      }
      $('#phone2').focus();
    }
  });
  
  $('#phone2').keyup(function() {
    if($(this).val().length >= 3) {
      if($('#phone3').val().indexOf('#') != -1) {
        $('#phone3').val('');
      }
      $('#phone3').focus();
    }
  });

  $('#email-form').submit(function () {
    var errors = false;
    var errormsg = ""
    $('.required').each(function() {
      var value = $(this).attr('value');
      if(!isValid($(this).val(), $(this))) {
        errormsg += friendly($(this).attr('name')) + " cannot be blank\n";
        errors = true;
      }
    });
    if(!$('#phone1').val().match(/^\d{3}$/)) {
      errors = true;
      errormsg += "Please enter in your 3 digit area code\n";
    }
    if(!$('#phone2').val().match(/^\d{3}$/)) {
      errors = true;
      errormsg += "Please enter in the first 3 digits of your phone number\n";
    }
    if(!$('#phone3').val().match(/^\d{4}$/)) {
      errors = true;
      errormsg += "Please enter in the last 4 digits of your phone number\n";
    }

    if(isValid($('#email').val(), $('#email')) && $('#email').val() != $('#confirm-email').val()) {
      errors = true;
      errormsg += "Please type your email address a second time in the confirm email box.\n";
    }
    else if(isValid($('#email').val(), $('#email')) && !$('#email').val().match(/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/)) {
      errors = true;
      errormsg += "Please enter in a valid email address.\n";
    }
    
    if(isValid($('#zip-code').val(), $('#zip-code')) && !$('#zip-code').val().match(/^\d{5}$/)) {
      errors = true;
      errormsg += "Please enter in a valid 5 digit zip code.\n";
    }
    if(errors) {
      alert(errormsg);
      if(_gaq != null) {
        _gaq.push(['_setAccount', 'UA-21020305-2']);
        _gaq.push(['_trackEvent','Checkout', 'Error Received', 'Checkout', errormsg]);

        _gaq.push(['b._setAccount', 'UA-21020305-1']);
        _gaq.push(['b._trackEvent','Checkout', 'Error Received', 'Checkout', errormsg]);

        _gaq.push(['c._setAccount', 'UA-21020305-12']);
        _gaq.push(['c._trackEvent','Checkout', 'Error Received', 'Checkout', errormsg]);
      }
      return false;
    }
    else {
      $('#the-form').attr('action', '/sender');
    }
  });
});
