/** @file www/js/megaobchod.js

    knihovna zakladnich JavaScriptovych funkci a nastaveni

    @author Josef Zamrzla
*/

var img1 = new Image(1, 80);
img1.src = 'http://img.mego.cz/mego_cz-img/mo-oranzova-zalozka.gif';
var img2 = new Image(1, 80);
img2.src = 'http://img.mego.cz/mego_cz-img/mo-seda-zalozka.gif';
var img3 = new Image(9, 80);
img3.src = 'http://img.mego.cz/mego_cz-img/mo-zalozky-levy-roh.gif';
var img4 = new Image(9, 80);
img4.src = 'http://img.mego.cz/mego_cz-img/mo-zalozky-levy-roh-aktivni.gif';
var img5 = new Image(9, 80);
img5.src = 'http://img.mego.cz/mego_cz-img/mo-zalozky-pravy-roh.gif';
var img6 = new Image(9, 80);
img6.src = 'http://img.mego.cz/mego_cz-img/mo-zalozky-pravy-roh-aktivni.gif';

String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "")
};

// urceni prohlizece
if (document.all) document.getElementById = document.all;
var dhtml = (document.getElementById || document.all) && navigator.userAgent.indexOf('Opera') == -1 ? true : false;
var previousSmallTab = 1;

function keep_chosen(id)
{
    try
    {
        document.getElementById('optional-parameter-' + id).click();
        document.getElementById('optional-parameter-' + id).checked = true;
    }
    catch(e)
    {
        alert('Error (keep_chosen): ' + id);
    }
}

function choose_parameter(group, id)
{
    try
    {
        chosen[group] = id;
        document.getElementById('group-display-link-' + group).style.display = 'block';
        document.getElementById('group-hide-link-' + group).style.display = 'none';
    }
    catch(e)
    {
        alert('Error (choose_parameter): group:' + group + ', id: ' + id);
    }
    
    hide_group(group);
}

function display_chosen()
{
    hide_all();
    for(var i in chosen)
    {
        try
        {
            document.getElementById('optional-parameter-box-'+chosen[i]).style.display = 'block';
        }
        catch(e)
        {
            alert('Error (display_chosen): ' + chosen[i]);
        }
    }
}

function hide_all()
{
    for(var group in all_parameters)
    {
        for(var parameter in all_parameters[group])
        {
            try
            {
                document.getElementById('optional-parameter-box-'+all_parameters[group][parameter]).style.display = 'none';
            }
            catch(e)
            {
                alert('Error (hide_all): ' + all_parameters[group][parameter]);
            }
        }

        try
        {
            document.getElementById('group-display-link-' + group).style.display = 'block';
            document.getElementById('group-hide-link-' + group).style.display = 'none';
        }
        catch(e)
        {
            alert('Error (hide_all), group links: ' + group);
        }
    }
}

function display_all()
{
    for(var group in all_parameters)
    {
        for(var parameter in all_parameters[group])
        {
            try
            {
                document.getElementById('optional-parameter-box-'+all_parameters[group][parameter]).style.display = 'block';
            }
            catch(e)
            {
                alert('Error (display_all): ' + all_parameters[group][parameter]);
            }
        }

        try
        {
            document.getElementById('group-display-link-' + group).style.display = 'none';
            document.getElementById('group-hide-link-' + group).style.display = 'block';
        }
        catch(e)
        {
            alert('Error (display_all), group links: ' + group);
        }
    }
}

function display_group(group)
{
    for(var parameter in all_parameters[group])
    {
        try
        {
            document.getElementById('optional-parameter-box-'+all_parameters[group][parameter]).style.display = 'block';
        }
        catch(e)
        {
            alert('Error (display_group): ' + all_parameters[group][parameter]);
        }
    }

    try
    {    
        document.getElementById('group-display-link-' + group).style.display = 'none';
        document.getElementById('group-hide-link-' + group).style.display = 'block';
    }
    catch(e)
    {
        alert('Error (display_group), group links: ' + group);
    }
}

function hide_group(group)
{
    for(var parameter in all_parameters[group])
    {
        try
        {
            document.getElementById('optional-parameter-box-'+all_parameters[group][parameter]).style.display = 'none';
        }
        catch(e)
        {
            alert('Error (display_group), hiding group: ' + all_parameters[group][parameter]);
        }
    }

    try
    {
        document.getElementById('optional-parameter-box-' + chosen[group]).style.display = 'block';
    }
    catch(e)
    {
        alert('Error (hide_group), displaying chosen[group]: ' + group);
    }

    try
    {
        document.getElementById('group-display-link-' + group).style.display = 'block';
        document.getElementById('group-hide-link-' + group).style.display = 'none';
    }
    catch(e)
    {
        alert('Error (hide_group), group links: ' + group);
    }
}

