function vardump(){    
    if (Element.getStyle("vardump", "display") == "block")
        Element.hide("vardump");
    else
        Element.show("vardump");
}

function dbdump(){
    if (Element.getStyle("dbdump", "display") == "block")
        Element.hide("dbdump");
    else
        Element.show("dbdump");
}

function skasujkomunikat(id){
    resultTo = 'strefa_komunikatu';
    Element.update(resultTo,'');

    new Ajax.Updater(resultTo, "ajax,skasujkomunikat.html",
                    {
                        parameters : "password=BaltA_2010&id="+id,
                        onFailure : function(resp) {
                            Element.update(resultTo, 'Błąd pobierania danych');
                        }
                    }
    );
}

function changeQuantity(value){

    try{

        if (isNaN(value)){
            v = 1;

        }else{
         v = value;
        }

        Form.Element.setValue("related_quantity",v);
    } catch (e){ }

    relatedProductSelect();
}


function cartaddress(value){
    $$('.hid').each(
        function(e){
            e.hide();
        }
    );

    $$('.tr_'+value).each(
        function(e){
            e.show();
        }
    );


//    alert(value);
}

function asortyment(){
    //alert();

    display = Element.getStyle('asortyment','display');

    if (display == 'none'){
        Effect.BlindDown('asortyment');
        try{ Element.hide('attributes'); } catch(e){ }
        try{ Element.hide('price'); } catch(e){ }
        try{ Element.hide('producers'); } catch(e){ }
        try{ Element.hide('rozwin'); } catch(e){ }
        try{ Element.show('zwin'); } catch(e){ }
        try{ Element.hide('rozwin2'); } catch(e){ }
        try{ Element.show('zwin2'); } catch(e){ }
    }else{
        try{ Element.show('rozwin'); } catch(e){ }
        try{ Element.hide('zwin'); } catch(e){ }
        try{ Element.show('rozwin2'); } catch(e){ }
        try{ Element.hide('zwin2'); } catch(e){ }
        try{ Element.show('attributes'); } catch(e){ }
        try{ Element.show('price');  } catch(e){ }
        try{ Element.show('producers'); } catch(e){ }
        Effect.BlindUp('asortyment');
    }

}

function checkQuery(s){

    s = $F('query');

    s = s.toString();

    s = s.replace(/^\s+|\s+$/, '');
    size = s.length;

    if (s == 'Wpisz szukany tekst'){
        size = 0;
    }
    
    if (size < 3){
        resultTo = "lightboxInner";
        Element.update(resultTo,'<div class="wrapper"><h1>Informacje</h1><p>Musisz podać conajmniej 3 znaki</p></div>');
        test.open();
    }

    return size;
}

function dostepnosccamino(id){
    dostepnosc(id,0,'show_dost_www');
    dostepnosc(id,9,'show_dost_sklep');
    $('dostepnosc').hide();

    Element.update('show_dost_www','Sprawdzam dla zamówień www...');
    Element.update('show_dost_sklep','Sprawdzam dla odbioru osobistego...');

    Element.show('dost_sklep_label');
    Element.show('dost_www_label');
}

function dostepnosc(id,mag,resultTo){
    Element.update(resultTo,'Sprawdzam...');

    new Ajax.Updater(resultTo, "ajax,webapidostepnosc.html",
                    {
                        parameters : "password=BaltA_2010&id="+id+"&mag="+mag,
                        onFailure : function(resp) {
                            Element.update(resultTo, 'Błąd pobierania danych');
                        }
                    }
    );    
}

function konf(){

    sum = 0;

    $$('.selecty p select').each(
        function (e){
            v = e.getValue();

            if (v != '-'){
                v = v.split('-');
                sum += Number(v[1]);
            }
        }
    );
    sum_string = getSumString(sum);
    Element.update('js_konf_wartosc',sum_string+ " zł");

}

function setView(view){
    Form.Element.setValue('view','view'+view);
}

function setGuard(){
    try{
        Form.Element.setValue('guard','1');
        }catch(e){
            alert(e);
            return false;
        }
}

function relatedProductSelect(){
    var sum = 0;

    sum = Number($F("related_price")*$F("related_quantity"));

    $$(".related_checkbox").each(
        function (t){
            id = t.getAttribute("id");

            if (t.getValue() == "on"){
                sum += Number($F(id+"_price"));
            }
        }
    );
    
    sum_string = getSumString(sum);
    $("sum").update(sum_string);
}

