if(!window.jQuery)
{
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "https://code.jquery.com/jquery-latest.js";
script.onload= function () {
setFormLeadEventHandler();
};
document.getElementsByTagName('head')[0].appendChild(script);
}else{
setFormLeadEventHandler();
}
function validateForm(frm){
const nameRegex = /^[A-Za-z\s]{3,30}$/; // Allows letters and spaces, length 3-30
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // Simple
for(var i = 0; i < frm.length; i++){
//nsole.log('Elem '+frm[i].name + ": " + frm[i].value);
var key = frm[i].name;
var kval = frm[i].value;
if(key =='phone_number_ori' || key == ''){
}else{
if(kval.length == 0) {
console.log(key+' Cannot be empty');
alert(key+' Cannot be empty');
return false;
}
if(/name/i.test(key)){
if (!nameRegex.test(kval)){
alert(key+' Not valid');
return false;
}
}
if(/email/i.test(key)){
if (!emailRegex.test(kval)){
alert(key+' Not valid');
return false;
}
}
}
}
return true;
}
function setFormLeadEventHandler(){
console.log('Event handler for leads called ');
$('form').each(function(index) {
var cForm = this;
var hasName = false;
var hasPhone = false;
var hasEmail = false;
var user_id_f = 853;
var phoneField = '';
for(var i = 0; i < this.length; i++){
console.log('Elem '+this[i].name + ": " + this[i].value);
var nam = this[i].name.toLowerCase();
if(nam.includes("name")){
hasName = true;
}else if(nam.includes("phone") || nam.includes("number")){
phoneField = nam;
hasPhone = true;
}else if(nam.includes("email")){
hasEmail = true;
}
if(hasName && hasPhone && hasEmail){
console.log('Adding submit handler ');
//ad token for cross site spam issues
addServerToken(cForm);
addOriPhone(cForm);
setPhoneFormat(this,phoneField);
$(this).submit(function( event ) {
console.log('Submit called ');
if(validateForm(this))
$.ajax({
type: 'post',
url: 'https://www.reviewsmanagement.com/main_profile/constactus_custom/'+user_id_f,
data: $(this).serialize(),
success: function (data) {
if(data.includes("Token")){
alert("Invalid or expired Token Reloading");
location.reload();
}
console.log('form was submitted');
event.preventDefault();
alert(data);
}
});
event.preventDefault();
});
return false;
}
}//eno of for loop
}); //endo fo form each function
}
function setPhoneFormat(frm,phoneF) {
"use strict";
// When ready.
$(function() {
var $form = $( frm );
var $phoneEl =$form.find('input[name="'+phoneF+'"]');
$phoneEl.on('keypress', function(e) {
var key = e.charCode || e.keyCode || 0;
var phone = $(this);
if (phone.val().length === 0) {
phone.val(phone.val() + '(');
}
// Auto-format- do not expose the mask as the user begins to type
if (key !== 8 && key !== 9) {
if(phone.val().charAt(0)!=='('){
phone.val('('+phone.val() );
}
if (phone.val().length === 4) {
phone.val(phone.val() + ')');
}
if (phone.val().length === 5) {
phone.val(phone.val() + ' ');
}
if (phone.val().length === 9) {
phone.val(phone.val() + '-');
}
if (phone.val().length >= 14) {
phone.val(phone.val().slice(0, 13));
}
}
// Allow numeric (and tab, backspace, delete) keys only
return (key == 8 || key == 9 || key == 46 || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
}).on('focus', function(e) {
if ($phoneEl.val().length === 0) {
$phoneEl.val('(');
} else {
var val = $phoneEl.val();
$phoneEl.val('').val(val); // Ensure cursor remains at the end
}
}).on('blur', function(e) {
if ($phoneEl.val() === '(') {
$phoneEl.val('');
}
});
});
}
function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}
return result;
}
function addServerToken(frm){
console.log('Adding token into form for the spam control');
const rstr = generateRandomString(10);
xhrFields: {
withCredentials: true
}
$.ajax({
url: "https://www.reviewsmanagement.com/main_profile/ss_token?r="+rstr, // Target URL
type: "GET", // HTTP method
xhrFields: {
withCredentials: true
},
success: function(data) { // Callback on success
console.log('Got results');
console.log(data);
$("").attr({
name: "ss_v_token",
id: "ss_v_tokenid",
type: "hidden",
value: data['token']
}).appendTo(frm);
},
error: function(xhr, status, error) { // Callback on failure
console.error("Error:", error);
}
});
}
function addOriPhone(frm){
console.log('Adding Original Phone number');
$("").attr({
name: "phone_number_ori",
type: "hidden",
value: "",
autocomplete: "off",
style : "display: none;"
}).appendTo(frm);
}