var INPUTCOLOR = ""; //colore del bordo dell'input di default var INPUTERROR = "1px solid #FF9900"; var GIORNI = Array("lun","mar","mer","gio","ven","sab","dom"); var timeOutId; //Timeout di alertWin var URLSITO = 'https://www.ciaospesa.it/'; var SCMD = 'condizioniGenerali'; var NODISPO = ""; var LOGGEDIN = false; /* ALERT ********************************/ function alertSwal(ty,ti,mex,fn){ //type = warning, error, success, info, question if(fn==undefined) fn=function(){}; swal({ title: ti, text: mex, type: ty, timer: 6000}, function(confirm){ setTimeout(fn,200); }); }; function confirmSwal(mex,fn,fn2){ swal({ title: "Avviso", text: mex, type: 'warning', showCancelButton: true, confirmButtonText: 'Ok', cancelButtonText: 'Annulla' }, function(confirm){ if(confirm){ setTimeout(fn,200); }else{ setTimeout(fn2,200); } }); } function viewLoader(mex){ var ldr = document.getElementById('nonsolospinner'); var ldrdiv = document.getElementById('nonsolospinnerdiv'); ldr.firstChild.innerHTML = mex; ldr.style.display = "block"; ldrdiv.style.display = "block"; } function hideLoader(cont,dbg){ if(dbg){ if(typeof(cont)!='string') cont = JSON.stringify(cont); var ldr = document.getElementById('nonsolospinner'); ldr.firstChild.innerHTML = cont; }else{ var ldr = document.getElementById('nonsolospinner'); var ldrdiv = document.getElementById('nonsolospinnerdiv'); ldr.style.display = "none"; ldrdiv.style.display = "none"; ldr.firstChild.innerHTML = ''; } } function notificaTopRight(tot,img,nome,qta){ var tmpl = '
FB ID: '+response.id+'
Name: '+response.first_name+' '+response.last_name+'
Email: '+response.email+'
Gender: '+response.gender+'
Locale: '+response.locale+'
Picture:
FB Profile: click to view profile
';*/ /*if(response.age_range==undefined) response.age_range = {'min':18} if(response.age_range['min']<18){ FB.api('/me/permissions', 'delete', function(response) { alertSwal('warning','Richiesta Facebook', 'rifiutata per minori di anni 18', function(){fbLogout()}); }); }else{*/ if(!LOGGEDIN) saveUserData(response); /*}*/ }); } function fbLogout() { var dummy = FB.getLoginStatus(function(response) { if (response.status === 'connected') { FB.logout(function() { location.href='https://www.ciaospesa.it/logout'; }); }else{ location.href='https://www.ciaospesa.it/logout'; } }); if(dummy==undefined){ location.href='https://www.ciaospesa.it/logout'; }; } function glLogin(response) { const responsePayload = decodeJWT(response.credential); saveUserDataG(responsePayload); /*console.log(" Full Name: " + responsePayload.name); console.log(" Given Name: " + responsePayload.given_name); console.log(" Family Name: " + responsePayload.family_name); console.log(" Unique ID: " + responsePayload.sub); console.log(" Profile image URL: " + responsePayload.picture); console.log(" Email: " + responsePayload.email);*/ } function decodeJWT(token) { let base64Url = token.split(".")[1]; let base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); let jsonPayload = decodeURIComponent( atob(base64) .split("") .map(function (c) { return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); }) .join("") ); return JSON.parse(jsonPayload); } function saveUserDataG(userData){ viewLoader('Caricamento in corso...'); $.post('https://www.ciaospesa.it/userDataG', {oauth_provider:'google',userData: JSON.stringify(userData)}, function(){},'json') .done(function(resp){ if (resp.result=="ok") { window.location.reload(false); } else { alertSwal('warning','Richiesta google', 'richiesta annullata o non autorizzata.'); } }).fail(function(){alertSwal('error','', "Errore di connessione");}) .always(function(resp){hideLoader(resp,0);}); } function eliminaUtente() { confirmSwal('1/2 Facendo clic su Ok cancellerai il tuo profilo e i tuoi dati.',function(){ confirmSwal('2/2 Sei sicuro di voler cancellare il tuo account?',function(){ viewLoader('Eliminazione in corso...'); $.post('https://www.ciaospesa.it/userDel','',function(){},'json') .done(function(resp){ if(resp.result=="ok"){ var fn = function(){window.location.href=URLSITO+'logout'} alertSwal('success',"Eliminazione effettuata", 'i tuoi dati sono stati cancellati.', fn); }else if(resp.result=="ko"){ if(resp.error == "ordini"){ alertSwal('warning','Eliminazione annullata', 'hai un ordine pendente.'); } } }).fail(function(){alertSwal('error','', "Errore di connessione");}) .always(function(resp){hideLoader(resp,0);}); }); }); } function saveUserData(userData){ viewLoader('Caricamento in corso...'); $.post('https://www.ciaospesa.it/userData', {oauth_provider:'facebook',userData: JSON.stringify(userData)}, function(){},'json') .done(function(resp){ if (resp.result=="ok") { window.location.reload(false); } else { alertSwal('warning','Richiesta Facebook', 'richiesta annullata o non autorizzata.'); } }).fail(function(){alertSwal('error','', "Errore di connessione");}) .always(function(resp){hideLoader(resp,0);}); } function forgottenPass(obj) { var frm = formica(obj); flag = true; flag = flag && controlForm(frm, 'Email','email',''); flag = flag && controlForm(frm, 'Email','email','','@'); if (flag) { viewLoader('Caricamento in corso...'); $.post('https://www.ciaospesa.it/forgottenPass', frm.buildUri('php'), function () {}, 'json').done(function (resp) { if (resp.s == "ok") { alertSwal('success','', 'A breve riceverai un\'e-mail per il ripristino della password','window.location.href=URLSITO'); } else if (resp.s == 'no') { alertSwal('error','', "Errore durante la modifica"); } else { alertSwal('error','', "Errore durante la modifica"); } }).fail(function () { alertSwal('error','', "Errore di connessione"); }).always(function (resp) {hideLoader(resp,0);}); } } function changePass(obj) { var frm = formica(obj); flag = true; flag = flag && controlForm(frm, 'Password','password',''); flag = flag && controlForm(frm, 'Password','password','6','c<'); flag = flag && controlForm(frm, 'Le password','password','password2','='); if (flag) { viewLoader('Caricamento in corso...'); $.post('https://www.ciaospesa.it/resetPass', frm.buildUri('php'), function () {}, 'json').done(function (resp) { if (resp.s == "ok") { alertSwal('success','', 'Password modificata',"location.href='https://www.ciaospesa.it/'"); } else { alertSwal('error','', "Errore durante la modifica"); } }).fail(function () { alertSwal('error','', "Errore di connessione"); }).always(function (resp) { hideLoader(resp,0); }); } } function addCoupon(obj){ var frm = formica(obj); flag = true; flag = flag && controlForm(frm, 'Codice coupon','codice',''); if(flag){ viewLoader('Inserimento in corso...'); var fn = function(){window.location.href=URLSITO+'coupon'} $.post('https://www.ciaospesa.it/couponAdd',frm.buildUri('php'),function(){},'json') .done(function(resp){ if(resp.result=="ok"){ alertSwal('success',"Inserimento effettuato", 'il coupon è sato aggiunto.', fn); }else if(resp.result=="ko"){ alertSwal('warning','Codice coupon', 'non valido o scaduto.', fn); } }).fail(function(){alertSwal('error','', "Errore di connessione");}) .always(function(resp){hideLoader(resp,0);}); } } function modServizio(idS,pubS){// simile a carrello.switchServizi viewLoader('Caricamento in corso...'); var strUri="idS[]="+idS+"&pubS[]="+pubS; $.post(URLSITO+'modServizio', strUri, function () {}, 'json') .done(function (resp) { if (resp.s == "ok") { window.location.reload(false); } else { alertSwal('error','', "Errore durante la modifica"); } }).fail(function(){alertSwal('error','', "Errore di connessione");}) .always(function(resp){hideLoader(resp,0);}); } function voto(obj,id,curr,evnt){ var hidd = document.getElementById('rank'+curr); var currentVal = hidd.value; var links = getChildTag(obj.parentNode,"A"); for(var l in links){ if(links[l].className.indexOf('star')>-1){//### stelle switch(evnt){ case "over": if(l
');
//tmplS = tmplS.replace(/##promocss##/g,'consegna');
tmpl = tmpl.replace(/##prezzo##/g, '€ '+p.pz);
tmpl = tmpl.replace(/##tagliato##/g, '');
}
tmplS = tmplS.replace(/##promo##/g, p.pro.nme.replace('"','\"'));
tmpl = tmpl.replace(/##sconto##/g, tmplS);
}else{
tmpl = tmpl.replace(/##sconto##/g, '');
tmpl = tmpl.replace(/##prezzo##/g, '€ '+p.pz);
tmpl = tmpl.replace(/##tagliato##/g, '');
}
if(p.img=="") p.img = "img/prodotti/generico.png";
tmpl = tmpl.replace(/##img##/g, URLSITO+p.img);
tmpl = tmpl.replace(/##prezzoKg##/g, p.kg);
tmpl = tmpl.replace(/##um##/g, p.ump);
tmpl = tmpl.replace(/##qta##/g, p.minC);
var ele = document.createElement('DIV');
ele.className = 'prodotto';
ele.innerHTML = tmpl;
var spans = getChildTag(ele,"SPAN");
for(var l in spans){
switch(spans[l].className){
case "quantita":
this.qta = spans[l];//### span quantita
break;
}
}
this.carrello = {qta:p.minC,qMax:p.qMax,qStp:p.qStp,qVar:p.qVar};
this.tOutId=0;
var links = getChildTag(ele,"A");
for(var l in links){
switch(links[l].className){
case "preferito":
this.pf = links[l];//### link preferito
links[l].onclick = function(){dispatcher.switchPreferito(p.id);};//niente this
if(p.pf=="1") replaceClass(this.pf,"","attivo");
break;
case "piu":
this.piu = links[l];//### link piu'
if(p.minC>"0") replaceClass(this.piu.parentNode,"","aperto");
links[l].onclick = this.setTimeQuantita.bind(this,"+");
break;
case "meno":
links[l].onclick = this.setTimeQuantita.bind(this,"-");//### link meno
break;
case "detProdotto":
links[l].onclick = this.getDettaglio.bind(this);//### link modale
break;
}
}
// modale der il dettaglio
this.modal = document.getElementById('modals-detProdotto');
this.pop = {};
/*"inC": "0", "qMin":1*/
this.dom = ele;
return this;
}
prodotto.prototype.switchPreferito = function(res){
if(res=="0"){
replaceClass(this.pf,"attivo","");
}else if(res=="1"){
replaceClass(this.pf,"","attivo");
}
};
prodotto.prototype.setTimeQuantita = function(ty){
if(this.tOutId) clearTimeout(this.tOutId);
this.tOutId=0;
var qta = parseInt(this.carrello.qta,10);
//this.carrello = {qta:p.minC,qMax:p.qMax,qStp:p.qStp,qVar:p.qVar};
if(ty=='+'){
qta += this.carrello.qStp;
}else{
qta -= this.carrello.qStp;
}
if(qta<=0){
qta=0;
replaceClass(this.piu.parentNode,"aperto","");
}else{
replaceClass(this.piu.parentNode,"","aperto");
}
if(qta>this.carrello.qMax){
qta=this.carrello.qMax;
}
this.qta.innerHTML = qta;
this.carrello.qta = qta;
var idProdotto=this.id;
var fn = function(){dispatcher.switchQuantita(idProdotto,qta);};
this.tOutId = setTimeout(fn,700);//
};
prodotto.prototype.switchQuantita = function(qta){
qta = parseInt(qta,10);
if(qta<=0){
qta=0;
replaceClass(this.piu.parentNode,"aperto","");
}else{
replaceClass(this.piu.parentNode,"","aperto");
}
if(qta>this.carrello.qMax){
qta=this.carrello.qMax;
}
this.qta.innerHTML = qta;
};
prodotto.prototype.getDettaglio = function(){
var self = this;
viewLoader('Caricamento in corso...');
$.post(URLSITO+'detProdotto','idProdotto='+this.id,function(){},'json')
.done(function(resp){self.getDettaglioDone(resp);})
.fail(function(){alertSwal('error','', "Errore di connessione");})
.always(function(resp){hideLoader(resp,0);});
};
prodotto.prototype.getDettaglioDone = function(resp){
if(resp.result == "ok"){
var pr = resp.prodotto[0];
this.pop.id = pr.id;
var a,b,c,d,e,f;
var divs = getChildTag(this.modal,"DIV");
for(var l in divs){
switch(divs[l].className){
case "carousel-inner":
a=divs[l];
break;
case "dati col-sm-6":
var spans = getChildTag(divs[l],"SPAN");
for(var l in spans){
switch(spans[l].className){
case "marca":
spans[l].innerHTML = pr.mrk.replace('"','\"');
break;
case "nome_prodotto":
spans[l].innerHTML = pr.nom.replace('"','\"');
break;
case "prezzo":
case "prezzo_listino":
if(pr.pro.id!=''){
if(pr.pro.cons==0){
if(spans[l].className=='prezzo'){
var prezzo = parseFloat(pr.pz.replace(",",'.')).toFixed(2);
//var sconto = (prezzo*parseInt(pr.pro.sco,10)/100).toFixed(2);
var sconto = (prezzo*parseFloat(pr.pro.scoF)/100);
prezzo = (prezzo - sconto).toFixed(2);
//tmplS = '
');
//tmplS = tmplS.replace(/##promocss##/g,'consegna');
}
var nomeP = pr.pro.nme.replace('"','\"');
if(pr.pro.dsc.trim()!='') nomeP = nomeP+' *';// se manca la sercrizione non mettere *
tmplS = tmplS.replace(/##promo##/g, nomeP);
divs[l].outerHTML = tmplS;
/*var ele = document.createElement('SPAN');
ele.className = 'apice colore1';
ele.innerHTML = ' *';
divs[l].appendChild(ele);*/
}else{
divs[l].style.display='none';
}
break;
case "nota asterisco":
if(pr.pro.dsc.trim()!=''){
var tasto = '
');
//tmpl = tmpl.replace(/##prezzo##/g, '€ '+p.pz);
}
tmplS = tmplS.replace(/##promo##/g, p.pro.nme.replace('"','\"'));
tmpl = tmpl.replace(/##sconto##/g, tmplS);
}else{
//tmpl = tmpl.replace(/##peso##/g, '');
tmpl = tmpl.replace(/##prezzo##/g, '€ '+p.pz);
tmpl = tmpl.replace(/##sconto##/g, '');
}
if(p.img=="") p.img = "img/prodotti/generico.png";
tmpl = tmpl.replace(/##img##/g, URLSITO+p.img);
//tmpl = tmpl.replace(/##prezzo##/g, "€ "+p.pz);
tmpl = tmpl.replace(/##qta##/g, p.minC);
var ele = document.createElement('DIV');
ele.className = 'dettaglio_prodotto';
ele.innerHTML = tmpl;
var spans = getChildTag(ele,"SPAN");
for(var l in spans){
switch(spans[l].className){
case "quantita":
this.qta = spans[l];//### span quantita
break;
case "prezzo":
this.pz = spans[l];
break;
case "prezzo_listino":
this.pzT = spans[l];
break;
}
}
/*var spans = getChildTag(ele,"DIV");
for(var l in spans){
switch(spans[l].className){
case "prezzo":
this.pz = spans[l];//### div prezzo
break;
}
}*/
var selss = getChildTag(ele,"SELECT");
for(var l in selss){
switch(selss[l].className){
case "selectpicker":
this.noDispo = selss[l];//### select no dispo// ora caratteristiche
//this.noDispo.innerHTML = NODISPO;
//this.noDispo.value = p.noId;
if(p.car.length>0){
this.noDispo.name='idCaratteristica';
this.noDispo[0] = new Option("Pezzi/Fette o altro", '0', false, false);//Scegli un'opzione
oThis = this;
var xarat = '0';
p.car.forEach(function (carat){
oThis.noDispo[oThis.noDispo.options.length] = new Option(carat.n, carat.id, false, carat.s=='1');
if(carat.s=='1') xarat = carat.id;
});
this.noDispo.value = xarat;
this.noDispo.onchange = this.setTimeQuantita.bind(this,"=");
}else{
this.noDispo.parentNode.style.display='none';
}
break;
}
}
var PQ=this.prezzoQuantita(p.minC,p.pz,p.qVar,p.ps,p.kg,p.pro.id,p.pro.cons,p.pro.sco,p.pro.scoF);
this.pz.innerHTML = PQ.prezzo;
this.pzT.innerHTML = PQ.tagliato;
this.carrello = {qta:p.minC,qMax:p.qMax,qStp:p.qStp,qVar:p.qVar,ps:p.ps,pz:p.pz,kg:p.kg,proId:p.pro.id,proCons:p.pro.cons,proSco:p.pro.sco,proScoF:p.pro.scoF};
this.tOutId=0;
var links = getChildTag(ele,"A");
for(var l in links){
switch(links[l].className){
case "preferito":
this.pf = links[l];//### link preferito
links[l].onclick = this.switchPreferito.bind(this);
if(p.pf=="1") replaceClass(this.pf,"","attivo");
break;
case "piu":
this.piu = links[l];//### link piu'
if(p.minC>"0") replaceClass(this.piu.parentNode,"","aperto");
links[l].onclick = this.setTimeQuantita.bind(this,"+");
break;
case "meno":
links[l].onclick = this.setTimeQuantita.bind(this,"-");//### link meno
break;
}
}
if(typeof carrello != 'undefined' && carrello.instance=="carrello") this.controller = carrello;
this.dom = ele;
return this;
}
prodottoCarrello.prototype.switchPreferito = function(){
var oThis = this;
viewLoader('Caricamento in corso...');
$.post(URLSITO+"preferiti","idProdotto="+encodeURIComponent(this.id),function(){},'json')
.done(function(resp){
if(resp.result=="ok"){
oThis.switchPreferitoDone(resp.preferito);
}
}).fail(function(){alertSwal('error','', "Errore di connessione");})
.always(function(resp){hideLoader(resp,0);});
};
prodottoCarrello.prototype.switchPreferitoDone = function(res){
if(res=="0"){
replaceClass(this.pf,"attivo","");
}else if(res=="1"){
replaceClass(this.pf,"","attivo");
}
};
prodottoCarrello.prototype.setTimeQuantita = function(ty){
if(this.tOutId) clearTimeout(this.tOutId);
this.tOutId=0;
var qta = parseInt(this.carrello.qta,10);
if(ty=='+'){
qta += this.carrello.qStp;
}else if(ty=='-'){
qta -= this.carrello.qStp;
}
if(qta<=0){
qta=0;
}
if(qta>this.carrello.qMax){
qta=this.carrello.qMax;
}
this.qta.innerHTML = qta;
this.carrello.qta = qta;
var PQ = this.prezzoQuantita(this.carrello.qta.toString(),this.carrello.pz,this.carrello.qVar,this.carrello.ps,this.carrello.kg,this.carrello.proId,this.carrello.proCons,this.carrello.proSco,this.carrello.proScoF);
this.pz.innerHTML = PQ.prezzo;
this.pzT.innerHTML = PQ.tagliato;
var fn = this.switchQuantita.bind(this);
this.tOutId = setTimeout(fn,700);//
};
prodottoCarrello.prototype.switchQuantita = function(){
var oThis = this;
var strUri= "idProdotto="+encodeURIComponent(this.id)+"&qta="+encodeURIComponent(this.carrello.qta)+"&idNoDisp=&idCarat="+encodeURIComponent(this.noDispo.value);
viewLoader('Caricamento in corso...');// da un po' fastidio onestamente
$.post(URLSITO+"prodottiCarrello",strUri,function(){},'json')
.done(function(resp){
if(resp.result=="ok"){
oThis.switchQuantitaDone(resp);
}
}).fail(function(){alertSwal('error','', "Errore di connessione");})
.always(function(resp){hideLoader(resp,0);});
},
prodottoCarrello.prototype.switchQuantitaDone = function(resp){
if(resp.result=="ok"){
var prod = resp.prodotti[0];
this.qta.innerHTML = prod.qta;
//this.noDispo.value = prod.noId;
var PQ = this.prezzoQuantita(prod.minC,prod.pz,prod.qVar,prod.ps,prod.kg,prod.pro.id,prod.pro.cons,prod.pro.sco,prod.pro.scoF);
this.pz.innerHTML = PQ.prezzo;
this.pzT.innerHTML = PQ.tagliato;
this.controller.getCarrello();
}
};
prodottoCarrello.prototype.prezzoQuantita = function(qta,pz,qVar,ps,kg,pId,pCon,pSco,pScoF){
if(qta=="0") return {'prezzo':'€ 0,00','tagliato':''};
qta = qta.replace(',', '.');
qta = parseFloat(qta).toFixed(2);
pz = pz.replace(',', '.');
pz = parseFloat(pz).toFixed(2);
kg = kg.replace(',', '.');
kg = parseFloat(kg).toFixed(2);
var pr = parseFloat(qta * pz).toFixed(2);
if(qVar=="1") pr = parseFloat(qta * kg / 1000).toFixed(2);
/*return "€ "+pr.toFixed(2).toString().replace('.', ',');*/
if(pId!=''){
if(pCon==0){
var prezzo = pr;
//var sconto = (prezzo*parseInt(pSco,10)/100).toFixed(2);
var sconto = (prezzo*parseFloat(pScoF)/100);
prezzo = (prezzo - sconto).toFixed(2);
//return '€ '+prezzo.replace('.',',')+' € '+pr.replace('.', ',')+'';
return {'prezzo':'€ '+prezzo.replace('.',',')+' ','tagliato':'€ '+pr.replace('.', ',')};
}else{
//return '€ '+pr.replace('.', ',');
return {'prezzo':'€ '+pr.replace('.',','),'tagliato':''};
}
}else{
//return '€ '+pr.replace('.', ',');
return {'prezzo':'€ '+pr.replace('.',','),'tagliato':''};
}
};
// ************* !Prodotto *************
// ************* Pageing *************
function pageingClass(obj,div,rxp,nr){
this.oObj = obj;
this.currPage = 0;
this.numPages = Math.ceil(nr/rxp)-1;
this.rxp = rxp;
this.box = document.getElementById(div);
this.box.innerHTML = '