function gEBI(id) {
    if (Boolean(document.getElementById)) {
        return document.getElementById(id);
    } else if (Boolean(document.all)) {
        return eval('document.all.' + id);
    }
}

function expup(artno, wid) {
    window.open('product_expectation.jsp?artno=' + artno + (typeof(wid) != 'undefined' ? '&w=' + wid : ''), artno, 'width=545,height=220,pixelstoolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0');
}

function picup(unid) {
    window.open('./product_image.jsp?fr=1&doc=' + unid, 'picture', 'scrollbars=yes,resizable=yes,toolbar=no,status=no');
}

function addFavouriteToBasket(siCode, paramList){
    var qtyField = gEBI('qty_' + siCode);
    var list = '';  // UNID seznamu pro presmerovani zpet
    if (paramList != null && paramList != '') {
        list = '?list='+paramList
    }
    if (qtyField){
        window.location = 'basket_add.jsp?prd=' + siCode + '&qty=' + qtyField.value +'&from=favourite.jsp' + list;
    }
}

function popup(url,x,y) {
	var winopened = window.open(url,'popup','toolbar=no,status=no,width='+x+',height='+y + ',resizable=yes,scrollbars=yes');
    winopened.focus();
}

var  picOrder = 1;
var  picSelected = 1;
var  picCount = 0;
var  picShow = 0;
var picUrlPrefix = '';

function cursorPic(pic, selected) {
    if (selected == picSelected) {
        pic.style.cursor='default';
    } else {
        pic.style.cursor='pointer';
    }
}

function selectPic(pic, selected){
    picSelected = selected;
    img = gEBI('img_selected');
    img.src = picUrlPrefix + '_ss02_o' + picSelected;
    link = gEBI('img_link');
    link.href = picUrlPrefix + '_ss03_o' + picSelected;
    pic.style.cursor='default';
}

function showNextPic(){
    // neco delame, jen pokud jsou dalsi obrazky pro posunuti v pasu
    if (picOrder + picShow - 1 < picCount){
        // skryjeme prvni obrazek
        img = gEBI('img_thumb_' + picOrder);
        img.style.display = 'none';

        // zobrazime dalsi obrazek v poradi
        img = gEBI('img_thumb_' + (picOrder + picShow));
        img.style.display = 'inline';
        picOrder++;
    }
}

function showPrevPic(){
    if (picOrder > 1){
        picOrder--;

        // skryjeme posledni obrazek
        img = gEBI('img_thumb_' + (picOrder + picShow));
        img.style.display = 'none';

        // zobrazime predchozi obrazek
        img = gEBI('img_thumb_' + picOrder);
        img.style.display = 'inline';
    }
}

function popupPic(){
    popup(picUrlPrefix + '_ss03_o' + picSelected, 800, 500);
}

function initPic(show, count, urlPrefix){
    picCount = count;
    picShow = show;
    picOrder = 1;
    picUrlPrefix = urlPrefix;
    for (i = 1; i <= picCount; i++){
        img = gEBI('img_thumb_' + i);
        if (i <= picShow){
            img.style.display = 'inline';
        } else {
            img.style.display = 'none';
        }
    }

    link = gEBI('img_link');
//    link.href = 'javascript:popupPic();';
}


var formSent = false;
function sendForm() {
    if (formSent) {
        return false;
    } else {
        formSent = true;
        return true;
    }
}

var helpCount = 0;
function initHelps(count) {
    initHelps(count, 'inline');
}
function initHelps(count, displayStyle) {
    helpCount = count;
    hideHelps(displayStyle);
    helpOpened = -1; // ID otevrene napovedy
}

function showHelp(help, displayStyle) {
    if (!displayStyle) displayStyle = 'inline';
    hideHelps('close');

    helpBox = gEBI('help_' + help);
    if (helpBox) {
        if (helpOpened != help) {
            setTimeout(function foo(){
                helpBox = gEBI('help_' + help);
                helpBox.style.display = displayStyle;
                helpOpened = help;
            }, 100);
        } else {
            helpOpened = -1;
        }
    }
}

function hideHelp(help, displayStyle) {
    if (!displayStyle) displayStyle = 'inline';
    helpBox = gEBI('help_' + help);

    if (helpBox) {
        if (helpBox.style.display == displayStyle) {
            helpBox.style.display = 'none';
        }
    }
}

function hideHelps(displayStyle) {
    if (displayStyle == 'close') {
        displayStyle = 'inline';
    } else {
        helpOpened = -1;  // bylo volano ze stranky, zrusime zapamatovanou otevrenou napovedu
    }
    for (i = 0; i < helpCount; i++) {
        hideHelp(i, displayStyle);
    }

    hideDwnldHelpBox();
}

var openedBubbleHelp = -1;
function showBubbleHelp(help) {
    hideBubbleHelps(openedBubbleHelp);
    if (openedBubbleHelp != help) {
        if (document.getElementById('help_' + help)) {
            setTimeout(function foo(){
                document.getElementById('help_' + help).style.visibility='visible';
                openedBubbleHelp = help;
            }, 20);
        }
    } else {
        openedBubbleHelp = -1;
    }
}

function hideBubbleHelps(help) {
    for (i = 0; i < helpCount; i++) {
        if (document.getElementById('help_' + i)) {
            document.getElementById('help_' + i).style.visibility='hidden';
        }
    }
    openedBubbleHelp = help; // pri zavolani s -1 se otevrena napoveda zapomene 
}

function initDwnldHelpBox() {
    helpBox=gEBI('dwnld-help-box');

    if (helpBox) {
        helpBox.style.display = 'none';
    }
}

function hideDwnldHelpBox() {
    helpBox=gEBI('dwnld-help-box');

    if (helpBox) {
        if (helpBox.style.display == 'block') {
            helpBox.style.display = 'none';
        }
    }
}

function showDwnldHelpBox() {
    hideHelps();

    helpBox=gEBI('dwnld-help-box');

    if (helpBox) {
        setTimeout(function foo(){
            helpBox=gEBI('dwnld-help-box');
            helpBox.style.display = 'block';
        }, 100);
    }
}



function checkTradeConds(errMsg){
    docInput = gEBI("ord-doc");
    checkbox1 = gEBI("ord-agree-1");
    checkbox2 = gEBI("ord-agree-2");

    if (checkbox1 && checkbox2  && docInput){
        if (checkbox1.checked || checkbox2.checked){
            window.location='order_aproove.jsp?doc=' + docInput.value;
        } else {
            alert(errMsg); return false;
        }
    }
}

function agreementChanged(nr){
    checkbox = gEBI("ord-agree-" + (3-nr));
    checkbox.checked = gEBI("ord-agree-" + nr).checked;
}

function switchPaymentType(){
    var ptid = $('#PT_ID').val();
    var ttid = $('#TT_ID').val();

    intelekCTX.getPaymentSelect(ttid, ptid, {callback: renderPaymentSelect});
}

function renderPaymentSelect(data){
    if ($('#payment-select-container') && data){
        $('#payment-select-container').html(data);
    }
}

