/*!
 * @copyright 2011-Present Advanced Care Solutions, Inc.
 * @author Christopher Rahauiser <crahauiser@acs-web.com>
 */
(function($) {
  // open links with the newwin class in a new window
  function newWindow(event) {
    event.preventDefault();
    var win = window.open(this.href, 'newWindow',
      'width=750,height=600,scrollbars=yes');
    win && win.focus && win.focus();
  }
  $('a.newwin').click(newWindow);

  // setup the left column's locations
  /*function toggleTriggerClass() {
    $(this).toggleClass('over');
  }
  function toggleLocationContainer() {
    $(this)
      .nextAll('.location-container')
        .toggle();
  }
  $('#locations')
    .find('.location-container')
      .hide()
    .end()
    .find('.trigger')
      .bind({
        click: toggleLocationContainer,
        mouseover: toggleTriggerClass,
        mouseout: toggleTriggerClass
      });
*/
  // setup the contact form's watermarking, validation, and ajax submission
  var watermarks = $('#name,#address,#phone,#email,#message').watermark(),
      name = watermarks.eq(0),
      address = watermarks.eq(1),
      phone = watermarks.eq(2),
      email = watermarks.eq(3),
      message = watermarks.eq(4),
      postAction = name.closest('form').attr('action'),
      messages = $('#messages'),
      errorPrefix = '<p class="error">',
      errorSuffix = '</p>',
      successPrefix = '<p class="success">',
      successSuffix = '</p>',
      emailRegex = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|jobs|museum|gov|edu|mil|asia|int)$/i,
      phoneRegex = /^\d{10}$/;
  function ajaxErrorHandler() {
    var err = 'We apologize, but an error has occurred. Your information ' +
      'was not able to be sent.';
    messages
      .empty()
      .append(errorPrefix + err + errorSuffix);
  }
  function ajaxSuccessHandler(data) {
    if (data.success) {
      messages
        .empty()
        .append(successPrefix + data.success + successSuffix);
      watermarks
        .each(function() {
          $(this).val('');
        })
        .watermark('showAllWatermarks');
    } else if (data.model_error) {
      messages
        .empty()
        .append(data.model_error);
    } else {
      ajaxErrorHandler();
    }
  }
  function processForm(event) {
    event.preventDefault();

    messages.empty();

    watermarks.watermark('hideAllWatermarks');

    var nameVal = $.trim(name.val()),
        phoneVal = $.trim(phone.val()),
        emailVal = $.trim(email.val()),
        messageVal = $.trim(message.val()),
        error = '';

    if (phoneVal != '') {
      phoneVal =
          phoneVal
            .replace(/\(/g, '')
            .replace(/\)/g, '')
            .replace(/\s/g, '')
            .replace(/\-/g, '')
            .replace(/\./g, '');
    }

    if (nameVal == '') {
      error = 'The Full Name field is required.';
    } else if (phoneVal != '' && ! phoneRegex.test(phoneVal)) {
      error = 'The Phone field must be in the format (XXX) XXX-XXXX.';
    } else if (emailVal == '') {
      error = 'The Email Address field is required.';
    } else if (! emailRegex.test(emailVal)) {
      error = 'The Email Address field must be a valid email address.';
    } else if (messageVal == '') {
      error = 'The Message field is required.';
    }

    if (error != '') {
      messages.append(errorPrefix + error + errorSuffix);
    } else {
      $.ajax({
        url: postAction,
        dataType: 'json',
        type: 'POST',
        data: watermarks.serialize(),
        error: ajaxErrorHandler,
        success: ajaxSuccessHandler
      });
    }
    watermarks.watermark('showAllWatermarks');
  }
  $('#send_btn').click(processForm);
})(jQuery);

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-22895089-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
