// JavaScript Document
function kosarba(cikk) {
var cikkszam = document.getElementById('cikkszam'+cikk).value;
var ar = document.getElementById("ar"+cikk).value;
var mennyi = 1;
if ( $("#menny") != null ) {
var mennyi = $("#menny").val();
}
var opciok = $("#variacio").val();

/*
$('select').each(function () {
if (this.className.indexOf("ktopcio") != -1) {
//console.log(this.id+':'+$(this).val());
if ( $(this).val() != '' ) {
opciok = opciok+'['+$(this).val()+']';
}
}
});
*/

$.get("kosarba.php?cikk="+cikk+"&ar="+ar+"&mennyi="+mennyi+"&opciok="+opciok,function(obj) {
bootbox.alert(obj);
kiskosarfriss();
window.setTimeout(function(){
bootbox.hideAll();
}, 2000);
});
}

function kosarbaUpsell(cikk) {
var cikkszam = document.getElementById('cikkszam'+cikk).value;
var ar = document.getElementById("ar"+cikk).value;
var mennyi = 1;
if ( $("#menny") != null ) {
var mennyi = $("#menny").val();
}
var opciok = $("#variacio").val();
/*
$('select').each(function () {
if (this.className.indexOf("ktopcio") != -1) {
//console.log(this.id+':'+$(this).val());
if ( $(this).val() != '' ) {
opciok = opciok+'['+$(this).val()+']';
}
}
});
*/
$.get("kosarba.php?cikk="+cikk+"&ar="+ar+"&mennyi="+mennyi+"&opciok="+opciok,function(obj) {
bootbox.alert(obj);
fullKosarFriss();
upsellFriss();
kiskosarfriss();
window.setTimeout(function(){
bootbox.hideAll();
}, 2000);
});
}

function fullKosarFriss() {
$.get("fullkosarfriss.php",function(obj) {
$("#fullkosar").html(obj);
//kiskosarfriss();
});
}

function upsellFriss() {
$.get("upsellfriss.php",function(obj) {
$("#upsell").html(obj);
//kiskosarfriss();
});
}

function kiskosarfriss() {
$.get("kiskosar.php",function(obj) {
//alert(obj);
$("#kiskosar").html(obj);
$("#kiskosarmobil").html(obj);
});
}

function novel() {
var szam = document.getElementById("menny").value;
var szamInt = parseInt(szam);
szamInt = szamInt+1;
var termekid = $("input[name=cikkszam]").val();
var mx = 0;
var opciok = $("#variacio").val();

$.get("kosarmennyisegnovel.php?cikk="+termekid+"&mennyi="+szamInt+"&opciok="+opciok,function(obj) {
  mx = parseInt(obj);
  console.log(mx);
  if ( szamInt >= mx ) { szamInt = mx; }
  document.getElementById("menny").value = szamInt;
});
}

function csokkent() {
var szam = document.getElementById("menny").value;
var szamInt = parseInt(szam);
szamInt = szamInt-1;
if ( szamInt < 1 ) { szamInt = 1; }
document.getElementById("menny").value = szamInt;
}


function addKosarItem(kosid) {
$.get("addkosaritem.php?kosid="+kosid,function(obj) {
$("#fullkosar").html(obj);
kiskosarfriss();
upsellFriss();
});
}

function remKosarItem(kosid) {
$.get("remkosaritem.php?kosid="+kosid,function(obj) {
$("#fullkosar").html(obj);
kiskosarfriss();
upsellFriss();
});
}

function delKosarItem(kosid) {
$.get("delkosaritem.php?kosid="+kosid,function(obj) {
$("#fullkosar").html(obj);
kiskosarfriss();
upsellFriss();
});
}

function kedvencbe(cikk, gilf) {
$.get("kedvencbe.php?cikk="+cikk,function(obj) {
//$("#vegossz").html(obj);
bootbox.alert(obj);
$( "#"+gilf ).toggleClass( "glyphicon-heart" );
$( "#"+gilf ).toggleClass( "glyphicon-heart-empty" );
window.setTimeout(function(){
bootbox.hideAll();
}, 2000);
});
}

function kedvencbeKdv(cikk, gilf='') {
$.get("kedvencbelistaval.php?cikk="+cikk,function(obj) {
$("#kdvlist").html(obj);
});
}

/*címek*/


function ujCim() {

$.get("ujcim.php",function(obj) {
$("#ucn").html(obj);
$("#ucn").show();

$("#ucn2").html('');
$("#ucn2").hide();
});

}

function ujCim2() {
$.get("ujcim2.php",function(obj) {
$("#ucn").html('');
$("#ucn").hide();

$("#ucn2").html(obj);
$("#ucn2").show();
});

}

