window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-EDT9R03DHT');

(function () {
let CFSTP = function () {
if (!(this instanceof CFSTP)) {
return;
}
this.forms = [
//blokhin.ru
{
container: "form#contact-form-callback",
success: {
part: "pathname",
text: "mail-sent",
},
},
{
container: "form#contact-form-price",
success: {
part: "pathname",
text: "mail-sent",
},
},
//blokhin.ru
//frauklinik.ru
{
page: "/form/",
container: "form",
success: {
element: "div#first h1",
text: "Ваша заявка отправлена",
attribute: "innerText",
},
},
//frauklinik.ru
];
this.mnemonic = {
name: ["text-707", "phone"],
phone: ["tel-87", "name"],
email: [],
form_name: [],
};
(this.config = {
debug: false,
fnames: false,
fnamesMess: false,
}),
(this.version = "1.0");
};

//Methods

CFSTP.prototype.getData = function (form) {
let result = [];
let fields = form.querySelectorAll("input, select, textarea");
for (const key in fields) {
if (fields.hasOwnProperty(key)) {
if (
fields[key]["type"] &&
fields[key]["type"] == "radio" &&
!fields[key]["checked"]
) {
continue;
}
let fieldName = [];
let fieldValue = "";
if (fields[key].hasAttribute("name")) {
fieldName.push(fields[key]["name"]);
} else if (fields[key].hasAttribute("id")) {
fieldName.push(fields[key]["id"]);
} else if (fields[key].hasAttribute("class")) {
fields[key]["classList"].forEach((element) => {
fieldName.push(element);
});
} else {
continue;
}
if (
fields[key]["tagName"] == "SELECT" &&
fields[key].querySelector(
'option[value="' + fields[key]["value"].replace(/"/g, '\"') + '"]'
)
) {
fieldValue = fields[key].querySelector(
'option[value="' + fields[key]["value"].replace(/"/g, '\"') + '"]'
).innerText;
} else {
fieldValue = fields[key]["value"];
}
if (fieldName && fieldValue) {
fieldName.forEach((element) => {
result.push({
name: element,
value: fieldValue,
});
});
}
}
}
return result;
};

CFSTP.prototype.writeData = function (form) {
this.formData = {};
if (
this.forms[this.currentForm] &&
this.forms[this.currentForm]["widget"]
) {
this.formData.cWidget = this.forms[this.currentForm]["widget"];
} else {
this.formData.cWidget = "request";
}
this.formData.cData = this.createObject(this.getData(form));
if (this.config.debug) {
console.group(
"%c%s%s",
"color: red;",
"Writing form data №",
this.currentForm
);
console.log(this.formData);
console.groupEnd();
}
sessionStorage.setItem("cFormData", JSON.stringify(this.formData));
};

CFSTP.prototype.createObject = function (formData) {
let cData = { name: "", message: "" };
if (!formData) {
return false;
}
for (const key in formData) {
if (formData.hasOwnProperty(key)) {
this.mnemonic.name.forEach(function (item, i, arr) {
if (formData[key]["name"] == item) {
cData.name += formData[key]["value"] + " ";
}
});
this.mnemonic.phone.forEach(function (item, i, arr) {
if (formData[key]["name"] == item) {
cData.phone = formData[key]["value"];
}
});
this.mnemonic.email.forEach(function (item, i, arr) {
if (formData[key]["name"] == item) {
cData.email = formData[key]["value"];
}
});
for (const key2 in this.mnemonic.message) {
if (this.mnemonic.message.hasOwnProperty(key2)) {
if (
formData[key]["name"] == this.mnemonic.message[key2]["name"] &&
formData[key]["name"] != ""
) {
cData.message +=
this.mnemonic.message[key2]["text"] +
" : " +
formData[key]["value"] +
"n";
}
}
}
for (const key2 in this.mnemonic.form_name) {
if (this.mnemonic.message.hasOwnProperty(key2)) {
if (
formData[key]["name"] == this.mnemonic.form_name[key2]["name"] &&
formData[key]["name"] != ""
) {
cData.form_name = formData[key]["value"];
}
}
}
}
}
if (!this.config["fnames"] && cData.form_name) {
cData.form_name = "";
}
if (this.config["fnames"] && !cData.form_name) {
if (this.forms[this.currentForm].hasOwnProperty("formName")) {
cData.form_name = this.forms[this.currentForm]["formName"];
if (this.config["fnamesMess"]) {
cData.message +=
"FormName: " + this.forms[this.currentForm]["formName"] + ";";
}
}
}
document
.querySelectorAll(".simplecheckout-cart .name a")
.forEach((element) => {
cData.message += element.innerText + "; ";
});
return cData;
};

CFSTP.prototype.formHandler = function (e) {
let form;
if (!e.isTrusted) {
return;
}
for (const key in this.forms) {
if (this.forms.hasOwnProperty(key)) {
const container = this.forms[key]["container"];
form = e.target.closest(container);
if (form) {
if (
this.forms[key].page &&
!location.href.includes(this.forms[key].page)
) {
continue;
} else {
this.currentForm = key;
this.writeData(form);
break;
}
}
}
}
//console.log(form);
if (!form) {
return;
}
};

//Sending
CFSTP.prototype.waitingMainScript = function () {
let that = this;
let counter = 0;
let is_good = false;
let int = setInterval(function () {
if (is_good || counter > 40) {
clearInterval(int);
if (is_good) {
if (that.config.debug) {
console.log("GOOD");
}
that.sendData();
}
if (counter > 40) {
if (that.config.debug) {
console.log("BAD");
that.sendData();
}
}
}
counter++;
if (!window.Comagic) {
return;
}
is_good = true;
counter++;
let credentials;
try {
credentials = Comagic.getCredentials();
} catch {
return;
}
for (var field in credentials) {
if (credentials.hasOwnProperty(field)) {
if (credentials[field]) {
if (field == "hit_id" && isNaN(credentials[field])) {
is_good = false;
}
} else {
is_good = false;
}
}
}
}, 500);
};

CFSTP.prototype.sendData = function () {
for (const key in this.forms) {
if (this.forms.hasOwnProperty(key)) {
let success = this.forms[key]["success"];
if (
success.part &&
document.location[success.part].indexOf(success.text) + 1
) {
this.sendFormToCM();
break;
} else if (
success.element &&
document.querySelector(success.element) &&
(success.attribute == "innerText" ||
document
.querySelector(success.element)
.hasAttribute(success.attribute)) &&
(document
.querySelector(success.element)
.innerText.indexOf(success.text) + 1 ||
document
.querySelector(success.element)
.getAttribute(success.attribute)
.indexOf(success.text) + 1)
) {
this.sendFormToCM();
break;
}
}
}
};

CFSTP.prototype.sendFormToCM = function () {
let cFormData = JSON.parse(sessionStorage.getItem("cFormData"));
if (!cFormData) {
return;
}
sessionStorage.removeItem("cFormData");
let cData = cFormData.cData;
if (!cData) {
if (this.config.debug) {
console.group("%c%s", "color: gray;", "Form Data is missing");
console.groupEnd();
}
return;
}
if (this.config.debug) {
console.group("%c%s", "color: green;", "Sending form to Comagic-> ");
console.log(cData);
console.groupEnd();
} else {
if (cFormData.cWidget == "request") {
window.Comagic &&
Comagic.addOfflineRequest &&
Comagic.addOfflineRequest(cData);
} else if (cFormData.cWidget == "call") {
window.Comagic && Comagic.sitePhoneCall && Comagic.sitePhoneCall(cData);
} else if (cFormData.cWidget == "both") {
window.Comagic &&
Comagic.addOfflineRequest &&
Comagic.addOfflineRequest(cData) &&
Comagic.sitePhoneCall &&
Comagic.sitePhoneCall(cData);
}
}
};

CFSTP.prototype.init = function () {
document.addEventListener("click", this.formHandler.bind(this), true);
this.waitingMainScript();
};

let ComagicFormsSenderTP = new CFSTP();
ComagicFormsSenderTP.init();
})();