<commit_before>/*!
  * CallPowerForm.js
  * Connects embedded action form to CallPower campaign
  * Requires jQuery >= 1.7.0
  *    proxy (1.4), deferred (1.5), promises (1.6), on (1.7)
  *
  * Displays call script in overlay or by replacing form
  * Override functions onSuccess or onError in CallPowerOptions
  * Instantiate with form selector to connect callbacks
  * (c) Spacedog.xyz 2015, license AGPLv3
  */

// revealing module pattern
var CallPowerForm = function (formSelector, $) {
  // instance variables
  this.$ = $;  // stash loaded version of jQuery, in case there are conflicts with window
  this.form = this.$(formSelector);
  {% if campaign.embed %}
  this.locationField = this.$('{{campaign.embed.get("location_sel","#location_id")}}');
  this.phoneField = this.$('{{campaign.embed.get("phone_sel","#phone_id")}}');
  {% else %}
  this.locationField = this.$("#location_id");
  this.phoneField = this.$("#phone_id");
  {% endif %}
  this.scriptDisplay = 'overlay';
  
  // allow options to override settings
  for (var option in window.CallPowerOptions || []) {
    var setting = CallPowerOptions[option];
    // accept jquery selectors for form and fields
    var selectorFields = ['form', 'locationField', 'phoneField'];
    if ($.inArray(option, selectorFields) != -1 && typeof setting === 'string') {
      this[option] = this.$(setting);
    } else { 
      this[option] = setting;
    }
  }

  // bind our submit event to makeCall
  this.form.on("submit.CallPower", this.$.proxy(this.makeCall, this));
  // include custom css
  if(this.customCSS !== undefined) {
    var link_elem = 'link rel="stylesheet" href="';
    // one weird trick to create the element without it rendering in browsers
    this.$('head').append('<'+link_elem+this.customCSS+'" />');
  }
};