function ujCimTar() {  
var orszag = document.getElementById("orszag2").value;
var irsz = document.getElementById("irsz2").value;
var telepules = document.getElementById("telepules2").value;
var kozterulet = document.getElementById("kozterulet2").value;
var hsz = document.getElementById("hsz2").value;

if ( irsz != '' && telepules != '' && kozterulet != '' && hsz != '' ) {
$.get("ujcimsave.php?orszag="+orszag+"&irsz="+irsz+"&telepules="+telepules+"&kozterulet="+kozterulet+"&hsz="+hsz,function(obj) {
$("#cimlista").html(obj);
$("#ucn").toggle();
$("#ucn").html("");
mgButtonEnableMeg();
}); 

} else {
//alert("HIBA!");
$.get("hibascim.php",function(obj) {
bootbox.alert(obj);
window.setTimeout(function(){
bootbox.hideAll();
}, 2000);
});
}
}

function ujCimTar2() {  
var orszag = document.getElementById("orszag2").value;
var irsz = document.getElementById("irsz2").value;
var telepules = document.getElementById("telepules2").value;
var kozterulet = document.getElementById("kozterulet2").value;
var hsz = document.getElementById("hsz2").value;

if ( irsz != '' && telepules != '' && kozterulet != '' && hsz != '' ) {
$.get("ujcimsave2.php?orszag="+orszag+"&irsz="+irsz+"&telepules="+telepules+"&kozterulet="+kozterulet+"&hsz="+hsz,function(obj) {
$("#cimlista2").html(obj);
$("#ucn2").toggle();
$("#ucn2").html("");
mgButtonEnableMeg();
}); 

} else {
//alert("HIBA!");
$.get("hibascim.php",function(obj) {
bootbox.alert(obj);
window.setTimeout(function(){
bootbox.hideAll();
}, 2000);
});
}
}

function ujCimTar2_copy(id) {  
$.get("ujcimsave2_copy.php?id="+id,function(obj) {
$("#cimlista2").html(obj);
mgButtonEnableMeg();
});
}

function editDel(cimid) {
$.get("cimdel.php?id=" + cimid,function(obj) {
$("#ucn").html('');
$("#ucn").hide();
$("#ucn2").html('');
$("#ucn2").hide();
$('#cimlista').html(obj);
mgButtonEnableMeg();
});  
}

function editDel2(cimid) {
$.get("cimdel2.php?id=" + cimid,function(obj) {
$("#ucn").html('');
$("#ucn").hide();
$("#ucn2").html('');
$("#ucn2").hide();
$('#cimlista2').html(obj);
mgButtonEnableMeg();
});  
}

function editDel3(cimid) {
$.get("cimdel3.php?id=" + cimid,function(obj) {
$('#cimlista').html(obj);
});  
}

function mgButtonEnable() {
var enabled = false;
if ( $("input[name='szamcim']:checked").val() && $("input[name='szallcim']:checked").val() ) {
enabled = true;
}

if ( enabled == true ) {
var e = document.getElementById("mg");
e.removeAttribute("disabled");
} else {
var e = document.getElementById("mg");
e.setAttribute('disabled','disabled');
}
}

var rszallmod = false;
var rfizmod = false;
var radatvedelem = false;

function mgButtonEnableMeg() {
var enabled = false;
/*
console.log ("Cég:"+document.getElementById("adoszam_2").checked);
console.log ("Adószám:"+$("#inputAdoszam").val());
console.log ("Magán:"+document.getElementById("adoszam_1").checked) ;
*/

if( $("input[name='szamcim']:checked").val() != undefined && ($("input[name='szallcim']:checked").val() != undefined || $("input[name='automataid']").val() != '' ) && $("input[name='szallmod']:checked").val() && $("input[name='fizmod']:checked").val() && document.getElementById("celf").checked == true && ( (document.getElementById("adoszam_2").checked == true && $("#inputAdoszam").val() != '') || document.getElementById("adoszam_1").checked == true ) && $("#inputName").val() != '' && $("#inputEmail").val() != '' && $("#tel").val() != '' && $("#inputCegnev").val() != '' ) {
enabled = true;
}

if ( enabled == true ) {
var e = document.getElementById("mg");
e.removeAttribute("disabled");
} else {
var e = document.getElementById("mg");
e.setAttribute('disabled','disabled');
}
}

function addFizModAr(ar) {
var szallmod = $("input[type='radio'][name='szallmod']:checked").val();
var fizmod = $("input[type='radio'][name='fizmod']:checked").val();
$.get("fizmodar.php?ar="+ar+"&szallmod="+szallmod+"&fizmod="+fizmod,function(obj) {
$("#vegossz").html(obj);
rfizmod = true;
mgButtonEnableMeg();
kuponCheck();
});
}

function addSzallModAr(ar) {
var szallmod = $("input[type='radio'][name='szallmod']:checked").val();
var fizmod = $("input[type='radio'][name='fizmod']:checked").val();
$.get("szallmodar.php?ar="+ar+"&szallmod="+szallmod+"&fizmod="+fizmod,function(obj) {
$("#vegossz").html(obj);
rszallmod = true;
mgButtonEnableMeg();
kuponCheck();
});
}

