﻿jQuery(document).ready(
  function ()
  {
    $('select#ddlProjectType').selectmenu({ width: 144, style: 'dropdown' });
  }
);

$('#txtZipCode').watermark('e.g. 90210', { className: 'input_watermark', useNative: false });
$.validator.addMethod("zipcode", function (value, element) { return this.optional(element) || /^(\d{5})$/i.test(value); });
$(".ellipsis_title").ThreeDots({ max_rows: 1, ellipsis_string: '&hellip;' });
$(".ellipsis_content").ThreeDots({ max_rows: 3, ellipsis_string: '&hellip;', whole_word: true });

// call to action submit
$("#frmCTA").validate
({
  rules:
  {
    txtZipCode:
    {
      required: true,
      zipcode: true
    }
  },
  messages:
  {
    txtZipCode: ""
  },
  errorLabelContainer: "#cta_error_box",
  invalidHandler: function ()
  {
    $("#cta_error_box").html("Invalid Zipcode.");
    $.watermark.showAll();
  },
  submitHandler: function (form)
  {
    var data = 'zipcode=' + $("input#txtZipCode").val() + "&project_type=" + $("select#ddlProjectType").val();

    $.ajax(
    {
      type: "POST",
      url: "/ajax/directory-cta",
      data: data,
      success: function (result)
      {
        if (result.redirect && result.url)
        {
          window.location.href = result.url;
        }
        else
        {
          $("#cta_error_box").html("Error occured. Try again.").show();
        }

        $.watermark.showAll();
      },
      error: function ()
      {
        $("#cta_error_box").html("Error occured. Try again.").show();
        $.watermark.showAll();
      }
    });
  }
});
