//////////////////////////////////////////////////////////////////////////////
//
//      Script: enquiry.js
// Description: Memi enquiry ajax client.
//      Author: Ivan Lutrov <ivan@provisionit.com.au>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//////////////////////////////////////////////////////////////////////////////

function checkfield(s, f) {
   if (isempty(s)) {
      return "Please enter a " + f + ".\n";
   }
   var badchars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
   if (s.match(badchars)) {
      return "The " + f + " contains illegal characters.\n";
   }
   if (f == 'email address') {
      var filter = /^.+@.+\..{2,3}$/;
      if ((filter.test(s))) {
         var badchars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
         if (s.match(badchars)) {
            return "The email address contains illegal characters.\n";
         }
      } else {
         return "Please enter a valid email address.\n";
      }
   }
   return '';
}

function isempty(s) {
   s = trim(s);
   if (s.length == 0) {
      return true;
   }
   return false;
}

function trim(s) {
   while (s.substring(0, 1) == ' ') {
      s = s.substring(1, s.length);
   }
   while (s.substring(s.length - 1, s.length) == ' ') {
      s = s.substring(0, s.length - 1);
   }
   return s;
}

$(document).ready(
   function() {
      $('#enquiry input[name=submit]').click(
         function() {
            error = checkfield($('#enquiry input[name=firstname]').val(), 'first name');
            if (!isempty(error)) {
               $('#enquiry input[name=firstname]').focus();
               $.growl(error);
               return false;
            }
            error = checkfield($('#enquiry input[name=lastname]').val(), 'last name');
            if (!isempty(error)) {
               $('#enquiry input[name=lastname]').focus();
               $.growl(error);
               return false;
            }
            error = checkfield($('#enquiry input[name=email]').val(), 'email address');
            if (!isempty(error)) {
               $('#enquiry input[name=email]').focus();
               $.growl(error);
               return false;
            }
            error = checkfield($('#enquiry input[name=mobile]').val(), 'mobile number');
            if (!isempty(error)) {
               $('#enquiry input[name=mobile]').focus();
               $.growl(error);
               return false;
            }
            data =
               '&firstname=' + $('#enquiry input[name=firstname]').val() +
               '&lastname=' + $('#enquiry input[name=lastname]').val() +
               '&address=' + $('#enquiry input[name=address]').val() +
               '&city=' + $('#enquiry input[name=city]').val() +
               '&state=' + $('#enquiry select[name=state]').val() +
               '&postcode=' + $('#enquiry input[name=postcode]').val() +
               '&email=' + $('#enquiry input[name=email]').val() +
               '&phone=' + $('#enquiry input[name=phone]').val() +
               '&mobile=' + $('#enquiry input[name=mobile]').val() +
               '&expenses=' + $('#enquiry input[name=expenses]').val() +
               '&accountant=' + $('#enquiry input[name=accountant]').val() +
               '&packages=' + $('#enquiry input[name=packages]').val() +
               '&referral=' + $('#enquiry select[name=referral]').val() +
               '&ifother=' + $('#enquiry input[name=ifother]').val() +
               '&time=' + $('#enquiry input[name=time]').val() +
               '&ip=' + $('#enquiry input[name=ip]').val() +
               '&referer=' + $('#enquiry input[name=referer]').val() +
               '&ua=' + $('#enquiry input[name=ua]').val()
            ;
            $.ajax(
               {
                  type: 'POST',
                  url: 'account/ps/enquiry.php',
                  data: data,
                  success: function(r) {
                     $('#enquiry input[name=submit]').attr('disabled', 'true');
                     $.growl("Your enquiry has been sent to our team and you can expect to hear from us shortly.", {header: 'Thank you.'});
                  }
               }
            );
         }
      );
      $('#enquiry select[name=referral]').change(
         function() {
            if ($('#enquiry select[name=referral]').val() == 'Other') {
               $('#enquiry input[name=ifother]').removeAttr('disabled');
               $('#enquiry input[name=ifother]').focus();
            } else {
               $('#enquiry input[name=ifother]').attr('disabled', true);
            }
         }
      );
   }
);