function init_default_state()
{
    for(var group in all_parameters)
    {
        for(var parameter in all_parameters[group])
        {
            try
            {
                document.getElementById('optional-parameter-'+all_parameters[group][parameter]).click();
                document.getElementById('optional-parameter-'+all_parameters[group][parameter]).checked = true;
            }
            catch(e)
            {
                alert('Error (init_default_chosen): ' + all_parameters[group][parameter]);
            }

            // zajima mne pouze prvni prvek, ale neznam jeho index
            break;
        }
    }
}

function startMenu() 
{
    if(document.all && document.getElementById) 
    {
        var multimenu = document.getElementById('multimenu');
        for(i=0;i<multimenu.childNodes.length;i++) 
        {
            var ul = multimenu.childNodes[i];
            if(ul.nodeName == 'UL') 
            {
                for(j=0;j<ul.childNodes.length;j++)
                {
                    var node = ul.childNodes[j];
                    if(node.nodeName == 'LI')
                    {
                        node.onmouseover = function() { this.className += ' over'; }
                        node.onmouseout = function() { this.className = this.className.replace(' over', ''); }
                    }
                }
            }
        }
    }
}

if(window.attachEvent) 
{
    window.attachEvent('onload', startMenu);
}
else
{
    window.onload = startMenu;
}

function aktivni_zalozka(id)
{
    try
    {
        document.getElementById('levy-roh-' + id).className = 'levy-roh-aktivni';
        document.getElementById('zalozka-' + id).className = 'domena-zalozka-aktivni';
        document.getElementById('pravy-roh-' + id).className = 'pravy-roh-aktivni';
    }
    catch(e) {}
}

function neaktivni_zalozka(id)
{
    try
    {
        document.getElementById('levy-roh-' + id).className = 'levy-roh';
        document.getElementById('zalozka-' + id).className = 'domena-zalozka';
        document.getElementById('pravy-roh-' + id).className = 'pravy-roh';
    }
    catch(e) {}
}

function aktivni_mala_zalozka(id)
{
    try
    {
        if(previousSmallTab)
        {
            neaktivni_mala_zalozka(previousSmallTab);
        }
        
        document.getElementById('mz-levy-roh-' + id).className = 'dpz-levy-roh-aktivni';
        document.getElementById('mz-' + id).className = 'dpz-zalozka-aktivni';
        document.getElementById('mz-pravy-roh-' + id).className = 'dpz-pravy-roh-aktivni';
        
        previousSmallTab = id;
    }
    catch(e) {}
}

function neaktivni_mala_zalozka(id)
{
    try
    {
        document.getElementById('mz-levy-roh-' + id).className = 'dpz-levy-roh';
        document.getElementById('mz-' + id).className = 'dpz-zalozka';
        document.getElementById('mz-pravy-roh-' + id).className = 'dpz-pravy-roh';
    }
    catch(e) {}
}

function detailTabPopis(product_id)
{
    detailTabLoading();
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { detailTabObsah(http_request); };
    http_request.open('GET', '/ajax_scripts/detail_zbozi_popis.php?product=' + product_id, true);
    http_request.send(null);
}

function detailTabGalerie(product_id)
{
    detailTabLoading();
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { detailTabObsah(http_request); };
    http_request.open('GET', '/ajax_scripts/detail_zbozi_ilustrace.php?product=' + product_id, true);
    http_request.send(null);
}

function detailTabPrislusenstvi(product_id)
{
    detailTabLoading();
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { detailTabObsah(http_request); };
    http_request.open('GET', '/ajax_scripts/detail_zbozi_prislusenstvi.php?product=' + product_id, true);
    http_request.send(null);
}

function detailTabDotaz(product_id)
{
    detailTabLoading();
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { detailTabObsah(http_request); };
    http_request.open('GET', '/ajax_scripts/detail_zbozi_dotaz.php?product=' + product_id, true);
    http_request.send(null);
}

function detailTabDostupnost(product_id)
{
    detailTabLoading();
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { detailTabObsah(http_request); };
    http_request.open('GET', '/ajax_scripts/detail_zbozi_dostupnost.php?product=' + product_id, true);
    http_request.send(null);
}

function detailTabSplatky(product_id)
{
    detailTabLoading();
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { detailTabObsah(http_request); };
    http_request.open('GET', '/ajax_scripts/detail_zbozi_splatky.php?product=' + product_id, true);
    http_request.send(null);
}

function detailTabLoading()
{
    document.getElementById('detail-produktu-zalozky-obsah-kotva').style.display = 'none';
    document.getElementById('detail-produktu-zalozky-obsah-kotva').innerHTML = '';
    document.getElementById('detail-produktu-zalozky-loading').style.display = 'block';
}

