$(document).ready(function() { 
  $('form.signup').submit(function() {
    if ($("#terms").attr("checked") != true) {
      $("div.checkbox_field").addClass("highlight");
      $("div.checkbox_field").effect("shake", { times:2, distance:5, direction:"right" }, 300);
    } else {
      $('div.submit_field input[type="image"]').attr("disabled", "disabled");
      $('div.submit_field input[type="image"]').attr("src", "/images/trial_button_active.png");
      //$('div.ajaxloader').show();

      //insertTimeZoneIntoForm($(this));
      //insertSourceIntoForm($(this));
      //insertCountryCodeIntoForm($(this));
      
      //var queryString = $('form.signup').formSerialize();
      //var domain = jQuery.url.setUrl(document.location).attr("host").split(".").splice(1,2).join(".");
      
      //$.getJSON("http://signup." + domain + "/accounts.json?_method=post&callback=?", queryString, processSignupResponse);
      return true;
    }
    
    // return false to prevent normal browser submit and page navigation
    return false;
  });
  
  $('#terms').click(function() {
    enableOrDisableSubmitButton($('#terms'), $('div.submit_field input[type="image"]'));
  });
  
  $("input#company_name").blur(function () {
    domainTextField = $("input#site_address");
    if (domainTextField.attr("value").length == 0) {
      domainTextField.attr("value", $(this).attr("value").toLowerCase().replace(/\W/g, ''));
    }
  });
});

function enableOrDisableSubmitButton(termsCheckbox, submitButton) {
  if (termsCheckbox.attr("checked") == true) {
    $("div.checkbox_field").removeClass("highlight");
    submitButton.attr("src", "/images/trial_button.png");
  } else {
    submitButton.attr("src", "/images/trial_button_disabled.png");
  }
}

function isDST() {
   var today = new Date();
   var jan = new Date(today.getFullYear(), 0, 1, 0, 0, 0, 0);
   var jul = new Date(today.getFullYear(), 6, 1, 0, 0, 0, 0);
   var temp = jan.toGMTString();
   var jan_local = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   var temp = jul.toGMTString();
   var jul_local = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   var hoursDiffStdTime = (jan - jan_local) / (1000 * 60 * 60);
   var hoursDiffDaylightTime = (jul - jul_local) / (1000 * 60 * 60);

   return hoursDiffDaylightTime != hoursDiffStdTime;
}

function insertTimeZoneIntoForm(formElement) {
  var timeZoneOffset = (new Date()).getTimezoneOffset() / 60 * (-1);
  timeZoneOffset -= (isDST() ? 1 : 0);

  if ($("input[name='account[utc_offset]']").length == 0) {
    formElement.prepend('<input type="hidden" name="account[utc_offset]" value="' + timeZoneOffset + '" />');
  }
}

function insertSourceIntoForm(formElement) {
  var source = $.cookie("_website_referrer");
  
  if (source != null) {
    if ($("input[name='account[source]']").length == 0) {
      formElement.prepend('<input type="hidden" name="account[source]" value="' + source + '" />');
    }
  }
}

function insertCountryCodeIntoForm(formElement) {
  if (google.loader.ClientLocation && google.loader.ClientLocation.address) {
    var countryCode = google.loader.ClientLocation.address.country_code;
    
    if ($("input[name='address[country_code]']").length == 0) {
      formElement.prepend('<input type="hidden" name="address[country_code]" value="' + countryCode + '" />');
    }
  }
}

function processSignupResponse(response) {
  if (response.success) {
    displaySuccessMessage(response);
  } else {
    enableOrDisableSubmitButton($('#terms'), $('div.submit_field input[type="image"]'));
    displayErrorMessages(response);
  }
  $('div.ajaxloader').hide();
  $('div.submit_field input[type="image"]').removeAttr("disabled");
}

function displaySuccessMessage(data) {
  $("div.signup_success a.right_away_link").attr("href", data.right_away_link);

  $("div.signup_success a.owner_email_link").attr("href", "mailto:" + data.owner_email);
  $("div.signup_success a.owner_email_link").html(data.owner_email);

  $("div.signup_success a.help_desk_link").each(function(i) {
    $(this).attr("href", data.help_desk_link);
    $(this).html(data.help_desk_link);
  });
  $("div.signup_success").show();
  
  $("form.signup, .errors, .NiCHE_details").remove();
  $("#pricing-table").remove();
  
  $("div.signup_success").after('<iframe src="/signup_success.html"></iframe>');
}

function displayErrorMessages(data) {
  var errorContent = '<div class="errors">';
  
  errorContent += "<h3>" + data.message + "</h3>";
  
  errorContent += "<ul>";
  $.each(data.errors, function (indexInArray, valueOfElement) {
    errorContent += "<li>" + valueOfElement + "</li>";
  });
  errorContent += "</ul></div>";
  
  errorsContainer = $("div.errors");
  if (errorsContainer.length > 0) {
    errorsContainer.replaceWith(errorContent);
  } else {
    $("form.signup").before(errorContent);
  }
  
  $.scrollTo($("div.errors"), 500);
}
