﻿function prepoctiEshop(obj) {

    var trs = document.getElementById('eshopFormTable').getElementsByTagName('tr');
    var celkem = 0;
    for (var i = 0; i < trs.length; i++) {
        var sely = trs[i].getElementsByTagName('select');
        var inpy = trs[i].getElementsByTagName('input');
        var pocet = 0;
        if (sely.length > 0) {
            pocet = 1 * sely[0].options[sely[0].selectedIndex].value;
        }
        if (inpy.length > 0 && inpy[0].type == 'text') {
            pocet = 1 * inpy[0].value;
        }
        if (pocet > 0 && inpy.length > 0) {
            for (var j = 0; j < inpy.length; j++) {
                if (inpy[j].type != "checkbox" || !inpy[j].checked) {
                    continue;
                }
                celkem += pocet * inpy[j].title.substr(0, inpy[j].title.length - 2);
            }
        }
    }
    if (document.getElementById('postovne')) {
        if (document.getElementById('odberPostou').checked) {
            celkem += 1 * document.getElementById('postovne').value;
            document.getElementById('zpusobPlatby').className = 'posta';
        } else {
            document.getElementById('zpusobPlatby').className = 'osobni';
        }
    }
    if (document.getElementById('dobirka')) {
        if (document.getElementById('platbaDobirkou').checked) {
            celkem += 1 * document.getElementById('dobirka').value;
        }
    }
    document.getElementById('celkemEshop').value = celkem + " Kč";

}

function toggleFakturacni(zobraz) {
    if (zobraz) {
        document.getElementById('fakturacniadresa').style.display = '';
    } else {
        document.getElementById('fakturacniadresa').style.display = 'none';
    }
}

function setZpusobyPlatby(zpusob) {
    if (zpusob == 'osobni') {
        document.getElementById('zpusobPlatby').className = 'osobni';
        document.getElementById('adresaDodani').style.display = 'none';
        document.getElementById('platbaPrevodem').checked = true;
    } else if (zpusob == 'posta') {
        document.getElementById('adresaDodani').style.display = '';
        document.getElementById('zpusobPlatby').className = 'posta';
        document.getElementById('platbaPrevodem').checked = true;
    }
}

function checkObjednavka(f) {

    // je něco objednáno?
    var jsouProdukty = false;
    var trs = document.getElementById('eshopFormTable').getElementsByTagName('tr');
    var inpy;
    if (trs.length > 0) {
        for (j = 0; j < trs.length; j++) {
            inpy = trs[j].getElementsByTagName('input');
            if (inpy && inpy.length > 1 && !isNaN(inpy[0].value) && 1 * inpy[0].value > 0) {
                for (i = 1; i < inpy.length; i++) {
                    if (inpy[i].type.toLowerCase() == 'checkbox' && inpy[i].checked) {
                        jsouProdukty = true;
                        break;
                    }
                }
                if (jsouProdukty) break;
            }
        }
    }
    if (!jsouProdukty) {
        alert('Vyberte prosím nějaké produkty - vyplňte počet kusů a zaškrtněte požadovanou variantu produktu.');
        return false;
    }
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(f['email'].value)) {
        alert('Vyplňte prosím platný email!');
        f['email'].focus();
        return false;
    }
    if (document.getElementById('odberPostou').checked && f['adresa'].value.length < 20) {
        alert('Vyplňte prosím adresu dodání');
        f['email'].focus();
        return false;
    }
    return true;
}

function checkEmailForm(f) {
    var inpy = f.getElementsByTagName('input');
    if (inpy.length) {
        for (var i = 0; i < inpy.length; i++) {
            if (inpy[i].type == 'text') {
                if (inpy[i].className == 'kontrola2') {
                    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
                    if (!filter.test(inpy[i].value)) {
                        alert('Vyplňte prosím platnou emailovou adresu.');
                        inpy[i].focus();
                        return false;
                    }
                } else if (inpy[i].className == 'kontrola3') {
                    if (inpy[i].value == '' || isNaN(inpy[i].value)) {
                        alert('Vyplňte prosím povinnou kolonku "' + inpy[i].parentNode.parentNode.getElementsByTagName('td')[0].innerHTML + '" číslem.');
                        inpy[i].focus();
                        return false;
                    }
                } else if (inpy[i].className == 'kontrola4') {
                    if (inpy[i].value == '') {
                        alert('Vyplňte prosím povinnou kolonku "' + inpy[i].parentNode.parentNode.getElementsByTagName('td')[0].innerHTML + '".');
                        inpy[i].focus();
                        return false;
                    }
                }
            }
        }
    }
}

function initStrankaForm() {
    //document.getElementById('strankaform')["sablona"].onchange = function () { nastavStrankaFormClass(); };
}

function nastavStrankaFormClass() {
    //document.getElementById('strankaform').className = "strankaform" + document.getElementById('strankaform')["sablona"].options[document.getElementById('strankaform')["sablona"].selectedIndex].value;
}