CallPowerForm.prototype = function() {
  // prototype variables
  var createCallURL = '{{url_for("call.create", _external=True)}}';
  var campaignId = "{{campaign.id}}";

  var simpleGetCountry = function() {
    return "{{campaign.country_code|default('US')}}";
  };

  var cleanUSZipcode = function(val) {
    if (val.length === 0) { return undefined; }
    var isValid = /(\d{5}([\-]\d{4})?)/.test(val);
    return isValid ? val : false;
  };

  var cleanCAPostal = function(val) {
      if (val === 0) { return undefined; }
      var valNospace = val.replace(/\W+/g, '');
      var isValid = /([ABCEGHJKLMNPRSTVXY]\d)([ABCEGHJKLMNPRSTVWXYZ]\d){2}/i.test(valNospace);
      return isValid ? valNospace : false;
  };

  // very simple country specific validators
  // override with eg google places autocomplete
  var simpleValidateLocation = function() {
    countryCode = this.country();

    if (this.locationField.length == 1) {
      var locationVal = this.locationField.val();
    } else {
      var locationVal = '';
      this.locationField.each(function() { 
        locationVal += (' ' + $(this).val());
      });
      locationVal = locationVal.trim();
    }
  };

  // very simple country specific length validators
  // override with eg: google libphonenumber
  var simpleValidatePhone = function(countryCode) {
    countryCode = this.country();

    if (this.phoneField.length === 0) { return undefined; }
    // remove whitespace, parents, plus, dashes from phoneField
    var num = this.phoneField.val()
      .replace(/\s/g, '').replace(/\(/g, '').replace(/\)/g, '')
      .replace('+', '').replace(/\-/g, '');
    return num;

    var isValid = false;
    var prefix;
    if (countryCode === 'US' || countryCode === 'CA') {
        prefix = "1";
        // strip leading prefix
        if (num.charAt(0) === prefix) {
          num = num.substr(1);
        }
        isValid = (num.length == 10); // ensure just 10 digits remain<commit_msg>Only run locationField values through country-specific postal validation if campaign.locate_by == ‘postal’<commit_after>/*!
  * CallPowerForm.js
  * Connects embedded action form to CallPower campaign
  * Requires jQuery >= 1.7.0
  *    proxy (1.4), deferred (1.5), promises (1.6), on (1.7)
  *
  * Displays call script in overlay or by replacing form
  * Override functions onSuccess or onError in CallPowerOptions
  * Instantiate with form selector to connect callbacks
  * (c) Spacedog.xyz 2015, license AGPLv3
  */

// revealing module pattern
var CallPowerForm = function (formSelector, $) {
  // instance variables
  this.$ = $;  // stash loaded version of jQuery, in case there are conflicts with window
  this.form = this.$(formSelector);
  {% if campaign.embed %}
  this.locationField = this.$('{{campaign.embed.get("location_sel","#location_id")}}');
  this.phoneField = this.$('{{campaign.embed.get("phone_sel","#phone_id")}}');
  {% else %}
  this.locationField = this.$("#location_id");
  this.phoneField = this.$("#phone_id");
  {% endif %}
  this.locateBy = '{{campaign.locate_by}}';
  this.scriptDisplay = 'overlay';
  
  // allow options to override settings
  for (var option in window.CallPowerOptions || []) {
    var setting = CallPowerOptions[option];
    // accept jquery selectors for form and fields
    var selectorFields = ['form', 'locationField', 'phoneField'];
    if ($.inArray(option, selectorFields) != -1 && typeof setting === 'string') {
      this[option] = this.$(setting);
    } else { 
      this[option] = setting;
    }
  }

  // bind our submit event to makeCall
  this.form.on("submit.CallPower", this.$.proxy(this.makeCall, this));
  // include custom css
  if(this.customCSS !== undefined) {
    var link_elem = 'link rel="stylesheet" href="';
    // one weird trick to create the element without it rendering in browsers
    this.$('head').append('<'+link_elem+this.customCSS+'" />');
  }
};

CallPowerForm.prototype = function() {
  // prototype variables
  var createCallURL = '{{url_for("call.create", _external=True)}}';
  var campaignId = "{{campaign.id}}";

  var simpleGetCountry = function() {
    return "{{campaign.country_code|default('US')}}";
  };

  var cleanUSZipcode = function(val) {
    if (val.length === 0) { return undefined; }
    var isValid = /(\d{5}([\-]\d{4})?)/.test(val);
    return isValid ? val : false;
  };

  var cleanCAPostal = function(val) {
      if (val === 0) { return undefined; }
      var valNospace = val.replace(/\W+/g, '');
      var isValid = /([ABCEGHJKLMNPRSTVXY]\d)([ABCEGHJKLMNPRSTVWXYZ]\d){2}/i.test(valNospace);
      return isValid ? valNospace : false;
  };

  // very simple country specific validators
  // override with eg google places autocomplete
  var simpleValidateLocation = function() {
    countryCode = this.country();

    if (this.locationField.length == 1) {
      var locationVal = this.locationField.val();
    } else {
      var locationVal = '';
      this.locationField.each(function() { 
        locationVal += (' ' + $(this).val());
      });
      locationVal = locationVal.trim();
    }

    if (this.locateBy === 'postal') {
      if (countryCode === 'US') { return cleanUSZipcode(locationVal); }
      else if (countryCode === 'CA') { return cleanCAPostal(locationVal); }
    }
    return locationVal;
  };

  // very simple country specific length validators
  // override with eg: google libphonenumber
  var simpleValidatePhone = function(countryCode) {
    countryCode = this.country();

    if (this.phoneField.length === 0) { return undefined; }
    // remove whitespace, parents, plus, dashes from phoneField
    var num = this.phoneField.val()
      .replace(/\s/g, '').replace(/\(/g, '').replace(/\)/g, '')
      .replace('+', '').replace(/\-/g, '');
    return num;

    var isValid = false;
    var prefix;
    if (countryCode === 'US' || countryCode === 'CA') {
        prefix = "1";
        // strip leading prefix
        if (num.charAt(0) === prefix) {
          num = num.substr(1);
        }
        isValid = (num.length == 10); // ensure just 10 digits remain
{'fraction_non_alphanumeric': 0.16131907308377896, 'fraction_numerical': 0.00789406671759613, 'mean_word_length': 3.164899257688229, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2261357', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2397, 'n_words': 627}