(function ($) {
  Drupal.behaviors.sanky_contact_form = {
    attach: function(context, settings) {
      // If the country is not the US, disable state to be a required field.
      $('#edit-country').change(function() {
        if($(this).val() == 'United States' || $(this).val() == 'United States Minor Outlying Islands') {
          $('#edit-state-wrapper').css('display', 'block');
        }
        else {
          $('#edit-state-wrapper').css('display', 'none');
        }
      });
      
      // Before the form is submitted, check that the required fields aren't empty.
      $('#sanky-contact-form').submit(function() {
        if($('#edit-inquiry').val() == '') {
          alert('Please select what your Inquiry relates to.');
          $('#edit-inquiry').focus();
          return false;
        }
        
        if($('#edit-email').val() == '') {
          alert('Please enter your Email address.');
          $('#edit-email').focus();
          return false;
        }
        
        if($('#edit-first-name').val() == '') {
          alert('Please enter your First name.');
          $('#edit-first-name').focus();
          return false;
        }
        
        if($('#edit-last-name').val() == '') {
          alert('Please enter your Last name.');
          $('#edit-last-name').focus();
          return false;
        }
        
        return true;
      });
    }
  };
})(jQuery);;
(function ($) {
  Drupal.behaviors.sanky_quick_email_signup_form = {
    attach: function(context, settings) {
      $('.sanky-quick-email-signup', context).submit(function(e) {
        e.preventDefault();
  
        switch($(this).attr('id')) {
          case 'footer-email-signup':
            $email = $('#footer-email-signup-field').val();
            if(validEmail($email)) {
              $('#footer-stay-informed div.content').html('<div style="margin: 30px 0px 0px 0px; font-size: 18px; color: #ffffff;">Submitting...</div>');
        		trackAction(location.href);	
                $.post($(this).attr('action'), 
                {email: $email},
        				function(data) {
        					if(data.added == true) {
                    $('#footer-stay-informed div.content').html('<div style="margin: 30px 0px 0px 0px; font-size: 18px; color: #ffffff;">Thank You!</div>');
        					}
        					else {
        						$('#footer-stay-informed div.content').html('<div style="margin: 30px 0px 0px 0px; font-size: 18px; color: #ff0000;">Signup Failed!</div>');
        					}
        				}, 'json'
        			);
        		}
            break;
          
          case 'sidebar-email-signup':
            $email = $('#sidebar-email-signup-field').val();
            if(validEmail($email)) {
              $('#sidebar-stay-informed div.content').html('<div style="margin: 30px 0px 0px 0px; font-size: 18px; color: #405194;">Submitting...</div>');
              trackAction(location.href);
        			$.post($(this).attr('action'), 
                {email: $email},
        				function(data) {
        					if(data.added == true) {
                    $('#sidebar-stay-informed div.content').html('<div style="margin: 30px 0px 0px 0px; font-size: 18px; color: #405194;">Thank You!</div>');

        					}
        					else {
        						$('#sidebar-stay-informed div.content').html('<div style="margin: 30px 0px 0px 0px; font-size: 18px; color: #ff0000;">Signup Failed!</div>');
        					}
        				}, 'json'
        			);
        		}
            break;
        }
  	  });
    }
  };
})(jQuery);

function validEmail(e) {
	var filter = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	return String(e).search (filter) != -1;
}

function trackAction(url) {
    if(typeof(_gaq) == "object") {
        _gaq.push(['_trackEvent', 'Conversion', 'Email Signup', url]);
    }
};