function getSumString(sum){
    sum = sum.toFixed(2);
    sum = sum.sub('.',',');

    tablica = sum.toArray();
    i = 1;
    sum_string = "";

    tablica = tablica.reverse();

    tablica.each(
            function (t){
                i++;

                if (i>4){
                        if (i%4 == 0){
                            sum_string = " " + sum_string;
                        }
                }

                sum_string = t + sum_string;

            }
    );
return sum_string;
}

function cartSelect(){
  Form.Element.setValue("selectedproducts","");
  try{Form.Element.setValue("selectedproducts2",""); }catch(e){ }
  try{Form.Element.setValue("selectedproducts3",""); }catch(e){ }
    $$('.selection').each(   
        function (t){
            name = t.getAttribute('name');

            if (t.getValue() == 'on'){
                Form.Element.setValue("selectedproducts",Form.Element.getValue("selectedproducts")+name+" ");
                try{Form.Element.setValue("selectedproducts2",Form.Element.getValue("selectedproducts2")+name+" ");}catch(e){ }
                try{Form.Element.setValue("selectedproducts3",Form.Element.getValue("selectedproducts3")+name+" ");}catch(e){ }
            }
        }
    );
}

function cartSelectAll(){
    $$('.selection').each(
        function (t){
            t.setValue('on');
        }
    );

    cartSelect();
}

function cartUnselect(){
  Form.Element.setValue("selectedproducts","");
  try{Form.Element.setValue("selectedproducts2",""); }catch(e){ }
  try{Form.Element.setValue("selectedproducts3",""); }catch(e){ }
  
    $$('.selection').each(
        function (t){
            t.setValue(null);
        }
    );
}

function displayPhoto(src){

    // usun aktywne ze wszystkich
    var g = $$('div.gallery_min img');

    g.each(
        function (t){
            t.removeClassName('active');
        }
    );

    // ustaw aktywny
    Element.addClassName('tzd_'+src, 'active');

    // pobierz sciezke
    var url = $('tzd_'+src).readAttribute('src');
    url = url.sub('60x60','450x450');

    
    Element.writeAttribute('fproductphoto','src',url)

    //$('fproductphoto').writeAttribute('src',src);
}

function ajax(functionName, resultTo,page){
    
    
    Element.update(resultTo,'<p id="loading"><img src="img/site/ajax-loader2.gif" alt=""/>Wczytywanie...</p>');

    new Ajax.Updater(resultTo, "ajax,"+functionName+".html",
                    {
                        parameters : "password=BaltA_2010&page="+page,
                        onFailure : function(resp) {
                            Element.update(resultTo, 'Błąd pobierania danych');
                        }
                    }
    );
}


function documentDetail(trn_gidnumer){
    functionName = "webapidocument";
    resultTo = "ajaxDocuments";

    Element.update(resultTo,'<p id="loading" class="loading_margin"><img src="img/site/ajax-loader2.gif" alt=""/>Wczytywanie...</p>');

    new Ajax.Updater(resultTo, "ajax,"+functionName+".html",
                    {
                        parameters : "password=BaltA_2010&trn_gidnumer="+trn_gidnumer,
                        onFailure : function(resp) {
                            Element.update(resultTo, 'Błąd pobierania danych');
                        }
                    }
    );
}

function orderDetail(functionName, resultTo, zan_gidnumer){
    Element.update(resultTo,'<p id="loading" class="loading_margin"><img src="img/site/ajax-loader2.gif" alt=""/>Wczytywanie...</p>');

    new Ajax.Updater(resultTo, "ajax,"+functionName+".html",
                    {
                        parameters : "password=BaltA_2010&zan_gidnumer="+zan_gidnumer,
                        onFailure : function(resp) {
                            Element.update(resultTo, 'Błąd pobierania danych');
                        }
                    }
    );
}

function productlistinsert(id){
    resultTo = "lightboxInner";
    functionName = "productlistinsert";

    list_name = $F("list_name");

    if (list_name == ""){
        return false;
    }

    list_przypomnij = $F("list_przypomnij");

    list_powtarzaj = $F("list_powtarzaj");

    Element.update(resultTo,'<div class="wrapper"><p class="loading"><img src="img/site/ajax-loader.gif" alt=""/>Wczytywanie...</p></div>');

    new Ajax.Updater(resultTo, "ajax,"+functionName+".html",
                    {
                        parameters : "password=BaltA_2010&lzn_nazwa="+list_name+"&lzn_przypomnienie="+list_przypomnij+"&lzn_powtarzaj="+list_powtarzaj+"&id="+id,
                        onFailure : function(resp) {
                            Element.update(resultTo, 'Błąd pobierania danych');
                        }
                    }
    );

    return false;
}
