function openPrintWindow(lang, order_id){
    window.open('/pages/print_order.php?lang=' + lang + '&order_id=' + order_id, 'print', 'scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
}

function openNewWindow(url){
    window.open(url);
}

function disableElement(element){
    document.getElementById(element).disabled = true;
    document.getElementById(element).value = '';
}

function enableElement(element){
    document.getElementById(element).disabled = false;
}

function hideJavaScriptAlert(){
    document.getElementById('javascript_alert').style.display = 'none';
}

function makeSaferPasswords(ids){
    document.getElementById('js_login').value = 'active';
    for (var i = 0; i < ids.length; i++) {
        if (document.getElementById(ids[i]).value != '') {
            document.getElementById(ids[i]).value = md5(document.getElementById(ids[i]).value);
        }
    }
}

function makeLink(prefix){
    sdiak = 'áäčďéěíĺľňóôőöŕšťúůűüýřžÁÄČĎÉĚÍĹĽŇÓÔŐÖŔŠŤÚŮŰÜÝŘŽ +,/#&@"\'';
    bdiak = 'aacdeeillnoooorstuuuuyrzAACDEEILLNOOOORSTUUUUYRZ---------';
    
    tx = '';
    txt = document.getElementById(prefix + '_name').value;
    
    for (p = 0; p < txt.length; p++) {
        if (sdiak.indexOf(txt.charAt(p)) != -1) 
            tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p)));
        else 
            tx += txt.charAt(p);
    }
    document.getElementById(prefix + '_link').value = tx.toLowerCase();
}

function sending(message){
    if (send) {
        document.getElementById('send_order').disabled = true;
        document.getElementById('sending').innerHTML = message;
    }
}

function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            oldonload();
            func();
        }
    }
}

function setDefaultText(elem, text){
    if (elem.value == '') {
        elem.value = text;
    }
}

function removeDefaultText(elem, text){
    if (elem.value == text) {
        elem.value = '';
    }
}

function switchCheckedAll(name, checked){
    var elements = document.getElementsByName(name);
    for (var i = 0; i < elements.length; i++) {
        elements[i].checked = checked;
    }
}

function countPrice(price, priceWithVat){
    var vat = priceWithVat - price;
    var oldPrice = parseFloat(document.getElementById('price').innerHTML.replace(/,/g, '.').replace(/ /g, ''));
    var oldPriceWithVat = parseFloat(document.getElementById('priceWithVat').innerHTML.replace(/,/g, '.').replace(/ /g, ''));
    
    var newPrice = oldPrice + price;
    var newPriceWithVat = oldPriceWithVat + priceWithVat;
    
    document.getElementById('priceNew').innerHTML = formatPrice(newPrice);
    document.getElementById('priceWithVatNew').innerHTML = formatPrice(newPriceWithVat);
}

function formatPrice(price){
    formattedPrice = price.toFixed(2);
    formattedPrice += '';
    x = formattedPrice.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ' ' + '$2');
    }
    formattedPrice = x1 + x2;
    return formattedPrice.replace(/\./g, ',');
}

function filterSuppliers(el, supplier){
    var hashes = window.location.search.slice(window.location.search.indexOf('?') + 1).split('&');
    var query = '';
    
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        if (hash[0]) {
            if (hash[0] == 'filter[]' && supplier == hash[1] && el.checked) 
                continue;
            query += '&' + hash[0]
        }
        if (hash[1]) {
            query += '=' + hash[1];
        }
    }
    
    if (!el.checked) {
        query += '&filter[]=' + supplier;
    }
    
    window.location.href = '?' + query;
}

function reverseChecked(el) {
    el.checked = !el.checked;
}

function openCalendar(params, form, field, type) {
    window.open("/calendar.php?" + params, "calendar", "width=400,height=200,status=yes");
    dateField = document.getElementById(field);
    dateType = type;
}