/* soac.js */

$(function () {
  $('#newslettersubscribe').submit(function () {
    $(this).fadeTo(200,.1);
    $.post('/engineware/s/mailer/s/', $('#newslettersubscribe').serializeArray(),function (d) {
      if (d == '1') {
        $('#newslettersubscribe').html('<p><br>Thank you for subscribing<br><br></p>');
      }else{
        $('#newslettersubscribe').html('<p><br>There was a problem with your request, please try again later<br><br></p>');
      }
      $('#newslettersubscribe').fadeTo(200,1);
    });
    return false;
  });
  $('#petition').submit(function () {
    if ($('#email2').val() != $('#email22').val()) {
      alert('Email addresses don\'t match');
      return false;
    }
    $(this).fadeTo(200,.1);
    $.post($(this).attr('action'), $(this).serializeArray(),function (d) {
      if (d == 1) {
        $('#petition').fadeTo('200',1,function () { $(this).html('') });
        $('#petitionlist').prepend('<li>'+$('#name2').val()+'</li>').fadeIn('slow');
      }else{
        $('#petition').fadeTo('200',1).prepend('<span style="color:red;">We were unable to sign your name up to the petition, please try again later</span>');
      }
    });
    return false;
  });
  $('#contactform').submit(function () {
    $(this).fadeTo(200,.1);
    $.post($(this).attr('action'), $(this).serializeArray(),function (d) {
      if (d == 1) {
        $('#contactform').html('Thank you for your comments').fadeTo('200',1);
      }else{
        $('#contactform').fadeTo('200',1).prepend('<span style="color:red;">We were unable to send your message at this time, please try again later</span>');
      }
    });
    return false;
  });
  $('#sendMulti').click(function () {
    $('#multiform').fadeTo(200,.1);
    $.post('/engineware/s/soac/multi', $('#multiform :input').serialize(),function (d) {
      if (d == 1) {
        $('#multiform').html('<p class="style1">Email sent successfully to the following recipients:</p><p class="style1"><strong>Christchurch City Council: </strong><br />  Bob Parker, Claudia Reid, Gail Sheriff, Chrissie Williams,<br />Sally Buck, Mike Wall, David Cox, Yani Johanson,<br />Helen Broughton, Bob Shearing, Ngaire Button, Norm Withers<br />Barry Corbett, Sue Wells, Tony Marryatt</p><p class="style1"><strong>Canterbury University:<br /></strong>Rod Carr, Ian Town, Steve Weaver<br />  Kenneth Strongman, Jeff Field</p>').fadeTo('200',1);
      }else{
        $('#multiform').fadeTo(200,1)
        $('#sendMulti').after('<span style="color:red;">We were unable to send your message at this time, please ensure you have entered a name and valid email address and try resubmitting.</span>');
      }
    });
    return false;
  });
  if ($('#submisform').length) {
    $('#submisSubmit').click(function () {
      if (!$('#subname').val()) {
        alert('Please enter your name');
        $('#name').focus();
        return false;
      }
      if (!$('#subemail').val()) {
        alert('Please enter your email address');
        $('#email').focus();
        return false;
      }
      if (!$('#ph_day').val()) {
        alert('Please enter your phone number');
        $('#ph_day').focus();
        return false;
      }
      $.post('/engineware/s/soac/submission', $('#submissionTable :input').serialize(),function (d) {
        if (d.substr(0,5) == 'Thank') {
          $('#submisform').html('<h1>Thank you</h1><p>your submission has been sent</p>');
        }else{
          alert('Sorry, we were unable to process your submission, please try again later');
        }
      });
      return false;
    });

  }
});
