$(document).ready(function() {
  // update validator with method to check for US phone numbers
  jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
      phone_number = phone_number.replace(/\s+/g, "");
    return this.optional(element) || phone_number.length > 9 &&
      phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})[-\. ]?[2-9]\d{2}[-\. ]?\d{4}$/);
  }, "Please specify a valid phone number");

  $(".dropDownForm").hide();

  $(".ctaForm").click( function() {
    src = $(".ctaForm").attr("href");
    if(src == '/ads/606') { // On these pages, the form will pop! 
      if($(".dropDownForm").is(":hidden")) {
        toggleColumnContent();
	  }

      $(".dropDownForm").slideToggle("slow", function () {
        if($(".dropDownForm").is(":visible")) {
          src = $(".ctaForm").attr("href");

          //Ajax to bring in form
          $.ajax({
            url: src,
            success: function(html){
              $(".dropDownForm").empty();
              $(".dropDownForm").append(html);
							$("#documentDownloads").hide();
              $("p.error").hide();

              //validate init
              $("form#leadGen").validate({
                rules: {
                  firstName: { required: true },
                  lastName: { required: true },
                  phone: { required: true, phoneUS: true },
                  email: { required: true, email: true },
                  company: { required: true },
                  title: { required: true }
                },
                messages: {
                  firstName: "First name is required",
                  lastName: "Last name is required",
                  email: {
                    required: "Email is required",
                    email: "Your email address must be in the format of name@domain.com"
                  },
                  phone: {
                    required: "Phone is required",
                    phoneUs: "Please specify a valid phone number"
                  },
                  company: "Company is required",
                  title: "Title is required"
                },
                errorPlacement: function(error, element) {
                  if(element.siblings("label").css("color") != "rgb(193, 59, 32)" && element.siblings("label").css("color") != "#c13b20") {
                    $("p.error").append("<br/>");
                    $("p.error").append(error);
                    element.siblings().css("color","#c13b20");

                    if($("p.error").is(":hidden")) {
                      $("p.error").show();
                      $("p.content").hide();
                    }
                  }
                },
                invalidHandler: function(form, validator) {
                  $("form#leadGen li label").css("color","#000");
                  var errors = validator.numberOfInvalids();
                  if (errors) {
                    $("p.error").empty();
                    $("p.error").html("There was a problem with your inquiry.<br/><br/>");

                    $("p.error").show();
                    $("p.content").hide();
                  } else {
                    $("p.error").hide();
                    $("p.content").show();
                  }
                },
                submitHandler: function(form) {
                  //ajax submit to salesForce
                  SFurl = $("form#" + form.id).attr("action");
                  formData = $("form#" + form.id).serialize();
                  if (SFurl.indexOf("?") == -1) {
                    SFurl = SFurl + "?x=y";	
                  }
                  iframUrl = SFurl + "&" + formData;

                  callIframe(iframUrl,function() {
                    $("#formContainer").hide();
                    $("#documentDownloads").show();
                  });

                  return false;
                }
              });

              $(".closeDDForm").click(function(){
                $(".dropDownForm").slideToggle("slow", function () {
                  toggleColumnContent();
                });

                return false;
              });
            }
          });
        } else {
          toggleColumnContent();
		}
      });

      return false;
    }
  });
});

function toggleColumnContent() {
  $(".emerging").toggle();
  $("#insightContainer").toggle();
}

function callIframe(url, callback) {
    $(document.body).append('<IFRAME id="myId" height="1" width="1" style="display:none;">');
    $('iframe#myId').attr('src', url);

    $('iframe#myId').load(function()
    {
        callback(this);
    });
}
