function scrollTo(selector) {
   var targetOffset = $(selector).offset().top;
   $('html,body').animate({scrollTop: targetOffset}, 500);
}

$(document).ready(function(){    
    // sekcja "składający zamówienie"
    
    if ($('input:radio[name=query]:checked').val() == 'person') {
        $('#fieldset-person_group').show(20);
        $('#fieldset-company_group').hide();
        //alert('person');
    } else {
        $('#fieldset-person_group').hide();
        $('#fieldset-company_group').show(20);
        //alert('company');
    }
    
    $('input[name=query]').click(function(){
        // czyszczenie sekcji "składający zamówienie"
        $('#fieldset-person_group input').val('');
        $('#fieldset-person_group textarea').val('');
        $('#fieldset-company_group input').val('');
        $('#fieldset-company_group textarea').val('');
        // ukrycie i wyczyszczenie pól sekcji "dane do faktury"
        $('#fieldset-facture_group').hide();
        $('#fieldset-facture_group input').val('');
        $('#fieldset-facture_group textarea').val('');
        // przełączenie radio button na "no"
        $('#query2-no').attr('checked', 'checked');

        if ($('input:radio[name=query]:checked').val() == 'person') {
            $('#fieldset-person_group').show(2000);
            $('#fieldset-company_group').hide();
            //alert('person1');
        } else {
            $('#fieldset-person_group').hide();
            $('#fieldset-company_group').show(2000);
            //alert('company1');
        }
/*
        if ($(this).val() == 'person') {
            $('#query-company').attr('checked', 'checked')
        } else {
            $('#query-company').attr('checked', 'checked')
        }
*/
        //.not(':submit, :checkbox, :radio')                  
    });
    //sekcja dane do faktury
    if ($('input:radio[name=query2]:checked').val() == 'yes') {
        $('#fieldset-facture_group').show(20);
    } else {
        $('#fieldset-facture_group').hide();
    }
    
    $('input[name=query2]').click(function(){        
        if ($('input:radio[name=query2]:checked').val() == 'yes') {
            $('#fieldset-facture_group').show(2000);

            if ($('input:radio[name=query]:checked').val() == 'person') {
                $('#f_name').val($('#p_name').val());
                $('#f_address').val($('#p_address').val());
            } else {
                $('#f_name').val($('#c_name').val());
                $('#f_address').val($('#c_address').val());
                $('#f_nip').val($('#c_nip').val());
            }
        } else {
            $('#fieldset-facture_group').hide();
            // wyczyść pola sekcji
            $('#fieldset-facture_group input').val('');
            $('#fieldset-facture_group textarea').val('');
        }
    });
    
    
    $("a.new_window").click(function() {
        window.open(this.href);
        return false;
    });

    //sekcja "preferowany sposób odbioru"
    $('#fieldset-payform1_group').hide();
    $('#fieldset-payform2_group').hide();
/*
    if ($('input:radio[name=query4]:checked').val() == 'gotówka w kasie') {
        $('#fieldset-payform1_group').show(20);
        alert("1");
    }
*/    
    $('input[name=query3]').click(function(){
        if ($('input:radio[name=query3]:checked').val() == 'odbiór osobisty') {
            $('#fieldset-payform1_group').show(2000);
            $('#fieldset-payform2_group').hide();
        } else {
            $('#fieldset-payform1_group').hide()
            $('#fieldset-payform2_group').show(2000);
        }       
    });

    // nowe
    if ($('input:radio[name=query3]:checked').val() == 'odbiór osobisty') {
        $('#fieldset-payform1_group').show(20);
        $('#fieldset-payform2_group').hide();
    } 
    
    if ($('input:radio[name=query3]:checked').val() == 'przesyłka pocztowa') {
        $('#fieldset-payform1_group').hide();
        $('#fieldset-payform2_group').show(20);        
    }
/*
    $('#Form3').submit(function() {
        alert($.scrollTop); 
        alert('sss');
        //if ($(this).val() != 'Wyślij formularz') {            
            return true;
            $(document).ready(function(){
                //alert('wysłano formularz');
                
                scrollTo('#fieldset-submit');
            });
            //return false;
        //}
    });
*/    
    //przycisk "dodaj pozycje"
    // akcja: wysłanie formularza
    //TODO: dopisać procedurę chowającą przycisku "dodaj pozycję" w przypadku wysłania formularza

/*
    $('input:hidden').each(function() {
        //alert($(this).val() + ' ' + $(this).attr('name'));
       if ($(this).val() == 'show') {
            $(this).parent().parent().prev().show(20).next().show(20);
       } else {
            if ($(this).val() == 'hide') {
                $(this).parent().parent().prev().hide().next().hide();
            }
                

       }
    });
*/
/*
    if ($('input:hidden[name=visible1]').val() == 'show') {
        $('#fieldset-position1_group').show(200).next().show(200);
        alert('t');
    } else {
        $('#fieldset-position1_group').hide().next().hide();;
        alert('f');
    }
    if ($('input:hidden[name=visible2]').val() == 'show') {
        $('#fieldset-position2_group').show(200).next().show(200);
    } else {
        $('#fieldset-position2_group').hide().next().hide(200);;
    }
*/ 


//$('#fieldset-position2_group').hide();

/*
    $('input:submit').click(function(){
        if ($(this).val() != 'Wyślij formularz') {
            $(this).parent().next().show(2000).next().show(2000);
            $(this).parent().next().next().children().eq(2).children().val('show');
            //alert($(this).next().next().attr('name'));
            //alert($(this).next().attr('name'));
            //$(this).parent().parent().next().fadeIn("slow");//show(200);//parent()
            
            
            //$('input:hidden[name=visible2]').val('show');
            //$('input:hidden[name=visible2]').val('show');
            $(this).hide();
            return false;
        }
    });
*/    
});