function termekErtekeles(termek, pont) {
$.get("termekertekeles.php?termek="+termek+"&pont="+pont,function(obj) {
$("#ertekeles").html(obj);
});
}

function killHhirlevPopup(id) {
$.get("killpopup.php?id="+id,function(obj) {
$("[data-dismiss=modal]").trigger({ type: "click" });
});
}

//kuponkód ellenőrzés
function kuponCheck() {
if ( $("#kuponkod").val().length > 3 ) {
var szallmod = $("input[type='radio'][name='szallmod']:checked").val();
var fizmod = $("input[type='radio'][name='fizmod']:checked").val();
$.get("getkupon.php?kod="+$("#kuponkod").val()+"&fizmod="+fizmod+"&szallmod="+szallmod,function(obj) {
let objarr = obj.split("|");
$("#kuponres").html(objarr[0]);
$("#vegossz").html(objarr[1]);
if (objarr[2] == '1' ) {
  $(".szallarc").hide();
} else {
  $(".szallarc").show();
}
}); 
}
}

$(document).on('keyup', '.kuponkod', function (e) {
kuponCheck();
});

$(document).on('cnage', '.kuponkod', function (e) {
kuponCheck();
});

$(document).on('blur', '.kuponkod', function (e) {
kuponCheck();
});

//értékelés csillag mouseover és out
$(document).on('mouseover', '.ertekel', function (e) {
var elementid = $(this).attr('id').replace("star", "");
console.log('Fölötte: '+elementid);
for (var i = elementid; i >= 0; i--) {
$('#star'+i).addClass('fa-star');
$('#star'+i).removeClass('fa-star-o');
}
});

$(document).on('mouseout', '.ertekel', function (e) {
$('.ertekel').addClass('fa-star-o');
$('.ertekel').removeClass('fa-star');
});


//opció select kosár gomb engedélyezés
$(document).on('change', '.chkopcio', function (e) {
  var termekid = $("input[name=cikkszam]").val();
  var opcionum = 0;
  var addedopc = 0;
  var opcAr = parseInt($("input[name=ar]").val());
  $('.chkopcio').each(function () {
    if ( $(this).val() != '' ) {
      addedopc++;
      $.get("gettermekopcioar.php?to="+$(this).val()+"&term="+termekid,function(obj) {
        //var er = parseInt(obj);
        opcAr = opcAr+parseInt(obj);
        //console.log(opcAr);
        if ( opcAr > 1 ) {
          $("#nagybrutto").html(opcAr.toLocaleString("hu-HU")+" Ft");
        }
      });
    }
    //console.log($(this).attr('id'));
    opcionum = opcionum+1;
  });
  
  let menydis = $("#menny").is(':disabled');
  $("#menny").val('1');
  //console.log( menydis+' == false && '+addedopc+' == '+opcionum );
  if ( menydis == false && addedopc == opcionum ) {
    //console.log( 'enable' );
    $('.ksrgomb').prop("disabled", false);
    $('.mnygomb').prop("disabled", false);
  } else {
    //console.log( 'disable' );
    $('.ksrgomb').prop("disabled", true);
    $('.mnygomb').prop("disabled", true);
  }
});

function formreset() {
$("input[type=text]").attr('value', '');
$("input[type=text]").removeAttr('value');
$("input[type=text]").val('');
$("input[type=radio]").removeAttr('checked');
$("input[type=checkbox]").removeAttr('checked');
$("option:selected").removeAttr('selected');
}

function setListaRend() {
var rend = $("#arrange").val();
document.cookie = "arrange="+rend+"; path=/";
location.reload();
}

function setListaLimit() {
var rend = $("#lim").val();
document.cookie = "lim="+rend+"; path=/";
location.reload();
}


$(document).on('click', '.kfocus', function (e) {
setTimeout(function() {
$('#main_search').focus();
//alert ("fok");
}, 500);
});

$('.szamla-blokk').on("click", function () {
if ($('#adoszam_1').is(':checked')) {
$('#adoszam').addClass('d-n');
}
if ($('#adoszam_2').is(':checked')) {
$('#adoszam').removeClass('d-n');
}
mgButtonEnableMeg();
});


function variacioValaszt(bid, bparam) {
  $(".btn-varia").removeClass("btn-selected");
  $("#"+bid).addClass("btn-selected");
  $("#variacio").val(bparam);
  $("#menny").val('1');
  $('.ksrgomb').prop("disabled", false);
  $('.mnygomb').prop("disabled", false);

  var opcAr = parseInt($("input[name=ar]").val());
  var termekid = $("input[name=cikkszam]").val();
  $.get("gettermekopcioar.php?to="+bparam+"&term="+termekid,function(obj) {
    //var er = parseInt(obj);
    opcAr = opcAr+parseInt(obj);
    //console.log(opcAr);
    if ( opcAr > 1 ) {
      $("#nagybrutto").html(opcAr.toLocaleString("hu-HU")+" Ft");
    }
  });
}