$(function () {
    var definedDate = new Date(Date.now() + (6 * 60 * 60 * 1000));
    var countdownExpire = String((definedDate.toLocaleDateString().split('.').reverse().join('/') + ' ' + definedDate.toLocaleTimeString()).replace('-', '/'));
    var cookieCountdownExpire = $.cookie(location.hostname);
    if (cookieCountdownExpire) {
        countdownExpire = cookieCountdownExpire;
    } else {
        $.cookie(location.hostname, countdownExpire);
    }
    $(".clock_3").countdown(countdownExpire, function (event) {
        var $this = $(this).html(event.strftime('' + '<span class="box hour">%H</span>' + '<span class="box minute">%M</span>' + '<span class="box second">%S</span>'));
    });
    $(".product-counter_3").productCounter({
        min: 357, max: 785, minTimeout: 2500, expire: 2700000
    });
});

function formGonder() {
    var errors = [];
    var data = $('form').serializeArray();

    var requiredFields = [
        'sp_musteri',
        'sp_telefon',
        'sp_sehir',
        'sp_ilce',
        'sp_adres',
        'sp_odeme_yontemi',
        'paket'
    ];

    // varyasyonları da kontrol et
    $('select[name^="varyasyon"]').each(function () {
        requiredFields.push($(this).attr('name'));
    });

    // inputları kontrol et
    requiredFields.forEach(function (fieldName) {
        var field = data.find(x => x.name === fieldName);
        if (!field || field.value.trim().replace(/_|-|\(|\)/gi, "").length === 0) {
            errors.push(fieldName);
            $('[name="' + fieldName + '"]').nextAll(".validate_error").css("display", "block");
        }

        // özel kontrol: telefon
        if (fieldName === 'sp_telefon' && field && field.value.replace(/\D/g, '').length < 10) {
            errors.push(fieldName);
            $('[name="' + fieldName + '"]').nextAll(".validate_error").css("display", "block");
        }
    });

    console.log("Validasyon tamamlandı. Hatalı alanlar:", errors);
    return errors.length === 0;
}


$('input').keyup(function () {
    if ($(this).val().length > 5) $(this).nextAll(".validate_error").css("display", "none");
    if ($(this).attr("name") == "sp_telefon") {
        if (/\D/g.test(this.value)) {
            this.value = this.value.replace(/\D/g, '');
        }
        if (this.value.slice(0, 1) == 9 || this.value.slice(0, 1) == 0) {
            this.value = this.value = '';
        }
    }
});

$('select').change(function () {
    if ($(this).val().length) $(this).nextAll(".validate_error").css("display", "none");
});

$('textarea').keyup(function () {
    if ($(this).val().length > 5) $(this).nextAll(".validate_error").css("display", "none");
});

$('select[name="sp_sehir"]').change(function () {
    $('select[name="sp_ilce"]').prop('disabled', true);
    var ilceler = $('select[name="sp_sehir"] option:selected').data('ilceler');
    $('select[name="sp_ilce"]').html('');
    $('select[name="sp_ilce"]').append('<option value="">İlçe Seçin</option>');
    $(ilceler).each(function (e, v) {
        $('select[name="sp_ilce"]').append('<option value="' + v.ilce_id + '">' + v.ilce_adi + '</option>');
    });
    $('select[name="sp_ilce"]').prop('disabled', false);
});

$('input[name="paket"]').click(function () {
    $('html,body').animate({
        scrollTop: $('#varyasyonlar').offset().top
    }, 'slow');
});

$(document).ready(function () {
    $('input[name="sp_telefon"]').inputmask({"mask": "(999) 999-99-99"});
    $('input[name="sp_telefon"]').keyup(function () {
        var telefon = $(this).val();
        telefon = telefon.replace(/_|-|\(|\)/gi, "");
        telefon = telefon.replace(/\s/g, "");
        var deger = telefon[0];
        if (deger != 5) {
            $('input[name="sp_telefon"]').val('');
        }
    });
});