//if( !window['jcClWidgetBase'] ){
//открытие по тэгу
if( typeof jcClWidgetBaseOk === 'undefined'){
var jcClWidgetBase = function (param) {
this.hash = param.hash;
this.wid = param.wid;
this.host = param.host;
this.type = '';
this.content = '';
this.button = '';
this.cssfile = this.host+'/widget/widget.css';
this.parentid = 0; //родительская сделка
this.customer = {};
this.init(param);
this.referrer = '';
this.setstat();
this.onopenform = '';
this.oncloseform = '';
this.progressDiv = false;
this.progress = false;
if(!jcClWidgetBase.mapWidgets[this.wid])
jcClWidgetBase.mapWidgets[this.wid] = this;
}
jcClWidgetBase.prototype = {
init: function(param) {
var thisObj = this;
this.maindiv = document.createElement('div');
this.maindiv.id='cl_widget_'+this.hash;
this.maindiv.innerHTML = '';
this.img = document.createElement('img');
this.img.id='cl_widget_'+this.hash+'_img';
this.img.className = 'clwidget_img';
this.maindiv.appendChild(this.img);
this.btn = document.createElement('div');
this.btn.id = 'cl_widget_button_'+this.hash;
this.btn.className = 'clwidget-btn';
this.btn.onclick = function(e) {
thisObj.openform();
}
this.maindiv.appendChild(this.btn);
this.container = document.createElement('div');
this.container.id='cl_widget_content_'+this.hash;
this.container.className='clwidget-content';
this.container.style.display = 'none';
this.maindiv.appendChild(this.container);
this.modal = document.createElement('div');
this.modal.id='cl_widget_overlay_'+this.hash;
this.modal.className='clwidget-overlay';
this.modal.onclick = function(e) {
thisObj.closeform();
}
this.maindiv.appendChild(this.modal)
document.body.appendChild(this.maindiv);
this.modal.style.zIndex = 9990;
this.container.style.zIndex = 9999;
jcClWidgetBase.mapWidgets[ this.wid ] = this;
this.parentid = 0;
this.customer = { id:0, fname:'', mname:'', lname:'', phone:'', email:'', city:''};
}, //init
openform: function() {
this.modal.style.display='block';
this.modal.style.opacity = 0.7;
this.container.style.display='block';
if(this.onopenform)
this.onopenform();
},
closeform: function() {
this.modal.style.display='none';
this.modal.style.opacity = 0;
this.container.style.display='none';
if(this.oncloseform)
this.oncloseform();
},
getcookie: function(name) {
var reg = new RegExp('('+name+'=)([^;]*)','i');
var matches = document.cookie.match(reg);
return matches ? decodeURIComponent(matches[2]) : false;
},
setcookie: function(name,val,options) {
var str=name+'='+val+';path=/;';
if(options && options.expires)
str+='expires='+options.expires;
document.cookie = str;
},
deletecookie: function(name) {
this.setcookie(name, "", {expires: -1})
},
setstat: function() {
this.referrer = this.getcookie('clwd_ref');
if(this.referrer == false) {
this.referrer = document.referrer;
var date = new Date;
date.setDate(date.getDate() + 7);
this.setcookie('clwd_ref',document.referrer,{expires : date.toUTCString()});
}
},
toYandex: function(target) {
//Yandex metrika
var yaID=0;
if (typeof Ya !== "undefined")
if(Ya && Ya._metrika && Ya._metrika.getCounters && Ya._metrika.getCounters().length && Ya._metrika.getCounters()[0].id) {
yaID = Ya._metrika.getCounters()[0].id+'';
if(eval('yaCounter'+yaID)) {
var yaCNT = eval('yaCounter'+yaID);
yaCNT.reachGoal(target);
}
}
},
toGoogle: function(target) {
//Google Analytics
if (typeof _gaq !== "undefined")
if(_gaq && _gaq.push)
_gaq.push(['_trackEvent', 'CloffWidget', target]);
//Universal Analytics
else if (typeof ga !== "undefined")
if(ga)
ga('send', 'event', 'CloffWidget', target);
},
callscript: function(url,data,fcallback) {
var t = new Date().getTime();
var str = url+'?_t='+t+'&_w='+this.wid+'&_h='+this.hash+'&_r='+encodeURIComponent(this.referrer);
if(data)
str+='&'+data;
this.img.onload = function() {
var nstatus = parseInt(this.naturalHeight,10);
if(fcallback)
fcallback(nstatus);
}
this.img.src = str;
},
getparams: function(){
var t = new Date().getTime();
return ('_t='+t+'&_w='+this.wid+'&_h='+this.hash+'&_r='+encodeURIComponent(this.referrer));
},
//отправка запроса CORS
callajax: function(url, data, fcallback, isprogress, errfunc) {
var t = new Date().getTime();
var thisObj = this;
var xmlHttp = new XMLHttpRequest();
xmlHttp.withCredentials = true;
if(data){
xmlHttp.open( (data?'POST':'GET'), url, true);
xmlHttp.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
data += ('&_t='+t+'&_w='+this.wid+'&_h='+this.hash+'&_r='+encodeURIComponent(this.referrer));
}
else {
url += ( ( (url.indexOf('?')<=0) ? '?' : '&')+'_t='+t+'&_w='+this.wid+'&_h='+this.hash+'&_r='+encodeURIComponent(this.referrer));
xmlHttp.open( (data?'POST':'GET'), url, true);
}
xmlHttp.onreadystatechange = function(){
var param = false;
//console.log('xmlHttp.readyState',xmlHttp.readyState, 'isprogress', isprogress);
if(isprogress){
//console.log('xmlHttp.readyState if(isprogress){ ',(25*xmlHttp.readyState));
thisObj.setProgress(25*xmlHttp.readyState);
}
if(xmlHttp.readyState == 4){
if(isprogress){ thisObj.hideProgress(); }
if(xmlHttp.status !=200 ) {
if(errfunc)
errfunc();
if( xmlHttp.status != 0 ){
alert('ERROR: request error.\nResponce status:'+xmlHttp.status);
return false;
}
else{
xmlHttp.abort();
return false;
}
}
var resp = xmlHttp.responseText;
try {
param = eval(resp)[0];
} catch (e) {
console.log('Ошибка обработки ответа:', resp);
console.error(e);
if(errfunc)
errfunc();
return false;
};
if(fcallback) fcallback(param);
}
};
if(isprogress){ this.showProgress(); }
xmlHttp.send(data);
var n = 0;
var prog = function(){
n++;
thisObj.setProgress(4*n);
if(n<20)
window.setTimeout( prog, 100);
};
prog();
},
setProgress: function(v){
this.progress.value = v;
},
showProgress: function(){
if(!this.progressDiv){
this.progressDiv = document.createElement("DIV");
this.progressDiv.className = 'clwidget-progress-overlay';
this.progressDiv.style.display = 'none';
this.progress = document.createElement("PROGRESS");
this.progress.className = 'clwidget-progress';
this.progress.max = 100;
this.progressDiv.appendChild(this.progress);
document.body.appendChild(this.progressDiv);
}
//получим zIndex
this.progress.value = 0;
this.progressDiv.style.zIndex = ( this.zIndex ? parseInt(this.zIndex,10)+10000: (this.container?parseInt(this.container.style.zIndex,10):0)+5);
this.progressDiv.style.display = 'block';
},
hideProgress: function(){
this.progressDiv.style.display = 'none';
}, //hideProgress
setParent: function(pid){
this.parentid = pid;
}, //setParent
setCustomer: function(oc){
this.customer = Object.assign({}, oc);
}, //setCustomer
}
jcClWidgetBase.mapWidgets = {};
jcClWidgetBase.getWidget = function(wid){
return ( jcClWidgetBase.mapWidgets[wid] ? jcClWidgetBase.mapWidgets[wid]: false );
};
jcClWidgetBase.onHASH = function(e){
console.log('HASH333', location.hash);
if(location.hash.substr(0,6)==='#CLWDG'){
let wid = parseInt(location.hash.substr(6),10);
if(wid){
let wgd = jcClWidgetBase.getWidget(wid);
if(wgd)
wgd.openform();
}
}
}
window.addEventListener('DOMContentLoaded', (event) => {
window.addEventListener("hashchange"
, function(e) {
jcClWidgetBase.onHASH(e);
}
, false);
console.log('OK DOMContentLoaded', jcClWidgetBase.mapWidgets);
//ну и проверим а вдруг сразу?
if( location.hash && location.hash.length)
setTimeout( function(){ jcClWidgetBase.onHASH(); },800);
});
console.log('SET DOMContentLoaded', jcClWidgetBase.mapWidgets);
var jcClWidgetBaseOk = 1;
} //END if jcClWidgetBaseOk;
//} //if( !window['jcClWidgetBase'] ){
function jcClWidgetTracking(param) {
this.init = function() {
this.css = document.createElement('style');
this.css.type='text/css';
this.css.innerHTML = '.clCallTrackingPoint {}';
document.body.appendChild(this.css);
}
jcClWidgetBase.apply(this, arguments);
this.content = param.content;
this.onopenform = function() { };
this.oncloseform = function() { };
}
jcClWidgetTracking.prototype = Object.create(jcClWidgetBase.prototype);
jcClWidgetTracking.prototype.tracking = function() {
var phone = this.getcookie('clwd_phone');
if(phone === false) { //
//
if(window.location.search.length>1) {
var params = this.parseGetParams(window.location.search.substring(1));
var id = this.searchParams(params);
if(id!==false) { //
//
this.callajax(this.host+'/widget/setstat.php','url='+encodeURIComponent(window.location.href)+'&pid='+id);
return this.setNumbers(this.track[id].phone);
}
}
//
if(this.referrer.length) {
var get = this.referrer.split('?');
if(get.length>1) {
var params = this.parseGetParams(get[1]);
var id = this.searchParams(params);
if(id!==false) { //
//
this.callajax(this.host+'/widget/setstat.php','pid='+id);
return this.setNumbers(this.track[id].phone);
}
}
}
//
if(this.referrer.length) {
var host = this.referrer.split( '//' )[1].split('/')[0];
var id = this.searchHost(host);
if(id!==false) { //
//
this.callajax(this.host+'/widget/setstat.php','pid='+id);
return this.setNumbers(this.track[id].phone);
}
}
//
//
this.callajax(this.host+'/widget/setstat.php','pid='+this.getDefaultId());
return this.setNumbers(this.getDefault());
} else
this.setNumbers(phone);
}
jcClWidgetTracking.prototype.searchHost = function(host) {
for(var i in this.track) {
if(this.track[i].option!=='')
continue;
if(host.indexOf(this.track[i].host)>-1)
return i;
}
return false;
}
jcClWidgetTracking.prototype.getDefault = function() {
var first = false;
for(var i in this.track) {
if(first===false)
first=i;
if(this.track[i].def==1)
return this.track[i].phone;
}
return this.track[first].phone;
}
jcClWidgetTracking.prototype.getDefaultId = function() {
var first = false;
for(var i in this.track) {
if(first===false)
first=i;
if(this.track[i].def==1)
return i;
}
return first;
}
jcClWidgetTracking.prototype.searchParams = function(params) {
if(typeof(params)!='object')
return false;
for(var i in this.track) {
if(this.track[i].option=='')
continue;
var options = this.parseGetParams(this.track[i].option);
for(var o in options) {
var isok=false;
for(var p in params) {
if((o==p) && (options[o]==params[p])) {
isok=true;
break;
}
}
if(!isok)
break;
}
if(isok) {
return i;
}
}
return false;
}
jcClWidgetTracking.prototype.setNumbers = function(phone) {
this.setcookie('clwd_phone', phone);
var tels = document.querySelectorAll('.clCallTrackingPoint');
for(t in tels) {
tels[t].innerHTML = phone;
tels[t].value = phone;
}
return true;
}
jcClWidgetTracking.prototype.parseGetParams = function(str) {
var obj = {};
var arr = str.split("&");
for(var i=0; i