function detailTabObsah(http_request)
{
    if(http_request.readyState == 4) 
    {
        if(http_request.status == 200) 
        {
            document.getElementById('detail-produktu-zalozky-obsah-kotva').innerHTML = http_request.responseText;
            document.getElementById('detail-produktu-zalozky-loading').style.display = 'none';
            document.getElementById('detail-produktu-zalozky-obsah-kotva').style.display = 'block';
        } 
        else 
        {
            alert('zobrazitDalsiPrislusenstvi failed.');
        }
    }
}

function open_in_popup(url, w, h)
{
    var vlevo = (screen.width-w)/2;
    var zhora = (screen.height-h)/2;
    okno = window.open(url, 'NewWin','resizable=0,top=' + zhora + ',left=' + vlevo + ',menubar=0,width=' + w + ',height=' + h);
    okno.focus()
}

/* AJAX */
function nahratDalsiPrislusenstvi(product_id, limit_from)
{
    document.getElementById('dalsi-prislusenstvi-kotva').style.display = 'none';
    document.getElementById('dalsi-prislusenstvi-kotva').innerHTML = '';
    document.getElementById('dalsi-prislusenstvi-loading').style.display = 'block';
    if(window.XMLHttpRequest) 
    { 
        http_request = new XMLHttpRequest();
    } 
    else if(window.ActiveXObject) 
    { 
        try 
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { zobrazitDalsiPrislusenstvi(http_request); };
    http_request.open('GET', '/ajax_scripts/zbozi_prislusenstvi.php?product=' + product_id + '&from=' + limit_from, true);
    http_request.send(null);
}

function zobrazitDalsiPrislusenstvi(http_request)
{
    if(http_request.readyState == 4) 
    {
        if(http_request.status == 200) 
        {
            document.getElementById('dalsi-prislusenstvi-kotva').innerHTML = http_request.responseText;
            document.getElementById('dalsi-prislusenstvi-loading').style.display = 'none';
            document.getElementById('dalsi-prislusenstvi-kotva').style.display = 'block';
            document.getElementById('dalsi-prislusenstvi-open-link').style.display = 'none';
            document.getElementById('dalsi-prislusenstvi-close-link').style.display = 'block';
        } 
        else 
        {
            alert('zobrazitDalsiPrislusenstvi failed.');
        }
    }
}

function skrytDalsiPrislusenstvi()
{
    document.getElementById('dalsi-prislusenstvi-open-link').style.display = 'block';
    document.getElementById('dalsi-prislusenstvi-close-link').style.display = 'none';
    document.getElementById('dalsi-prislusenstvi-kotva').style.display = 'none';
    location.href='#prislusenstvi';
}

/* KASA pomocnik */
function close_btn_down()
{
    document.getElementById('close-btn').style.borderTop = 'solid #666666 1px';
    document.getElementById('close-btn').style.borderLeft = 'solid #666666 1px';
}

function close_btn_up()
{
    document.getElementById('close-btn').style.borderTop = '0px';
    document.getElementById('close-btn').style.borderLeft = '0px';
}

function close_helper()
{
    document.getElementById('help-banner-shadow').style.display = 'none';
    document.getElementById('help-banner').style.display = 'none';
    document.getElementById('close-btn').style.display = 'none';
    
    var platnost = new Date();
    platnost.setDate(platnost.getDate() + 1);
    document.cookie="kasa_helper_{/literal}{$zbozi->zb_parametry}{literal}=-10; expires=0; path=/; domain=kasa.cz";
}

function check_help_request()
{
    re = new RegExp('^(\\+)?[0-9 ]{9,20}$');
    if(document.getElementById('help-jmeno').value == "")
    {
        alert("Zadejte prosím své jméno");
        document.getElementById('help-jmeno').focus();
        return false;
    }
    else if(!re.test(document.getElementById('help-telefon').value))
    {
        alert("Telefonní číslo není platné");
        document.getElementById('help-telefon').focus();
        return false;
    }
    else
        return true;
}
/* END of KASA pomocnik */

function KontrolaTelefonu(e)
{
    try
    {   
        if(document.all) // verze pro M$ osla 
        {  
            if( event.keyCode == 43 || event.keyCode == 8 || event.keyCode == 0 || (event.keyCode >= 48 && event.keyCode <= 57) )
                return true;
            else
                return false;
        }
        else 
        {  
            if(e.which)
            {
                if( e.which == 43 || e.which == 8 || e.which == 0 || (e.which >= 48 && e.which <= 57) )
                    return true;
                else
                    return false;
            }
            else // nekompatibilni prohlizec
                return true;
        }
    }
    catch(e) {}
}

function KontrolaDIC(e)
{
    try
    {   
        if(document.all) // verze pro M$ osla 
        {  
            if( event.keyCode == 67 || event.keyCode == 99 || event.keyCode == 90 || event.keyCode == 122 || event.keyCode == 43 || event.keyCode == 8 || event.keyCode == 0 || (event.keyCode >= 48 && event.keyCode <= 57) )
                return true;
            else
                return false;
        }
        else 
        {  
            if(e.which)
            {
                if( e.which == 67 || e.which == 99 || e.which == 90 || e.which == 122 || e.which == 43 || e.which == 8 || e.which == 0 || (e.which >= 48 && e.which <= 57) )
                    return true;
                else
                    return false;
            }
            else // nekompatibilni prohlizec
                return true;
        }
    }
    catch(e) {}
}

function kontrolaFormulareDotaz()
{
    re = new RegExp('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
    if(!re.test(document.getElementById('dotaz-email').value))
    {
        alert("Zadaná adresa není platnou adresou elektronické pošty");
        document.getElementById('dotaz-email').focus();
        return false;
    }
    else
        return true;
}

/** kontrola prvku s pozadovanou mail. adresou */
function mailCheck(prvek)
{
    if (window.RegExp)
    {
        reEM = new RegExp('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
        if (!reEM.test(prvek.value))
        {
            alert("Zadaná adresa není platnou adresou elektronické pošty");
            prvek.focus();
            return false;
        }
    }
    return true;
}

/** kontrola prvku s pozadovanym telefonnim cislem */
function telefonCheck(prvek)
{
    if (window.RegExp)
    {
        reEM = new RegExp('^(\\+)?[0-9 ]{9,20}$');
        if (!reEM.test(prvek.value)) {
        alert("Zadané číslo není platné.");
            prvek.focus();
            return false;
        }
    }
    return true;
}

/** obecna kontrola formulare */
function formCheck(prvky)
{
    for (var i=0; i < prvky.length; i++) {
        if (prvky[i][0].value == '') {
            alert(prvky[i][1]);
            prvky[i][0].focus();
            return false;
        }
    }

    return true;
}

/** pozmeni styl v menu u dane kategorie */
function menuAktivni(k_index)
{
    if (k_index) {
        var element = document.getElementById('menu_a_'+k_index);
        if((element != 0) || (element != false))
        { 
            try
            {
                element.className = 'menu_aktivni';
            }
            catch(e) {}
        }
    }
}

/** doplnuje 0 do desetinneho cisla, aby bylo ve tvaru XX.X0 nebo XX.00 nebo XX.XX  */
function cenaToString(cena)
{
    cena = cena*100;
    cena = Math.round(cena);
    cena = cena/100;

    var zaokrouhlena   = Math.floor(cena);
    var zaokrouhlena10 = Math.floor(cena*10);
    var cena10		   = cena*10;

    if (zaokrouhlena == cena)
        cenaStr = cena.toString() + '.00';
  	else {
		if (zaokrouhlena10 == cena10)
            cenaStr = cena.toString()+'0';
		else
            cenaStr = cena;
	}

    return cenaStr;
}

function toggleDisplay(elementId,display)
{
    var element = document.getElementById(elementId);
    try
    {
        if(display)
            element.style.display='block';
        else
            element.style.display='none';
    }
    catch(e) {}
}

/** otevre okenko pro zaslani hesla */
function posliHeslo(elementId)
{
    var email = document.getElementById(elementId);

    if (email.value == '') {
        alert("Nejprve vyplntě svůj login (e-mailovou adresu).");
        email.focus();
    }
    else {
        var wSirka = 300;
        var wVyska = 80;
        var vyska = screen.height;
        var sirka = screen.width;
        var levy = (sirka - wSirka)/2;
        var horni = (vyska - wVyska-25)/2;

        window.open('heslo.php?'+email.value,'',"width="+wSirka+", height="+wVyska+", left="+levy+", top="+horni);
    }
}

function dostupnost(zbozi)
{
    var wSirka = 500;
	var wVyska = 550;
    var vyska = screen.height;
    var sirka = screen.width;
    var levy = (sirka - wSirka)/2;
    var horni = (vyska - wVyska-50)/2;
    window.open('/dostupnost/?id='+zbozi, '', "scrollbars= yes, width="+wSirka+", height="+wVyska+", left="+levy+", top="+horni);
}

function check_comp_form()
{
    re = new RegExp('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
    if(document.getElementById('soutez-jmeno').value == '')
    {
        document.getElementById('soutez-jmeno').focus();
        alert("Vyplňte prosím jméno!");
        return false;
    }
    else if(document.getElementById('soutez-prijmeni').value == '')
    {
        document.getElementById('soutez-prijmeni').focus();
        alert("Vyplňte prosím příjmení!");
        return false;
    }
    else if(!re.test(document.getElementById('soutez-email').value))
    {
        document.getElementById('soutez-email').focus();
        alert("Zadaná e-mailová adresa není platná!");
        return false;
    }
    else
        return true;
}