﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

$(function()
{
  $('#contactButton').focus();
  $('#aspnetForm').validate(
  {
    onsubmit: false,
    rules:
    {
      name: "required",
      email: { required: true, email: true }
    },
    messages:
    {
      name: "Your name is required",
      email:
      {
        required: "Your email address is required",

        email: "Please enter a valid email address"
      }
    },
    errorLabelContainer: '#errorContainer ul',
    wrapper: 'li',
    highlight: function(element)
    {
      $(element).addClass('errorField');
    },
    unhighlight: function(element)
    {
      $(element).removeClass('errorField');
    }
  });

  $('#submitButton').click(submitContact);

  $('#test :text').keydown(function(evt)
  {
    if (evt.keyCode == 13)
    {
      evt.preventDefault();
      var $inputs = $(evt.currentTarget).parents('.validationGroup').find(':input');
      var i = $inputs.index(this);
      var $nextInput = $inputs.eq(i + 1);
      if ($nextInput.is('#submitButton'))
        $nextInput.click();
      else
        $nextInput.focus();
    }
  });

  $('#contactButton').click(function()
  {
    var w = '-=' + $('#contactParent').width();
    $('#base').animate({ left: w }, 1000, 'easeOutExpo');
    $('#test').animate({ left: w }, 1000, 'easeOutExpo', function()
    {
      $('#test :input:first').focus();
    });
    return false;
  });

  $('#formClose').click(function() {
      var w = '+=' + $('#contactParent').width();
      $('#base').animate({ left: w }, 1000, 'easeOutExpo');
      $('#test').animate({ left: w }, 1000, 'easeOutExpo');
      return false;
  });

  $('#tryAgain').click(function()
  {
    var h = '+=' + $('#contactParent').height();
    $('#test').animate({ top: h }, 750, 'easeOutExpo');
    $('#error').animate({ top: h }, 750, 'easeOutExpo');
    return false;
  });
});

function submitContact(evt)
{
  var $group = $(evt.currentTarget).parents('.validationGroup');
  var isValid = true;
  $group.find(':input').each(function(i, item)
  {
    if (!$(item).valid())
      isValid = false;
  });

  if (isValid)
  {
    var submitData = {};
    $('#test .contactForm').each(function() { submitData[this.name] = this.value; });
    $.ajax({ url: 'SubmitContact.ashx', type: 'POST', data: submitData, success: submitDone, error: submitError });
  }
  
  return false;
}

function submitDone(data, status)
{
  if (data == 'OK')
  {
    var h = '+=' + $('#contactParent').height();
    $('#test').animate({ top: h }, 750, 'easeOutExpo');
    $('#done').animate({ top: h }, 750, 'easeOutExpo');
  }
  else
    serverError('The server returned an error.');
}

function serverError(msg)
{
  $('#errorDescription').text(msg);
  var h = '-=' + $('#contactParent').height();
  $('#test').animate({ top: h }, 750, 'easeOutExpo');
  $('#error').animate({ top: h }, 750, 'easeOutExpo');
}

function submitError()
{
  serverError('There was an error communicating with the server.');
}