/*function changeC() {
	var el = document.getElementById('cus');
	if (el.className == 'off')
		el.className = 'on';		
	else
		el.className = 'off';
}*/	

function wypisz() {
    alert(' zostałem kliknięty! ');
}

/*document.getElementById('cuss').onclick = wypisz;*/

function Foo() {
   var el = document.getElementsByName('query');
       p_group = document.getElementById('fieldset-person_group');
       c_group = document.getElementById('fieldset-company_group');

   for(var i = 0; i < el.length; i++)
   {
      if(el[i].checked)
      {
         if (el[i].value == 'company')
         {
            p_group.className = 'off';
            c_group.className = 'on';
         }
         if (el[i].value == 'person')
         {
            p_group.className = 'on';
            c_group.className = 'off';
         }
      }
   }
}

function menuOn(element) {
	var el = document.getElementById(element);
	if (el.className == 'off')
		el.className = 'on';		
	else
		el.className = 'off';
}	


function menuOff(element) {
	var el = document.getElementById(element);
	el.className = 'off';
}	


function menuOnLeft() {
	var el = document.getElementById('emm-left-menu');
	if (el.className == 'off')
		el.className = 'on';		
	else
		el.className = 'off';

    document.getElementById('emm-center-menu').className ='off';
    document.getElementById('emm-right-menu').className ='off';
}	

function menuOnCenter() {
	var el = document.getElementById('emm-center-menu');
	if (el.className == 'off')
		el.className = 'on';		
	else
		el.className = 'off';

	document.getElementById('emm-left-menu').className ='off';
    document.getElementById('emm-right-menu').className ='off';
}	

function menuOnRight() {
	var el = document.getElementById('emm-right-menu');
	if (el.className == 'off')
		el.className = 'on';		
	else
		el.className = 'off';
	
	document.getElementById('emm-left-menu').className ='off';
    document.getElementById('emm-center-menu').className ='off';
}	

function allMenuOff() {	
	document.getElementById('emm-left-menu').className ='off';
    document.getElementById('emm-center-menu').className ='off';	
	document.getElementById('emm-right-menu').className ='off';
}


/*
    $(document).ready(function(){
    // czyszczenie sekcji "składający zamówienie"
    $('input[name=query]').click(function(){
        $('#fieldset-person_group input').val('');
        $('#fieldset-person_group textarea').val('');
        $('#fieldset-company_group input').val('');
        $('#fieldset-company_group textarea').val('');

        // ukrycie i wyczyszczenie pól sekcji "dane do faktury"
        $('#fieldset-facture_group').hide();
        $('#fieldset-facture_group input').val('');
        $('#fieldset-facture_group textarea').val('');
        // przełączenie radio button na "no"
        $('#query2-no').attr('checked', 'checked');

/*
        if ($(this).val() == 'person') {
            $('#query-company').attr('checked', 'checked')
        } else {
            $('#query-company').attr('checked', 'checked')
        }
*/
        //.not(':submit, :checkbox, :radio')                  
/*    });
    //sekcja dane do faktury
    if ($('input:radio[name=query2]:checked').val() == 'yes') {
        $('#fieldset-facture_group').show();
    } else {
        $('#fieldset-facture_group').hide();
    }
    
    $('input[name=query2]').click(function(){        
        if ($('input:radio[name=query2]:checked').val() == 'yes') {
            $('#fieldset-facture_group').show();

            if ($('input:radio[name=query]:checked').val() == 'person') {
                $('#f_name').val($('#p_name').val());
                $('#f_address').val($('#p_address').val());
            } else {
                $('#f_name').val($('#c_name').val());
                $('#f_address').val($('#c_address').val());
                $('#f_nip').val($('#c_nip').val());
            }
        } else {
            $('#fieldset-facture_group').hide();
            // wyczyść pola sekcji
            $('#fieldset-facture_group input').val('');
            $('#fieldset-facture_group textarea').val('');
        }
    });
});

 */
