/*
 * Provides site wide JS functionality. Should be included into every public
 * (ie., not reporting) page. Mostly does AJAXy stuff.
 *
 * Depends: jQuery, jQuery Form Plugin
 */

//used in beforeSubmit to get formData key/value pairs out of the array
function getFormPair(arr, key)
{
  for(var i = 0; i < arr.length; i++)
    if(arr[i].name == key)
      return arr[i].value;

  return null;
}

$(document).ready(function(){
  $(".regrets").click(function () {
    $(".separate").show("fast");
    $(".dis").hide("fast");
  });

  $("#referral").ajaxForm({
    url: "./actions.php",
    type: "post",
    dataType: "json",
    beforeSubmit: function(formData, jqForm, options)
    {
      //we intercept the one form, split it into two ajax/actions calls, and 
      //never actually submit the form content in whole
      $.post(
        options.url, 
        {
          act: "sendRegret",
          firstName: getFormPair(formData, "firstName"),
          lastName: getFormPair(formData, "lastName"),
          companyName: getFormPair(formData, "companyName")
        },
        function(data, statusText)
        {
          $('p#sendRegretResult').html('<h2>Thank you for letting us know - we hope to see you next time!</h2>').show();
        },
        "json"
      );

      //we don't take any client side action for recommendations
      $.post(
        options.url,
        {
          act: "recommend",
          firstName: getFormPair(formData, "recFirstName"),
          lastName: getFormPair(formData, "recLastName"),
          companyName: getFormPair(formData, "recCompanyName"),
          email: getFormPair(formData, "recEmail")
        },
        null,
        "json"
      );

      //prevent this form from being submitted
      return false;
    },
    success: function(responseText, statusText)
    {
      $('#sendRegretResult').html(
        "<h2>"+((responseText.success) ? "Thank you, we will be in touch with them shortly." : responseText.error)+"</h2>"
      ).show(); 
    }
  });

  $("#registrationForm").ajaxForm({
    url: "./actions.php",
    type: "post",
    dataType: "json",
    success: function(responseText, statusText)
    {
      $('#registerResult').html(
        "<h2>"+((responseText.success) ? "We look forward to seeing you!" : responseText.error)+"</h2>"
      ).show();
    }
  });

  $("#loginForm").ajaxForm({
    url: "./actions.php",
    type: "post",
    dataType: "json",
    beforeSubmit: function(data, form, options)
    {
      $(form).find("input[name=password]").val("");
    },
    success: function(responseText, statusText)
    {
      if(!responseText.success)
        $('#loginResult').html('<h2>Incorrect email/password combo. Please try again.</h2>').show();
      else
      {
        //swap out the login with the registration
//        $('#txt').html($('#registration').html());
        $("div#login").hide();
        $("div#registration").show();
       
        //bind the form (didn't bind when it was invisible)
        $("#registrationForm").ajaxForm({
          url: "./actions.php",
          type: "post",
          dataType: "json",
          beforeSubmit: function()
          {
            //for re-submissions
            $('#registerResult').html("").hide();
          },
          success: function(responseText, statusText)
          {  
            $('#registerResult').html(
              "<h2>"+((responseText.success) ? "Updated - we look forward to seeing you!" : responseText.error)+"</h2>"
            ).show();
          }
        });

        //drop values from result into the registration form
        $.each(responseText.registration, function(i, item)
        {
          switch(i)
          {
            case "mealsAttending":
              $.each(item, function(j, subItem)
              {
                $('input[name=mealsAttending[]][value='+subItem+']').click();
              });
              break;

            case "needHotel":
              $.each(item, function(j, subItem)
              {
                $('input[name=needHotel[]][value='+subItem+']').click();
              });
              break;

            case "companyState":
              $('select[name='+i+']').val(item);
              break;

            default:
              $('input[name='+i+']').val(item);
              break;
          }
        });
      }
    }
  });
});
